Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-tool-call-content-null.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/openai': patch
---

fix(openai): send content: null instead of content: "" for tool-call-only assistant messages
73 changes: 72 additions & 1 deletion packages/openai/src/chat/convert-to-openai-chat-messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ describe('tool calls', () => {
expect(result.messages).toEqual([
{
role: 'assistant',
content: '',
content: null,
tool_calls: [
{
type: 'function',
Expand All @@ -475,6 +475,77 @@ describe('tool calls', () => {
]);
});

it('should send content as null for tool-call-only assistant messages', () => {
const result = convertToOpenAIChatMessages({
prompt: [
{
role: 'assistant',
content: [
{
type: 'tool-call',
input: { location: 'Seattle' },
toolCallId: 'call_1',
toolName: 'get_weather',
},
],
},
],
});

expect(result.messages).toEqual([
{
role: 'assistant',
content: null,
tool_calls: [
{
type: 'function',
id: 'call_1',
function: {
name: 'get_weather',
arguments: JSON.stringify({ location: 'Seattle' }),
},
},
],
},
]);
});

it('should preserve text content alongside tool calls', () => {
const result = convertToOpenAIChatMessages({
prompt: [
{
role: 'assistant',
content: [
{ type: 'text', text: 'Let me check the weather.' },
{
type: 'tool-call',
input: { location: 'Seattle' },
toolCallId: 'call_1',
toolName: 'get_weather',
},
],
},
],
});

expect(result.messages).toEqual([
{
role: 'assistant',
content: 'Let me check the weather.',
tool_calls: [
{
type: 'function',
id: 'call_1',
function: {
name: 'get_weather',
arguments: JSON.stringify({ location: 'Seattle' }),
},
},
],
},
]);
});

it('should handle different tool output types', () => {
const result = convertToOpenAIChatMessages({
prompt: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export function convertToOpenAIChatMessages({

messages.push({
role: 'assistant',
content: text,
content: text || null,
tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
});

Expand Down