fix(bedrock/converse): strip tool_choice from inference_params when no tools present

When a client (e.g. n8n) sends tool_choice unconditionally but
the message has no tools, Bedrock throws ValidationException because
tool_choice is passed through to inferenceConfig.

Add an else branch that pops tool_choice from inference_params
when bedrock_tools is empty, consistent with how the if-branch
already pops it when tools ARE present.
This commit is contained in:
yryzhan 2026-05-20 15:26:49 +02:00
parent e59e34bed3
commit 947acce268
2 changed files with 41 additions and 0 deletions

View file

@ -1519,6 +1519,8 @@ class AmazonConverseConfig(BaseConfig):
)
if tool_choice_values is not None:
bedrock_tool_config["toolChoice"] = tool_choice_values
else:
inference_params.pop("tool_choice", None)
data: CommonRequestObject = {
"additionalModelRequestFields": additional_request_params,

View file

@ -4578,3 +4578,42 @@ def test_transform_response_does_not_leak_body_on_parse_failure():
msg = str(exc_info.value)
assert "secret content" not in msg
assert "Error converting to valid response block" in msg
def test_tool_choice_stripped_when_no_tools():
"""
When bedrock_tools is empty but inference_params contains tool_choice,
it must be popped before _transform_inference_params to avoid
ValidationException from Bedrock.
"""
from unittest.mock import MagicMock, patch
from litellm.llms.bedrock.chat.converse_transformation import (
AmazonConverseConfig,
)
config = AmazonConverseConfig()
inference_params = {
"temperature": 0.7,
"tool_choice": {"auto": {}},
}
with patch.object(
config,
"_transform_request",
wraps=config._transform_request,
):
messages = [{"role": "user", "content": [{"text": "Hello"}]}]
optional_params = dict(inference_params)
result = config.transform_request(
model="anthropic.claude-3-sonnet-20240229-v1:0",
messages=[{"role": "user", "content": "Hello"}],
optional_params=optional_params,
litellm_params={"api_base": None},
headers={},
)
inference_config = result.get("inferenceConfig", {})
assert "tool_choice" not in inference_config
assert result.get("toolConfig") is None