The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(responses): echo a named tool_choice in the Responses API shape on the chat-completions bridge
mateo-berricce1d2087b9a
diff --git a/litellm/responses/litellm_completion_transformation/streaming_iterator.py b/litellm/responses/litellm_completion_transformation/streaming_iterator.pyindex d7f8cd8f8bd..660dd8f0c92 100644--- a/litellm/responses/litellm_completion_transformation/streaming_iterator.py+++ b/litellm/responses/litellm_completion_transformation/streaming_iterator.py@@ -437,14 +437,11 @@ class LiteLLMCompletionStreamingIterator(ResponsesAPIStreamingIterator):response_created_event_data["temperature"] = self.responses_api_request["temperature"]if "text" in self.responses_api_request:response_created_event_data["text"] = self.responses_api_request["text"]- if "tool_choice" in self.responses_api_request:- # Transform tool_choice from dict format (e.g., {"type": "auto"}) to string format- response_created_event_data["tool_choice"] = (- LiteLLMCompletionResponsesConfig._transform_tool_choice(self.responses_api_request["tool_choice"])- or "auto"+ response_created_event_data["tool_choice"] = (+ LiteLLMCompletionResponsesConfig._transform_tool_choice_for_responses_api_response(+ self.responses_api_request.get("tool_choice"))- else:- response_created_event_data["tool_choice"] = "auto"+ )if "tools" in self.responses_api_request:response_created_event_data["tools"] = self.responses_api_request["tools"]else:diff --git a/litellm/responses/litellm_completion_transformation/transformation.py b/litellm/responses/litellm_completion_transformation/transformation.pyindex b2d1a69e0d8..878f493b58e 100644--- a/litellm/responses/litellm_completion_transformation/transformation.py+++ b/litellm/responses/litellm_completion_transformation/transformation.py@@ -27,6 +27,7 @@ from openai.types.chat.chat_completion_named_tool_choice_param import ()from openai.types.responses import ResponseFunctionToolCallfrom openai.types.responses.response_create_params import ResponseInputParam+from openai.types.responses.tool_choice_function_param import ToolChoiceFunctionParamfrom openai.types.responses.tool_param import FunctionToolParamfrom pydantic import TypeAdapterfrom typing_extensions import ReadOnly, TypedDict@@ -68,6 +69,7 @@ from litellm.types.llms.openai import (ResponsesAPIOptionalRequestParams,ResponsesAPIResponse,ResponsesAPIStatus,+ ToolChoice,ValidChatCompletionMessageContentTypes,ValidChatCompletionMessageContentTypesLiteral,)@@ -126,6 +128,7 @@ _STR_KEY_DICT_ADAPTER: Final = TypeAdapter(dict[str, object])_OBJECT_LIST_ADAPTER: Final = TypeAdapter(list[object])_DICT_ITEMS_LIST_ADAPTER: Final = TypeAdapter(list[dict[object, object]])_TEXT_ADAPTER: Final = TypeAdapter(str)+_RESPONSES_API_TOOL_CHOICE_ADAPTER: Final = TypeAdapter(ToolChoice)@runtime_checkable@@ -267,6 +270,17 @@ class LiteLLMCompletionResponsesConfig:# Return as-is for unknown formatsreturn tool_choice+ @staticmethod+ def _transform_tool_choice_for_responses_api_response(tool_choice: object) -> ToolChoice:+ normalized: Final = LiteLLMCompletionResponsesConfig._transform_tool_choice(tool_choice)+ match normalized:+ case None:+ return "auto"+ case {"type": "function", "function": {"name": str(function_name)}}:+ return ToolChoiceFunctionParam(type="function", name=function_name)+ case _:+ return _RESPONSES_API_TOOL_CHOICE_ADAPTER.validate_python(normalized)+@staticmethoddef _should_drop_derived_web_search_options(model: str, custom_llm_provider: str | None) -> bool:"""@@ -2263,7 +2277,9 @@ class LiteLLMCompletionResponsesConfig:),parallel_tool_calls=getattr(chat_completion_response, "parallel_tool_calls", False),temperature=getattr(chat_completion_response, "temperature", 0),- tool_choice=getattr(chat_completion_response, "tool_choice", "auto"),+ tool_choice=LiteLLMCompletionResponsesConfig._transform_tool_choice_for_responses_api_response(+ responses_api_request.get("tool_choice")+ ),tools=getattr(chat_completion_response, "tools", []),top_p=getattr(chat_completion_response, "top_p", None),max_output_tokens=getattr(chat_completion_response, "max_output_tokens", None),diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py b/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.pyindex 2068f10ea2d..2aab660b5b7 100644--- a/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py+++ b/tests/test_litellm/responses/litellm_completion_transformation/test_litellm_completion_responses.py@@ -1421,6 +1421,58 @@ class TestToolChoiceTransformation:)assert result == "required"+ @pytest.mark.parametrize(+ "request_tool_choice,expected",+ [+ ({"type": "function", "name": "run_command"}, {"type": "function", "name": "run_command"}),+ ({"type": "function", "function": {"name": "run_command"}}, {"type": "function", "name": "run_command"}),+ ({"type": "custom", "name": "ApplyPatch"}, {"type": "function", "name": "ApplyPatch"}),+ ({"type": "tool"}, "required"),+ ({"type": "auto"}, "auto"),+ ("required", "required"),+ ("none", "none"),+ (None, "auto"),+ ],+ )+ def test_transform_tool_choice_for_responses_api_response(self, request_tool_choice, expected):+ result = LiteLLMCompletionResponsesConfig._transform_tool_choice_for_responses_api_response(+ request_tool_choice+ )+ assert result == expected++ def test_non_streamed_response_echoes_named_tool_choice_in_responses_api_shape(self):+ chat_completion_response = ModelResponse(+ id="chatcmpl-named-tool-choice",+ created=1748575031,+ model="claude-haiku-4-5",+ object="chat.completion",+ choices=[+ Choices(+ index=0,+ finish_reason="tool_calls",+ message=Message(+ role="assistant",+ content=None,+ tool_calls=[+ ChatCompletionMessageToolCall(+ id="call_pwd",+ type="function",+ function=Function(name="run_command", arguments='{"command":"pwd"}'),+ )+ ],+ ),+ )+ ],+ )++ responses_api_response = LiteLLMCompletionResponsesConfig.transform_chat_completion_response_to_responses_api_response(+ request_input="Run the command pwd.",+ responses_api_request={"tool_choice": {"type": "function", "name": "run_command"}},+ chat_completion_response=chat_completion_response,+ )++ assert responses_api_response.tool_choice == {"type": "function", "name": "run_command"}+class TestContentTypeTransformation:"""Test content type transformation from Responses API to Chat Completion format"""diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator_transformation.py b/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator_transformation.pyindex 719d51c11e3..d4b565f82a1 100644--- a/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator_transformation.py+++ b/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator_transformation.py@@ -628,3 +628,56 @@ def test_streamed_anthropic_tool_call_events_correlate_on_normalized_item_id():assert item_dones[0].item.call_id == "toolu_01AbCdEf"for evt in deltas + dones:assert evt.item_id == added[0].item.id+++def _tool_call_chunk(finish_reason: str | None = None) -> ModelResponseStream:+ return ModelResponseStream(+ id=CHAT_COMPLETION_ID,+ created=1748575031,+ model="claude-haiku-4-5",+ object="chat.completion.chunk",+ choices=[+ StreamingChoices(+ index=0,+ delta=Delta(+ role="assistant",+ content=None,+ tool_calls=[+ {+ "id": "call_pwd",+ "type": "function",+ "function": {"name": "run_command", "arguments": '{"command":"pwd"}'},+ "index": 0,+ }+ ],+ ),+ finish_reason=finish_reason,+ )+ ],+ )+++def test_streamed_named_tool_choice_is_echoed_in_responses_api_shape():+ iterator = 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": {"type": "function", "name": "run_command"},+ },+ custom_llm_provider="anthropic",+ litellm_metadata={},+ )++ events = list(iterator)++ response_events = [event for event in events if getattr(event, "type", None) in RESPONSE_ID_EVENT_TYPES]+ assert [event.type for event in response_events] == [+ "response.created",+ "response.in_progress",+ "response.completed",+ ]+ assert response_events[0].response.tool_choice == {"type": "function", "name": "run_command"}+ assert response_events[1].response.tool_choice == {"type": "function", "name": "run_command"}+ assert any(getattr(event, "type", None) == "response.output_item.done" for event in events)