mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-25 01:02:15 +00:00
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:
parent
e59e34bed3
commit
947acce268
2 changed files with 41 additions and 0 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue