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 litellm_gigachat_passthrough_25886

mateo-berri59732f068b0e
diff --cc litellm/proxy/common_request_processing.py
index 67306ea7d46,ed66bb65ced..1d860b02875
--- a/litellm/proxy/common_request_processing.py
+++ b/litellm/proxy/common_request_processing.py
@@@ -2409,64 -2380,15 +2409,25 @@@ class ProxyBaseLLMRequestProcessing
if requested_model_from_client:
self.data["_litellm_client_requested_model"] = requested_model_from_client
- # Streaming: attach a closure that fires after all guardrail
- # end-of-stream blocks complete. CSW.__anext__ stores the
- # assembled response on logging_obj; the outer consumer
- # (ProxyLogging._fire_deferred_stream_logging) fires the
- # closure after the full streaming pipeline finishes.
- # The closure runs non-apply_guardrail hooks on the
- # assembled response, then fires success logging.
- # Only for CustomStreamWrapper — raw async generators from
- # passthrough routes bypass CSW and would orphan the closure.
- from litellm.litellm_core_utils.streaming_handler import (
- CustomStreamWrapper,
- )
-
- if _post_call_guardrails_active and isinstance(response, CustomStreamWrapper):
- # Intentionally a live reference (not a copy) — mirrors
- # ProxyLogging.post_call_success_hook which also mutates
- # data["guardrail_to_apply"] during iteration.
- _captured_data: Final = self.data
- _captured_user_api_key_dict: Final = user_api_key_dict
- _captured_logging_obj: Final = logging_obj
-
- async def _on_deferred_stream_complete(assembled_response: object, cache_hit: object) -> None:
- await ProxyBaseLLMRequestProcessing._run_deferred_stream_guardrails(
- captured_data=_captured_data,
- captured_user_api_key_dict=_captured_user_api_key_dict,
- captured_logging_obj=_captured_logging_obj,
- assembled_response=assembled_response,
- cache_hit=cache_hit,
- )
-
- logging_obj._on_deferred_stream_complete = _on_deferred_stream_complete
- elif (
- _post_call_guardrails_active
- and route_type == "anthropic_messages"
- and self._is_streaming_response(response)
- ):
- from litellm.litellm_core_utils.logging_worker import (
- GLOBAL_LOGGING_WORKER,
+ if _post_call_guardrails_active:
+ self._arm_deferred_stream_dispatch(
+ response=response,
+ route_type=route_type,
+ user_api_key_dict=user_api_key_dict,
+ logging_obj=logging_obj,
)
- async def _on_deferred_native_stream_complete(
- logging_coroutine: Coroutine[object, object, object],
- ) -> None:
- GLOBAL_LOGGING_WORKER.ensure_initialized_and_enqueue(async_coroutine=logging_coroutine)
-
- logging_obj._on_deferred_stream_complete = _on_deferred_native_stream_complete
-
if route_type == "allm_passthrough_route":
+ upstream_response_headers: Final = getattr(response, "headers", None)
+ streaming_headers: Final = (
+ ProxyBaseLLMRequestProcessing._merge_passthrough_streaming_headers(
+ response_headers=upstream_response_headers,
+ custom_headers=custom_headers,
+ )
+ if upstream_response_headers is not None
+ else custom_headers
+ )
+
# Check if response is an async generator
if self._is_streaming_response(response):
if asyncio.iscoroutine(response):