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/utils.py): run guardrails before running other logging hooks on "async_post_call_success_hook"

Krrish Dholakia60063202517c
diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py
index 5b11c25b2bf..d3d2972abfa 100644
--- a/litellm/proxy/utils.py
+++ b/litellm/proxy/utils.py
@@ -1395,9 +1395,12 @@ class ProxyLogging:
3. /image/generation
4. /files
"""
+ from litellm.types.guardrails import GuardrailEventHooks
- for callback in litellm.callbacks:
- try:
+ guardrail_callbacks: List[CustomGuardrail] = []
+ other_callbacks: List[CustomLogger] = []
+ try:
+ for callback in litellm.callbacks:
_callback: Optional[CustomLogger] = None
if isinstance(callback, str):
_callback = litellm.litellm_core_utils.litellm_logging.get_custom_logger_compatible_class(
@@ -1407,36 +1410,37 @@ class ProxyLogging:
_callback = callback # type: ignore
if _callback is not None:
+ if isinstance(_callback, CustomGuardrail):
+ guardrail_callbacks.append(_callback)
+ else:
+ other_callbacks.append(_callback)
############## Handle Guardrails ########################################
#############################################################################
- if isinstance(callback, CustomGuardrail):
- # Main - V2 Guardrails implementation
- from litellm.types.guardrails import GuardrailEventHooks
- if (
- callback.should_run_guardrail(
- data=data, event_type=GuardrailEventHooks.post_call
- )
- is not True
- ):
- continue
+ for callback in guardrail_callbacks:
+ # Main - V2 Guardrails implementation
+ if (
+ callback.should_run_guardrail(
+ data=data, event_type=GuardrailEventHooks.post_call
+ )
+ is not True
+ ):
+ continue
- await callback.async_post_call_success_hook(
- user_api_key_dict=user_api_key_dict,
- data=data,
- response=response,
- )
+ await callback.async_post_call_success_hook(
+ user_api_key_dict=user_api_key_dict,
+ data=data,
+ response=response,
+ )
- ############ Handle CustomLogger ###############################
- #################################################################
- elif isinstance(_callback, CustomLogger):
- await _callback.async_post_call_success_hook(
- user_api_key_dict=user_api_key_dict,
- data=data,
- response=response,
- )
- except Exception as e:
- raise e
+ ############ Handle CustomLogger ###############################
+ #################################################################
+ for callback in other_callbacks:
+ await callback.async_post_call_success_hook(
+ user_api_key_dict=user_api_key_dict, data=data, response=response
+ )
+ except Exception as e:
+ raise e
return response
async def async_post_call_streaming_hook(