Skip to content

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]

perf: cache model_fields.keys() as frozensets in convert_to_model_response_object (15% faster)

Ryan Crabbe2065e5b88b43
diff --git a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py
index 25ad0a570cb..f98158f0124 100644
--- a/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py
+++ b/litellm/litellm_core_utils/llm_response_utils/convert_dict_to_response.py
@@ -6,7 +6,6 @@ from typing import Dict, Iterable, List, Literal, Optional, Tuple, Union
import litellm
from litellm._logging import verbose_logger
-from litellm._uuid import uuid
from litellm.constants import RESPONSE_FORMAT_TOOL_NAME
from litellm.litellm_core_utils.prompt_templates.common_utils import (
_extract_reasoning_content,
@@ -46,6 +45,12 @@ from litellm.types.utils import (
from .get_headers import get_response_headers
+_MESSAGE_FIELDS: frozenset = frozenset(Message.model_fields.keys())
+_CHOICES_FIELDS: frozenset = frozenset(Choices.model_fields.keys())
+_MODEL_RESPONSE_FIELDS: frozenset = frozenset(ModelResponse.model_fields.keys()) | {
+ "usage"
+}
+
def _safe_convert_created_field(created_value) -> int:
"""
@@ -443,7 +448,6 @@ def convert_to_model_response_object( # noqa: PLR0915
bool
] = None, # used for supporting 'json_schema' on older models
):
- received_args = locals()
additional_headers = get_response_headers(_response_headers)
if hidden_params is None:
@@ -546,11 +550,10 @@ def convert_to_model_response_object( # noqa: PLR0915
message = litellm.Message(content=json_mode_content_str)
finish_reason = "stop"
if message is None:
- provider_specific_fields = {}
- message_keys = Message.model_fields.keys()
- for field in choice["message"].keys():
- if field not in message_keys:
- provider_specific_fields[field] = choice["message"][field]
+ provider_specific_fields = {
+ f: choice["message"][f]
+ for f in choice["message"].keys() - _MESSAGE_FIELDS
+ }
# Handle reasoning models that display `reasoning_content` within `content`
reasoning_content, content = _extract_reasoning_content(
@@ -599,10 +602,9 @@ def convert_to_model_response_object( # noqa: PLR0915
finish_reason = "tool_calls"
## PROVIDER SPECIFIC FIELDS ##
- provider_specific_fields = {}
- for field in choice.keys():
- if field not in Choices.model_fields.keys():
- provider_specific_fields[field] = choice[field]
+ provider_specific_fields = {
+ f: choice[f] for f in choice.keys() - _CHOICES_FIELDS
+ }
logprobs = choice.get("logprobs", None)
enhancements = choice.get("enhancements", None)
@@ -626,7 +628,7 @@ def convert_to_model_response_object( # noqa: PLR0915
)
if "id" in response_object:
- model_response_object.id = response_object["id"] or str(uuid.uuid4())
+ model_response_object.id = response_object["id"]
if "system_fingerprint" in response_object:
model_response_object.system_fingerprint = response_object[
@@ -661,10 +663,8 @@ def convert_to_model_response_object( # noqa: PLR0915
if _response_headers is not None:
model_response_object._response_headers = _response_headers
- special_keys = list(litellm.ModelResponse.model_fields.keys())
- special_keys.append("usage")
for k, v in response_object.items():
- if k not in special_keys:
+ if k not in _MODEL_RESPONSE_FIELDS:
setattr(model_response_object, k, v)
return model_response_object
@@ -781,6 +781,17 @@ def convert_to_model_response_object( # noqa: PLR0915
return model_response_object
except Exception:
+ received_args = dict(
+ response_object=response_object,
+ model_response_object=model_response_object,
+ response_type=response_type,
+ stream=stream,
+ start_time=start_time,
+ end_time=end_time,
+ hidden_params=hidden_params,
+ _response_headers=_response_headers,
+ convert_tool_call_to_json_mode=convert_tool_call_to_json_mode,
+ )
raise Exception(
f"Invalid response object {traceback.format_exc()}\n\nreceived_args={received_args}"
)
diff --git a/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.py b/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.py
index c151150f634..6ae3d273e28 100644
--- a/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.py
+++ b/tests/llm_translation/test_llm_response_utils/test_convert_dict_to_chat_completion.py
@@ -1059,3 +1059,182 @@ def test_convert_to_model_response_object_with_error_code_only():
_response_headers=None,
convert_tool_call_to_json_mode=False,
)
+
+
+def test_model_prefix_preservation():
+ """
+ Test that when model_response_object has a prefix like 'openai/gpt-4'
+ and the response contains a different model name, the prefix is preserved.
+ """
+ response_object = {
+ "id": "chatcmpl-prefix-test",
+ "choices": [
+ {
+ "index": 0,
+ "message": {"role": "assistant", "content": "Hello"},
+ "finish_reason": "stop",
+ }
+ ],
+ "usage": {"prompt_tokens": 5, "completion_tokens": 3, "total_tokens": 8},
+ "model": "gpt-4o",
+ }
+
+ result = convert_to_model_response_object(
+ model_response_object=ModelResponse(model="openai/gpt-4"),
+ response_object=response_object,
+ stream=False,
+ start_time=datetime.now(),
+ end_time=datetime.now(),
+ )
+
+ assert result.model == "openai/gpt-4o"
+
+
+def test_model_without_prefix():
+ """
+ Test that when model_response_object has no prefix (e.g. 'gpt-4'),
+ the original model is kept (provider response model is ignored).
+ """
+ response_object = {
+ "id": "chatcmpl-no-prefix",
+ "choices": [
+ {
+ "index": 0,
+ "message": {"role": "assistant", "content": "Hi"},
+ "finish_reason": "stop",
+ }
+ ],
+ "usage": {"prompt_tokens": 5, "completion_tokens": 2, "total_tokens": 7},
+ "model": "gpt-4o-2024-08-06",
+ }
+
+ result = convert_to_model_response_object(
+ model_response_object=ModelResponse(model="gpt-4"),
+ response_object=response_object,
+ stream=False,
+ start_time=datetime.now(),
+ end_time=datetime.now(),
+ )
+
+ assert result.model == "gpt-4"
+
+
+def test_extra_response_fields_preserved():
+ """
+ Test that extra response fields (e.g. service_tier) are preserved
+ on the returned ModelResponse object.
+ """
+ response_object = {
+ "id": "chatcmpl-extra-fields",
+ "choices": [
+ {
+ "index": 0,
+ "message": {"role": "assistant", "content": "Hello"},
+ "finish_reason": "stop",
+ }
+ ],
+ "usage": {"prompt_tokens": 5, "completion_tokens": 3, "total_tokens": 8},
+ "model": "gpt-4o",
+ "service_tier": "default",
+ }
+
+ result = convert_to_model_response_object(
+ model_response_object=ModelResponse(),
+ response_object=response_object,
+ stream=False,
+ start_time=datetime.now(),
+ end_time=datetime.now(),
+ )
+
+ assert result.service_tier == "default"
+
+
+def test_hidden_params_and_response_headers_set():
+ """
+ Test that _hidden_params and _response_headers are correctly set
+ on the returned ModelResponse.
+ """
+ response_object = {
+ "id": "chatcmpl-headers",
+ "choices": [
+ {
+ "index": 0,
+ "message": {"role": "assistant", "content": "Hello"},
+ "finish_reason": "stop",
+ }
+ ],
+ "usage": {"prompt_tokens": 5, "completion_tokens": 3, "total_tokens": 8},
+ "model": "gpt-4o",
+ }
+ response_headers = {"x-request-id": "req_abc123"}
+
+ result = convert_to_model_response_object(
+ model_response_object=ModelResponse(),
+ response_object=response_object,
+ stream=False,
+ start_time=datetime.now(),
+ end_time=datetime.now(),
+ hidden_params={"custom_key": "custom_value"},
+ _response_headers=response_headers,
+ )
+
+ assert result._hidden_params is not None
+ assert result._hidden_params["custom_key"] == "custom_value"
+ assert "additional_headers" in result._hidden_params
+ assert result._response_headers == response_headers
+
+
+def test_response_ms_computed():
+ """
+ Test that _response_ms is computed correctly from start_time and end_time.
+ """
+ response_object = {
+ "id": "chatcmpl-timing",
+ "choices": [
+ {
+ "index": 0,
+ "message": {"role": "assistant", "content": "Hello"},
+ "finish_reason": "stop",
+ }
+ ],
+ "usage": {"prompt_tokens": 5, "completion_tokens": 3, "total_tokens": 8},
+ "model": "gpt-4o",
+ }
+ start = datetime(2024, 1, 1, 12, 0, 0)
+ end = start + timedelta(milliseconds=250)
+
+ result = convert_to_model_response_object(
+ model_response_object=ModelResponse(),
+ response_object=response_object,
+ stream=False,
+ start_time=start,
+ end_time=end,
+ )
+
+ assert result._response_ms == pytest.approx(250.0)
+
+
+def test_error_message_includes_function_args():
+ """
+ Test that when an exception occurs, the error message includes
+ the function arguments for debugging (deferred locals() - Opt 2).
+ """
+ # Pass a response_object that will cause an error inside the try block
+ # (e.g. choices is not iterable)
+ response_object = {
+ "choices": None, # will fail the assert
+ }
+
+ with pytest.raises(Exception) as exc_info:
+ convert_to_model_response_object(
+ model_response_object=ModelResponse(),
+ response_object=response_object,
+ stream=False,
+ start_time=datetime.now(),
+ end_time=datetime.now(),
+ )
+
+ error_msg = str(exc_info.value)
+ assert "received_args=" in error_msg
+ assert "response_object" in error_msg
+ assert "response_type" in error_msg