Repository navigation
Bedrock: send the reasoning blocks of a turn back with the tool results - #75
Open
alexey-milovidov wants to merge 2 commits into
Open
alexey-milovidov wants to merge 2 commits into
alexey-milovidov wants to merge 2 commits into
Conversation
A model that reasons before it answers returns `reasoningContent` blocks in the message of a Converse response: the reasoning text with a signature, or redacted content. The current Claude models reason by default, also when the request says nothing about it, and then the text is empty and the signature is there. Converse wants these blocks back unchanged, in the turn that they are a part of, when the results of the tool calls of that turn are sent. The provider dropped them: `parse_converse_response` kept the text and the tool calls, and the request mapper had nothing to send them back with. So the request after a tool call carried the turn of the assistant without the reasoning that led to the call. Now the parser returns such a turn in `response_messages` with its blocks in their order, the reasoning ones as `ProviderContentPart`s of the provider `bedrock`, and the mapper sends them back as they came. It is what the Gemini and the OpenAI Responses providers do with their thought signatures and `reasoning` items, so the multi-step loop, and an application that appends `response_messages` to the conversation, replay them without a change. A turn without reasoning is returned as before. Amazon Bedrock itself was not called: what it does with a turn that lacks its reasoning, an error or an answer without that reasoning in the context, is not known here. Found in the review of ClickHouse/ClickHouse#137792: ClickHouse/ClickHouse#137792 (comment) Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
alexey-milovidov
added a commit
to ClickHouse/ClickHouse
that referenced
this pull request
Dec 6, 2026
…sults A model that reasons before it answers returns `reasoningContent` blocks in the message of a Converse response: the reasoning text with a signature, or redacted content. The current Claude models reason by default, also when the request says nothing about it, and then the text is empty and the signature is there. Converse wants these blocks back unchanged, in the turn that they are a part of, when the results of the tool calls of that turn are sent. The Bedrock provider of the AI SDK dropped them, so the request after a tool call carried the turn of the assistant without the reasoning that led to the call. `contrib/ai-sdk-cpp` moves to the head of ClickHouse/ai-sdk-cpp#75, which is one commit on top of `main` of that repository: the provider returns a turn that has reasoning with its blocks in their order, the reasoning ones as `ProviderContentPart`s, and sends them back as they came. The client already keeps and replays a turn that has parts of the provider (`AIAgentStep::provider_turn`), as it does for the OpenAI Responses API and for Gemini, so nothing changes in it. The submodule moves to the commit of `main` once that pull request is merged. The test `06038_client_ai_bedrock_provider` has the case `reasoning_goes_back_with_the_tool_result`: the mock answers with a block of reasoning before the text and the tool call, and the next request shows the turn of the assistant with the block as the mock gave it, in the first place. Before the change the turn was `text, toolUse` only. Amazon Bedrock itself was not called: what it does with a turn that lacks its reasoning, an error or an answer without that reasoning in the context, is not known here. The Anthropic provider of the AI SDK does the same with `thinking` blocks. It is not a part of this pull request: ClickHouse/ai-sdk-cpp#76 Review thread: #137792 (comment) Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…de in The signature of a reasoning block of a Claude model records the system prompt, the tools and every earlier message, and a request that sends the block back after any of them has changed can be refused. The Bedrock provider returns these blocks as `ai::ProviderContentPart`s now, so an application that trims its history, or builds the system prompt or the tools again, has to remove them from the messages that it keeps. The README says so, with the link to the documentation of the check. Found in the review of ClickHouse/ClickHouse#137792: ClickHouse/ClickHouse#137792 (comment) Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The Bedrock provider now keeps the blocks of reasoning of a Converse response and sends them back, unchanged and in their place, with the turn that they are a part of.
Why. A model that reasons before it answers returns
reasoningContentblocks in the message of a Converse response: the reasoning text with a signature, or redacted content. The current Claude models reason by default, also when the request says nothing about it ("A request that omits the thinking field runs with adaptive thinking", https://docs.aws.amazon.com/bedrock/latest/userguide/claude-messages-adaptive-thinking.html), and then the text is empty and the signature is there. Converse wants the blocks back when the results of the tool calls of that turn are sent ("You must include the signature and all previous messages in subsequent Converse requests", https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html).The provider dropped them:
parse_converse_responsekept the text and the tool calls, and the request mapper had nothing to send them back with. So the request after a tool call carried the turn of the assistant without the reasoning that led to the call. Found in the review of ClickHouse/ClickHouse#137792: ClickHouse/ClickHouse#137792 (comment)What changes.
BedrockResponseParserreturns a turn that has reasoning inGenerateResult::response_messageswith its blocks in their order: the text, the tool calls, and everyreasoningContentblock as aProviderContentPartof the providerbedrock, with the block as it came. Reasoning alone is not a turn. A turn without reasoning is returned as before: the text, with the tool calls intool_calls.BedrockRequestMappersends such a part back as the block that it holds, in its place. The part of another provider is not sent.It is what the Gemini and the OpenAI Responses providers do with their thought signatures and
reasoningitems, so the multi-step loop (max_steps > 1), and an application that appendsresponse_messagesto the conversation, replay the reasoning without a change. The stream is not changed: it does not return the reasoning blocks.Tests. Five more unit tests: the parser (reasoning with a signature before a text and a tool call, redacted reasoning, reasoning alone), the mapper (the block goes back as it came, the part of another provider does not), and the two requests of a tool loop through the client. The 89 unit tests of the provider that need no AWS SDK pass.
Not verified. Amazon Bedrock itself was not called: what it does with a turn that lacks its reasoning, an error or an answer without that reasoning in the context, is not known here.
Related: ClickHouse/ClickHouse#137792
🤖 Generated with Claude Code
Note
Medium Risk
Changes Bedrock request/response mapping for tool loops with reasoning models; incorrect replay could break Claude tool calls, but scope is isolated to the Bedrock provider with new unit tests.
Overview
Bedrock now preserves and replays Claude
reasoningContentblocks so multi-step tool loops and conversations continued fromresponse_messagessatisfy Converse’s requirement to return reasoning (with signatures) alongside the same turn’s tool results.BedrockResponseParsercaptures eachreasoningContentblock in order with text and tool calls asProviderContentPartentries under provider"bedrock"inGenerateResult::response_messages(reasoning-only output does not produce a turn).BedrockRequestMappermaps those parts back into the Converse body unchanged and in place; non-Bedrock provider parts are dropped.README documents this behavior, automatic replay via
max_steps, streaming limitations, and prefix/signature constraints when system prompt, tools, or history change.Five unit tests cover parsing (signed, redacted, reasoning-only), mapper replay, and a two-step tool client flow.
Reviewed by Cursor Bugbot for commit 0455f2d. Bugbot is set up for automated code reviews on this repo. Configure here.