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): build failure headers immutably to keep LIT002 within budget

yucheng99aa9f76c8fc
diff --git a/litellm/proxy/batches_endpoints/endpoints.py b/litellm/proxy/batches_endpoints/endpoints.py
index f37c06aea97..5d9ecddd4c2 100644
--- a/litellm/proxy/batches_endpoints/endpoints.py
+++ b/litellm/proxy/batches_endpoints/endpoints.py
@@ -7,6 +7,7 @@
import asyncio
import os
from collections.abc import Mapping
+from types import MappingProxyType
from typing import Any, Final, cast
from fastapi import APIRouter, Depends, HTTPException, Path, Request, Response
@@ -731,7 +732,7 @@ async def list_batches(
)
verbose_proxy_logger.debug("GET /v1/batches after=%s limit=%s", after, limit)
- data: dict = {}
+ data: Mapping[str, object] = MappingProxyType({})
try:
if llm_router is None:
raise HTTPException(
diff --git a/litellm/proxy/common_utils/openai_error_payload.py b/litellm/proxy/common_utils/openai_error_payload.py
index cbc8c78d4f9..202c61b620e 100644
--- a/litellm/proxy/common_utils/openai_error_payload.py
+++ b/litellm/proxy/common_utils/openai_error_payload.py
@@ -68,3 +68,10 @@ def with_litellm_call_id(exc: ProxyException, litellm_call_id: str | None) -> Pr
if litellm_call_id is not None:
exc.headers.setdefault(LITELLM_CALL_ID_HEADER, litellm_call_id)
return exc
+
+
+def headers_with_litellm_call_id(headers: Mapping[str, str] | None, litellm_call_id: str) -> Mapping[str, str]:
+ """``headers`` plus ``x-litellm-call-id``, keeping the value they already carry under that name."""
+ if headers is None:
+ return MappingProxyType({LITELLM_CALL_ID_HEADER: litellm_call_id})
+ return MappingProxyType({LITELLM_CALL_ID_HEADER: litellm_call_id, **headers})
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 8f72cabf076..45beb8cc93c 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -392,7 +392,7 @@ from litellm.proxy.common_utils.openai_endpoint_utils import (
remove_sensitive_info_from_deployment,
)
from litellm.proxy.common_utils.openai_error_payload import (
- LITELLM_CALL_ID_HEADER,
+ headers_with_litellm_call_id,
litellm_call_id_headers,
with_litellm_call_id,
)
@@ -11696,7 +11696,7 @@ async def audio_speech(
raise HTTPException(
status_code=e.status_code,
detail=e.detail,
- headers={LITELLM_CALL_ID_HEADER: litellm_call_id, **(e.headers or {})},
+ headers=headers_with_litellm_call_id(e.headers, litellm_call_id),
)
raise ProxyException(
message=getattr(e, "message", f"{e}"),