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): normalize chat tool_choice for completions→responses bridge (#27634)
Sameer Kankute5e016f9f7488
diff --git a/litellm/completion_extras/litellm_responses_transformation/transformation.py b/litellm/completion_extras/litellm_responses_transformation/transformation.pyindex da3b9184edb..32423f23314 100644--- a/litellm/completion_extras/litellm_responses_transformation/transformation.py+++ b/litellm/completion_extras/litellm_responses_transformation/transformation.py@@ -119,6 +119,20 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):def __init__(self):pass+ def _normalize_tool_choice_for_responses_api(self, tool_choice: Any) -> Any:+ """Chat tool_choice uses function.name; Responses API expects top-level name."""+ if not isinstance(tool_choice, dict) or tool_choice.get("type") != "function":+ return tool_choice+ if isinstance(tool_choice.get("name"), str) and tool_choice.get("name"):+ # Return only Responses shape so stray chat ``function`` key is not sent upstream.+ return {"type": "function", "name": tool_choice["name"]}+ fn = tool_choice.get("function")+ if isinstance(fn, dict):+ fn_name = fn.get("name")+ if isinstance(fn_name, str) and fn_name:+ return {"type": "function", "name": fn_name}+ return tool_choice+def _handle_raw_dict_response_item(self, item: Dict[str, Any], index: int) -> Tuple[Optional[Any], int]:@@ -309,6 +323,10 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):text_format = self._transform_response_format_to_text_format(value)if text_format:responses_api_request["text"] = text_format # type: ignore+ elif key == "tool_choice":+ responses_api_request["tool_choice"] = ( # type: ignore[assignment]+ self._normalize_tool_choice_for_responses_api(value)+ )elif key in ResponsesAPIOptionalRequestParams.__annotations__.keys():responses_api_request[key] = value # type: ignoreelif key == "previous_response_id":diff --git a/tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py b/tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.pyindex e40543e01a0..697a9ebc720 100644--- a/tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py+++ b/tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py@@ -2098,6 +2098,56 @@ def test_map_optional_params_preserves_reasoning_summary():assert responses_api_request["reasoning"]["summary"] == "detailed"+def test_map_optional_params_tool_choice_chat_nested_to_responses_api():+ """Chat tool_choice must become Responses ToolChoiceFunction (top-level name)."""+ from litellm.completion_extras.litellm_responses_transformation.transformation import (+ LiteLLMResponsesTransformationHandler,+ )+ from litellm.types.llms.openai import ResponsesAPIOptionalRequestParams++ handler = LiteLLMResponsesTransformationHandler()+ responses_api_request = ResponsesAPIOptionalRequestParams()+ handler._map_optional_params_to_responses_api_request(+ {+ "stream": False,+ "tool_choice": {+ "type": "function",+ "function": {"name": "Echo"},+ },+ },+ responses_api_request,+ )+ assert responses_api_request["tool_choice"] == {+ "type": "function",+ "name": "Echo",+ }+++ ("tool_choice", "expected"),+ [+ ("auto", "auto"),+ ("none", "none"),+ (+ {"type": "function", "name": "Echo"},+ {"type": "function", "name": "Echo"},+ ),+ (+ {"type": "function", "name": "foo", "function": {"name": "bar"}},+ {"type": "function", "name": "foo"},+ ),+ ({"type": "required"}, {"type": "required"}),+ ],+)+def test_normalize_tool_choice_for_responses_api(tool_choice, expected):+ from litellm.completion_extras.litellm_responses_transformation.transformation import (+ LiteLLMResponsesTransformationHandler,+ )++ handler = LiteLLMResponsesTransformationHandler()+ assert handler._normalize_tool_choice_for_responses_api(tool_choice) == expected++def test_convert_chat_completion_file_type_to_input_file():"""Test that Chat Completion content with type 'file' is correctly mapped