Summary
In @ai-sdk/openai-compatible (v1.0.32), the convertToOpenAICompatibleChatMessages function always outputs content: text (where text is initialized as "") for assistant messages. When an assistant message contains only tool-call parts and no text parts, this results in content: "" being sent in the API request.
Providers backed by AWS Bedrock reject this with:
ValidationException: messages: text content blocks must be non-empty
Root Cause
In dist/index.mjs (line ~103-109):
case "assistant": {
let text = "";
// ... loops over content parts ...
messages.push({
role: "assistant",
content: text, // <-- empty string when no text parts exist
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
...metadata
});
}
When the assistant message only contains tool-call parts (no text parts), text remains "" and the output is content: "" rather than content: null.
The same pattern exists in @ai-sdk/openai's convertToOpenAIChatMessages.
Proposed Fix
Change content: text to content: text || null for assistant messages:
messages.push({
role: "assistant",
content: text || null, // null instead of empty string
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
...metadata
});
This matches the OpenAI API spec where content is nullable for assistant messages with tool calls.
Reproduction
- Use
@ai-sdk/openai-compatible with a Bedrock-backed endpoint
- Have a multi-turn conversation involving tool calls
- After 2-5 rounds, an assistant message with only tool-call parts is sent with
content: ""
- Bedrock returns HTTP 400:
ValidationException: messages: text content blocks must be non-empty
Environment
@ai-sdk/openai-compatible: 1.0.32
@ai-sdk/openai: 2.0.89 (same issue)
- Provider backend: AWS Bedrock Runtime
Related
Summary
In
@ai-sdk/openai-compatible(v1.0.32), theconvertToOpenAICompatibleChatMessagesfunction always outputscontent: text(wheretextis initialized as"") for assistant messages. When an assistant message contains onlytool-callparts and no text parts, this results incontent: ""being sent in the API request.Providers backed by AWS Bedrock reject this with:
Root Cause
In
dist/index.mjs(line ~103-109):When the assistant message only contains
tool-callparts (notextparts),textremains""and the output iscontent: ""rather thancontent: null.The same pattern exists in
@ai-sdk/openai'sconvertToOpenAIChatMessages.Proposed Fix
Change
content: texttocontent: text || nullfor assistant messages:This matches the OpenAI API spec where
contentis nullable for assistant messages with tool calls.Reproduction
@ai-sdk/openai-compatiblewith a Bedrock-backed endpointcontent: ""ValidationException: messages: text content blocks must be non-emptyEnvironment
@ai-sdk/openai-compatible: 1.0.32@ai-sdk/openai: 2.0.89 (same issue)Related