fix(bedrock): JSON-parse accumulated_tool_input at contentBlockStop in streaming handlers#5739
Open
Ghraven wants to merge 2 commits intocrewAIInc:mainfrom
Open
Conversation
…andler Fixes crewAIInc#4972 When using Bedrock Converse API in streaming mode, tool input arrives as JSON string deltas that get concatenated into `accumulated_tool_input`. At content block stop, this accumulated string is stored directly in `current_tool_use["input"]` and then passed to `_execute_single_native_tool_call` as `function_args` — but it's still a raw JSON string, not a dict. Tools that use Pydantic validation then receive a string instead of the expected dict and fail immediately with `Field required [type=missing, input_value={}, input_type=dict]` because the string can't be validated as a model schema. Fix: at content-block-stop in both sync and async streaming handlers, check if `current_tool_use.get('input')` is a string and JSON-parse it before passing to the executor. Falls back to empty dict on parse failure.
…n streaming Fixes crewAIInc#4972 During streaming, Bedrock sends tool input as incremental string deltas that get concatenated into `accumulated_tool_input`. When `contentBlockStop` fires, the code reads `current_tool_use.get('input', {})` — but `input` was never updated during streaming; it still holds the initial empty string from `contentBlockStart`. The accumulated JSON string is only in `accumulated_tool_input`, not in `current_tool_use['input']`. This causes every streaming tool call to receive `{}` as arguments, resulting in Pydantic validation failures on tools with required fields. Fix: at `contentBlockStop`, if `accumulated_tool_input` is non-empty, parse it as JSON and write it back to `current_tool_use['input']` before reading `function_args`. Both sync and async streaming handlers are fixed.
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.
Fixes #4972
Problem
When using the Bedrock Converse API in streaming mode, tool input arrives as incremental JSON string deltas that get concatenated into
accumulated_tool_input. AtcontentBlockStop, the code readscurrent_tool_use.get("input", {})— butcurrent_tool_use["input"]was never updated during streaming; it still holds the initial empty value fromcontentBlockStart.The accumulated JSON string lives only in
accumulated_tool_input, never incurrent_tool_use["input"]. So every streaming tool call receives{}as its arguments, causing:Non-streaming Bedrock calls are unaffected because
function_argsis read directly from the completed response block.Fix
At
contentBlockStop, in both the sync (_handle_converse_stream) and async (_ahandle_converse_stream) handlers: ifaccumulated_tool_inputis non-empty, JSON-parse it and write the result back tocurrent_tool_use["input"]before readingfunction_args. Falls back to an empty dict on parse failure.