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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat - allow accessing data post success call
Ishaan Jaffedf05073999c
diff --git a/docs/my-website/docs/proxy/call_hooks.md b/docs/my-website/docs/proxy/call_hooks.mdindex ce34e5ad6b6..25a46609d39 100644--- a/docs/my-website/docs/proxy/call_hooks.md+++ b/docs/my-website/docs/proxy/call_hooks.md@@ -47,6 +47,7 @@ class MyCustomHandler(CustomLogger): # https://docs.litellm.ai/docs/observabilitasync def async_post_call_success_hook(self,+ data: dict,user_api_key_dict: UserAPIKeyAuth,response,):diff --git a/enterprise/enterprise_hooks/aporio_ai.py b/enterprise/enterprise_hooks/aporio_ai.pyindex 6529ddcbaa4..5d1b081fe31 100644--- a/enterprise/enterprise_hooks/aporio_ai.py+++ b/enterprise/enterprise_hooks/aporio_ai.py@@ -133,6 +133,7 @@ class _ENTERPRISE_Aporio(CustomLogger):async def async_post_call_success_hook(self,+ data: dict,user_api_key_dict: UserAPIKeyAuth,response,):diff --git a/enterprise/enterprise_hooks/banned_keywords.py b/enterprise/enterprise_hooks/banned_keywords.pyindex 4d6545eb076..e282ee5ab44 100644--- a/enterprise/enterprise_hooks/banned_keywords.py+++ b/enterprise/enterprise_hooks/banned_keywords.py@@ -90,6 +90,7 @@ class _ENTERPRISE_BannedKeywords(CustomLogger):async def async_post_call_success_hook(self,+ data: dict,user_api_key_dict: UserAPIKeyAuth,response,):diff --git a/litellm/integrations/custom_logger.py b/litellm/integrations/custom_logger.pyindex 98b0da25c5d..47d28ab56a7 100644--- a/litellm/integrations/custom_logger.py+++ b/litellm/integrations/custom_logger.py@@ -122,6 +122,7 @@ class CustomLogger: # https://docs.litellm.ai/docs/observability/custom_callbacasync def async_post_call_success_hook(self,+ data: dict,user_api_key_dict: UserAPIKeyAuth,response,):diff --git a/litellm/proxy/custom_callbacks1.py b/litellm/proxy/custom_callbacks1.pyindex 37e4a6cdb3d..05028f033c0 100644--- a/litellm/proxy/custom_callbacks1.py+++ b/litellm/proxy/custom_callbacks1.py@@ -40,6 +40,7 @@ class MyCustomHandler(async def async_post_call_success_hook(self,+ data: dict,user_api_key_dict: UserAPIKeyAuth,response,):diff --git a/litellm/proxy/hooks/azure_content_safety.py b/litellm/proxy/hooks/azure_content_safety.pyindex 972ac999289..ccadafaf2e0 100644--- a/litellm/proxy/hooks/azure_content_safety.py+++ b/litellm/proxy/hooks/azure_content_safety.py@@ -1,10 +1,15 @@-from litellm.integrations.custom_logger import CustomLogger-from litellm.caching import DualCache-from litellm.proxy._types import UserAPIKeyAuth-import litellm, traceback, sys, uuid+import sys+import traceback+import uuid+from typing import Optional+from fastapi import HTTPException++import litellmfrom litellm._logging import verbose_proxy_logger-from typing import Optional+from litellm.caching import DualCache+from litellm.integrations.custom_logger import CustomLogger+from litellm.proxy._types import UserAPIKeyAuthclass _PROXY_AzureContentSafety(@@ -15,12 +20,12 @@ class _PROXY_AzureContentSafety(def __init__(self, endpoint, api_key, thresholds=None):try:from azure.ai.contentsafety.aio import ContentSafetyClient- from azure.core.credentials import AzureKeyCredentialfrom azure.ai.contentsafety.models import (- TextCategory,AnalyzeTextOptions,AnalyzeTextOutputType,+ TextCategory,)+ from azure.core.credentials import AzureKeyCredentialfrom azure.core.exceptions import HttpResponseErrorexcept Exception as e:raise Exception(@@ -132,6 +137,7 @@ class _PROXY_AzureContentSafety(async def async_post_call_success_hook(self,+ data: dict,user_api_key_dict: UserAPIKeyAuth,response,):diff --git a/litellm/proxy/hooks/dynamic_rate_limiter.py b/litellm/proxy/hooks/dynamic_rate_limiter.pyindex 4bf08998a4c..57985e9a690 100644--- a/litellm/proxy/hooks/dynamic_rate_limiter.py+++ b/litellm/proxy/hooks/dynamic_rate_limiter.py@@ -254,7 +254,7 @@ class _PROXY_DynamicRateLimitHandler(CustomLogger):return Noneasync def async_post_call_success_hook(- self, user_api_key_dict: UserAPIKeyAuth, response+ self, data: dict, user_api_key_dict: UserAPIKeyAuth, response):try:if isinstance(response, ModelResponse):@@ -287,7 +287,9 @@ class _PROXY_DynamicRateLimitHandler(CustomLogger):return responsereturn await super().async_post_call_success_hook(- user_api_key_dict, response+ data=data,+ user_api_key_dict=user_api_key_dict,+ response=response,)except Exception as e:verbose_proxy_logger.exception(diff --git a/litellm/proxy/hooks/presidio_pii_masking.py b/litellm/proxy/hooks/presidio_pii_masking.pyindex 933d925507c..6af7e3d1e58 100644--- a/litellm/proxy/hooks/presidio_pii_masking.py+++ b/litellm/proxy/hooks/presidio_pii_masking.py@@ -322,6 +322,7 @@ class _OPTIONAL_PresidioPIIMasking(CustomLogger):async def async_post_call_success_hook(self,+ data: dict,user_api_key_dict: UserAPIKeyAuth,response: Union[ModelResponse, EmbeddingResponse, ImageResponse],):diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex 3820f0ea33a..9eb8edd3c26 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -3139,7 +3139,7 @@ async def chat_completion(### CALL HOOKS ### - modify outgoing dataresponse = await proxy_logging_obj.post_call_success_hook(- user_api_key_dict=user_api_key_dict, response=response+ data=data, user_api_key_dict=user_api_key_dict, response=response)hidden_params = (@@ -3353,6 +3353,11 @@ async def completion(media_type="text/event-stream",headers=custom_headers,)+ ### CALL HOOKS ### - modify outgoing data+ response = await proxy_logging_obj.post_call_success_hook(+ data=data, user_api_key_dict=user_api_key_dict, response=response+ )+fastapi_response.headers.update(get_custom_headers(user_api_key_dict=user_api_key_dict,diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.pyindex d1d17d0ef5d..94862db9ac2 100644--- a/litellm/proxy/utils.py+++ b/litellm/proxy/utils.py@@ -717,6 +717,7 @@ class ProxyLogging:async def post_call_success_hook(self,+ data: dict,response: Union[ModelResponse, EmbeddingResponse, ImageResponse],user_api_key_dict: UserAPIKeyAuth,):@@ -738,7 +739,9 @@ class ProxyLogging:_callback = callback # type: ignoreif _callback is not None and isinstance(_callback, CustomLogger):await _callback.async_post_call_success_hook(- user_api_key_dict=user_api_key_dict, response=response+ user_api_key_dict=user_api_key_dict,+ data=data,+ response=response,)except Exception as e:raise ediff --git a/litellm/tests/test_azure_content_safety.py b/litellm/tests/test_azure_content_safety.pyindex 7b040fb2522..dc80c163c38 100644--- a/litellm/tests/test_azure_content_safety.py+++ b/litellm/tests/test_azure_content_safety.py@@ -1,8 +1,13 @@# What is this?## Unit test for azure content safety-import sys, os, asyncio, time, random-from datetime import datetime+import asyncio+import os+import random+import sys+import timeimport traceback+from datetime import datetime+from dotenv import load_dotenvfrom fastapi import HTTPException@@ -13,11 +18,12 @@ sys.path.insert(0, os.path.abspath("../..")) # Adds the parent directory to the system pathimport pytest+import litellmfrom litellm import Router, mock_completion-from litellm.proxy.utils import ProxyLogging-from litellm.proxy._types import UserAPIKeyAuthfrom litellm.caching import DualCache+from litellm.proxy._types import UserAPIKeyAuth+from litellm.proxy.utils import ProxyLogging@pytest.mark.asyncio@@ -177,7 +183,13 @@ async def test_strict_output_filtering_01():with pytest.raises(HTTPException) as exc_info:await azure_content_safety.async_post_call_success_hook(- user_api_key_dict=UserAPIKeyAuth(), response=response+ user_api_key_dict=UserAPIKeyAuth(),+ data={+ "messages": [+ {"role": "system", "content": "You are an helpfull assistant"}+ ]+ },+ response=response,)assert exc_info.value.detail["source"] == "output"@@ -216,7 +228,11 @@ async def test_strict_output_filtering_02():)await azure_content_safety.async_post_call_success_hook(- user_api_key_dict=UserAPIKeyAuth(), response=response+ user_api_key_dict=UserAPIKeyAuth(),+ data={+ "messages": [{"role": "system", "content": "You are an helpfull assistant"}]+ },+ response=response,)@@ -251,7 +267,11 @@ async def test_loose_output_filtering_01():)await azure_content_safety.async_post_call_success_hook(- user_api_key_dict=UserAPIKeyAuth(), response=response+ user_api_key_dict=UserAPIKeyAuth(),+ data={+ "messages": [{"role": "system", "content": "You are an helpfull assistant"}]+ },+ response=response,)@@ -286,5 +306,9 @@ async def test_loose_output_filtering_02():)await azure_content_safety.async_post_call_success_hook(- user_api_key_dict=UserAPIKeyAuth(), response=response+ user_api_key_dict=UserAPIKeyAuth(),+ data={+ "messages": [{"role": "system", "content": "You are an helpfull assistant"}]+ },+ response=response,)diff --git a/litellm/tests/test_presidio_masking.py b/litellm/tests/test_presidio_masking.pyindex 193fcf113b7..35a03ea5e36 100644--- a/litellm/tests/test_presidio_masking.py+++ b/litellm/tests/test_presidio_masking.py@@ -88,7 +88,11 @@ async def test_output_parsing():mock_response="Hello <PERSON>! How can I assist you today?",)new_response = await pii_masking.async_post_call_success_hook(- user_api_key_dict=UserAPIKeyAuth(), response=response+ user_api_key_dict=UserAPIKeyAuth(),+ data={+ "messages": [{"role": "system", "content": "You are an helpfull assistant"}]+ },+ response=response,)assert (