From db720da71fab2837e9e800fe72f19f750b725772 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 18:42:28 -0700 Subject: [PATCH] fix(responses): drop a tool_choice naming a tool the request does not send, and run the unknown-effort drop on Azure o-series --- .../responses/o_series_transformation.py | 16 +++- .../llms/openai/responses/transformation.py | 40 +++++++++- .../test_openai_responses_transformation.py | 76 ++++++++++++++++++- tests/unit/responses/test_responses_utils.py | 21 +++++ 4 files changed, 145 insertions(+), 8 deletions(-) diff --git a/litellm/llms/azure/responses/o_series_transformation.py b/litellm/llms/azure/responses/o_series_transformation.py index 59abafea3f8..cf33d623396 100644 --- a/litellm/llms/azure/responses/o_series_transformation.py +++ b/litellm/llms/azure/responses/o_series_transformation.py @@ -8,6 +8,7 @@ Translations handled by LiteLLM: - Other parameters follow base Azure OpenAI Responses API behavior """ +from collections.abc import Collection, Mapping from typing import TYPE_CHECKING, Any, Final from litellm._logging import verbose_logger @@ -51,12 +52,14 @@ class AzureOpenAIOSeriesResponsesAPIConfig(AzureOpenAIResponsesAPIConfig): return o_series_supported_params - def map_openai_params( + def map_openai_params_for_deployment( self, response_api_optional_params: ResponsesAPIOptionalRequestParams, model: str, drop_params: bool, - ) -> dict: + *, + declared_reasoning_efforts: Collection[str] | None, + ) -> Mapping[str, object]: """ Map OpenAI parameters for Azure OpenAI O-series Responses API. @@ -65,13 +68,20 @@ class AzureOpenAIOSeriesResponsesAPIConfig(AzureOpenAIResponsesAPIConfig): """ if not drop_params: return dict(response_api_optional_params) - kept: Final = self._well_formed_params(response_api_optional_params) + kept: Final = self.params_dropped_for_deployment( + response_api_optional_params, model, declared_reasoning_efforts=declared_reasoning_efforts + ) if "temperature" in kept: verbose_logger.debug( "Dropping unsupported parameter 'temperature' for Azure OpenAI O-series responses API model %s", model ) return {name: value for name, value in kept.items() if name != "temperature"} + def _reasoning_refusal( + self, reasoning: object, model: str, declared_reasoning_efforts: Collection[str] | None + ) -> None: + return None + def is_o_series_model(self, model: str) -> bool: """ Check if the model is an O-series model. diff --git a/litellm/llms/openai/responses/transformation.py b/litellm/llms/openai/responses/transformation.py index 2a77291e6dc..f3266383c91 100644 --- a/litellm/llms/openai/responses/transformation.py +++ b/litellm/llms/openai/responses/transformation.py @@ -1,6 +1,7 @@ from collections.abc import Callable, Collection, Iterable, Mapping, Sequence from dataclasses import dataclass from functools import lru_cache +from itertools import chain from types import MappingProxyType from typing import TYPE_CHECKING, Any, Final, Protocol, TypeAlias, cast, get_args, get_type_hints @@ -325,13 +326,50 @@ _PROVIDERS_CHECKED_FOR_MALFORMED_VALUES: Final = (LlmProviders.OPENAI, LlmProvid _REASONING_EFFORTS: Final = frozenset(get_args(REASONING_EFFORT)) +_NON_HOSTED_TOOL_TYPES: Final = frozenset({"function", "custom", "mcp", "namespace"}) + + +def _tool_mappings(tools: object) -> tuple[Mapping[str, object], ...]: + return tuple(mapping for mapping in map(as_str_mapping, _json_array_or_empty(tools)) if mapping is not None) + + +def _sent_tools(tools: object) -> tuple[Mapping[str, object], ...]: + top_level: Final = _tool_mappings(tools) + namespaced: Final = (_tool_mappings(tool.get("tools")) for tool in top_level if tool.get("type") == "namespace") + return top_level + tuple(chain.from_iterable(namespaced)) + + +def _names_a_sent_tool(tool_choice: object, tools: tuple[Mapping[str, object], ...]) -> bool: + choice: Final = as_str_mapping(tool_choice) + if choice is None: + return True + match choice.get("type"): + case "allowed_tools": + allowed: Final = choice.get("tools") + entries: Final = _json_array_or_empty(allowed) + return not isinstance(allowed, list | tuple) or all(_names_a_sent_tool(entry, tools) for entry in entries) + case "function": + return any(tool.get("type") == "function" and tool.get("name") == choice.get("name") for tool in tools) + case "custom": + return any(tool.get("type") == "custom" for tool in tools) + case "mcp": + return any( + tool.get("type") == "mcp" and tool.get("server_label") == choice.get("server_label") for tool in tools + ) + case str(): + return any(tool.get("type") not in _NON_HOSTED_TOOL_TYPES for tool in tools) + case _: + return True + + def _accepts_tool_choice(tool_choice: object, params: Mapping[str, object]) -> bool: if tool_choice is None or params.get("prompt") is not None: return True tools: Final = params.get("tools") if isinstance(tool_choice, str): return tool_choice in _TOOL_CHOICES_WITHOUT_TOOLS or (tool_choice == "required" and bool(tools)) - return bool(tools) + listed: Final = _json_array_or_empty(tools) + return bool(tools) and (not isinstance(tools, list | tuple) or _names_a_sent_tool(tool_choice, _sent_tools(listed))) def _is_well_formed(name: str, value: object, params: Mapping[str, object]) -> bool: diff --git a/tests/unit/llms/openai/responses/test_openai_responses_transformation.py b/tests/unit/llms/openai/responses/test_openai_responses_transformation.py index a4a930c42a2..53d40bb9e01 100644 --- a/tests/unit/llms/openai/responses/test_openai_responses_transformation.py +++ b/tests/unit/llms/openai/responses/test_openai_responses_transformation.py @@ -3306,6 +3306,9 @@ class TestReasoningFollowsModelSupport: _WEATHER_TOOL: Final = {"type": "function", "name": "get_weather", "parameters": {"type": "object", "properties": {}}} +_SQL_TOOL: Final = {"type": "custom", "name": "run_sql"} +_DOCS_MCP_TOOL: Final = {"type": "mcp", "server_label": "docs", "server_url": "https://example.com/mcp"} +_WEATHER_NAMESPACE: Final = {"type": "namespace", "name": "weather", "description": "Weather", "tools": [_WEATHER_TOOL]} class TestMalformedParamsFollowDropParams: @@ -3320,6 +3323,24 @@ class TestMalformedParamsFollowDropParams: pytest.param({"tool_choice": "required"}, id="required-without-tools"), pytest.param({"tool_choice": "required", "tools": []}, id="required-with-empty-tools"), pytest.param({"tool_choice": {"type": "function", "name": "get_weather"}}, id="function-without-tools"), + pytest.param( + {"tool_choice": {"type": "function", "name": "get_time"}, "tools": [_WEATHER_TOOL]}, + id="function-naming-a-tool-not-sent", + ), + pytest.param({"tool_choice": {"type": "custom", "name": "run_sql"}, "tools": [_WEATHER_TOOL]}, id="custom"), + pytest.param({"tool_choice": {"type": "mcp", "server_label": "wiki"}, "tools": [_DOCS_MCP_TOOL]}, id="mcp"), + pytest.param({"tool_choice": {"type": "file_search"}, "tools": [_WEATHER_TOOL]}, id="hosted-tool-not-sent"), + pytest.param( + { + "tool_choice": { + "type": "allowed_tools", + "mode": "auto", + "tools": [{"type": "function", "name": "get_time"}], + }, + "tools": [_WEATHER_TOOL], + }, + id="allowed-tools-naming-a-tool-not-sent", + ), pytest.param({"parallel_tool_calls": "true"}, id="parallel-tool-calls-string"), pytest.param({"previous_response_id": 12}, id="previous-response-id-int"), pytest.param({"previous_response_id": ""}, id="previous-response-id-empty"), @@ -3363,11 +3384,50 @@ class TestMalformedParamsFollowDropParams: pytest.param({"parallel_tool_calls": None}, id="parallel-tool-calls-null"), pytest.param({"previous_response_id": "resp_abc"}, id="previous-response-id-string"), pytest.param({"tool_choice": "required", "prompt": {"id": "pmpt_abc"}}, id="required-with-stored-prompt"), + pytest.param( + {"tool_choice": {"type": "function", "name": "get_weather"}, "tools": [_WEATHER_NAMESPACE]}, + id="function-in-a-namespace", + ), + pytest.param( + {"tool_choice": {"type": "custom", "name": "other"}, "tools": [_SQL_TOOL]}, + id="custom-with-a-custom-tool", + ), + pytest.param( + {"tool_choice": {"type": "mcp", "server_label": "docs"}, "tools": [_DOCS_MCP_TOOL]}, id="mcp-label-sent" + ), + pytest.param( + {"tool_choice": {"type": "computer_use"}, "tools": [{"type": "computer"}]}, + id="hosted-alias-of-a-sent-tool", + ), + pytest.param( + { + "tool_choice": { + "type": "allowed_tools", + "mode": "required", + "tools": [{"type": "function", "name": "get_weather"}, {"type": "mcp", "server_label": "docs"}], + }, + "tools": [_WEATHER_TOOL, _DOCS_MCP_TOOL], + }, + id="allowed-tools-all-sent", + ), ], ) def test_a_well_formed_value_is_kept_under_drop_params(self, local_model_cost_map, params): assert OpenAIResponsesAPIConfig().map_openai_params(dict(params), "gpt-6.1-sol", True) == params + @pytest.mark.parametrize( + "params", + [ + pytest.param({"tool_choice": {"name": "get_time"}, "tools": [_WEATHER_TOOL]}, id="choice-without-a-type"), + pytest.param({"tool_choice": ["get_time"], "tools": [_WEATHER_TOOL]}, id="choice-not-an-object"), + pytest.param( + {"tool_choice": {"type": "function", "name": "get_time"}, "tools": _WEATHER_TOOL}, id="tools-not-a-list" + ), + ], + ) + def test_a_tool_choice_the_check_cannot_read_is_kept_under_drop_params(self, local_model_cost_map, params): + assert OpenAIResponsesAPIConfig().map_openai_params(dict(params), "gpt-6.1-sol", True) == params + def test_providers_outside_openai_and_azure_keep_these_values_as_sent(self, local_model_cost_map): params: Final = {"tool_choice": "required", "parallel_tool_calls": "true", "previous_response_id": 12} @@ -3439,8 +3499,12 @@ class TestMalformedParamsFollowDropParams: @pytest.mark.parametrize( ("config", "model"), - [(OpenAIResponsesAPIConfig(), "gpt-6.1-sol"), (AzureOpenAIResponsesAPIConfig(), "gpt-6.1-sol")], - ids=["openai", "azure"], + [ + (OpenAIResponsesAPIConfig(), "gpt-6.1-sol"), + (AzureOpenAIResponsesAPIConfig(), "gpt-6.1-sol"), + (AzureOpenAIOSeriesResponsesAPIConfig(), "o_series/gpt-6.1-sol"), + ], + ids=["openai", "azure", "azure-o-series"], ) def test_an_effort_outside_the_known_levels_is_dropped_unless_the_deployment_declares_it( self, local_model_cost_map, monkeypatch, config, model @@ -3450,8 +3514,12 @@ class TestMalformedParamsFollowDropParams: dropped: Final = config.map_openai_params({"reasoning": dict(reasoning)}, model, True) sent: Final = config.map_openai_params({"reasoning": dict(reasoning)}, model, False) - declared: Final = config.map_openai_params_for_deployment( - {"reasoning": dict(reasoning)}, model, True, declared_reasoning_efforts=("not-an-effort",) + declared: Final = ResponsesAPIRequestUtils.get_optional_params_responses_api( + model=model, + responses_api_provider_config=config, + response_api_optional_params={"reasoning": dict(reasoning)}, + drop_params=True, + declared_reasoning_efforts=("not-an-effort",), ) assert dropped["reasoning"] == {"summary": "auto"} diff --git a/tests/unit/responses/test_responses_utils.py b/tests/unit/responses/test_responses_utils.py index 5e6c98267fb..8de72b4c3bb 100644 --- a/tests/unit/responses/test_responses_utils.py +++ b/tests/unit/responses/test_responses_utils.py @@ -1038,3 +1038,24 @@ def test_the_chat_bridge_drops_a_reasoning_value_no_openai_row_declares_under_dr assert bridged(True) == {"effort": "high", "summary": "auto"} assert bridged(False) == requested + +def test_the_chat_bridge_keeps_only_effort_for_a_catalog_model_that_does_not_reason(local_model_cost_map): + non_reasoning_model: Final = next( + name + for name, row in litellm.model_cost.items() + if row.get("litellm_provider") == "openai" + and row.get("mode") == "chat" + and row.get("supports_reasoning") is False + and OpenAIGPTConfig.is_openai_catalog_model(name) + ) + + bridged: Final = openai_bridge_reasoning( + requested={"effort": "low", "summary": "auto", "mode": "standard"}, + effort_kept=True, + summary_alias=None, + model=non_reasoning_model, + drop_params=True, + additional_drop_params=None, + ) + + assert bridged == {"effort": "low"}