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]

fix(proxy): keep the model on failed rows that resolved to a configured model group

mateo-berria93753e73bfc
diff --git a/litellm/proxy/spend_tracking/spend_tracking_utils.py b/litellm/proxy/spend_tracking/spend_tracking_utils.py
index 5690e43ab2b..f8cd73938b4 100644
--- a/litellm/proxy/spend_tracking/spend_tracking_utils.py
+++ b/litellm/proxy/spend_tracking/spend_tracking_utils.py
@@ -443,9 +443,11 @@ def get_logging_payload(kwargs, response_obj, start_time, end_time) -> SpendLogs
resolved_model: Final = (
standard_logging_payload.get("model") if standard_logging_payload is not None else None
) or reconstruct_model_name(raw_model, custom_llm_provider, metadata or {})
- failed_with_prompt_shaped_model: Final = _get_status_for_spend_log(
- metadata=metadata
- ) == "failure" and not _looks_like_model_name(resolved_model)
+ failed_with_prompt_shaped_model: Final = (
+ _get_status_for_spend_log(metadata=metadata) == "failure"
+ and not _model_group
+ and not _looks_like_model_name(resolved_model)
+ )
model_name: Final = (
UNKNOWN_MODEL_SPEND_LOG_MODEL
if rejected_as_unknown_model or failed_with_prompt_shaped_model
diff --git a/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py b/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py
index 11cf7ac530e..03c34e9e198 100644
--- a/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py
+++ b/tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py
@@ -141,7 +141,9 @@ def test_batch_lifecycle_rows_derive_the_same_session_from_the_batch_id():
from litellm.proxy.spend_tracking.spend_tracking_utils import _get_batch_trace_session_id
create_session: Final = _get_batch_trace_session_id(call_type="acreate_batch", request_id="batch-uid-1")
- cost_session: Final = _get_batch_trace_session_id(call_type="aretrieve_batch", request_id="batch-uid-1_batch_cost")
+ cost_session: Final = _get_batch_trace_session_id(
+ call_type="aretrieve_batch", request_id="batch-uid-1_batch_cost"
+ )
assert create_session == cost_session == "batch-uid-1"
@@ -974,17 +976,29 @@ def test_get_logging_payload_replaces_rejected_or_prompt_shaped_models_with_the_
assert payload["model"] == expected_model
-def test_get_logging_payload_keeps_a_whitespace_model_name_on_success():
+ ("metadata", "response_obj"),
+ [
+ ({"user_api_key": "sk-test"}, litellm.ModelResponse(id="chatcmpl-test", choices=[])),
+ (
+ {"user_api_key": "sk-test", "model_group": "team alias", "status": "failure"},
+ ValueError("provider timed out"),
+ ),
+ ],
+)
+def test_get_logging_payload_keeps_a_whitespace_model_name_on_success_or_a_routed_failure(
+ metadata: dict[str, str], response_obj: litellm.ModelResponse | Exception
+):
kwargs: Final = {
"model": _RAW_MODEL_WITH_PROMPT,
"messages": [{"role": "user", "content": "hi"}],
"call_type": "acompletion",
- "litellm_params": {"metadata": {"user_api_key": "sk-test"}},
+ "litellm_params": {"metadata": metadata},
}
payload: Final = get_logging_payload(
kwargs=kwargs,
- response_obj=litellm.ModelResponse(id="chatcmpl-test", choices=[]),
+ response_obj=response_obj,
start_time=datetime.datetime.now(timezone.utc),
end_time=datetime.datetime.now(timezone.utc),
)
@@ -4322,7 +4336,7 @@ ANTHROPIC_MESSAGES_SSE_CHUNKS: Final = (
'event: content_block_stop\ndata: {"type":"content_block_stop","index":0}\n\n',
'event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"end_turn"},'
'"usage":{"output_tokens":4}}\n\n',
- 'event: message_stop\ndata: {"type":"message_stop"}\n\n',
+ "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n",
)
@@ -4360,7 +4374,9 @@ def test_spend_log_request_id_is_the_message_id_a_non_streaming_messages_caller_
"""
logging_obj = _anthropic_messages_logging_obj(stream=False)
- logged_response = logging_obj._handle_anthropic_messages_response_logging(result=ANTHROPIC_MESSAGES_RESPONSE)
+ logged_response = logging_obj._handle_anthropic_messages_response_logging(
+ result=ANTHROPIC_MESSAGES_RESPONSE
+ )
assert logged_response.id == "msg_01Lit6806NonStreaming"
assert (
@@ -4436,7 +4452,9 @@ def test_spend_log_request_id_still_falls_back_to_litellm_call_id_without_a_prov
end_time=datetime.datetime.now(timezone.utc),
logging_obj=logging_obj,
)
- assert logging_obj.model_call_details["complete_streaming_response"].id == ("6806cafe-0000-4000-8000-000000000001")
+ assert logging_obj.model_call_details["complete_streaming_response"].id == (
+ "6806cafe-0000-4000-8000-000000000001"
+ )
def test_spend_log_request_id_for_chat_completions_is_untouched():