ai icon indicating copy to clipboard operation
ai copied to clipboard

amazon-bedrock: two signed thinking blocks on one contentBlockIndex in ConverseStream are merged, losing the first signature

Open marcelpotirnac opened this issue 2 weeks ago • 5 comments

Description

With Claude models on Bedrock, ConverseStream can deliver two consecutive signed thinking blocks on the same contentBlockIndex. @ai-sdk/amazon-bedrock keeps one reasoning part per index, so the two blocks become one part that carries both texts and only the second signature. The first block's signature is lost, and the next request replays a thinking block the provider never produced.

Checked on @ai-sdk/amazon-bedrock 5.0.101 and in the source of 5.0.145 (the code path is unchanged), with ai 7.0.122, models us.anthropic.claude-opus-5-5 and us.anthropic.claude-sonnet-5-5 in us-east-1.

What Bedrock sends

When the model thinks twice before a tool call, the stream looks like this (no contentBlockStop between the two blocks; the tool block is renumbered to 1):

contentBlockDelta  { contentBlockIndex: 0, delta: { reasoningContent: { text: "first thought" } } }
contentBlockDelta  { contentBlockIndex: 0, delta: { reasoningContent: { signature: "SIG_A" } } }
contentBlockDelta  { contentBlockIndex: 0, delta: { reasoningContent: { text: "second thought" } } }
contentBlockDelta  { contentBlockIndex: 0, delta: { reasoningContent: { signature: "SIG_B" } } }
contentBlockStop   { contentBlockIndex: 0 }
contentBlockStart  { contentBlockIndex: 1, start: { toolUse: { ... } } }

Converse (non-streaming) and InvokeModelWithResponseStream return the same response as two separate thinking blocks (indices 0 and 1, tool at 2). So the merge onto one index looks like a ConverseStream quirk, but the SDK is where the block is lost.

What the SDK does

In amazon-bedrock-chat-language-model.ts, the reasoningContent branch of the stream transform emits reasoning-start once per contentBlockIndex and sends every later text and signature delta into that same part (id: String(blockIndex)). streamText then assigns providerMetadata last-write-wins.

Result for the stream above: one reasoning part with text "first thoughtsecond thought" and signature SIG_B.

Why it matters

On the next step, convert-to-amazon-bedrock-chat-messages.ts replays that single part as one reasoningContent block with SIG_B.

  • With the provider's thinking-binding check enforced (beta thinking-binding-controls-2026-08-01, thinking.block_binding.prefix_mismatch_behavior: "error"), the request is rejected with 400: "The thinking block that preceded this one when it was created is missing from this request."
  • Without it, the request returns 200 but the provider drops the block silently: the prompt is smaller than the correct two-block replay, and the model loses its earlier reasoning.

It is not tied to any particular step: any response with two thinking blocks before a tool call breaks the following request. It happens with display: "summarized" and with display: "omitted" (there the second block arrives as a second signature delta with no text).

Expected

Two reasoning parts, each with its own text and signature, replayed as two reasoningContent blocks. Rebuilding the next request that way returns 200 with the check enforced.

Suggested fix

In the reasoningContent branch, start a new reasoning part when a delta arrives on an index whose current part already received a signature (emit reasoning-end for the current part and reasoning-start with a fresh id). That covers a text delta after a signature and, for omitted thinking, a second signature after a first.

We work around it with a wrapLanguageModel middleware that does exactly this on the stream parts, and it has held up on both models.

Minimal check

Feed the six events above through doStream of the Bedrock chat model with a stubbed fetch that returns them as an event stream, collect the stream parts, and count the reasoning parts: one today, two expected.

marcelpotirnac avatar Nov 19 '26 11:11 marcelpotirnac

Identify issue type

Identified as Bug
Confidence: High

Rationale

The issue reports a concrete streaming and replay failure in the existing Bedrock provider implementation. The suggested fix addresses incorrect behavior rather than adding a new capability.

Evidence

  • Two signed thinking blocks sharing a contentBlockIndex are merged into one reasoning part, losing the first signature.
  • Replaying the merged part causes a 400 error with thinking-binding checks enabled, or silently drops reasoning without them.
  • The report identifies affected versions and provides stream events for a minimal reproduction.

ai-sdk-factory[bot] avatar Nov 19 '26 11:11 ai-sdk-factory[bot]

Bug discovery clarification

@marcelpotirnac, how did you discover this bug? Did you encounter it while using the AI SDK in production, find it while auditing or reviewing AI SDK source code, or discover it another way? Please describe the original discovery context.

The report documents provider testing, source inspection, and a working middleware workaround, but does not establish whether discovery occurred in production, during SDK auditing, or in local experimentation. Ask the original author how they first discovered it.

ai-sdk-factory[bot] avatar Nov 19 '26 11:11 ai-sdk-factory[bot]

Found while testing an application built on the AI SDK, not by auditing the SDK source.

We were validating multi-step tool use with Claude Opus 5.5 and Sonnet 5.5 on Bedrock, with the thinking-binding check enforced (prefix_mismatch_behavior: "error"), and some steps failed with the 400 quoted above. We captured the raw ConverseStream bytes of a failing step, saw two signed blocks on one contentBlockIndex, and then read the provider source to see why only one came back.

This was a test environment. With the check unenforced the same replay returns 200 and the block is dropped silently, so there is no production error to point to.

Re-checked today against ai 7.0.177 and @ai-sdk/amazon-bedrock 5.0.145 by feeding the captured stream through streamText with a stubbed fetch: still one reasoning part for two signed blocks.

marcelpotirnac avatar Nov 19 '26 14:11 marcelpotirnac

Identify bug discovery

Discovery source: discovery:other (high confidence).

The original author explicitly clarified that discovery occurred while testing an AI SDK application in a test environment, not in production or during an SDK source audit. Source inspection followed the observed failure.

  • Author: "Found while testing an application built on the AI SDK, not by auditing the SDK source."
  • Author: "This was a test environment."
  • Multi-step tool-use validation produced 400 errors; the author then captured the failing stream and read the provider source.

ai-sdk-factory[bot] avatar Nov 19 '26 14:11 ai-sdk-factory[bot]

Thanks @marcelpotirnac for the report and the captured-stream clarification.

With Codex assistance, I prepared a test-only reproduction using the existing binary EventStreamCodec helpers in packages/amazon-bedrock/src/amazon-bedrock-chat-language-model-event-stream.test.ts, through the real decoder and schema. It uses the reported textA/signatureA/textB/signatureB sequence on one index, with no intervening contentBlockStop, plus a single-block control.

Against main commit b469f1ef651ef1835c00d617ff1cc8ad8591f7fc, the original focused file passes all 10 tests in both Node and Edge. With the two cases added, each runtime has 11 passing tests and one failure: two reasoning IDs are expected, but only one is emitted.

Would this focused failing-test PR provide useful context, and how would you prefer its expected CI failure to be handled? The test would remain active; the contribution would contain no production fix, API change, or new dependency.

This is local parsing/segmentation verification only. No live Bedrock requests or tool-result replay were tested.

VIOLETee0 avatar Nov 19 '26 16:11 VIOLETee0