fix: filter empty text content blocks to prevent LLM API 400 errors#11808
Closed
fix: filter empty text content blocks to prevent LLM API 400 errors#11808
Conversation
Contributor
|
Docs Review: No documentation updates needed. This PR fixes internal message handling to prevent empty/whitespace text content blocks from being sent to LLM APIs. These are implementation-level changes that:
The fixes improve reliability behind the scenes—users will simply stop seeing the errors without needing to know or configure anything differently. |
Contributor
There was a problem hiding this comment.
2 issues found across 5 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="core/llm/openaiTypeConverters.ts">
<violation number="1" location="core/llm/openaiTypeConverters.ts:150">
P1: Whitespace-only assistant string content is still sent instead of being treated as empty.</violation>
<violation number="2" location="core/llm/openaiTypeConverters.ts:187">
P1: Whitespace-only user string content is not normalized and can still be sent to providers.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
7997113 to
f199b47
Compare
Collaborator
Author
|
this is a somewhat risky one, pending further evaluation |
f199b47 to
2abb9c9
Compare
…LLM APIs Empty or whitespace-only text content blocks cause 400 errors across multiple providers (Anthropic, Bedrock, Venice, OpenAI-compatible proxies). - Fix chatMessageIsEmpty to detect empty array content, not just strings - Replace addSpaceToAnyEmptyMessages (which added " " rejected by strict providers) with stripEmptyContentParts that removes empty text parts - Fix toChatMessage to use null for empty assistant content instead of " " - Fix toResponsesInput to skip empty assistant/system messages - Fix Bedrock _convertMessages to gracefully skip empty turns instead of throwing - Filter empty text parts in Bedrock adapter's user message conversion Fixes #9232, #9765, #9767, #10148, #10293, #10504, #10804, #11045, #11264, #11446, #11497, #11728
2abb9c9 to
1ef4a16
Compare
5 tasks
Collaborator
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Empty or whitespace-only text content blocks in array message content cause 400 errors across multiple providers (Anthropic, Bedrock, Venice, and other OpenAI-compatible proxies). This has been reported in 12 open issues.
Root causes fixed:
chatMessageIsEmpty()only checked string content — array content like[{type:"text", text:""}]was not detected as empty, so these messages passed through filteringaddSpaceToAnyEmptyMessages()only handled the string case, leaving empty text parts in arrays to reach provider APIs_convertMessageContentToBlocksdidn't filter empty text — blank text blocks were passed directly to the Bedrock APIChanges:
core/llm/messages.ts: FixchatMessageIsEmptyto detect empty array content. ReplaceaddSpaceToAnyEmptyMessageswithstripEmptyContentPartsthat removes empty text parts from arrayscore/llm/openaiTypeConverters.ts: ExtracttoAssistantContenthelper that filters empty text parts from assistant array content. Filter empty text parts in multi-media user messagescore/llm/llms/Bedrock.ts: Skip empty/whitespace text in_convertMessageContentToBlockspackages/openai-adapters/src/apis/Bedrock.ts: Filter empty text parts in user message conversioncore/llm/countTokens.ts: Use newstripEmptyContentPartsWhat this does NOT change:
" "fallbacks for string content (preserves compatibility with LM Studio, Ollama, etc.)toResponsesInputbehavior (preserves Responses API ID chains)Issues Fixed
Fixes #9232, fixes #9765, fixes #9767, fixes #10148, fixes #10293, fixes #10504, fixes #10804, fixes #11045, fixes #11264, fixes #11446, fixes #11497, fixes #11728
Test plan
openaiTypeConverters.test.tstests pass (24/24)countTokens.test.tstests pass