Fix Bedrock Converse API returning both json_tool_call and real tools when tools and response_format are used #18384
+226
−34
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
Fixed issue #18381 where Bedrock Converse API returns both
json_tool_call(internal tool for structured output) and real tools whentoolsandresponse_formatparameters are used together.Changes
Modified
_transform_responsemethod inlitellm/llms/bedrock/chat/converse_transformation.py:json_tool_calland real tools are present in the responsejson_tool_callwhen multiple tools are returned and json_mode is enabledjson_tool_callis the only tool (convert to content)json_tool_callimplementation detailAdded comprehensive tests in
tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py:test_transform_response_with_structured_response_calling_tool: Tests scenario where only real tool is calledtest_transform_response_with_both_json_tool_call_and_real_tool: Tests the bug scenario where both tools are returnedjson_tool_callis properly filtered out from the responseRelevant issues
Fixes #18381
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unitType
🐛 Bug Fix
Changes
Problem
When using both
toolsandresponse_formatparameters with Bedrock Converse API, LiteLLM internally adds a fake tool calledjson_tool_callto handle structured output. However, Bedrock sometimes returns both:json_tool_call(internal implementation detail)This caused the response to contain the internal
json_tool_callin the tool_calls array, which should be hidden from the user.Solution
Modified the
_transform_responsemethod to intelligently filter tool calls based on the scenario:json_tool_calland only return real toolsCode Changes
File:
litellm/llms/bedrock/chat/converse_transformation.pyLines 1473-1525: Refactored tool filtering logic to handle three scenarios:
json_tool_callis present in the responsejson_tool_calland return only real toolstool_callsif there are tools remaining after filteringTesting
Added two comprehensive test cases:
test_transform_response_with_structured_response_calling_tool(line 623):test_transform_response_with_both_json_tool_call_and_real_tool(line 750):json_tool_calland real tooljson_tool_callis filtered out and only real tool is returnedAll 58 tests in
test_converse_transformation.pyare passing.Edge Cases Handled
json_tool_callonly → Convert to contentjson_tool_call→ Filter outjson_tool_calljson_tool_call→ Return all toolsBackward Compatibility
This change is fully backward compatible:
json_tool_callis preserved