fix(bedrock): normalize empty toolResult content arrays in _format_bedrock_messages#2123
Open
ghhamel wants to merge 2 commits intostrands-agents:mainfrom
Open
fix(bedrock): normalize empty toolResult content arrays in _format_bedrock_messages#2123ghhamel wants to merge 2 commits intostrands-agents:mainfrom
ghhamel wants to merge 2 commits intostrands-agents:mainfrom
Conversation
…drock_messages
Some model providers (e.g., Nemotron) reject toolResult blocks with
content: [] via the Bedrock Converse API, while others (e.g., Claude)
accept them. This causes a ValidationException when the SDK passes
empty tool result content through to the Converse API unchanged.
Normalize empty toolResult content arrays to [{"text": ""}] in
_format_bedrock_messages, following the same pattern as the TypeScript
SDK's _formatMessages in bedrock.ts and existing Python SDK
normalizations (SDK_UNKNOWN_MEMBER filtering, DeepSeek reasoningContent).
Fixes strands-agents#2122
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.
Description
Some model providers (e.g., Nemotron) reject
toolResultblocks withcontent: []via the Bedrock Converse API, while others (e.g., Claude) accept them. When a tool returns an empty result (common with MCP tools likeawslabs/postgres-mcp-server), the SDK passes the empty content array through to the Converse API unchanged, causing aValidationExceptionon strict models.This PR normalizes empty
toolResult.contentarrays to[{"text": ""}]in_format_bedrock_messages, following the same pattern as:_formatMessagesinbedrock.ts, which already filters empty contentSDK_UNKNOWN_MEMBERfiltering, DeepSeekreasoningContentdropping)Related Issues
Fixes #2122
Documentation PR
N/A
Type of Change
Bug fix
Testing
Added
test_format_bedrock_messages_normalizes_empty_tool_result_content— verifies emptytoolResult.contentis replaced with[{"text": ""}]Added
test_format_bedrock_messages_preserves_nonempty_tool_result_content— verifies non-empty content is not modifiedAll 129 existing
test_bedrock.pytests pass with no regressionsVerified fix against live Bedrock endpoints: Nemotron succeeds with the fix, Claude continues to work unchanged
I ran
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.