Skip to content

fix(providers/anthropic): serialize Bedrock system as string#28

Open
ibetitsmike wants to merge 1 commit intocoder_2_33from
mike/bedrock-system-string
Open

fix(providers/anthropic): serialize Bedrock system as string#28
ibetitsmike wants to merge 1 commit intocoder_2_33from
mike/bedrock-system-string

Conversation

@ibetitsmike
Copy link
Copy Markdown

@ibetitsmike ibetitsmike commented Apr 23, 2026

Mux working on behalf of Mike.

Problem

For Bedrock, fantasy was serializing system as an Anthropic-style array of text blocks:

"system":[{"text":"you are helpful","type":"text"}]

Bedrock's Anthropic Messages schema expects system as a plain string, so this triggers a 400 validationException on affected paths.

Fix

Bedrock-only, post-serialization override:

  • prepareParams still produces the usual Anthropic params. When useBedrock is set, it now also returns a flattened system string and leaves params.System nil.
  • buildRequestOptions accepts the override and applies it via option.WithJSONSet("system", flattened).
  • Both Generate and Stream go through the same seam, so the wire shape cannot drift between streaming and non-streaming.
  • New helper flattenSystemForBedrock in providers/anthropic/bedrock.go joins non-empty text blocks with \n\n, preserving the existing toPrompt / groupIntoBlocks semantics.

Direct Anthropic behavior is unchanged and locked in by a new assertion.

Tests

TestBedrockSystemPromptWireShape covers Generate and Stream across:

  • single system message
  • three consecutive system messages joined with \n\n in order
  • system, user, system preserving only the first contiguous system block (matches today's toPrompt behavior)
  • direct Anthropic still sends system as an array

Recorded shapes:

  • Bedrock, before: {"system":[{"text":"you are helpful","type":"text"}]}
  • Bedrock, after: {"system":"you are helpful"}
  • Direct Anthropic, before and after: {"system":[{"text":"you are helpful","type":"text"}]}

Scope

No changes to groupIntoBlocks, toPrompt, or any non-Bedrock path. No changes to providers/bedrock/*.

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