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]

Addressing comments

yuneng-jiang7833b3fdb4ed
diff --git a/litellm/proxy/common_utils/callback_utils.py b/litellm/proxy/common_utils/callback_utils.py
index fb7ada8ab10..eb312612779 100644
--- a/litellm/proxy/common_utils/callback_utils.py
+++ b/litellm/proxy/common_utils/callback_utils.py
@@ -3,8 +3,12 @@ from typing import Any, Dict, List, Literal, Optional
import litellm
from litellm import get_secret
from litellm._logging import verbose_proxy_logger
+from litellm.integrations.custom_logger import CustomLogger
from litellm.proxy._types import CommonProxyErrors, LiteLLMPromptInjectionParams
from litellm.proxy.types_utils.utils import get_instance_fn
+from litellm.proxy.common_utils.encrypt_decrypt_utils import (
+ decrypt_value_helper,
+)
blue_color_code = "\033[94m"
reset_color_code = "\033[0m"
@@ -382,3 +386,25 @@ def get_metadata_variable_name_from_kwargs(
- LiteLLM is now moving to using `litellm_metadata` for our metadata
"""
return "litellm_metadata" if "litellm_metadata" in kwargs else "metadata"
+
+def process_callback(_callback: str, callback_type: str, environment_variables: dict) -> dict:
+ """Process a single callback and return its data with environment variables"""
+ env_vars = CustomLogger.get_callback_env_vars(_callback)
+
+ env_vars_dict: dict[str, str | None] = {}
+ for _var in env_vars:
+ env_variable = environment_variables.get(_var, None)
+ if env_variable is None:
+ env_vars_dict[_var] = None
+ else:
+ # decode + decrypt the value
+ decrypted_value = decrypt_value_helper(
+ value=env_variable, key=_var
+ )
+ env_vars_dict[_var] = decrypted_value
+
+ return {
+ "name": _callback,
+ "variables": env_vars_dict,
+ "type": callback_type
+ }
\ No newline at end of file
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 090757da4ad..205609a645c 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -47,6 +47,7 @@ from litellm.types.utils import (
TokenCountResponse,
)
from litellm.utils import load_credentials_from_list
+from litellm.proxy.common_utils.callback_utils import process_callback
if TYPE_CHECKING:
from aiohttp import ClientSession
@@ -9582,29 +9583,6 @@ async def get_config(): # noqa: PLR0915
_general_settings = config_data.get("general_settings", {})
environment_variables = config_data.get("environment_variables", {})
- # Helper function to process callbacks and get environment variables
- def process_callback(_callback: str, callback_type: str) -> dict:
- """Process a single callback and return its data with environment variables"""
- env_vars = CustomLogger.get_callback_env_vars(_callback)
-
- env_vars_dict: dict[str, str | None] = {}
- for _var in env_vars:
- env_variable = environment_variables.get(_var, None)
- if env_variable is None:
- env_vars_dict[_var] = None
- else:
- # decode + decrypt the value
- decrypted_value = decrypt_value_helper(
- value=env_variable, key=_var
- )
- env_vars_dict[_var] = decrypted_value
-
- return {
- "name": _callback,
- "variables": env_vars_dict,
- "type": callback_type
- }
-
_success_callbacks = _litellm_settings.get("success_callback", [])
_failure_callbacks = _litellm_settings.get("failure_callback", [])
_success_and_failure_callbacks = _litellm_settings.get("callbacks", [])
@@ -9626,13 +9604,13 @@ async def get_config(): # noqa: PLR0915
"""
for _callback in _success_callbacks:
- _data_to_return.append(process_callback(_callback, "success"))
+ _data_to_return.append(process_callback(_callback, "success", environment_variables))
for _callback in _failure_callbacks:
- _data_to_return.append(process_callback(_callback, "failure"))
+ _data_to_return.append(process_callback(_callback, "failure", environment_variables))
for _callback in _success_and_failure_callbacks:
- _data_to_return.append(process_callback(_callback, "success_and_failure"))
+ _data_to_return.append(process_callback(_callback, "success_and_failure", environment_variables))
# Check if slack alerting is on
_alerting = _general_settings.get("alerting", [])
diff --git a/tests/proxy_unit_tests/test_proxy_server.py b/tests/proxy_unit_tests/test_proxy_server.py
index 71b4503ff3c..17a6f8eb9ae 100644
--- a/tests/proxy_unit_tests/test_proxy_server.py
+++ b/tests/proxy_unit_tests/test_proxy_server.py
@@ -2409,7 +2409,7 @@ async def test_get_config_callbacks_with_all_types(client_no_auth):
Test that /get/config/callbacks returns all three callback types:
- success_callback with type="success"
- failure_callback with type="failure"
- - callbacks (generic) with type="generic"
+ - callbacks (success_and_failure) with type="success_and_failure"
"""
from litellm.proxy.proxy_server import ProxyConfig
@@ -2437,7 +2437,7 @@ async def test_get_config_callbacks_with_all_types(client_no_auth):
with patch.object(
proxy_config, "get_config", new=AsyncMock(return_value=mock_config_data)
), patch(
- "litellm.proxy.proxy_server.decrypt_value_helper",
+ "litellm.proxy.common_utils.callback_utils.decrypt_value_helper",
side_effect=lambda value, key=None: value
):
response = client_no_auth.get("/get/config/callbacks")
@@ -2452,20 +2452,20 @@ async def test_get_config_callbacks_with_all_types(client_no_auth):
callbacks = result["callbacks"]
- # Verify we have all 5 callbacks (2 success + 1 failure + 2 generic)
+ # Verify we have all 5 callbacks (2 success + 1 failure + 2 success_and_failure)
assert len(callbacks) == 5
# Group callbacks by type
success_callbacks = [cb for cb in callbacks if cb.get("type") == "success"]
failure_callbacks = [cb for cb in callbacks if cb.get("type") == "failure"]
- generic_callbacks = [cb for cb in callbacks if cb.get("type") == "generic"]
+ success_and_failure_callbacks = [cb for cb in callbacks if cb.get("type") == "success_and_failure"]
# Verify all callbacks have required fields
for callback in callbacks:
assert "name" in callback
assert "variables" in callback
assert "type" in callback
- assert callback["type"] in ["success", "failure", "generic"]
+ assert callback["type"] in ["success", "failure", "success_and_failure"]
# Verify success callbacks
assert len(success_callbacks) == 2
@@ -2477,11 +2477,11 @@ async def test_get_config_callbacks_with_all_types(client_no_auth):
assert len(failure_callbacks) == 1
assert failure_callbacks[0]["name"] == "sentry"
- # Verify generic callbacks
- assert len(generic_callbacks) == 2
- generic_names = [cb["name"] for cb in generic_callbacks]
- assert "otel" in generic_names
- assert "langsmith" in generic_names
+ # Verify success_and_failure callbacks
+ assert len(success_and_failure_callbacks) == 2
+ success_and_failure_names = [cb["name"] for cb in success_and_failure_callbacks]
+ assert "otel" in success_and_failure_names
+ assert "langsmith" in success_and_failure_names
@pytest.mark.asyncio
@@ -2521,7 +2521,7 @@ async def test_get_config_callbacks_environment_variables(client_no_auth):
with patch.object(
proxy_config, "get_config", new=AsyncMock(return_value=mock_config_data)
), patch(
- "litellm.proxy.proxy_server.decrypt_value_helper",
+ "litellm.proxy.common_utils.callback_utils.decrypt_value_helper",
side_effect=mock_decrypt
):
response = client_no_auth.get("/get/config/callbacks")
@@ -2548,12 +2548,12 @@ async def test_get_config_callbacks_environment_variables(client_no_auth):
assert "LANGFUSE_HOST" in langfuse_vars
assert langfuse_vars["LANGFUSE_HOST"] == "https://cloud.langfuse.com"
- # Find otel callback (generic type)
+ # Find otel callback (success_and_failure type)
otel_callback = next(
(cb for cb in callbacks if cb["name"] == "otel"), None
)
assert otel_callback is not None
- assert otel_callback["type"] == "generic"
+ assert otel_callback["type"] == "success_and_failure"
assert "variables" in otel_callback
# Verify otel env vars are present
diff --git a/tests/test_litellm/proxy/common_utils/test_callback_utils.py b/tests/test_litellm/proxy/common_utils/test_callback_utils.py
index b9ed4b9b508..877f0092182 100644
--- a/tests/test_litellm/proxy/common_utils/test_callback_utils.py
+++ b/tests/test_litellm/proxy/common_utils/test_callback_utils.py
@@ -9,6 +9,9 @@ from litellm.proxy.common_utils.callback_utils import (
get_remaining_tokens_and_requests_from_request_data,
)
+from unittest.mock import patch
+from litellm.proxy.common_utils.callback_utils import process_callback
+
def test_get_remaining_tokens_and_requests_from_request_data():
model_group = "openrouter/google/gemini-2.0-flash-001"
@@ -27,3 +30,47 @@ def test_get_remaining_tokens_and_requests_from_request_data():
f"x-litellm-key-remaining-requests-{expected_name}": 100,
f"x-litellm-key-remaining-tokens-{expected_name}": 200,
}
+
+
+@patch(
+ "litellm.proxy.common_utils.callback_utils.CustomLogger.get_callback_env_vars",
+ return_value=["API_KEY", "MISSING_VAR"],
+)
+@patch(
+ "litellm.proxy.common_utils.callback_utils.decrypt_value_helper",
+ side_effect=lambda value, key: f"decrypted-{key}",
+)
+def test_process_callback_with_env_vars(mock_decrypt, mock_get_env_vars):
+ environment_variables = {
+ "API_KEY": "ENC_VALUE",
+ "UNUSED": "SHOULD_BE_IGNORED",
+ }
+
+ result = process_callback(
+ _callback="my_callback",
+ callback_type="input",
+ environment_variables=environment_variables,
+ )
+
+ assert result["name"] == "my_callback"
+ assert result["type"] == "input"
+ assert result["variables"] == {
+ "API_KEY": "decrypted-API_KEY",
+ "MISSING_VAR": None,
+ }
+
+
+@patch(
+ "litellm.proxy.common_utils.callback_utils.CustomLogger.get_callback_env_vars",
+ return_value=[],
+)
+def test_process_callback_with_no_required_env_vars(mock_get_env_vars):
+ result = process_callback(
+ _callback="another_callback",
+ callback_type="output",
+ environment_variables={"SHOULD_NOT_BE_USED": "VALUE"},
+ )
+
+ assert result["name"] == "another_callback"
+ assert result["type"] == "output"
+ assert result["variables"] == {}