fix: convert JSON content blocks to text for Amazon Nova models in tool results#1891
Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Open
fix: convert JSON content blocks to text for Amazon Nova models in tool results#1891giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Conversation
…ol results
Nova models (Nova Pro, Nova Lite, Nova Micro) hallucinate when tool results
contain JSON content blocks ({'json': {...}}). They produce incoherent responses,
fabricate information, role-play both sides of conversations, and hallucinate
fake XML tool calls. Claude models handle JSON blocks correctly.
This fix adds model-aware JSON-to-text conversion in _format_request_message_content()
that automatically serializes JSON content blocks to their text representation
(json.dumps) when the model is an Amazon Nova variant. Non-Nova models continue
to receive JSON blocks as-is.
The detection follows the existing pattern used for tool result status handling
(_MODELS_INCLUDE_STATUS / _should_include_tool_result_status), adding a parallel
_MODELS_CONVERT_JSON_TO_TEXT list and _should_convert_json_to_text() method.
Closes strands-agents#1095
eee3d48 to
c33d2a9
Compare
Contributor
Author
|
Friendly ping — converts JSON content blocks to text format for Amazon Nova models in tool results, which don't support structured content in tool responses. |
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.
Summary
Fixes #1095
Amazon Nova models (Pro, Lite, Micro) hallucinate when tool results contain JSON content blocks (
{"json": {...}}). The models produce incoherent responses, fabricate information, role-play both sides of conversations, and hallucinate fake XML tool calls. Claude models handle JSON content blocks correctly.Root Cause
The
_format_request_message_content()method passes JSON content blocks through to the Bedrock API as-is:While this is valid per the Bedrock API spec, Nova models cannot process JSON content blocks correctly and produce hallucinated output instead.
Fix
Added model-aware JSON-to-text conversion that automatically serializes JSON content blocks to their text representation (
json.dumps()) when the model is an Amazon Nova variant. Non-Nova models continue to receive JSON blocks unchanged.The implementation follows the existing pattern used for tool result status handling (
_MODELS_INCLUDE_STATUS/_should_include_tool_result_status()), adding a parallel_MODELS_CONVERT_JSON_TO_TEXTlist and_should_convert_json_to_text()method.Before (Nova receives JSON block → hallucination)
{"toolResult": {"content": [{"json": {"key": "value"}}], "toolUseId": "..."}}After (Nova receives text → correct behavior)
{"toolResult": {"content": [{"text": "{\"key\": \"value\"}"}], "toolUseId": "..."}}Changes
src/strands/models/bedrock.py:_MODELS_CONVERT_JSON_TO_TEXTlist with"amazon.nova"prefix_should_convert_json_to_text()helper method_format_request_message_content()to convert to text for Nova modelsTesting
Added 5 new tests:
test_nova_model_converts_json_to_text_in_tool_result— basic JSON-to-text conversiontest_nova_model_converts_mixed_json_and_text_in_tool_result— mixed content preservationtest_claude_model_preserves_json_in_tool_result— non-Nova models unaffectedtest_nova_model_handles_nested_json_in_tool_result— deeply nested structurestest_should_convert_json_to_text_nova_variants— all Nova model ID patternsAll 1871 existing + new tests pass.