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(proxy): skip model override when response has no model field (#31183)
Sameer Kankutee5da5a3b6d57
diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.pyindex 92f8dd8df98..c2ffd9b077d 100644--- a/litellm/proxy/common_request_processing.py+++ b/litellm/proxy/common_request_processing.py@@ -516,12 +516,13 @@ def _override_openai_response_model(LiteLLM internally prefixes some provider/deployment model identifiers (e.g. `hosted_vllm/...`).That internal identifier should not be returned to clients in the OpenAI `model` field.- Note: This is intentionally verbose. A model mismatch is a useful signal that an internal- model identifier is being stamped/preserved somewhere in the request/response pipeline.- We log mismatches as warnings (and then restamp to the client-requested value) so these- paths stay observable for maintainers/operators without breaking client compatibility.+ Note: This is intentionally verbose at debug level. A model mismatch is a useful signal that an+ internal model identifier is being stamped/preserved somewhere in the request/response pipeline.+ We log mismatches as debug (and then restamp to the client-requested value) so these paths stay+ observable for maintainers without breaking client compatibility or alarming operators.- Errors are reserved for cases where the proxy cannot read/override the response model field.+ Responses that omit an OpenAI-style `model` field are left unchanged (silent return),+ including dict responses with no `model` key.Exceptions:1. If a fallback occurred (indicated by x-litellm-attempted-fallbacks header),@@ -577,6 +578,8 @@ def _override_openai_response_model(returnif isinstance(response_obj, dict):+ if "model" not in response_obj:+ returndownstream_model = response_obj.get("model")if downstream_model != requested_model:verbose_proxy_logger.debug(@@ -589,11 +592,6 @@ def _override_openai_response_model(returnif not hasattr(response_obj, "model"):- verbose_proxy_logger.error(- "%s: cannot override response model; missing `model` attribute. response_type=%s",- log_context,- type(response_obj),- )returndownstream_model = getattr(response_obj, "model", None)@@ -608,7 +606,7 @@ def _override_openai_response_model(try:setattr(response_obj, "model", requested_model)except Exception as e:- verbose_proxy_logger.error(+ verbose_proxy_logger.debug("%s: failed to override response.model=%r on response_type=%s. error=%s",log_context,requested_model,diff --git a/tests/test_litellm/proxy/test_common_request_processing.py b/tests/test_litellm/proxy/test_common_request_processing.pyindex 364861d6e31..3d3a2cc1013 100644--- a/tests/test_litellm/proxy/test_common_request_processing.py+++ b/tests/test_litellm/proxy/test_common_request_processing.py@@ -2022,6 +2022,52 @@ class TestOverrideOpenAIResponseModel:assert response_obj.model == requested_model+ def test_skips_model_override_when_response_has_no_model_attribute(self):+ from litellm.llms.base_llm.search.transformation import SearchResponse, SearchResult++ response_obj = SearchResponse(+ results=[SearchResult(title="t", url="http://x.com", snippet="s")],+ object="search",+ )++ _override_openai_response_model(+ response_obj=response_obj,+ requested_model="my-search-tool",+ log_context="test_context",+ )++ assert not hasattr(response_obj, "model")++ def test_skips_model_override_for_dict_without_model_key(self):+ response_obj = {+ "object": "search",+ "results": [{"title": "t", "url": "http://x.com", "snippet": "s"}],+ }++ _override_openai_response_model(+ response_obj=response_obj,+ requested_model="my-search-tool",+ log_context="test_context",+ )++ assert "model" not in response_obj++ def test_override_model_swallows_setattr_failure(self):+ class ReadOnlyModelResponse:+ @property+ def model(self) -> str:+ return "downstream-model"++ response_obj = ReadOnlyModelResponse()++ _override_openai_response_model(+ response_obj=response_obj,+ requested_model="my-model",+ log_context="test_context",+ )++ assert response_obj.model == "downstream-model"+class TestIsAzureModelRouterRequest:"""Tests for _is_azure_model_router_request helper"""