Merge pull request #17129 from BerriAI/litellm_fix_mcp_responses_anthropic

Fix Thinking may not be enabled when tool_choice forces tool use
This commit is contained in:
Sameer Kankute 2025-11-26 22:06:59 +05:30 • committed by GitHub
commit 79c1203377
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 2 deletions

View file

@ -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(

View file

@ -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"},