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 branch 'main' into litellm_dev_sameer_16_march_week

Sameer Kankute4f1e484a9b36
diff --cc litellm/model_prices_and_context_window_backup.json
index 88e7f7d644d,879dd42be47..c2e1156b1d3
--- a/litellm/model_prices_and_context_window_backup.json
+++ b/litellm/model_prices_and_context_window_backup.json
@@@ -6153,7 -6152,7 +6153,8 @@@
"max_query_tokens": 4096,
"max_tokens": 32768,
"mode": "rerank",
-- "output_cost_per_token": 0.0
++ "output_cost_per_token": 0.0,
++ "source": "https://techcommunity.microsoft.com/blog/azure-ai-foundry-blog/introducing-cohere-rerank-4-0-in-microsoft-foundry/4477076"
},
"azure_ai/cohere-rerank-v4.0-fast": {
"input_cost_per_query": 0.002,
@@@ -6164,7 -6163,7 +6165,8 @@@
"max_query_tokens": 4096,
"max_tokens": 32768,
"mode": "rerank",
-- "output_cost_per_token": 0.0
++ "output_cost_per_token": 0.0,
++ "source": "https://techcommunity.microsoft.com/blog/azure-ai-foundry-blog/introducing-cohere-rerank-4-0-in-microsoft-foundry/4477076"
},
"azure_ai/deepseek-v3.2": {
"input_cost_per_token": 5.8e-07,
@@@ -6174,6 -6173,6 +6176,7 @@@
"max_tokens": 163840,
"mode": "chat",
"output_cost_per_token": 1.68e-06,
++ "source": "https://techcommunity.microsoft.com/blog/azure-ai-foundry-blog/introducing-deepseek-v3-2-and-deepseek-v3-2-speciale-in-microsoft-foundry/4477549",
"supports_assistant_prefill": true,
"supports_function_calling": true,
"supports_prompt_caching": true,
@@@ -6188,6 -6187,6 +6191,7 @@@
"max_tokens": 163840,
"mode": "chat",
"output_cost_per_token": 1.68e-06,
++ "source": "https://techcommunity.microsoft.com/blog/azure-ai-foundry-blog/introducing-deepseek-v3-2-and-deepseek-v3-2-speciale-in-microsoft-foundry/4477549",
"supports_assistant_prefill": true,
"supports_function_calling": true,
"supports_prompt_caching": true,
diff --cc litellm/proxy/common_request_processing.py
index 9253b1e93b7,bad70d30da3..15e37ad5358
--- a/litellm/proxy/common_request_processing.py
+++ b/litellm/proxy/common_request_processing.py
@@@ -30,6 -30,6 +30,7 @@@ from litellm.constants import
MAX_PAYLOAD_SIZE_FOR_DEBUG_LOG,
STREAM_SSE_DATA_PREFIX,
)
++from litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.litellm_core_utils.dd_tracing import tracer
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.litellm_core_utils.llm_response_utils.get_headers import (
@@@ -45,7 -45,9 +46,8 @@@ from litellm.proxy.common_utils.callbac
from litellm.proxy.dd_span_tagger import DDSpanTagger
from litellm.proxy.route_llm_request import route_request
from litellm.proxy.utils import ProxyLogging
-from litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.router import Router
+ from litellm.types.guardrails import GuardrailEventHooks
from litellm.types.utils import ServerToolUse
# Type alias for streaming chunk serializer (chunk after hooks + cost injection -> wire format)
@@@ -1012,31 -911,43 +912,51 @@@ class ProxyBaseLLMRequestProcessing
)
self._debug_log_request_payload()
- self.data, logging_obj = await self.common_processing_pre_call_logic(
- request=request,
- general_settings=general_settings,
- proxy_logging_obj=proxy_logging_obj,
- user_api_key_dict=user_api_key_dict,
- version=version,
- proxy_config=proxy_config,
- user_model=user_model,
- user_temperature=user_temperature,
- user_request_timeout=user_request_timeout,
- user_max_tokens=user_max_tokens,
- user_api_base=user_api_base,
- model=model,
- route_type=route_type,
- llm_router=llm_router,
- )
+ if skip_pre_call_logic:
+ logging_obj = self.data.get("litellm_logging_obj")
+ if logging_obj is None:
+ raise ValueError(
+ "skip_pre_call_logic=True requires litellm_logging_obj to be set in data. "
+ "Ensure common_processing_pre_call_logic was called before using this parameter."
+ )
+ else:
+ self.data, logging_obj = await self.common_processing_pre_call_logic(
+ request=request,
+ general_settings=general_settings,
+ proxy_logging_obj=proxy_logging_obj,
+ user_api_key_dict=user_api_key_dict,
+ version=version,
+ proxy_config=proxy_config,
+ user_model=user_model,
+ user_temperature=user_temperature,
+ user_request_timeout=user_request_timeout,
+ user_max_tokens=user_max_tokens,
+ user_api_base=user_api_base,
+ model=model,
+ route_type=route_type,
+ llm_router=llm_router,
+ )
+ # Defer async logging when post-call guardrails are configured so the
+ # StandardLoggingPayload is built after guardrails write to metadata.
+ # Cache the result to avoid scanning litellm.callbacks twice.
+ _post_call_guardrails_active = self._has_post_call_guardrails()
+
+ # Non-streaming: defer the create_task in wrapper_async so the
+ # SLP is built after guardrails write to metadata. Streaming
+ # uses a separate closure mechanism (see below).
+ #
+ # Edge case: if _is_streaming_request is False but the response
+ # turns out to be a CustomStreamWrapper (rare provider behavior),
+ # wrapper_async exits early before the _defer_async_logging block
+ # so _enqueue_deferred_logging is never stored — the finally
+ # block is a no-op. The CSW path handles this correctly via
+ # _on_deferred_stream_complete, which fires its own logging.
+ if _post_call_guardrails_active and not self._is_streaming_request(
+ data=self.data, is_streaming_request=is_streaming_request
+ ):
+ logging_obj._defer_async_logging = True # type: ignore
+
tasks = []
# Start the moderation check (during_call_hook) as early as possible
# This gives it a head start to mask/validate input while the proxy handles routing
@@@ -1073,53 -984,229 +993,234 @@@
response = responses[1]
- hidden_params = getattr(response, "_hidden_params", {}) or {}
- model_id = self._get_model_id_from_response(hidden_params, self.data)
+ _exception_raised = False
+ try:
+ hidden_params = getattr(response, "_hidden_params", {}) or {}
+ model_id = self._get_model_id_from_response(hidden_params, self.data)
- cache_key, api_base, response_cost = (
- hidden_params.get("cache_key", None) or "",
- hidden_params.get("api_base", None) or "",
- hidden_params.get("response_cost", None) or "",
- )
- fastest_response_batch_completion, additional_headers = (
- hidden_params.get("fastest_response_batch_completion", None),
- hidden_params.get("additional_headers", {}) or {},
- )
+ cache_key, api_base, response_cost = (
+ hidden_params.get("cache_key", None) or "",
+ hidden_params.get("api_base", None) or "",
+ hidden_params.get("response_cost", None) or "",
+ )
+ fastest_response_batch_completion, additional_headers = (
+ hidden_params.get("fastest_response_batch_completion", None),
+ hidden_params.get("additional_headers", {}) or {},
+ )
- # Post Call Processing
- if llm_router is not None:
- self.data["deployment"] = llm_router.get_deployment(model_id=model_id)
- asyncio.create_task(
- proxy_logging_obj.update_request_status(
- litellm_call_id=self.data.get("litellm_call_id", ""), status="success"
+ # Post Call Processing
+ if llm_router is not None:
+ self.data["deployment"] = llm_router.get_deployment(model_id=model_id)
+ asyncio.create_task(
+ proxy_logging_obj.update_request_status(
- litellm_call_id=self.data.get("litellm_call_id", ""), status="success"
++ litellm_call_id=self.data.get("litellm_call_id", ""),
++ status="success",
+ )
)
- )
- streaming_response = await self._maybe_return_streaming_response(
- request=request,
- response=response,
- route_type=route_type,
- user_api_key_dict=user_api_key_dict,
- proxy_logging_obj=proxy_logging_obj,
- select_data_generator=select_data_generator,
- logging_obj=logging_obj,
- requested_model_from_client=requested_model_from_client,
- model_id=model_id,
- cache_key=cache_key,
- api_base=api_base,
- version=version,
- response_cost=response_cost,
- fastest_response_batch_completion=fastest_response_batch_completion,
- additional_headers=additional_headers,
- hidden_params=hidden_params,
- is_streaming_request=is_streaming_request,
- )
- if streaming_response is not None:
- return streaming_response
+ if self._is_streaming_request(
+ data=self.data, is_streaming_request=is_streaming_request
+ ) or self._is_streaming_response(
+ response
+ ): # use generate_responses to stream responses
+ custom_headers = ProxyBaseLLMRequestProcessing.get_custom_headers(
+ user_api_key_dict=user_api_key_dict,
+ call_id=logging_obj.litellm_call_id,
+ model_id=model_id,
+ cache_key=cache_key,
+ api_base=api_base,
+ version=version,
+ response_cost=response_cost,
+ model_region=getattr(user_api_key_dict, "allowed_model_region", ""),
+ fastest_response_batch_completion=fastest_response_batch_completion,
+ request_data=self.data,
+ hidden_params=hidden_params,
+ litellm_logging_obj=logging_obj,
+ **additional_headers,
+ )
- ### CALL HOOKS ### - modify outgoing data
- response = await proxy_logging_obj.post_call_success_hook(
- data=self.data, user_api_key_dict=user_api_key_dict, response=response
- )
+ # Call response headers hook for streaming success
- callback_headers = await proxy_logging_obj.post_call_response_headers_hook(
- data=self.data,
- user_api_key_dict=user_api_key_dict,
- response=response,
- request_headers=dict(request.headers),
++ callback_headers = (
++ await proxy_logging_obj.post_call_response_headers_hook(
++ data=self.data,
++ user_api_key_dict=user_api_key_dict,
++ response=response,
++ request_headers=dict(request.headers),
++ )
+ )
+ if callback_headers:
+ custom_headers.update(callback_headers)
+
+ # Preserve the original client-requested model (pre-alias mapping) for downstream
+ # streaming generators. Pre-call processing can rewrite `self.data["model"]` for
+ # aliasing/routing, but the OpenAI-compatible response `model` field should reflect
+ # what the client sent.
+ if requested_model_from_client:
+ self.data[
+ "_litellm_client_requested_model"
+ ] = requested_model_from_client
+
+ # Streaming: attach a closure that CSW.__anext__ will call
+ # at stream end instead of firing logging directly. The
+ # closure runs ONLY guardrail hooks (not all callbacks) on
+ # the assembled response so guardrail_information is
+ # populated, then fires both logging handlers.
+ # 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 = self.data
+ _captured_user_api_key_dict = user_api_key_dict
+ _captured_logging_obj = logging_obj
+
+ async def _on_deferred_stream_complete(
+ assembled_response, cache_hit
+ ):
+ 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 # type: ignore[attr-defined]
+
+ if route_type == "allm_passthrough_route":
+ # Check if response is an async generator
+ if self._is_streaming_response(response):
+ if asyncio.iscoroutine(response):
+ generator = await response
+ else:
+ generator = response
+
+ # For passthrough routes, stream directly without error parsing
+ # since we're dealing with raw binary data (e.g., AWS event streams)
+ return StreamingResponse(
- content=generator,
++ content=generator, # type: ignore[arg-type]
+ status_code=status.HTTP_200_OK,
+ headers=custom_headers,
+ )
+ else:
+ # Traditional HTTP response with aiter_bytes
+ return StreamingResponse(
- content=response.aiter_bytes(),
- status_code=response.status_code,
++ content=response.aiter_bytes(), # type: ignore[union-attr]
++ status_code=response.status_code, # type: ignore[union-attr]
+ headers=custom_headers,
+ )
+ elif route_type == "anthropic_messages":
+ # Check if response is actually a streaming response (async generator)
+ # Non-streaming responses (dict) should be returned directly
+ # This handles cases like websearch_interception agentic loop
+ # which returns a non-streaming dict even for streaming requests
+ if self._is_streaming_response(response):
+ selected_data_generator = (
+ ProxyBaseLLMRequestProcessing.async_sse_data_generator(
+ response=response,
+ user_api_key_dict=user_api_key_dict,
+ request_data=self.data,
+ proxy_logging_obj=proxy_logging_obj,
+ )
+ )
+ return await create_response(
+ generator=selected_data_generator,
+ media_type="text/event-stream",
+ headers=custom_headers,
+ )
+ # Non-streaming response - fall through to normal response handling
+ elif select_data_generator:
+ selected_data_generator = select_data_generator(
+ response=response,
+ user_api_key_dict=user_api_key_dict,
+ request_data=self.data,
+ )
+ return await create_response(
+ generator=selected_data_generator,
+ media_type="text/event-stream",
+ headers=custom_headers,
+ )
+
+ ### CALL HOOKS ### - modify outgoing data
+ # If we reach here with a streaming closure still set, it means
+ # no early-return route consumed the CSW (hypothetical fallthrough).
+ # Clear the closure so guardrails run inline as before — this
+ # preserves blocking behavior and avoids double invocation.
+ if getattr(logging_obj, "_on_deferred_stream_complete", None):
+ logging_obj._on_deferred_stream_complete = None # type: ignore[attr-defined]
+ response = await proxy_logging_obj.post_call_success_hook(
- data=self.data, user_api_key_dict=user_api_key_dict, response=response
++ data=self.data,
++ user_api_key_dict=user_api_key_dict,
++ response=response, # type: ignore[arg-type]
+ )
+ except Exception:
+ _exception_raised = True
+ raise
+ finally:
+ # Enqueue deferred logging after post-call guardrails have written
+ # guardrail_information to metadata. The finally block ensures
+ # logging fires even if a guardrail raises.
+ # For streaming early-returns: no closure is stored (wrapper_async
+ # returns before the deferred block), so _enqueue_fn is None — no-op.
+ _enqueue_fn = getattr(logging_obj, "_enqueue_deferred_logging", None)
+ if _enqueue_fn is not None:
+ logging_obj._enqueue_deferred_logging = None # type: ignore[attr-defined]
+ try:
+ _enqueue_fn()
+ except Exception as e:
+ verbose_proxy_logger.exception(
+ "Error firing deferred logging: %s", e
+ )
+
+ # Streaming cleanup: if an exception occurred AND the deferred
+ # streaming closure is still set, no streaming route will
+ # consume the CSW — the closure is orphaned. Clear it and
+ # fire logging directly to avoid silent loss.
+ #
+ # On normal streaming returns the closure must stay: CSW calls
+ # it at stream end. _exception_raised is function-scoped and
+ # immune to outer exception context, avoiding false positives.
+ if _exception_raised:
+ _deferred_fn = getattr(
+ logging_obj, "_on_deferred_stream_complete", None
+ )
+ if _deferred_fn is not None:
+ logging_obj._on_deferred_stream_complete = None # type: ignore[attr-defined]
+ try:
+ asyncio.create_task(
+ logging_obj.async_success_handler(
+ response,
+ cache_hit=None,
+ start_time=None,
+ end_time=None,
+ )
+ )
+ except Exception as e:
+ verbose_proxy_logger.exception(
+ "Error in orphaned streaming async logging: %s", e
+ )
+ try:
+ from litellm.litellm_core_utils.thread_pool_executor import (
+ executor as _exc,
+ )
+
+ _exc.submit(
+ logging_obj.success_handler,
+ response,
+ cache_hit=None,
+ start_time=None,
+ end_time=None,
+ )
+ except Exception as e:
+ verbose_proxy_logger.exception(
+ "Error in orphaned streaming sync logging: %s", e
+ )
# Always return the client-requested model name (not provider-prefixed internal identifiers)
# for OpenAI-compatible responses.
@@@ -1257,6 -1344,131 +1358,132 @@@
return True
return False
+ @staticmethod
+ def _has_post_call_guardrails() -> bool:
+ """
- Check if any registered callback is a post-call guardrail.
-
- Uses the global litellm.callbacks list rather than per-request
- should_run_guardrail() — intentionally conservative so that the
- check is simple and stateless. The deferral path produces
- identical logging output, just fires it slightly later, so
- false-positives are harmless.
++ True when a guardrail explicitly registers post_call. event_hook=None
++ matches all hooks in should_run_guardrail but must not defer async logging
++ on non-streaming /chat/completions (no post_call_success_hook flush path).
+ """
+ for cb in litellm.callbacks:
- if isinstance(cb, CustomGuardrail) and cb._event_hook_is_event_type(
- GuardrailEventHooks.post_call
- ):
++ if not isinstance(cb, CustomGuardrail):
++ continue
++ if cb.event_hook is None:
++ continue
++ if cb._event_hook_is_event_type(GuardrailEventHooks.post_call):
+ return True
+ return False
+
+ @staticmethod
+ async def _run_deferred_stream_guardrails(
+ captured_data: dict,
+ captured_user_api_key_dict: "UserAPIKeyAuth",
+ captured_logging_obj: Any,
+ assembled_response: Any,
+ cache_hit: Any,
+ ) -> None:
+ """
+ Run only post-call guardrail hooks on an assembled streaming response,
+ then fire both async and sync logging handlers.
+
+ Called by CSW.__anext__ at stream end via a closure stored on
+ logging_obj._on_deferred_stream_complete.
+
+ This is audit-only — content has already been delivered to the client.
+ Blocking guardrails that raise HTTPException cannot prevent content
+ delivery for streaming. Per-chunk filtering should use
+ async_post_call_streaming_hook instead.
+
+ Extracted as a static method so tests can call the production
+ implementation directly rather than reimplementing the closure.
+ """
+ from litellm.litellm_core_utils.thread_pool_executor import executor
+
+ _response = assembled_response
+ try:
+ from litellm.proxy.proxy_server import llm_router as _global_llm_router
+ from litellm.proxy.utils import (
+ _check_and_merge_model_level_guardrails,
- unified_guardrail as _unified_guardrail,
+ )
++ from litellm.proxy.utils import unified_guardrail as _unified_guardrail
+
+ guardrail_data = _check_and_merge_model_level_guardrails(
+ data=captured_data, llm_router=_global_llm_router
+ )
+ for cb in litellm.callbacks:
+ if not isinstance(cb, CustomGuardrail):
+ continue
+ if not cb.should_run_guardrail(
+ data=guardrail_data,
+ event_type=GuardrailEventHooks.post_call,
+ ):
+ continue
+ try:
+ guardrail_result = None
+ if "apply_guardrail" in type(cb).__dict__:
+ guardrail_data["guardrail_to_apply"] = cb
+ guardrail_result = (
+ await _unified_guardrail.async_post_call_success_hook(
+ user_api_key_dict=captured_user_api_key_dict,
+ data=guardrail_data,
+ response=_response,
+ )
+ )
+ else:
+ guardrail_result = await cb.async_post_call_success_hook(
+ user_api_key_dict=captured_user_api_key_dict,
+ data=guardrail_data,
+ response=_response,
+ )
+ if guardrail_result is not None:
+ _response = guardrail_result
+ except Exception as e:
+ verbose_proxy_logger.exception(
+ "Error running post-call guardrail %s on streaming response: %s",
+ getattr(cb, "guardrail_name", type(cb).__name__),
+ e,
+ )
+ if isinstance(e, HTTPException) and hasattr(
+ captured_logging_obj, "model_call_details"
+ ):
+ captured_logging_obj.model_call_details.setdefault(
+ "metadata", {}
+ )["guardrail_blocked"] = True
+ except Exception as e:
+ verbose_proxy_logger.exception(
- "Error in deferred streaming guardrail initialization: %s", e,
++ "Error in deferred streaming guardrail initialization: %s",
++ e,
+ )
+ finally:
+ try:
+ asyncio.create_task(
+ captured_logging_obj.async_success_handler(
+ _response,
+ cache_hit=cache_hit,
+ start_time=None,
+ end_time=None,
+ )
+ )
+ except Exception as e:
+ verbose_proxy_logger.exception(
- "Error in deferred streaming async logging: %s", e,
++ "Error in deferred streaming async logging: %s",
++ e,
+ )
+
+ try:
+ executor.submit(
+ captured_logging_obj.success_handler,
+ _response,
+ cache_hit=cache_hit,
+ start_time=None,
+ end_time=None,
+ )
+ except Exception as e:
+ verbose_proxy_logger.exception(
- "Error in deferred streaming sync logging: %s", e,
++ "Error in deferred streaming sync logging: %s",
++ e,
+ )
+
async def _handle_llm_api_exception(
self,
e: Exception,
diff --cc litellm/utils.py
index 6981702d416,c674190ba8c..088ee07d630
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@@ -1945,23 -1944,43 +1945,41 @@@ def client(original_function): # noqa
)
# LOG SUCCESS - handle streaming success logging in the _next_ object
+ # Internal sub-calls (e.g. emulated file-search steps) share the
- # parent's logging obj; skip here so only the outer call bills once.
++ # parent's logging obj; skip async logging here so only the outer call bills once.
+ # NOTE: streaming requests return early (before this point) via
+ # CustomStreamWrapper, so this block is non-streaming only.
- if getattr(logging_obj, "_defer_async_logging", False):
- # Proxy has post-call guardrails that must complete before the
- # SLP is built. Store a closure the proxy will call after
- # post_call_success_hook so guardrail_information is in metadata.
- # Only create_task is deferred; sync callbacks fire immediately
- # (below, outside the if/else) for billing/rate-limiting.
- def _enqueue_deferred_logging() -> None:
+ if not _is_litellm_internal_call:
- asyncio.create_task(
- _client_async_logging_helper(
- logging_obj=logging_obj,
- result=result,
- start_time=start_time,
- end_time=end_time,
- is_completion_with_fallbacks=is_completion_with_fallbacks,
++ if getattr(logging_obj, "_defer_async_logging", False):
++
++ def _enqueue_deferred_logging() -> None:
++ asyncio.create_task(
++ _client_async_logging_helper(
++ logging_obj=logging_obj,
++ result=result,
++ start_time=start_time,
++ end_time=end_time,
++ is_completion_with_fallbacks=is_completion_with_fallbacks,
++ )
++ )
++
++ logging_obj._enqueue_deferred_logging = _enqueue_deferred_logging # type: ignore
++ else:
+ asyncio.create_task(
+ _client_async_logging_helper(
+ logging_obj=logging_obj,
+ result=result,
+ start_time=start_time,
+ end_time=end_time,
+ is_completion_with_fallbacks=is_completion_with_fallbacks,
+ )
)
- )
- logging_obj.handle_sync_success_callbacks_for_async_calls(
- result=result,
- start_time=start_time,
- end_time=end_time,
- )
+
- logging_obj._enqueue_deferred_logging = _enqueue_deferred_logging # type: ignore
- else:
- asyncio.create_task(
- _client_async_logging_helper(
- logging_obj=logging_obj,
- result=result,
- start_time=start_time,
- end_time=end_time,
- is_completion_with_fallbacks=is_completion_with_fallbacks,
- )
- )
-
- # Sync callbacks always fire immediately regardless of deferral
+ logging_obj.handle_sync_success_callbacks_for_async_calls(
+ result=result,
+ start_time=start_time,
+ end_time=end_time,
+ )
# REBUILD EMBEDDING CACHING
if (
isinstance(result, EmbeddingResponse)
diff --cc tests/test_litellm/proxy/guardrails/test_deferred_guardrail_logging.py
index 00000000000,c4d2dce5876..ca22c5aab56
mode 000000,100644..100644
--- a/tests/test_litellm/proxy/guardrails/test_deferred_guardrail_logging.py
+++ b/tests/test_litellm/proxy/guardrails/test_deferred_guardrail_logging.py
@@@ -1,0 -1,944 +1,955 @@@
+ """
+ Tests for deferred logging with post-call guardrails.
+
+ When post-call guardrails are configured, the async logging task is deferred
+ until after guardrails complete. This ensures the StandardLoggingPayload
+ is built with guardrail_information populated.
+
+ Non-streaming: create_task in wrapper_async is replaced by a closure that
+ the proxy fires in a try/finally after post_call_success_hook.
+
+ Streaming: a closure on logging_obj is called by CSW.__anext__ at stream end.
+ The closure runs ONLY guardrail hooks (not all callbacks), then fires
+ both logging handlers.
+ """
+
+ import asyncio
+ import os
+ import sys
+ from typing import Any
+ from unittest.mock import MagicMock, patch
+
+ import pytest
+
+ sys.path.insert(0, os.path.abspath("../../../.."))
+
+ import litellm
+ from litellm.caching.caching import DualCache
+ from litellm.integrations.custom_guardrail import CustomGuardrail
+ from litellm.integrations.custom_logger import CustomLogger
+ from litellm.proxy._types import UserAPIKeyAuth
+ from litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing
+ from litellm.proxy.utils import ProxyLogging
+ from litellm.types.guardrails import GuardrailEventHooks
+
-
+ # ---------------------------------------------------------------------------
+ # Helpers
+ # ---------------------------------------------------------------------------
+
+
+ class PostCallGuardrail(CustomGuardrail):
+ """A post-call guardrail."""
+
+ def __init__(self):
+ super().__init__(
+ guardrail_name="post-call",
+ default_on=True,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def async_post_call_success_hook(
+ self, data: dict, user_api_key_dict: UserAPIKeyAuth, response: Any
+ ) -> Any:
+ return response
+
+
+ class PreCallGuardrail(CustomGuardrail):
+ """A pre-call-only guardrail — should NOT trigger deferral."""
+
+ def __init__(self):
+ super().__init__(
+ guardrail_name="pre-call",
+ default_on=True,
+ event_hook=GuardrailEventHooks.pre_call,
+ )
+
+
+ class AllEventsGuardrail(CustomGuardrail):
+ """A guardrail with event_hook=None (runs on all events)."""
+
+ def __init__(self):
+ super().__init__(
+ guardrail_name="all-events",
+ default_on=True,
+ event_hook=None,
+ )
+
+
+ # ---------------------------------------------------------------------------
+ # 1. _has_post_call_guardrails detection
+ # ---------------------------------------------------------------------------
+
+
+ class TestHasPostCallGuardrails:
+ def test_returns_true_for_post_call_guardrail(self):
+ with patch("litellm.callbacks", [PostCallGuardrail()]):
+ assert ProxyBaseLLMRequestProcessing._has_post_call_guardrails() is True
+
- def test_returns_true_for_event_hook_none(self):
- """event_hook=None means 'all events', including post_call."""
++ def test_returns_false_for_event_hook_none(self):
++ """event_hook=None is not an explicit post_call registration for deferral."""
+ with patch("litellm.callbacks", [AllEventsGuardrail()]):
- assert ProxyBaseLLMRequestProcessing._has_post_call_guardrails() is True
++ assert ProxyBaseLLMRequestProcessing._has_post_call_guardrails() is False
+
+ def test_returns_false_for_pre_call_only(self):
+ with patch("litellm.callbacks", [PreCallGuardrail()]):
+ assert ProxyBaseLLMRequestProcessing._has_post_call_guardrails() is False
+
+ def test_returns_false_for_no_callbacks(self):
+ with patch("litellm.callbacks", []):
+ assert ProxyBaseLLMRequestProcessing._has_post_call_guardrails() is False
+
+ def test_ignores_non_guardrail_callbacks(self):
+ """String callbacks and CustomLogger instances are not guardrails."""
+ with patch("litellm.callbacks", ["langfuse", CustomLogger()]):
+ assert ProxyBaseLLMRequestProcessing._has_post_call_guardrails() is False
+
+ def test_returns_true_for_list_with_post_call(self):
+ """event_hook as a list containing post_call should trigger deferral."""
+
+ class ListGuardrail(CustomGuardrail):
+ def __init__(self):
+ super().__init__(
+ guardrail_name="list-post",
+ default_on=True,
- event_hook=[GuardrailEventHooks.pre_call, GuardrailEventHooks.post_call],
++ event_hook=[
++ GuardrailEventHooks.pre_call,
++ GuardrailEventHooks.post_call,
++ ],
+ )
+
+ with patch("litellm.callbacks", [ListGuardrail()]):
+ assert ProxyBaseLLMRequestProcessing._has_post_call_guardrails() is True
+
+ def test_returns_false_for_list_without_post_call(self):
+ """event_hook as a list without post_call should not trigger deferral."""
+
+ class ListGuardrail(CustomGuardrail):
+ def __init__(self):
+ super().__init__(
+ guardrail_name="list-pre",
+ default_on=True,
+ event_hook=[GuardrailEventHooks.pre_call],
+ )
+
+ with patch("litellm.callbacks", [ListGuardrail()]):
+ assert ProxyBaseLLMRequestProcessing._has_post_call_guardrails() is False
+
+
+ # ---------------------------------------------------------------------------
+ # 2. Non-streaming: deferral flag → closure stored, create_task skipped
+ # ---------------------------------------------------------------------------
+
+
+ @pytest.mark.asyncio
+ async def test_deferred_flag_stores_and_executes_closure():
+ """
+ When _defer_async_logging is True on logging_obj:
+ 1. wrapper_async stores a callable closure instead of calling create_task
+ 2. Calling the closure fires create_task
+ 3. Sync callbacks fire immediately (not deferred)
+ """
+ mock_logging_obj = MagicMock()
+ mock_logging_obj._defer_async_logging = True
+ mock_logging_obj._enqueue_deferred_logging = None
+
+ await litellm.acompletion(
+ model="gpt-3.5-turbo",
+ messages=[{"role": "user", "content": "hi"}],
+ mock_response="Hello!",
+ litellm_logging_obj=mock_logging_obj,
+ )
+
+ # Closure was stored
+ enqueue_fn = mock_logging_obj._enqueue_deferred_logging
+ assert callable(enqueue_fn), "Closure should be stored on logging_obj"
+
+ # Sync callbacks fired immediately
+ mock_logging_obj.handle_sync_success_callbacks_for_async_calls.assert_called_once()
+
+ # Calling the closure fires create_task
+ created_tasks = []
+ real_create_task = asyncio.create_task
+
+ def tracking_create_task(coro):
+ task = real_create_task(coro)
+ created_tasks.append(task)
+ return task
+
+ with patch("asyncio.create_task", side_effect=tracking_create_task):
+ enqueue_fn()
+
+ assert len(created_tasks) >= 1, "Closure should fire asyncio.create_task"
+
+ for task in created_tasks:
+ if not task.done():
+ task.cancel()
+ try:
+ await task
+ except (asyncio.CancelledError, Exception):
+ pass
+
+
+ # ---------------------------------------------------------------------------
+ # 3. Non-streaming regression: without flag, create_task fires normally
+ # ---------------------------------------------------------------------------
+
+
+ @pytest.mark.asyncio
+ async def test_no_flag_fires_create_task_normally():
+ """Without _defer_async_logging, wrapper_async calls create_task as before."""
+ created_tasks = []
+ real_create_task = asyncio.create_task
+
+ def tracking_create_task(coro):
+ task = real_create_task(coro)
+ created_tasks.append(task)
+ return task
+
+ with patch("asyncio.create_task", side_effect=tracking_create_task):
+ await litellm.acompletion(
+ model="gpt-3.5-turbo",
+ messages=[{"role": "user", "content": "hi"}],
+ mock_response="Hello!",
+ )
+
+ assert len(created_tasks) >= 1
+
+ for task in created_tasks:
+ if not task.done():
+ task.cancel()
+ try:
+ await task
+ except (asyncio.CancelledError, Exception):
+ pass
+
+
+ # ---------------------------------------------------------------------------
+ # 4. Non-streaming: deferred logging fires even if guardrail raises
+ # ---------------------------------------------------------------------------
+
+
+ @pytest.mark.asyncio
+ async def test_deferred_logging_fires_on_guardrail_exception():
+ """
+ If post_call_success_hook raises (e.g., guardrail blocks content),
+ the deferred logging closure must still fire (via try/finally).
+ """
+ from fastapi import HTTPException # noqa: local import for test isolation
+
+ enqueue_called = False
+
+ def mock_enqueue():
+ nonlocal enqueue_called
+ enqueue_called = True
+
+ class BlockingGuardrail(CustomGuardrail):
+ def __init__(self):
+ super().__init__(
+ guardrail_name="blocker",
+ default_on=True,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def async_post_call_success_hook(
+ self, data: dict, user_api_key_dict: UserAPIKeyAuth, response: Any
+ ) -> Any:
+ raise HTTPException(status_code=400, detail="Content blocked")
+
+ guardrail = BlockingGuardrail()
+
+ logging_obj = MagicMock()
+ logging_obj._enqueue_deferred_logging = mock_enqueue
+
+ with patch("litellm.callbacks", [guardrail]):
+ proxy_logging = ProxyLogging(user_api_key_cache=DualCache())
+
+ with pytest.raises(HTTPException):
+ try:
+ await proxy_logging.post_call_success_hook(
+ data={"model": "gpt-4", "metadata": {}},
+ response=MagicMock(),
+ user_api_key_dict=UserAPIKeyAuth(api_key="test"),
+ )
+ finally:
+ # Mirrors the proxy's finally block
+ _enqueue_fn = getattr(logging_obj, "_enqueue_deferred_logging", None)
+ if _enqueue_fn is not None:
+ logging_obj._enqueue_deferred_logging = None
+ _enqueue_fn()
+
+ assert enqueue_called is True
+ assert logging_obj._enqueue_deferred_logging is None
+
+
+ # ---------------------------------------------------------------------------
+ # 5. Streaming: closure defers logging at stream end
+ # ---------------------------------------------------------------------------
+
+
+ class TestDeferredStreamingClosure:
+ @pytest.mark.asyncio
+ async def test_streaming_closure_defers_logging(self):
+ """When _on_deferred_stream_complete is set, CSW calls the closure
+ instead of firing async_success_handler directly."""
+ mock_logging_obj = MagicMock()
+ callback_called = False
+ callback_args = {}
+
+ async def mock_callback(assembled_response, cache_hit):
+ nonlocal callback_called, callback_args
+ callback_called = True
+ callback_args = {"response": assembled_response, "cache_hit": cache_hit}
+
+ mock_logging_obj._on_deferred_stream_complete = mock_callback
+
+ resp = await litellm.acompletion(
+ model="gpt-3.5-turbo",
+ messages=[{"role": "user", "content": "hi"}],
+ mock_response="Hello!",
+ stream=True,
+ litellm_logging_obj=mock_logging_obj,
+ )
+ async for _ in resp:
+ pass
+
+ await asyncio.sleep(0)
+
+ assert callback_called is True, "Closure should be called at stream end"
+ assert callback_args["response"] is not None
+ assert mock_logging_obj._on_deferred_stream_complete is None
+
+ @pytest.mark.asyncio
+ async def test_streaming_no_closure_fires_normally(self):
+ """Regression: without closure, CSW fires logging immediately."""
+ created_tasks = []
+ real_create_task = asyncio.create_task
+
+ def tracking_create_task(coro):
+ task = real_create_task(coro)
+ created_tasks.append(task)
+ return task
+
+ resp = await litellm.acompletion(
+ model="gpt-3.5-turbo",
+ messages=[{"role": "user", "content": "hi"}],
+ mock_response="Hello!",
+ stream=True,
+ )
+ with patch("asyncio.create_task", side_effect=tracking_create_task):
+ async for _ in resp:
+ pass
+
+ assert len(created_tasks) >= 1
+ for task in created_tasks:
+ if not task.done():
+ task.cancel()
+ try:
+ await task
+ except (asyncio.CancelledError, Exception):
+ pass
+
+ @pytest.mark.asyncio
+ async def test_closure_runs_only_guardrail_hooks(self):
+ """The closure must call only CustomGuardrail hooks, not all callbacks.
+ This is the key v2 change — PR #23929 called post_call_success_hook
+ which ran ALL callbacks, causing behavioral changes for streaming."""
+ guardrail_called = False
+ logger_called = False
+
+ class TrackingGuardrail(CustomGuardrail):
+ def __init__(self):
+ super().__init__(
+ guardrail_name="tracker",
+ default_on=True,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def async_post_call_success_hook(
+ self, data: dict, user_api_key_dict: UserAPIKeyAuth, response: Any
+ ) -> Any:
+ nonlocal guardrail_called
+ guardrail_called = True
+ return response
+
+ class TrackingLogger(CustomLogger):
+ async def async_post_call_success_hook(
+ self, user_api_key_dict, data, response
+ ):
+ nonlocal logger_called
+ logger_called = True
+ return response
+
+ mock_logging_obj = MagicMock()
+ mock_logging_obj.model_call_details = {"metadata": {}}
+
+ async def track_async_success(*args, **kwargs):
+ pass
+
+ mock_logging_obj.async_success_handler = track_async_success
+
+ tracking_guardrail = TrackingGuardrail()
+ tracking_logger = TrackingLogger()
+
+ # Use the real production static method via a thin closure
+ _captured_data = {"model": "gpt-4", "metadata": {}}
+ _captured_user_api_key_dict = UserAPIKeyAuth(api_key="test")
+
+ async def _on_deferred_stream_complete(assembled_response, cache_hit):
+ await ProxyBaseLLMRequestProcessing._run_deferred_stream_guardrails(
+ captured_data=_captured_data,
+ captured_user_api_key_dict=_captured_user_api_key_dict,
+ captured_logging_obj=mock_logging_obj,
+ assembled_response=assembled_response,
+ cache_hit=cache_hit,
+ )
+
+ mock_logging_obj._on_deferred_stream_complete = _on_deferred_stream_complete
+
+ with patch("litellm.callbacks", [tracking_guardrail, tracking_logger]):
+ resp = await litellm.acompletion(
+ model="gpt-3.5-turbo",
+ messages=[{"role": "user", "content": "hi"}],
+ mock_response="Hello!",
+ stream=True,
+ litellm_logging_obj=mock_logging_obj,
+ )
+ async for _ in resp:
+ pass
+
+ await asyncio.sleep(0)
+ await asyncio.sleep(0)
+
+ assert guardrail_called is True, "Guardrail hook should be called"
- assert logger_called is False, "Non-guardrail logger should NOT be called by closure"
++ assert (
++ logger_called is False
++ ), "Non-guardrail logger should NOT be called by closure"
+
+ @pytest.mark.asyncio
+ async def test_closure_passes_guardrail_modified_response_to_logging(self):
+ """The production _run_deferred_stream_guardrails must pass the
+ guardrail-modified response to async_success_handler."""
+ logged_response = None
+ modified_response = MagicMock()
+
+ mock_logging_obj = MagicMock()
+ mock_logging_obj.model_call_details = {"metadata": {}}
+
+ async def track_async_success(*args, **kwargs):
+ nonlocal logged_response
+ logged_response = args[0] if args else None
+
+ mock_logging_obj.async_success_handler = track_async_success
+
+ class ModifyingGuardrail(CustomGuardrail):
+ def __init__(self):
+ super().__init__(
+ guardrail_name="modifier",
+ default_on=True,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def async_post_call_success_hook(
+ self, data: dict, user_api_key_dict: UserAPIKeyAuth, response: Any
+ ) -> Any:
+ return modified_response
+
+ guardrail = ModifyingGuardrail()
+
+ with patch("litellm.callbacks", [guardrail]):
+ await ProxyBaseLLMRequestProcessing._run_deferred_stream_guardrails(
+ captured_data={"model": "gpt-4", "metadata": {}},
+ captured_user_api_key_dict=UserAPIKeyAuth(api_key="test"),
+ captured_logging_obj=mock_logging_obj,
+ assembled_response=MagicMock(),
+ cache_hit=False,
+ )
+
+ await asyncio.sleep(0)
+ await asyncio.sleep(0)
+
- assert logged_response is modified_response, \
- "Logging must receive the guardrail-modified response"
++ assert (
++ logged_response is modified_response
++ ), "Logging must receive the guardrail-modified response"
+
+ @pytest.mark.asyncio
+ async def test_closure_logs_even_on_guardrail_exception(self):
+ """If a guardrail raises HTTPException, the production
+ _run_deferred_stream_guardrails must still fire logging
+ and set guardrail_blocked in metadata."""
+ from fastapi import HTTPException # noqa: local import for test isolation
+
+ logging_called = False
+
+ class BlockingGuardrail(CustomGuardrail):
+ def __init__(self):
+ super().__init__(
+ guardrail_name="blocker",
+ default_on=True,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def async_post_call_success_hook(
+ self, data: dict, user_api_key_dict: UserAPIKeyAuth, response: Any
+ ) -> Any:
+ raise HTTPException(status_code=400, detail="Blocked")
+
+ mock_logging_obj = MagicMock()
+ mock_logging_obj.model_call_details = {"metadata": {}}
+
+ async def track_async_success(*args, **kwargs):
+ nonlocal logging_called
+ logging_called = True
+
+ mock_logging_obj.async_success_handler = track_async_success
+
+ guardrail = BlockingGuardrail()
+
+ with patch("litellm.callbacks", [guardrail]):
+ await ProxyBaseLLMRequestProcessing._run_deferred_stream_guardrails(
+ captured_data={"model": "gpt-4", "metadata": {}},
+ captured_user_api_key_dict=UserAPIKeyAuth(api_key="test"),
+ captured_logging_obj=mock_logging_obj,
+ assembled_response=MagicMock(),
+ cache_hit=False,
+ )
+
+ await asyncio.sleep(0)
+ await asyncio.sleep(0)
+
- assert logging_called is True, \
- "Logging must fire even when guardrail raises HTTPException"
- assert mock_logging_obj.model_call_details["metadata"].get(
- "guardrail_blocked"
- ) is True, "guardrail_blocked must be set for HTTPException"
++ assert (
++ logging_called is True
++ ), "Logging must fire even when guardrail raises HTTPException"
++ assert (
++ mock_logging_obj.model_call_details["metadata"].get("guardrail_blocked")
++ is True
++ ), "guardrail_blocked must be set for HTTPException"
+
+ @pytest.mark.asyncio
+ async def test_transient_error_does_not_set_guardrail_blocked(self):
+ """Transient errors (not HTTPException) should NOT set
+ guardrail_blocked. Uses the production _run_deferred_stream_guardrails."""
+
+ class TransientErrorGuardrail(CustomGuardrail):
+ def __init__(self):
+ super().__init__(
+ guardrail_name="transient",
+ default_on=True,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def async_post_call_success_hook(
+ self, data: dict, user_api_key_dict: UserAPIKeyAuth, response: Any
+ ) -> Any:
+ raise ConnectionError("Network timeout")
+
+ mock_logging_obj = MagicMock()
+ mock_logging_obj.model_call_details = {"metadata": {}}
+
+ async def track_async_success(*args, **kwargs):
+ pass
+
+ mock_logging_obj.async_success_handler = track_async_success
+
+ guardrail = TransientErrorGuardrail()
+
+ with patch("litellm.callbacks", [guardrail]):
+ await ProxyBaseLLMRequestProcessing._run_deferred_stream_guardrails(
+ captured_data={"model": "gpt-4", "metadata": {}},
+ captured_user_api_key_dict=UserAPIKeyAuth(api_key="test"),
+ captured_logging_obj=mock_logging_obj,
+ assembled_response=MagicMock(),
+ cache_hit=False,
+ )
+
+ await asyncio.sleep(0)
+
- assert mock_logging_obj.model_call_details["metadata"].get(
- "guardrail_blocked"
- ) is not True, "guardrail_blocked must NOT be set for transient errors"
++ assert (
++ mock_logging_obj.model_call_details["metadata"].get("guardrail_blocked")
++ is not True
++ ), "guardrail_blocked must NOT be set for transient errors"
+
+ @pytest.mark.asyncio
+ async def test_production_closure_integration(self):
+ """Integration test: calls the real _run_deferred_stream_guardrails
+ static method and verifies it calls guardrail hooks and passes
+ the modified response to logging."""
+ hook_called = False
+ logged_response = None
+ modified_response = MagicMock()
+
+ mock_logging_obj = MagicMock()
+ mock_logging_obj.model_call_details = {"metadata": {}}
+
+ async def track_async_success(*args, **kwargs):
+ nonlocal logged_response
+ logged_response = args[0] if args else None
+
+ mock_logging_obj.async_success_handler = track_async_success
+
+ class TestGuardrail(CustomGuardrail):
+ def __init__(self):
+ super().__init__(
+ guardrail_name="test",
+ default_on=True,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def async_post_call_success_hook(
+ self, data: dict, user_api_key_dict: UserAPIKeyAuth, response: Any
+ ) -> Any:
+ nonlocal hook_called
+ hook_called = True
+ return modified_response
+
+ guardrail = TestGuardrail()
+
+ async def _on_deferred_stream_complete(assembled_response, cache_hit):
+ await ProxyBaseLLMRequestProcessing._run_deferred_stream_guardrails(
+ captured_data={"model": "gpt-4", "metadata": {}},
+ captured_user_api_key_dict=UserAPIKeyAuth(api_key="test"),
+ captured_logging_obj=mock_logging_obj,
+ assembled_response=assembled_response,
+ cache_hit=cache_hit,
+ )
+
+ mock_logging_obj._on_deferred_stream_complete = _on_deferred_stream_complete
+
+ with patch("litellm.callbacks", [guardrail]):
+ resp = await litellm.acompletion(
+ model="gpt-3.5-turbo",
+ messages=[{"role": "user", "content": "hi"}],
+ mock_response="Hello!",
+ stream=True,
+ litellm_logging_obj=mock_logging_obj,
+ )
+ async for _ in resp:
+ pass
+
+ await asyncio.sleep(0)
+ await asyncio.sleep(0)
+
- assert hook_called is True, \
- "Production closure must call guardrail hook"
- assert logged_response is modified_response, \
- "Production closure must pass guardrail-modified response to logging"
++ assert hook_called is True, "Production closure must call guardrail hook"
++ assert (
++ logged_response is modified_response
++ ), "Production closure must pass guardrail-modified response to logging"
+
+ @pytest.mark.asyncio
+ async def test_apply_guardrail_path_uses_unified_guardrail(self):
+ """Guardrails that define apply_guardrail should be dispatched through
+ UnifiedLLMGuardrails.async_post_call_success_hook via the real
+ _run_deferred_stream_guardrails static method."""
+ from litellm.types.utils import GenericGuardrailAPIInputs
+
+ unified_hook_called = False
+
+ class ApplyGuardrailType(CustomGuardrail):
+ def __init__(self):
+ super().__init__(
+ guardrail_name="apply-type",
+ default_on=True,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def apply_guardrail(
+ self, inputs, request_data, input_type, logging_obj=None
+ ) -> GenericGuardrailAPIInputs:
+ nonlocal unified_hook_called
+ unified_hook_called = True
+ return inputs
+
+ mock_logging_obj = MagicMock()
+ mock_logging_obj.model_call_details = {"metadata": {}}
+ logged_response = None
+
+ async def track_async_success(*args, **kwargs):
+ nonlocal logged_response
+ logged_response = args[0] if args else None
+
+ mock_logging_obj.async_success_handler = track_async_success
+
+ guardrail = ApplyGuardrailType()
+
+ async def _on_deferred_stream_complete(assembled_response, cache_hit):
+ await ProxyBaseLLMRequestProcessing._run_deferred_stream_guardrails(
+ captured_data={"model": "gpt-4", "metadata": {}},
+ captured_user_api_key_dict=UserAPIKeyAuth(api_key="test"),
+ captured_logging_obj=mock_logging_obj,
+ assembled_response=assembled_response,
+ cache_hit=cache_hit,
+ )
+
+ mock_logging_obj._on_deferred_stream_complete = _on_deferred_stream_complete
+
+ with patch("litellm.callbacks", [guardrail]):
+ resp = await litellm.acompletion(
+ model="gpt-3.5-turbo",
+ messages=[{"role": "user", "content": "hi"}],
+ mock_response="Hello!",
+ stream=True,
+ litellm_logging_obj=mock_logging_obj,
+ )
+ async for _ in resp:
+ pass
+
+ await asyncio.sleep(0)
+ await asyncio.sleep(0)
+
- assert unified_hook_called is True, \
- "apply_guardrail guardrails must be dispatched through UnifiedLLMGuardrails"
- assert logged_response is not None, \
- "Logging must fire after unified guardrail path"
++ assert (
++ unified_hook_called is True
++ ), "apply_guardrail guardrails must be dispatched through UnifiedLLMGuardrails"
++ assert (
++ logged_response is not None
++ ), "Logging must fire after unified guardrail path"
+
+ @pytest.mark.asyncio
+ async def test_hooks_receive_merged_guardrail_data(self):
+ """Hooks must receive guardrail_data (the merged dict from
+ _check_and_merge_model_level_guardrails), not the original
+ captured_data. This ensures model-level non-default guardrails
+ are visible to any inner should_run_guardrail re-checks.
+
+ Uses a deep-copy mock to break the shallow-copy side-effect that
+ would otherwise mask the bug — verifying the code is explicitly
+ correct, not correct-by-accident."""
+ import copy
+
+ hook_received_data = None
+
+ class InspectingGuardrail(CustomGuardrail):
+ def __init__(self):
+ super().__init__(
+ guardrail_name="inspector",
+ default_on=True,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def async_post_call_success_hook(
+ self, data: dict, user_api_key_dict: UserAPIKeyAuth, response: Any
+ ) -> Any:
+ nonlocal hook_received_data
+ hook_received_data = data
+ return response
+
+ mock_logging_obj = MagicMock()
+ mock_logging_obj.model_call_details = {"metadata": {}}
+
+ async def track_async_success(*args, **kwargs):
+ pass
+
+ mock_logging_obj.async_success_handler = track_async_success
+
+ guardrail = InspectingGuardrail()
+
+ captured_data = {"model": "gpt-4", "metadata": {"existing_key": "value"}}
+
+ def mock_merge(data, llm_router):
+ """Return a fully independent dict (deep copy) so the original
+ captured_data is NOT mutated. This simulates a correct merge
+ implementation and proves _run_deferred_stream_guardrails uses
+ the return value, not the original data."""
+ merged = copy.deepcopy(data)
+ merged["metadata"]["guardrails"] = ["model-guardrail"]
+ merged["_merged_marker"] = True
+ return merged
+
- with patch("litellm.callbacks", [guardrail]), \
- patch(
- "litellm.proxy.utils._check_and_merge_model_level_guardrails",
- side_effect=mock_merge,
- ):
++ with patch("litellm.callbacks", [guardrail]), patch(
++ "litellm.proxy.utils._check_and_merge_model_level_guardrails",
++ side_effect=mock_merge,
++ ):
+ await ProxyBaseLLMRequestProcessing._run_deferred_stream_guardrails(
+ captured_data=captured_data,
+ captured_user_api_key_dict=UserAPIKeyAuth(api_key="test"),
+ captured_logging_obj=mock_logging_obj,
+ assembled_response=MagicMock(),
+ cache_hit=False,
+ )
+
+ assert hook_received_data is not None, "Guardrail hook must be called"
- assert hook_received_data.get("_merged_marker") is True, \
- "Hook must receive guardrail_data (merged), not original captured_data"
++ assert (
++ hook_received_data.get("_merged_marker") is True
++ ), "Hook must receive guardrail_data (merged), not original captured_data"
+ assert "model-guardrail" in hook_received_data.get("metadata", {}).get(
+ "guardrails", []
+ ), "Hook data must contain model-level guardrails"
+
+ @pytest.mark.asyncio
+ async def test_apply_guardrail_path_receives_merged_guardrail_data(self):
+ """The apply_guardrail path (through UnifiedLLMGuardrails) must also
+ receive guardrail_data so that the inner should_run_guardrail re-check
+ inside UnifiedLLMGuardrails sees model-level guardrails.
+
+ This is the specific scenario Greptile flagged: a default_on=False
+ guardrail configured at the model level would pass the outer gate but
+ be silently skipped at execution time if captured_data (unmerged) were
+ passed instead of guardrail_data (merged)."""
+ import copy
++
+ from litellm.types.utils import GenericGuardrailAPIInputs
+
+ unified_received_data = None
+
+ class ModelLevelApplyGuardrail(CustomGuardrail):
+ def __init__(self):
+ super().__init__(
+ guardrail_name="model-apply-guardrail",
+ default_on=True,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def apply_guardrail(
+ self, inputs, request_data, input_type, logging_obj=None
+ ) -> GenericGuardrailAPIInputs:
+ return inputs
+
+ mock_logging_obj = MagicMock()
+ mock_logging_obj.model_call_details = {"metadata": {}}
+
+ async def track_async_success(*args, **kwargs):
+ pass
+
+ mock_logging_obj.async_success_handler = track_async_success
+
+ guardrail = ModelLevelApplyGuardrail()
+ captured_data = {"model": "gpt-4", "metadata": {}}
+
+ def mock_merge(data, llm_router):
+ merged = copy.deepcopy(data)
+ merged["metadata"]["guardrails"] = ["model-apply-guardrail"]
+ merged["_merged_marker"] = True
+ return merged
+
+ # Capture what UnifiedLLMGuardrails.async_post_call_success_hook receives
+ original_unified_hook = None
+ from litellm.proxy.guardrails.guardrail_hooks.unified_guardrail.unified_guardrail import (
+ UnifiedLLMGuardrails,
+ )
++
+ original_unified_hook = UnifiedLLMGuardrails.async_post_call_success_hook
+
+ async def tracking_unified_hook(self, user_api_key_dict, data, response):
+ nonlocal unified_received_data
+ unified_received_data = data
+ return response
+
- with patch("litellm.callbacks", [guardrail]), \
- patch(
- "litellm.proxy.utils._check_and_merge_model_level_guardrails",
- side_effect=mock_merge,
- ), \
- patch.object(
- UnifiedLLMGuardrails,
- "async_post_call_success_hook",
- tracking_unified_hook,
- ):
++ with patch("litellm.callbacks", [guardrail]), patch(
++ "litellm.proxy.utils._check_and_merge_model_level_guardrails",
++ side_effect=mock_merge,
++ ), patch.object(
++ UnifiedLLMGuardrails,
++ "async_post_call_success_hook",
++ tracking_unified_hook,
++ ):
+ await ProxyBaseLLMRequestProcessing._run_deferred_stream_guardrails(
+ captured_data=captured_data,
+ captured_user_api_key_dict=UserAPIKeyAuth(api_key="test"),
+ captured_logging_obj=mock_logging_obj,
+ assembled_response=MagicMock(),
+ cache_hit=False,
+ )
+
- assert unified_received_data is not None, \
- "UnifiedLLMGuardrails must be called for apply_guardrail guardrails"
- assert unified_received_data.get("_merged_marker") is True, \
- "UnifiedLLMGuardrails must receive guardrail_data (merged), not captured_data"
- assert "model-apply-guardrail" in unified_received_data.get(
- "metadata", {}
- ).get("guardrails", []), \
- "UnifiedLLMGuardrails data must contain model-level guardrails"
++ assert (
++ unified_received_data is not None
++ ), "UnifiedLLMGuardrails must be called for apply_guardrail guardrails"
++ assert (
++ unified_received_data.get("_merged_marker") is True
++ ), "UnifiedLLMGuardrails must receive guardrail_data (merged), not captured_data"
++ assert "model-apply-guardrail" in unified_received_data.get("metadata", {}).get(
++ "guardrails", []
++ ), "UnifiedLLMGuardrails data must contain model-level guardrails"
+
+ @pytest.mark.asyncio
+ async def test_multiple_guardrails_all_receive_merged_data(self):
+ """When multiple guardrails are configured, ALL of them must receive
+ guardrail_data (merged), not just the first one."""
+ import copy
+
+ received_data_per_guardrail = {}
+
+ class TaggedGuardrail(CustomGuardrail):
+ def __init__(self, name):
+ super().__init__(
+ guardrail_name=name,
+ default_on=True,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def async_post_call_success_hook(
+ self, data: dict, user_api_key_dict: UserAPIKeyAuth, response: Any
+ ) -> Any:
+ received_data_per_guardrail[self.guardrail_name] = data
+ return response
+
+ mock_logging_obj = MagicMock()
+ mock_logging_obj.model_call_details = {"metadata": {}}
+
+ async def track_async_success(*args, **kwargs):
+ pass
+
+ mock_logging_obj.async_success_handler = track_async_success
+
+ guardrail_a = TaggedGuardrail("guardrail-a")
+ guardrail_b = TaggedGuardrail("guardrail-b")
+
+ captured_data = {"model": "gpt-4", "metadata": {}}
+
+ def mock_merge(data, llm_router):
+ merged = copy.deepcopy(data)
+ merged["metadata"]["guardrails"] = ["guardrail-a", "guardrail-b"]
+ merged["_merged_marker"] = True
+ return merged
+
- with patch("litellm.callbacks", [guardrail_a, guardrail_b]), \
- patch(
- "litellm.proxy.utils._check_and_merge_model_level_guardrails",
- side_effect=mock_merge,
- ):
++ with patch("litellm.callbacks", [guardrail_a, guardrail_b]), patch(
++ "litellm.proxy.utils._check_and_merge_model_level_guardrails",
++ side_effect=mock_merge,
++ ):
+ await ProxyBaseLLMRequestProcessing._run_deferred_stream_guardrails(
+ captured_data=captured_data,
+ captured_user_api_key_dict=UserAPIKeyAuth(api_key="test"),
+ captured_logging_obj=mock_logging_obj,
+ assembled_response=MagicMock(),
+ cache_hit=False,
+ )
+
+ for name in ("guardrail-a", "guardrail-b"):
- assert name in received_data_per_guardrail, \
- f"{name} must be called"
- assert received_data_per_guardrail[name].get("_merged_marker") is True, \
- f"{name} must receive guardrail_data (merged), not captured_data"
++ assert name in received_data_per_guardrail, f"{name} must be called"
++ assert (
++ received_data_per_guardrail[name].get("_merged_marker") is True
++ ), f"{name} must receive guardrail_data (merged), not captured_data"
+
+ @pytest.mark.asyncio
+ async def test_logging_fires_even_if_guardrail_init_raises(self):
+ """If _check_and_merge_model_level_guardrails raises during
+ initialization, logging must still fire via the try/finally guard.
+ This prevents silent logging loss on transient init errors."""
+ logging_called = False
+
+ mock_logging_obj = MagicMock()
+ mock_logging_obj.model_call_details = {"metadata": {}}
+
+ async def track_async_success(*args, **kwargs):
+ nonlocal logging_called
+ logging_called = True
+
+ mock_logging_obj.async_success_handler = track_async_success
+
+ def exploding_merge(data, llm_router):
+ raise RuntimeError("Simulated init failure")
+
+ with patch(
+ "litellm.proxy.utils._check_and_merge_model_level_guardrails",
+ side_effect=exploding_merge,
+ ):
+ await ProxyBaseLLMRequestProcessing._run_deferred_stream_guardrails(
+ captured_data={"model": "gpt-4", "metadata": {}},
+ captured_user_api_key_dict=UserAPIKeyAuth(api_key="test"),
+ captured_logging_obj=mock_logging_obj,
+ assembled_response=MagicMock(),
+ cache_hit=False,
+ )
+
+ await asyncio.sleep(0)
+ await asyncio.sleep(0)
+
- assert logging_called is True, \
- "Logging must fire even when guardrail initialization raises"
++ assert (
++ logging_called is True
++ ), "Logging must fire even when guardrail initialization raises"