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]

Merge remote-tracking branch 'origin/litellm_internal_staging' into HEAD

user0c864880a81d
diff --cc litellm/proxy/middleware/prometheus_auth_middleware.py
index 513ecd1bc80,3b30fd3d63c..cfc4cbd64b2
--- a/litellm/proxy/middleware/prometheus_auth_middleware.py
+++ b/litellm/proxy/middleware/prometheus_auth_middleware.py
@@@ -38,33 -39,23 +39,42 @@@ class PrometheusAuthMiddleware
await self.app(scope, receive, send)
return
- # Only run auth if configured to do so
- if litellm.require_auth_for_metrics_endpoint is True:
+ # Run auth by default; allow legacy public metrics only when explicitly disabled.
+ if litellm.require_auth_for_metrics_endpoint is not False:
- # Construct Request only when auth is actually needed
- request = Request(scope, receive)
+ # user_api_key_auth reads the request body, which consumes ASGI `receive`.
+ # Buffer those messages and replay them for the inner app; otherwise a
+ # successful auth would forward an exhausted receive and /metrics hangs.
+ buffered_messages: List[MutableMapping[str, Any]] = []
+
+ async def receive_for_auth() -> MutableMapping[str, Any]:
+ message = await receive()
+ buffered_messages.append(message)
+ return message
+
+ request = Request(scope, receive_for_auth)
- api_key = request.headers.get(_AUTHORIZATION_HEADER) or ""
try:
- await user_api_key_auth(request=request, api_key=api_key)
+ await user_api_key_auth(
+ request=request,
+ api_key=request.headers.get(_AUTHORIZATION_HEADER) or "",
+ azure_api_key_header=request.headers.get(
+ SpecialHeaders.azure_authorization.value
+ )
+ or "",
+ anthropic_api_key_header=request.headers.get(
+ SpecialHeaders.anthropic_authorization.value
+ ),
+ google_ai_studio_api_key_header=request.headers.get(
+ SpecialHeaders.google_ai_studio_authorization.value
+ ),
+ azure_apim_header=request.headers.get(
+ SpecialHeaders.azure_apim_authorization.value
+ )
+ or "",
+ custom_litellm_key_header=request.headers.get(
+ SpecialHeaders.custom_litellm_api_key.value
+ ),
+ )
except Exception as e:
# Send 401 response directly via ASGI protocol
error_message = getattr(e, "message", str(e))
diff --cc tests/test_litellm/proxy/middleware/test_prometheus_auth_middleware.py
index 4bc707209ad,310ee11573b..6cab5baee9a
--- a/tests/test_litellm/proxy/middleware/test_prometheus_auth_middleware.py
+++ b/tests/test_litellm/proxy/middleware/test_prometheus_auth_middleware.py
@@@ -1,18 -1,18 +1,5 @@@
--import json
--import os
--import sys
--
--import pytest
--from fastapi.testclient import TestClient
--
--sys.path.insert(
-- 0, os.path.abspath("../../..")
--) # Adds the parent directory to the system path
--
--
import pytest
from fastapi import FastAPI
--from fastapi.responses import JSONResponse
from fastapi.testclient import TestClient
import litellm
@@@ -26,15 -26,24 +13,20 @@@ async def fake_valid_auth(request, api_
return
+ async def fake_valid_auth_reads_body(request, api_key, **kwargs):
+ """
+ Like real user_api_key_auth, consumes the ASGI body stream. Regression test
+ for successful auth passing a drained receive to the inner app (hang).
+ """
+ await request.body()
+ return
+
+
-async def fake_invalid_auth(request, api_key):
- print("running fake invalid auth", request, api_key)
+async def fake_invalid_auth(request, api_key, **kwargs):
- print("running fake invalid auth", request, api_key)
# Simulate invalid auth by raising an exception.
raise Exception("Invalid API key")
--from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
--
--
@pytest.fixture
def app_with_middleware():
"""Create a FastAPI app with the PrometheusAuthMiddleware and dummy endpoints."""