fix(responses): echo "auto" for a tool_choice the bridge cannot express instead of failing after the provider call

This commit is contained in:
mateo-berri 2026-09-09 18:27:12 -07:00
parent 7563d94e65
commit 4adf99557f
3 changed files with 50 additions and 2 deletions

View file

@ -287,8 +287,10 @@ class LiteLLMCompletionResponsesConfig:
return ToolChoiceCustomParam(type="custom", name=custom_name)
case _, {"type": "function", "function": {"name": str(function_name)}}:
return ToolChoiceFunctionParam(type="function", name=function_name)
case _, normalized:
return _RESPONSES_API_TOOL_CHOICE_ADAPTER.validate_python(normalized)
case _, "none" | "auto" | "required" as normalized:
return normalized
case _, _:
return "auto"
@staticmethod
def _should_drop_derived_web_search_options(model: str, custom_llm_provider: str | None) -> bool:

View file

@ -1435,6 +1435,9 @@ class TestToolChoiceTransformation:
("required", "required"),
("none", "none"),
(None, "auto"),
("any", "auto"),
("run_command", "auto"),
({"name": "run_command"}, "auto"),
],
)
def test_transform_tool_choice_for_responses_api_response(
@ -1478,6 +1481,29 @@ class TestToolChoiceTransformation:
assert responses_api_response.tool_choice == {"type": "function", "name": "run_command"}
def test_non_streamed_response_with_unrecognized_tool_choice_echoes_auto(self) -> None:
chat_completion_response: Final = ModelResponse(
id="chatcmpl-unrecognized-tool-choice",
created=1748575031,
model="claude-haiku-4-5",
object="chat.completion",
choices=[
Choices(
index=0,
finish_reason="stop",
message=Message(role="assistant", content="/Users/dev"),
)
],
)
responses_api_response: Final = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response(
request_input="Run the command pwd.",
responses_api_request={"tool_choice": "any"},
chat_completion_response=chat_completion_response,
)
assert responses_api_response.tool_choice == "auto"
class TestContentTypeTransformation:
"""Test content type transformation from Responses API to Chat Completion format"""

View file

@ -685,3 +685,23 @@ def test_streamed_named_tool_choice_is_echoed_in_responses_api_shape() -> None:
{"type": "function", "name": "run_command"},
]
assert any(getattr(event, "type", None) == "response.output_item.done" for event in events)
def test_streamed_unrecognized_tool_choice_is_echoed_as_auto() -> None:
iterator: Final = LiteLLMCompletionStreamingIterator(
model="claude-haiku-4-5",
litellm_custom_stream_wrapper=_FakeStreamWrapper([_tool_call_chunk(finish_reason="tool_calls")]),
request_input="Run the command pwd.",
responses_api_request={
"tools": [{"type": "function", "name": "run_command", "parameters": {"type": "object"}}],
"tool_choice": "any",
},
custom_llm_provider="anthropic",
litellm_metadata={},
)
response_events: Final = [
event for event in iterator if getattr(event, "type", None) in RESPONSE_ID_EVENT_TYPES
]
assert [event.response.tool_choice for event in response_events] == ["auto", "auto", "auto"]