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(test): add request_data param to test mock + black formatting

michelligabrielefa7ccf0893f6
diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py
index 5323f692b80..a5262505983 100644
--- a/litellm/litellm_core_utils/litellm_logging.py
+++ b/litellm/litellm_core_utils/litellm_logging.py
@@ -354,9 +354,9 @@ class Logging(LiteLLMLoggingBaseClass):
)
self.function_id = function_id
self.streaming_chunks: List[Any] = [] # for generating complete stream response
- self.sync_streaming_chunks: List[
- Any
- ] = [] # for generating complete stream response
+ self.sync_streaming_chunks: List[Any] = (
+ []
+ ) # for generating complete stream response
self.log_raw_request_response = log_raw_request_response
# Initialize dynamic callbacks
@@ -801,9 +801,9 @@ class Logging(LiteLLMLoggingBaseClass):
prompt_spec=prompt_spec,
dynamic_callback_params=dynamic_callback_params,
):
- self.model_call_details[
- "prompt_integration"
- ] = logger.__class__.__name__
+ self.model_call_details["prompt_integration"] = (
+ logger.__class__.__name__
+ )
return logger
except Exception:
# If check fails, continue to next logger
@@ -871,9 +871,9 @@ class Logging(LiteLLMLoggingBaseClass):
if anthropic_cache_control_logger := AnthropicCacheControlHook.get_custom_logger_for_anthropic_cache_control_hook(
non_default_params
):
- self.model_call_details[
- "prompt_integration"
- ] = anthropic_cache_control_logger.__class__.__name__
+ self.model_call_details["prompt_integration"] = (
+ anthropic_cache_control_logger.__class__.__name__
+ )
return anthropic_cache_control_logger
#########################################################
@@ -885,9 +885,9 @@ class Logging(LiteLLMLoggingBaseClass):
internal_usage_cache=None,
llm_router=None,
)
- self.model_call_details[
- "prompt_integration"
- ] = vector_store_custom_logger.__class__.__name__
+ self.model_call_details["prompt_integration"] = (
+ vector_store_custom_logger.__class__.__name__
+ )
# Add to global callbacks so post-call hooks are invoked
if (
vector_store_custom_logger
@@ -947,9 +947,9 @@ class Logging(LiteLLMLoggingBaseClass):
model
): # if model name was changes pre-call, overwrite the initial model call name with the new one
self.model_call_details["model"] = model
- self.model_call_details["litellm_params"][
- "api_base"
- ] = self._get_masked_api_base(additional_args.get("api_base", ""))
+ self.model_call_details["litellm_params"]["api_base"] = (
+ self._get_masked_api_base(additional_args.get("api_base", ""))
+ )
def pre_call(self, input, api_key, model=None, additional_args={}): # noqa: PLR0915
# Log the exact input to the LLM API
@@ -978,9 +978,7 @@ class Logging(LiteLLMLoggingBaseClass):
try:
# [Non-blocking Extra Debug Information in metadata]
if turn_off_message_logging is True:
- _metadata[
- "raw_request"
- ] = "redacted by litellm. \
+ _metadata["raw_request"] = "redacted by litellm. \
'litellm.turn_off_message_logging=True'"
else:
curl_command = self._get_request_curl_command(
@@ -992,35 +990,31 @@ class Logging(LiteLLMLoggingBaseClass):
_metadata["raw_request"] = str(curl_command)
# split up, so it's easier to parse in the UI
- self.model_call_details[
- "raw_request_typed_dict"
- ] = RawRequestTypedDict(
- raw_request_api_base=str(
- additional_args.get("api_base") or ""
- ),
- raw_request_body=self._get_raw_request_body(
- additional_args.get("complete_input_dict", {})
- ),
- # NOTE: setting ignore_sensitive_headers to True will cause
- # the Authorization header to be leaked when calls to the health
- # endpoint are made and fail.
- raw_request_headers=self._get_masked_headers(
- additional_args.get("headers", {}) or {},
- ),
- error=None,
+ self.model_call_details["raw_request_typed_dict"] = (
+ RawRequestTypedDict(
+ raw_request_api_base=str(
+ additional_args.get("api_base") or ""
+ ),
+ raw_request_body=self._get_raw_request_body(
+ additional_args.get("complete_input_dict", {})
+ ),
+ # NOTE: setting ignore_sensitive_headers to True will cause
+ # the Authorization header to be leaked when calls to the health
+ # endpoint are made and fail.
+ raw_request_headers=self._get_masked_headers(
+ additional_args.get("headers", {}) or {},
+ ),
+ error=None,
+ )
)
except Exception as e:
- self.model_call_details[
- "raw_request_typed_dict"
- ] = RawRequestTypedDict(
- error=str(e),
- )
- _metadata[
- "raw_request"
- ] = "Unable to Log \
- raw request: {}".format(
- str(e)
+ self.model_call_details["raw_request_typed_dict"] = (
+ RawRequestTypedDict(
+ error=str(e),
+ )
)
+ _metadata["raw_request"] = "Unable to Log \
+ raw request: {}".format(str(e))
if getattr(self, "logger_fn", None) and callable(self.logger_fn):
try:
self.logger_fn(
@@ -1320,13 +1314,13 @@ class Logging(LiteLLMLoggingBaseClass):
for callback in callbacks:
try:
if isinstance(callback, CustomLogger):
- response: Optional[
- MCPPostCallResponseObject
- ] = await callback.async_post_mcp_tool_call_hook(
- kwargs=kwargs,
- response_obj=post_mcp_tool_call_response_obj,
- start_time=start_time,
- end_time=end_time,
+ response: Optional[MCPPostCallResponseObject] = (
+ await callback.async_post_mcp_tool_call_hook(
+ kwargs=kwargs,
+ response_obj=post_mcp_tool_call_response_obj,
+ start_time=start_time,
+ end_time=end_time,
+ )
)
######################################################################
# if any of the callbacks modify the response, use the modified response
@@ -1527,9 +1521,9 @@ class Logging(LiteLLMLoggingBaseClass):
verbose_logger.debug(
f"response_cost_failure_debug_information: {debug_info}"
)
- self.model_call_details[
- "response_cost_failure_debug_information"
- ] = debug_info
+ self.model_call_details["response_cost_failure_debug_information"] = (
+ debug_info
+ )
return None
try:
@@ -1555,9 +1549,9 @@ class Logging(LiteLLMLoggingBaseClass):
verbose_logger.debug(
f"response_cost_failure_debug_information: {debug_info}"
)
- self.model_call_details[
- "response_cost_failure_debug_information"
- ] = debug_info
+ self.model_call_details["response_cost_failure_debug_information"] = (
+ debug_info
+ )
return None
@@ -1706,9 +1700,9 @@ class Logging(LiteLLMLoggingBaseClass):
self.model_call_details["litellm_params"].setdefault("metadata", {})
if self.model_call_details["litellm_params"]["metadata"] is None:
self.model_call_details["litellm_params"]["metadata"] = {}
- self.model_call_details["litellm_params"]["metadata"][
- "hidden_params"
- ] = getattr(logging_result, "_hidden_params", {})
+ self.model_call_details["litellm_params"]["metadata"]["hidden_params"] = (
+ getattr(logging_result, "_hidden_params", {})
+ )
def _process_hidden_params_and_response_cost(
self,
@@ -1737,9 +1731,9 @@ class Logging(LiteLLMLoggingBaseClass):
result=logging_result
)
- self.model_call_details[
- "standard_logging_object"
- ] = self._build_standard_logging_payload(logging_result, start_time, end_time)
+ self.model_call_details["standard_logging_object"] = (
+ self._build_standard_logging_payload(logging_result, start_time, end_time)
+ )
if (
standard_logging_payload := self.model_call_details.get(
@@ -1817,9 +1811,9 @@ class Logging(LiteLLMLoggingBaseClass):
end_time = datetime.datetime.now()
if self.completion_start_time is None:
self.completion_start_time = end_time
- self.model_call_details[
- "completion_start_time"
- ] = self.completion_start_time
+ self.model_call_details["completion_start_time"] = (
+ self.completion_start_time
+ )
self.model_call_details["log_event_type"] = "successful_api_call"
self.model_call_details["end_time"] = end_time
@@ -1856,10 +1850,10 @@ class Logging(LiteLLMLoggingBaseClass):
end_time=end_time,
)
elif isinstance(result, dict) or isinstance(result, list):
- self.model_call_details[
- "standard_logging_object"
- ] = self._build_standard_logging_payload(
- result, start_time, end_time
+ self.model_call_details["standard_logging_object"] = (
+ self._build_standard_logging_payload(
+ result, start_time, end_time
+ )
)
if (
standard_logging_payload := self.model_call_details.get(
@@ -1868,9 +1862,9 @@ class Logging(LiteLLMLoggingBaseClass):
) is not None:
emit_standard_logging_payload(standard_logging_payload)
elif standard_logging_object is not None:
- self.model_call_details[
- "standard_logging_object"
- ] = standard_logging_object
+ self.model_call_details["standard_logging_object"] = (
+ standard_logging_object
+ )
else:
self.model_call_details["response_cost"] = None
@@ -2028,20 +2022,20 @@ class Logging(LiteLLMLoggingBaseClass):
verbose_logger.debug(
"Logging Details LiteLLM-Success Call streaming complete"
)
- self.model_call_details[
- "complete_streaming_response"
- ] = complete_streaming_response
- self.model_call_details[
- "response_cost"
- ] = self._response_cost_calculator(result=complete_streaming_response)
+ self.model_call_details["complete_streaming_response"] = (
+ complete_streaming_response
+ )
+ self.model_call_details["response_cost"] = (
+ self._response_cost_calculator(result=complete_streaming_response)
+ )
self._merge_hidden_params_from_response_into_metadata(
complete_streaming_response
)
## STANDARDIZED LOGGING PAYLOAD
- self.model_call_details[
- "standard_logging_object"
- ] = self._build_standard_logging_payload(
- complete_streaming_response, start_time, end_time
+ self.model_call_details["standard_logging_object"] = (
+ self._build_standard_logging_payload(
+ complete_streaming_response, start_time, end_time
+ )
)
if (
standard_logging_payload := self.model_call_details.get(
@@ -2375,10 +2369,10 @@ class Logging(LiteLLMLoggingBaseClass):
)
else:
if self.stream and complete_streaming_response:
- self.model_call_details[
- "complete_response"
- ] = self.model_call_details.get(
- "complete_streaming_response", {}
+ self.model_call_details["complete_response"] = (
+ self.model_call_details.get(
+ "complete_streaming_response", {}
+ )
)
result = self.model_call_details["complete_response"]
openMeterLogger.log_success_event(
@@ -2402,10 +2396,10 @@ class Logging(LiteLLMLoggingBaseClass):
)
else:
if self.stream and complete_streaming_response:
- self.model_call_details[
- "complete_response"
- ] = self.model_call_details.get(
- "complete_streaming_response", {}
+ self.model_call_details["complete_response"] = (
+ self.model_call_details.get(
+ "complete_streaming_response", {}
+ )
)
result = self.model_call_details["complete_response"]
@@ -2544,9 +2538,9 @@ class Logging(LiteLLMLoggingBaseClass):
if complete_streaming_response is not None:
print_verbose("Async success callbacks: Got a complete streaming response")
- self.model_call_details[
- "async_complete_streaming_response"
- ] = complete_streaming_response
+ self.model_call_details["async_complete_streaming_response"] = (
+ complete_streaming_response
+ )
try:
if self.model_call_details.get("cache_hit", False) is True:
@@ -2557,10 +2551,10 @@ class Logging(LiteLLMLoggingBaseClass):
model_call_details=self.model_call_details
)
# base_model defaults to None if not set on model_info
- self.model_call_details[
- "response_cost"
- ] = self._response_cost_calculator(
- result=complete_streaming_response
+ self.model_call_details["response_cost"] = (
+ self._response_cost_calculator(
+ result=complete_streaming_response
+ )
)
verbose_logger.debug(
@@ -2577,10 +2571,10 @@ class Logging(LiteLLMLoggingBaseClass):
)
## STANDARDIZED LOGGING PAYLOAD
- self.model_call_details[
- "standard_logging_object"
- ] = self._build_standard_logging_payload(
- complete_streaming_response, start_time, end_time
+ self.model_call_details["standard_logging_object"] = (
+ self._build_standard_logging_payload(
+ complete_streaming_response, start_time, end_time
+ )
)
# print standard logging payload
@@ -2607,9 +2601,9 @@ class Logging(LiteLLMLoggingBaseClass):
# _success_handler_helper_fn
if self.model_call_details.get("standard_logging_object") is None:
## STANDARDIZED LOGGING PAYLOAD
- self.model_call_details[
- "standard_logging_object"
- ] = self._build_standard_logging_payload(result, start_time, end_time)
+ self.model_call_details["standard_logging_object"] = (
+ self._build_standard_logging_payload(result, start_time, end_time)
+ )
# print standard logging payload
if (
@@ -2852,18 +2846,18 @@ class Logging(LiteLLMLoggingBaseClass):
## STANDARDIZED LOGGING PAYLOAD
- self.model_call_details[
- "standard_logging_object"
- ] = get_standard_logging_object_payload(
- kwargs=self.model_call_details,
- init_response_obj={},
- start_time=start_time,
- end_time=end_time,
- logging_obj=self,
- status="failure",
- error_str=str(exception),
- original_exception=exception,
- standard_built_in_tools_params=self.standard_built_in_tools_params,
+ self.model_call_details["standard_logging_object"] = (
+ get_standard_logging_object_payload(
+ kwargs=self.model_call_details,
+ init_response_obj={},
+ start_time=start_time,
+ end_time=end_time,
+ logging_obj=self,
+ status="failure",
+ error_str=str(exception),
+ original_exception=exception,
+ standard_built_in_tools_params=self.standard_built_in_tools_params,
+ )
)
return start_time, end_time
@@ -3831,9 +3825,9 @@ def _init_custom_logger_compatible_class( # noqa: PLR0915
service_name=arize_config.project_name,
)
- os.environ[
- "OTEL_EXPORTER_OTLP_TRACES_HEADERS"
- ] = f"space_id={arize_config.space_key or arize_config.space_id},api_key={arize_config.api_key}"
+ os.environ["OTEL_EXPORTER_OTLP_TRACES_HEADERS"] = (
+ f"space_id={arize_config.space_key or arize_config.space_id},api_key={arize_config.api_key}"
+ )
for callback in _in_memory_loggers:
if (
isinstance(callback, ArizeLogger)
@@ -3859,13 +3853,13 @@ def _init_custom_logger_compatible_class( # noqa: PLR0915
existing_attrs = os.environ.get("OTEL_RESOURCE_ATTRIBUTES", "")
# Add openinference.project.name attribute
if existing_attrs:
- os.environ[
- "OTEL_RESOURCE_ATTRIBUTES"
- ] = f"{existing_attrs},openinference.project.name={arize_phoenix_config.project_name}"
+ os.environ["OTEL_RESOURCE_ATTRIBUTES"] = (
+ f"{existing_attrs},openinference.project.name={arize_phoenix_config.project_name}"
+ )
else:
- os.environ[
- "OTEL_RESOURCE_ATTRIBUTES"
- ] = f"openinference.project.name={arize_phoenix_config.project_name}"
+ os.environ["OTEL_RESOURCE_ATTRIBUTES"] = (
+ f"openinference.project.name={arize_phoenix_config.project_name}"
+ )
# Set Phoenix project name from environment variable
phoenix_project_name = os.environ.get("PHOENIX_PROJECT_NAME", None)
@@ -3873,19 +3867,19 @@ def _init_custom_logger_compatible_class( # noqa: PLR0915
existing_attrs = os.environ.get("OTEL_RESOURCE_ATTRIBUTES", "")
# Add openinference.project.name attribute
if existing_attrs:
- os.environ[
- "OTEL_RESOURCE_ATTRIBUTES"
- ] = f"{existing_attrs},openinference.project.name={phoenix_project_name}"
+ os.environ["OTEL_RESOURCE_ATTRIBUTES"] = (
+ f"{existing_attrs},openinference.project.name={phoenix_project_name}"
+ )
else:
- os.environ[
- "OTEL_RESOURCE_ATTRIBUTES"
- ] = f"openinference.project.name={phoenix_project_name}"
+ os.environ["OTEL_RESOURCE_ATTRIBUTES"] = (
+ f"openinference.project.name={phoenix_project_name}"
+ )
# auth can be disabled on local deployments of arize phoenix
if arize_phoenix_config.otlp_auth_headers is not None:
- os.environ[
- "OTEL_EXPORTER_OTLP_TRACES_HEADERS"
- ] = arize_phoenix_config.otlp_auth_headers
+ os.environ["OTEL_EXPORTER_OTLP_TRACES_HEADERS"] = (
+ arize_phoenix_config.otlp_auth_headers
+ )
for callback in _in_memory_loggers:
if (
@@ -4072,9 +4066,9 @@ def _init_custom_logger_compatible_class( # noqa: PLR0915
exporter="otlp_http",
endpoint="https://langtrace.ai/api/trace",
)
- os.environ[
- "OTEL_EXPORTER_OTLP_TRACES_HEADERS"
- ] = f"api_key={os.getenv('LANGTRACE_API_KEY')}"
+ os.environ["OTEL_EXPORTER_OTLP_TRACES_HEADERS"] = (
+ f"api_key={os.getenv('LANGTRACE_API_KEY')}"
+ )
for callback in _in_memory_loggers:
if (
isinstance(callback, OpenTelemetry)
@@ -4998,10 +4992,10 @@ class StandardLoggingPayloadSetup:
for key in StandardLoggingHiddenParams.__annotations__.keys():
if key in hidden_params:
if key == "additional_headers":
- clean_hidden_params[
- "additional_headers"
- ] = StandardLoggingPayloadSetup.get_additional_headers(
- hidden_params[key]
+ clean_hidden_params["additional_headers"] = (
+ StandardLoggingPayloadSetup.get_additional_headers(
+ hidden_params[key]
+ )
)
else:
clean_hidden_params[key] = hidden_params[key] # type: ignore
@@ -5640,9 +5634,9 @@ def scrub_sensitive_keys_in_metadata(litellm_params: Optional[dict]):
):
for k, v in metadata["user_api_key_metadata"].items():
if k == "logging": # prevent logging user logging keys
- cleaned_user_api_key_metadata[
- k
- ] = "scrubbed_by_litellm_for_sensitive_keys"
+ cleaned_user_api_key_metadata[k] = (
+ "scrubbed_by_litellm_for_sensitive_keys"
+ )
else:
cleaned_user_api_key_metadata[k] = v
diff --git a/litellm/llms/openai/chat/guardrail_translation/handler.py b/litellm/llms/openai/chat/guardrail_translation/handler.py
index f854cdb13d0..0ce9d57a65f 100644
--- a/litellm/llms/openai/chat/guardrail_translation/handler.py
+++ b/litellm/llms/openai/chat/guardrail_translation/handler.py
@@ -86,9 +86,9 @@ class OpenAIChatCompletionsHandler(BaseTranslation):
if tool_calls_to_check:
inputs["tool_calls"] = tool_calls_to_check # type: ignore
if messages:
- inputs[
- "structured_messages"
- ] = messages # pass the openai /chat/completions messages to the guardrail, as-is
+ inputs["structured_messages"] = (
+ messages # pass the openai /chat/completions messages to the guardrail, as-is
+ )
# Pass tools (function definitions) to the guardrail
tools = data.get("tools")
if tools:
diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py
index 7954f0b6460..b9c1e2f2bc3 100644
--- a/litellm/proxy/utils.py
+++ b/litellm/proxy/utils.py
@@ -1898,9 +1898,9 @@ class ProxyLogging:
normalized_call_type = CallTypes.aembedding.value
if normalized_call_type is not None:
litellm_logging_obj.call_type = normalized_call_type
- litellm_logging_obj.model_call_details[
- "call_type"
- ] = normalized_call_type
+ litellm_logging_obj.model_call_details["call_type"] = (
+ normalized_call_type
+ )
# Pass-through endpoints are logged via the callback loop's
# async_post_call_failure_hook — skip pre_call and failure handlers.
if litellm_logging_obj.call_type == CallTypes.pass_through.value:
@@ -2498,8 +2498,7 @@ class PrismaClient:
required_view = "LiteLLM_VerificationTokenView"
expected_views_str = ", ".join(f"'{view}'" for view in expected_views)
pg_schema = os.getenv("DATABASE_SCHEMA", "public")
- ret = await self.db.query_raw(
- f"""
+ ret = await self.db.query_raw(f"""
WITH existing_views AS (
SELECT viewname
FROM pg_views
@@ -2511,8 +2510,7 @@ class PrismaClient:
(SELECT COUNT(*) FROM existing_views) AS view_count,
ARRAY_AGG(viewname) AS view_names
FROM existing_views
- """
- )
+ """)
expected_total_views = len(expected_views)
if ret[0]["view_count"] == expected_total_views:
verbose_proxy_logger.info("All necessary views exist!")
@@ -2521,8 +2519,7 @@ class PrismaClient:
## check if required view exists ##
if ret[0]["view_names"] and required_view not in ret[0]["view_names"]:
await self.health_check() # make sure we can connect to db
- await self.db.execute_raw(
- """
+ await self.db.execute_raw("""
CREATE VIEW "LiteLLM_VerificationTokenView" AS
SELECT
v.*,
@@ -2532,8 +2529,7 @@ class PrismaClient:
t.rpm_limit AS team_rpm_limit
FROM "LiteLLM_VerificationToken" v
LEFT JOIN "LiteLLM_TeamTable" t ON v.team_id = t.team_id;
- """
- )
+ """)
verbose_proxy_logger.info(
"LiteLLM_VerificationTokenView Created in DB!"
@@ -2759,7 +2755,7 @@ class PrismaClient:
and reset_at is not None
):
response = await self.db.litellm_verificationtoken.find_many(
- where={ # type:ignore
+ where={ # type: ignore
"OR": [
{"expires": None},
{"expires": {"gt": expires}},
@@ -2819,7 +2815,7 @@ class PrismaClient:
) # type: ignore
elif query_type == "find_all" and reset_at is not None:
response = await self.db.litellm_usertable.find_many(
- where={ # type:ignore
+ where={ # type: ignore
"budget_reset_at": {"lt": reset_at},
}
)
@@ -2831,10 +2827,10 @@ class PrismaClient:
if expires is not None:
response = await self.db.litellm_usertable.find_many( # type: ignore
order={"spend": "desc"},
- where={ # type:ignore
+ where={ # type: ignore
"OR": [
- {"expires": None}, # type:ignore
- {"expires": {"gt": expires}}, # type:ignore
+ {"expires": None}, # type: ignore
+ {"expires": {"gt": expires}}, # type: ignore
],
},
)
@@ -2881,7 +2877,7 @@ class PrismaClient:
elif table_name == "budget" and reset_at is not None:
if query_type == "find_all":
response = await self.db.litellm_budgettable.find_many(
- where={ # type:ignore
+ where={ # type: ignore
"OR": [
{
"AND": [
@@ -2909,7 +2905,7 @@ class PrismaClient:
)
elif query_type == "find_all" and reset_at is not None:
response = await self.db.litellm_teamtable.find_many(
- where={ # type:ignore
+ where={ # type: ignore
"budget_reset_at": {"lt": reset_at},
}
)
diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/unified_guardrails/test_unified_guardrail.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/unified_guardrails/test_unified_guardrail.py
index 7c29c8161db..bbba8e4d03d 100644
--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/unified_guardrails/test_unified_guardrail.py
+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/unified_guardrails/test_unified_guardrail.py
@@ -78,9 +78,7 @@ class TestUnifiedLLMGuardrails:
data = {
"guardrail_to_apply": guardrail,
- "messages": [
- {"role": "user", "content": "Tool: test\nArguments: {}"}
- ],
+ "messages": [{"role": "user", "content": "Tool: test\nArguments: {}"}],
"model": "mcp-tool-call",
}
@@ -102,9 +100,7 @@ class TestUnifiedLLMGuardrails:
data = {
"guardrail_to_apply": guardrail,
- "messages": [
- {"role": "user", "content": "Tool: test\nArguments: {}"}
- ],
+ "messages": [{"role": "user", "content": "Tool: test\nArguments: {}"}],
"model": "mcp-tool-call",
}
@@ -168,6 +164,7 @@ class TestUnifiedLLMGuardrails:
guardrail_to_apply,
litellm_logging_obj=None,
user_api_key_dict=None,
+ request_data=None,
):
# Simulate what the real handler does:
# put combined text in first chunk, clear the rest
@@ -200,10 +197,12 @@ class TestUnifiedLLMGuardrails:
chunks = []
for i in range(10):
chunk = ModelResponseStream(
- choices=[StreamingChoices(
- delta=Delta(content=f"word{i} ", role="assistant"),
- finish_reason=None,
- )],
+ choices=[
+ StreamingChoices(
+ delta=Delta(content=f"word{i} ", role="assistant"),
+ finish_reason=None,
+ )
+ ],
)
chunks.append(chunk)
@@ -228,7 +227,9 @@ class TestUnifiedLLMGuardrails:
response=mock_stream(),
request_data=request_data,
):
- content = item.choices[0].delta.content if item.choices[0].delta else None
+ content = (
+ item.choices[0].delta.content if item.choices[0].delta else None
+ )
yielded_contents.append(content)
# Every chunk should have non-empty content
@@ -271,10 +272,15 @@ class TestUnifiedLLMGuardrails:
assert guardrail.event_history == [GuardrailEventHooks.pre_call]
assert len(guardrail.apply_calls) == 1
assert guardrail.apply_calls[0]["input_type"] == "request"
- assert "https://arxiv.org/pdf/2201.04234" in guardrail.apply_calls[0]["inputs"]["texts"]
+ assert (
+ "https://arxiv.org/pdf/2201.04234"
+ in guardrail.apply_calls[0]["inputs"]["texts"]
+ )
# Data should be returned with document intact
- assert result["document"]["document_url"] == "https://arxiv.org/pdf/2201.04234"
+ assert (
+ result["document"]["document_url"] == "https://arxiv.org/pdf/2201.04234"
+ )
@pytest.mark.asyncio
async def test_moderation_hook_invokes_ocr_handler(self):
@@ -302,7 +308,10 @@ class TestUnifiedLLMGuardrails:
assert guardrail.event_history == [GuardrailEventHooks.during_call]
assert len(guardrail.apply_calls) == 1
- assert "https://example.com/scan.png" in guardrail.apply_calls[0]["inputs"]["texts"]
+ assert (
+ "https://example.com/scan.png"
+ in guardrail.apply_calls[0]["inputs"]["texts"]
+ )
@pytest.mark.asyncio
async def test_post_call_success_hook_guardrails_ocr_output(self):
@@ -318,7 +327,9 @@ class TestUnifiedLLMGuardrails:
def should_run_guardrail(self, data, event_type): # type: ignore[override]
return True
- async def apply_guardrail(self, inputs, request_data, input_type, **kwargs):
+ async def apply_guardrail(
+ self, inputs, request_data, input_type, **kwargs
+ ):
texts = inputs.get("texts", [])
return {"texts": [t.replace("SECRET", "[REDACTED]") for t in texts]}