Skip to content

fix(session): never send an empty-content user message (Bedrock 400)#1731

Closed
wqymi wants to merge 0 commit into
mainfrom
fix/empty-user-message-content
Closed

fix(session): never send an empty-content user message (Bedrock 400)#1731
wqymi wants to merge 0 commit into
mainfrom
fix/empty-user-message-content

Conversation

@wqymi

@wqymi wqymi commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Root cause: toModelMessagesEffect in message-v2.ts:681 pushed a UIMessage with parts: [] into the result array before the loop populated parts. If all DB parts were filtered (ignored text, unsupported file types, etc.), the empty user message leaked through to convertToModelMessages, producing a ModelMessage with empty content that Bedrock/Anthropic reject with 400.
  • Secondary gap: normalizeMessages in transform.ts:64 passed through non-string/non-array content (null, undefined) without checking, allowing empty-content user messages to bypass the Anthropic/Bedrock filter.

Changes

packages/opencode/src/session/message-v2.ts

Build user message parts into a local userParts array, then only push the UIMessage when userParts.length > 0. Eliminates the push-before-populate race where an empty parts: [] message could leak through.

packages/opencode/src/provider/transform.ts

Added explicit guard at the top of normalizeMessages: user messages with null, undefined, or empty-array content are dropped before reaching the provider. Safety-net backstop for any future code path that might produce empty-content user messages.

packages/opencode/test/provider/transform.test.ts

3 new regression tests:

  • Filters user messages with null content for Bedrock
  • Filters user messages with empty array content for Bedrock
  • No user message in the built request has empty content (Bedrock regression — comprehensive sweep of empty string, null, empty array, empty parts)

Verification

  • bun typecheck passes (turbo typecheck: 12/12 packages)
  • All 212 transform tests pass (including 3 new)
  • All 26 message-v2 tests pass
  • All 5 message-v2-filter tests pass

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