From 86a9b74fc8381f942ae1a758ace04ee3b5940058 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Wed, 26 Nov 2025 13:32:13 +0530 Subject: [PATCH] Fix Thinking may not be enabled when tool_choice forces tool use --- .../bedrock/chat/converse_transformation.py | 14 +++++++++ .../chat/test_converse_transformation.py | 30 +++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.py index d76a3c31b51..9e001f533d1 100644 --- a/litellm/llms/bedrock/chat/converse_transformation.py +++ b/litellm/llms/bedrock/chat/converse_transformation.py @@ -580,6 +580,20 @@ class AmazonConverseConfig(BaseConfig): non_default_params=non_default_params, optional_params=optional_params ) + final_is_thinking_enabled = self.is_thinking_enabled(optional_params) + if ( + final_is_thinking_enabled + and "tool_choice" in optional_params + ): + tool_choice_block = optional_params["tool_choice"] + if isinstance(tool_choice_block, dict): + if "any" in tool_choice_block or "tool" in tool_choice_block: + verbose_logger.info( + f"{model} does not support forced tool use (tool_choice='required' or specific tool) " + f"when reasoning is enabled. Changing tool_choice to 'auto'." + ) + optional_params["tool_choice"] = ToolChoiceValuesBlock(auto={}) + return optional_params def _translate_response_format_param( diff --git a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py index c09d3b8d841..37c95be72ce 100644 --- a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py +++ b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py @@ -238,6 +238,30 @@ def test_transform_tool_call_with_cache_control(): assert "cachePoint" in transformed_cache_msg assert transformed_cache_msg["cachePoint"]["type"] == "default" + +def test_reasoning_with_forced_tool_choice_switches_to_auto(): + config = AmazonConverseConfig() + + non_default_params = { + "tools": [ + { + "type": "function", + "function": {"name": "get_current_weather", "parameters": {}}, + } + ], + "tool_choice": "required", + "reasoning_effort": "low", + } + + optional_params = config.map_openai_params( + model="bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0", + non_default_params=non_default_params, + optional_params={}, + drop_params=False, + ) + + assert optional_params["tool_choice"] == {"auto": {}} + def test_get_supported_openai_params(): config = AmazonConverseConfig() supported_params = config.get_supported_openai_params( @@ -2592,8 +2616,10 @@ def test_empty_assistant_message_handling(): empty or whitespace-only content with a placeholder to prevent AWS Bedrock Converse API 400 Bad Request errors. """ - from litellm.litellm_core_utils.prompt_templates.factory import _bedrock_converse_messages_pt - + from litellm.litellm_core_utils.prompt_templates.factory import ( + _bedrock_converse_messages_pt, + ) + # Test case 1: Empty string content - test with modify_params=True to prevent merging messages = [ {"role": "user", "content": "Hello"},