Skip to content

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
giulio-leone:fix/nova-json-tool-result
Open

fix: convert JSON content blocks to text for Amazon Nova models in tool results#1891
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/nova-json-tool-result

Conversation

@giulio-leone
Copy link
Contributor

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:

if "json" in tool_result_content:
    formatted_content.append({"json": tool_result_content["json"]})

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_TEXT list 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:
    • Added _MODELS_CONVERT_JSON_TO_TEXT list with "amazon.nova" prefix
    • Added _should_convert_json_to_text() helper method
    • Updated JSON block handling in _format_request_message_content() to convert to text for Nova models

Testing

Added 5 new tests:

  • test_nova_model_converts_json_to_text_in_tool_result — basic JSON-to-text conversion
  • test_nova_model_converts_mixed_json_and_text_in_tool_result — mixed content preservation
  • test_claude_model_preserves_json_in_tool_result — non-Nova models unaffected
  • test_nova_model_handles_nested_json_in_tool_result — deeply nested structures
  • test_should_convert_json_to_text_nova_variants — all Nova model ID patterns

All 1871 existing + new tests pass.

…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
@giulio-leone
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Amazon Nova Models Hallucinate with JSON Content Blocks in Tool Results

1 participant