Skip to content

fix: send content: null instead of content: "" for tool-only assistant messages#496

Open
robert-j-y wants to merge 1 commit into
mainfrom
devin/1778521888-fix-tool-only-content-null
Open

fix: send content: null instead of content: "" for tool-only assistant messages#496
robert-j-y wants to merge 1 commit into
mainfrom
devin/1778521888-fix-tool-only-content-null

Conversation

@robert-j-y

Copy link
Copy Markdown
Contributor

Description

Assistant messages containing only tool calls (no text) were serialized with content: "". Providers that strictly validate the OpenAI chat format (e.g. AWS Bedrock / Nova) reject blank content with a 400 error:

The text field in the ContentBlock object at messages.X.content.0 is blank. Add text to the text field, and try again.

Root cause: In convertToOpenRouterChatMessages, text is initialized as "" and never changes when the assistant turn has only tool-call parts. The type already allows string | null.

Fix: content: textcontent: text || null — one-line change that coerces empty string to null, which the OpenAI spec explicitly allows for assistant messages. Non-empty text is unaffected.

Before:

messages.push({
  role: 'assistant',
  content: text,           // "" for tool-only turns → Bedrock 400
  tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
});

After:

messages.push({
  role: 'assistant',
  content: text || null,   // null for tool-only turns → accepted
  tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
});

Same root cause Vercel fixed in @ai-sdk/openai-compatible (vercel/ai#13466).

Checklist

  • I have run pnpm stylecheck and pnpm typecheck
  • I have run pnpm test and all tests pass
  • I have added tests for my changes (if applicable)
  • I have updated documentation (if applicable)

Changeset

  • I have run pnpm changeset to create a changeset file

Note: A changeset is required for your changes to trigger a release. If your PR only contains docs, tests, or CI changes that don't need a release, run pnpm changeset --empty instead.

…t messages

Assistant messages containing only tool calls (no text) were serialized with
content: "". Providers that strictly validate the OpenAI chat format
(e.g. AWS Bedrock / Nova) reject blank content with a 400 error. Empty text is
now coerced to null, which the spec explicitly allows for assistant messages.

Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant