From e616590e5832538fafbfcf4e0dd8047c4c1e1022 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 20:22:25 -0700 Subject: [PATCH] fix(responses): drop Azure and off-catalog reasoning values and namespace tool_choice under drop_params Azure and models missing from the cost map now check reasoning mode, context and summary against the values any OpenAI row declares and drop unknown keys. A function tool_choice must name a top-level function, since OpenAI rejects one naming a function inside a namespace. --- .../llms/openai/responses/transformation.py | 17 +++---- litellm/responses/utils.py | 19 ++++--- .../test_openai_responses_transformation.py | 51 ++++++++++++++----- tests/unit/responses/test_responses_utils.py | 20 ++++++++ 4 files changed, 75 insertions(+), 32 deletions(-) diff --git a/litellm/llms/openai/responses/transformation.py b/litellm/llms/openai/responses/transformation.py index f3266383c91..e3b1973f505 100644 --- a/litellm/llms/openai/responses/transformation.py +++ b/litellm/llms/openai/responses/transformation.py @@ -1,7 +1,6 @@ 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 @@ -47,7 +46,7 @@ from litellm.responses.litellm_completion_transformation.tool_search import ( tools_with_anthropic_tool_search_mapped, tools_without_anthropic_tool_search, ) -from litellm.responses.utils import openai_well_formed_reasoning +from litellm.responses.utils import openai_api_well_formed_reasoning, openai_well_formed_reasoning from litellm.secret_managers.main import get_secret_str from litellm.types.llms.base import LiteLLMBaseModel from litellm.types.llms.openai import * @@ -333,12 +332,6 @@ 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: @@ -369,7 +362,9 @@ def _accepts_tool_choice(tool_choice: object, params: Mapping[str, object]) -> b if isinstance(tool_choice, str): return tool_choice in _TOOL_CHOICES_WITHOUT_TOOLS or (tool_choice == "required" and 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))) + return bool(tools) and ( + not isinstance(tools, list | tuple) or _names_a_sent_tool(tool_choice, _tool_mappings(listed)) + ) def _is_well_formed(name: str, value: object, params: Mapping[str, object]) -> bool: @@ -539,8 +534,10 @@ class OpenAIResponsesAPIConfig(BaseResponsesAPIConfig): return None def _accepted_reasoning(self, reasoning: Mapping[str, object], model: str) -> Mapping[str, object]: - if self.custom_llm_provider != LlmProviders.OPENAI or not OpenAIGPTConfig.is_openai_catalog_model(model): + if self.custom_llm_provider not in _PROVIDERS_CHECKED_FOR_MALFORMED_VALUES: return reasoning + if self.custom_llm_provider != LlmProviders.OPENAI or not OpenAIGPTConfig.is_openai_catalog_model(model): + return openai_api_well_formed_reasoning(reasoning) return openai_well_formed_reasoning(reasoning, model, model_reasons=self._supports_reasoning_param(model=model)) def _is_well_formed_effort(self, effort: object, declared_reasoning_efforts: Collection[str] | None) -> bool: diff --git a/litellm/responses/utils.py b/litellm/responses/utils.py index 73933901000..c0365e530ca 100644 --- a/litellm/responses/utils.py +++ b/litellm/responses/utils.py @@ -18,7 +18,6 @@ from litellm.litellm_core_utils.dot_notation_indexing import delete_nested_value from litellm.litellm_core_utils.get_model_cost_map import GetModelCostMap from litellm.litellm_core_utils.get_provider_specific_headers import ProviderSpecificHeaderUtils from litellm.llms.base_llm.responses.transformation import BaseResponsesAPIConfig, ReasoningSummaryLevel -from litellm.llms.openai.chat.gpt_transformation import OpenAIGPTConfig from litellm.types.llms.openai import ( AllMessageValues, InputTokensDetails, @@ -1452,14 +1451,14 @@ def _declared_values(values: Sequence[str] | None, undeclared: frozenset[str]) - def _openai_declared_reasoning_field_values(model_info: ModelInfo | None) -> Mapping[str, frozenset[str | None] | None]: - if model_info is None: - return MappingProxyType({"summary": _SUMMARY_VALUES, "mode": None, "context": None}) declared_anywhere: Final = _reasoning_values_any_openai_row_declares() + modes: Final = None if model_info is None else model_info.get("reasoning_modes") + contexts: Final = None if model_info is None else model_info.get("reasoning_contexts") return MappingProxyType( { "summary": _SUMMARY_VALUES, - "mode": _declared_values(model_info.get("reasoning_modes"), declared_anywhere["mode"]), - "context": _declared_values(model_info.get("reasoning_contexts"), declared_anywhere["context"]), + "mode": _declared_values(modes, declared_anywhere["mode"]), + "context": _declared_values(contexts, declared_anywhere["context"]), } ) @@ -1494,6 +1493,10 @@ def _openai_accepted_reasoning(reasoning: Mapping[str, object], model: str) -> M return _reasoning_with_accepted_fields(reasoning, _openai_reasoning_field_values(model)) +def openai_api_well_formed_reasoning(reasoning: Mapping[str, object]) -> Mapping[str, object]: + return _reasoning_with_accepted_fields(reasoning, _openai_declared_reasoning_field_values(None)) + + def openai_well_formed_reasoning( reasoning: Mapping[str, object], model: str, *, model_reasons: bool ) -> Mapping[str, object]: @@ -1525,11 +1528,7 @@ def openai_bridge_reasoning( return None kept: Final = requested if effort_kept else {key: value for key, value in requested.items() if key != "effort"} with_summary: Final = reasoning_with_summary_default(kept, summary_alias, model, "openai") - accepted: Final = ( - _openai_accepted_reasoning(with_summary, model) - if drop_params and OpenAIGPTConfig.is_openai_catalog_model(model) - else {**with_summary} - ) + accepted: Final = _openai_accepted_reasoning(with_summary, model) if drop_params else {**with_summary} remaining: Final = as_str_mapping( _apply_nested_drop_params({"reasoning_effort": accepted}, list(drop_paths)).get("reasoning_effort") ) 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 53d40bb9e01..d693df4d72c 100644 --- a/tests/unit/llms/openai/responses/test_openai_responses_transformation.py +++ b/tests/unit/llms/openai/responses/test_openai_responses_transformation.py @@ -3327,6 +3327,10 @@ class TestMalformedParamsFollowDropParams: {"tool_choice": {"type": "function", "name": "get_time"}, "tools": [_WEATHER_TOOL]}, id="function-naming-a-tool-not-sent", ), + pytest.param( + {"tool_choice": {"type": "function", "name": "get_weather"}, "tools": [_WEATHER_NAMESPACE]}, + id="function-inside-a-namespace", + ), 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"), @@ -3384,10 +3388,6 @@ 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", @@ -3429,7 +3429,12 @@ class TestMalformedParamsFollowDropParams: 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} + params: Final = { + "tool_choice": "required", + "parallel_tool_calls": "true", + "previous_response_id": 12, + "reasoning": {"effort": "high", "mode": "turbo", "generate_summary": "auto"}, + } assert HostedVLLMResponsesAPIConfig().map_openai_params(dict(params), "my-vllm-model", True) == params @@ -3528,17 +3533,39 @@ class TestMalformedParamsFollowDropParams: @pytest.mark.parametrize( ("config", "model"), - [(AzureOpenAIResponsesAPIConfig(), "gpt-6.1-sol"), (OpenAIResponsesAPIConfig(), "my-fine-tuned-model")], - ids=["azure", "outside-openai-catalog"], + [ + (AzureOpenAIResponsesAPIConfig(), "gpt-6.1-sol"), + (AzureOpenAIOSeriesResponsesAPIConfig(), "o_series/gpt-6.1-sol"), + (OpenAIResponsesAPIConfig(), "my-fine-tuned-model"), + ], + ids=["azure", "azure-o-series", "outside-openai-catalog"], ) - def test_reasoning_fields_pass_through_where_the_openai_catalog_does_not_apply( - self, local_model_cost_map, config, model + def test_reasoning_values_no_openai_row_declares_are_dropped_where_the_openai_catalog_does_not_apply( + self, local_model_cost_map, monkeypatch, config, model ): - reasoning: Final = {"effort": "high", "mode": "turbo", "generate_summary": "auto"} + monkeypatch.setattr(litellm, "drop_params", False) + row: Final = litellm.model_cost["gpt-6.1-sol"] + declared: Final = { + "effort": "high", + "mode": row["reasoning_modes"][-1], + "context": row["reasoning_contexts"][-1], + "summary": "concise", + } + malformed: Final = { + "effort": "high", + "mode": "turbo", + "context": "forever", + "summary": "sometimes", + "generate_summary": "auto", + } - mapped: Final = config.map_openai_params({"reasoning": dict(reasoning)}, model, True) + dropped: Final = config.map_openai_params({"reasoning": dict(malformed)}, model, True) + kept: Final = config.map_openai_params({"reasoning": dict(declared)}, model, True) + sent: Final = config.map_openai_params({"reasoning": dict(malformed)}, model, False) - assert mapped["reasoning"] == reasoning + assert dropped["reasoning"] == {"effort": "high"} + assert kept["reasoning"] == declared + assert sent["reasoning"] == malformed @pytest.mark.asyncio diff --git a/tests/unit/responses/test_responses_utils.py b/tests/unit/responses/test_responses_utils.py index 8de72b4c3bb..a9db0127ca6 100644 --- a/tests/unit/responses/test_responses_utils.py +++ b/tests/unit/responses/test_responses_utils.py @@ -1039,6 +1039,26 @@ 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_drops_a_reasoning_value_no_openai_row_declares_outside_the_openai_catalog( + local_model_cost_map, +): + declared_mode: Final = litellm.model_cost["gpt-6.1-sol"]["reasoning_modes"][-1] + requested: Final = {"effort": "high", "mode": declared_mode, "context": "forever", "generate_summary": "auto"} + + def bridged(drop_params: bool) -> object: + return openai_bridge_reasoning( + requested=requested, + effort_kept=True, + summary_alias=None, + model="my-fine-tuned-model", + drop_params=drop_params, + additional_drop_params=None, + ) + + assert bridged(True) == {"effort": "high", "mode": declared_mode} + 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