fix(streaming): handle tool use metadata in contentBlockDelta for non-standard models#2077
Open
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Open
Conversation
…-standard models
Some Bedrock models (e.g., Kimi K2.5) send toolUseId and name in
contentBlockDelta rather than in contentBlockStart. The parser only
extracted input from the delta, leaving current_tool_use without those
fields and crashing in handle_content_block_stop.
- Capture toolUseId and name from delta when not already set by start
- Use .get("input", "") to avoid KeyError when input key is absent
- Add warning + skip for incomplete tool use blocks in stop handler
Fixes strands-agents#1646
7 tasks
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.
Problem
When Kimi K2.5 (
moonshotai.kimi-k2.5) is used via the Amazon Bedrock Converse streaming API, tool calls on the second cycle crash with aKeyError. The streaming parser inhandle_content_block_deltaonly readsinputfromcontentBlockDelta.delta.toolUse, ignoringtoolUseIdandname. When a model provides those fields in the delta rather than incontentBlockStart(a non-standard but valid variation),current_tool_usereacheshandle_content_block_stopwithouttoolUseIdorname, causing a crash.A secondary gap: if
delta.toolUsehas noinputkey, the direct dict access raisesKeyErrorinstead of treating it as an empty string.Solution
In
handle_content_block_delta, capturetoolUseIdandnamefrom the delta when they are present and not already set from a priorcontentBlockStart. Use.get("input", "")to handle deltas with noinputkey.In
handle_content_block_stop, replace the directcurrent_tool_use["toolUseId"]andcurrent_tool_use["name"]accesses with.get()calls, and log a warning and skip the block if either field is still missing after processing all events.Testing
test_handle_content_block_delta_captures_tool_use_id_and_name_from_delta: delta with all three fields populatescurrent_tool_usecorrectly.test_handle_content_block_delta_does_not_override_existing_tool_use_id_and_name: fields set bycontentBlockStartare not overwritten by a later delta.test_handle_content_block_delta_tool_use_without_input_key: delta missinginputdoes not raise.test_handle_content_block_stop_skips_incomplete_tool_use_missing_id/_missing_name: warning logged and block skipped when fields are absent.test_process_stream_tool_use_info_in_delta: end-to-end check simulating a model that sends all tool use metadata in the delta.Fixes #1646
Documentation PR
N/A
Type of Change
Bug fix
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.