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
fix(logging): skip parsing redacted tool call arguments (#38169)
devin-ai-integration[bot]a9c7b848f26e
diff --git a/litellm/constants.py b/litellm/constants.pyindex d6a00294004..0a1ada3bab2 100644--- a/litellm/constants.py+++ b/litellm/constants.py@@ -48,6 +48,7 @@ LITELLM_MAX_STREAMING_DURATION_SECONDS: Final = (# Data URIs exceeding this are replaced with a size placeholder.# Set to 0 to disable truncation.MAX_BASE64_LENGTH_FOR_LOGGING: Final = int(os.getenv("MAX_BASE64_LENGTH_FOR_LOGGING", 64))+REDACTED_BY_LITELLM: Final = "redacted-by-litellm"MAX_STRING_LENGTH_STDOUT_LOG: Final = get_env_int("MAX_STRING_LENGTH_STDOUT_LOG", 4096)diff --git a/litellm/litellm_core_utils/prompt_templates/factory.py b/litellm/litellm_core_utils/prompt_templates/factory.pyindex b676077ab0e..826a890eca9 100644--- a/litellm/litellm_core_utils/prompt_templates/factory.py+++ b/litellm/litellm_core_utils/prompt_templates/factory.py@@ -16,6 +16,7 @@ import litellm.typesimport litellm.types.llmsfrom litellm import verbose_loggerfrom litellm._uuid import uuid+from litellm.constants import REDACTED_BY_LITELLMfrom litellm.litellm_core_utils.url_utils import async_safe_get, safe_getfrom litellm.llms.custom_httpx.http_handler import HTTPHandler, get_async_httpx_clientfrom litellm.types.files import get_file_extension_from_mime_type@@ -5383,12 +5384,13 @@ def _parse_tool_call_arguments(raw: Any, tool_name: str | None, context: str) ->return rawif not isinstance(raw, str):return {}+ normalized_raw: Final = "{}" if raw == REDACTED_BY_LITELLM else rawfrom litellm.litellm_core_utils.prompt_templates.common_utils import (parse_tool_call_arguments,)try:- parsed: Final = parse_tool_call_arguments(raw, tool_name=tool_name, context=context)+ parsed: Final = parse_tool_call_arguments(normalized_raw, tool_name=tool_name, context=context)except ValueError as e:verbose_logger.warning("Failed to parse tool call arguments: %s", e)return {}diff --git a/litellm/litellm_core_utils/redact_messages.py b/litellm/litellm_core_utils/redact_messages.pyindex 0d590e1ceba..be187b091b8 100644--- a/litellm/litellm_core_utils/redact_messages.py+++ b/litellm/litellm_core_utils/redact_messages.py@@ -13,6 +13,7 @@ import inspectfrom typing import TYPE_CHECKING, Any, Finalimport litellm+from litellm.constants import REDACTED_BY_LITELLMfrom litellm.integrations.custom_logger import CustomLoggerfrom litellm.litellm_core_utils.core_helpers import (get_metadata_variable_name_from_kwargs,@@ -84,29 +85,29 @@ def _redact_tool_calls(tool_calls) -> None:for tool_call in tool_calls:function = getattr(tool_call, "function", None)if function is not None and hasattr(function, "arguments"):- function.arguments = "redacted-by-litellm"+ function.arguments = REDACTED_BY_LITELLMdef _redact_function_call(function_call) -> None:"""Redact legacy assistant function_call arguments."""if function_call is not None and hasattr(function_call, "arguments"):- function_call.arguments = "redacted-by-litellm"+ function_call.arguments = REDACTED_BY_LITELLMdef _redact_choice_content(choice):"""Helper to redact content in a choice (message or delta)."""if isinstance(choice, litellm.Choices):- choice.message.content = "redacted-by-litellm"+ choice.message.content = REDACTED_BY_LITELLMif hasattr(choice.message, "reasoning_content"):- choice.message.reasoning_content = "redacted-by-litellm"+ choice.message.reasoning_content = REDACTED_BY_LITELLMif hasattr(choice.message, "thinking_blocks"):choice.message.thinking_blocks = None_redact_tool_calls(getattr(choice.message, "tool_calls", None))_redact_function_call(getattr(choice.message, "function_call", None))elif isinstance(choice, litellm.utils.StreamingChoices):- choice.delta.content = "redacted-by-litellm"+ choice.delta.content = REDACTED_BY_LITELLMif hasattr(choice.delta, "reasoning_content"):- choice.delta.reasoning_content = "redacted-by-litellm"+ choice.delta.reasoning_content = REDACTED_BY_LITELLMif hasattr(choice.delta, "thinking_blocks"):choice.delta.thinking_blocks = None_redact_tool_calls(getattr(choice.delta, "tool_calls", None))@@ -117,22 +118,22 @@ def _redact_responses_api_output(output_items):"""Helper to redact ResponsesAPIResponse output items."""for output_item in output_items:if hasattr(output_item, "text"):- output_item.text = "redacted-by-litellm"+ output_item.text = REDACTED_BY_LITELLMif hasattr(output_item, "content") and isinstance(output_item.content, list):for content_part in output_item.content:if hasattr(content_part, "text"):- content_part.text = "redacted-by-litellm"+ content_part.text = REDACTED_BY_LITELLM# Redact reasoning items in output arrayif hasattr(output_item, "type") and output_item.type == "reasoning":if hasattr(output_item, "summary") and isinstance(output_item.summary, list):for summary_item in output_item.summary:if hasattr(summary_item, "text"):- summary_item.text = "redacted-by-litellm"+ summary_item.text = REDACTED_BY_LITELLMif hasattr(output_item, "type") and output_item.type == "function_call" and hasattr(output_item, "arguments"):- output_item.arguments = "redacted-by-litellm"+ output_item.arguments = REDACTED_BY_LITELLMdef _redact_responses_api_output_dict(output_items, redacted_str: str):@@ -164,7 +165,7 @@ def _redact_standard_logging_object(model_call_details: dict):if standard_logging_object is None:return- redacted_str: Final = "redacted-by-litellm"+ redacted_str: Final = REDACTED_BY_LITELLMif standard_logging_object.get("messages") is not None:standard_logging_object["messages"] = [{"role": "user", "content": redacted_str}]@@ -235,7 +236,7 @@ def perform_redaction(model_call_details: dict, result, redact_streaming_responscopy via redact_streaming_responses_for_custom_logger instead."""# Redact model_call_details- model_call_details["messages"] = [{"role": "user", "content": "redacted-by-litellm"}]+ model_call_details["messages"] = [{"role": "user", "content": REDACTED_BY_LITELLM}]model_call_details["prompt"] = ""model_call_details["input"] = ""_redact_standard_logging_object(model_call_details)@@ -256,7 +257,7 @@ def perform_redaction(model_call_details: dict, result, redact_streaming_responsor hasattr(result, "__anext__") # async generator): # async iterator# For async objects, return a simple redacted response without deepcopy- return {"text": "redacted-by-litellm"}+ return {"text": REDACTED_BY_LITELLM}if not (isinstance(result, (litellm.ModelResponse, litellm.ResponsesAPIResponse, litellm.EmbeddingResponse))@@ -273,11 +274,11 @@ def perform_redaction(model_call_details: dict, result, redact_streaming_responselif isinstance(_result, dict) and "choices" in _result:# Handle dict representation of ModelResponse (e.g., from model_dump())if _result.get("choices") is not None:- _redact_model_response_dict_choices(_result["choices"], "redacted-by-litellm")+ _redact_model_response_dict_choices(_result["choices"], REDACTED_BY_LITELLM)redact_vertex_ai_metadata_from_logged_object(_result)elif isinstance(_result, dict) and "output" in _result:if isinstance(_result.get("output"), list):- _redact_responses_api_output_dict(_result["output"], "redacted-by-litellm")+ _redact_responses_api_output_dict(_result["output"], REDACTED_BY_LITELLM)elif isinstance(_result, litellm.ResponsesAPIResponse):if hasattr(_result, "output"):_redact_responses_api_output(_result.output)@@ -288,7 +289,7 @@ def perform_redaction(model_call_details: dict, result, redact_streaming_responsif hasattr(_result, "data") and _result.data is not None:_result.data = []else:- return {"text": "redacted-by-litellm"}+ return {"text": REDACTED_BY_LITELLM}return _resultdiff --git a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.pyindex 3a7e06d085a..6265779b90d 100644--- a/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py+++ b/tests/test_litellm/litellm_core_utils/prompt_templates/test_litellm_core_utils_prompt_templates_factory.py@@ -1,6 +1,8 @@import base64import json+import loggingimport os+from typing import Finalfrom unittest.mock import MagicMock, patchimport pytest@@ -3309,6 +3311,66 @@ def test_get_tool_calls_from_response_include_all_choices_reads_every_choice():assert names == ["tool_alpha", "tool_beta"]+def test_get_tool_calls_from_response_silences_redacted_arguments(caplog):+ from litellm.litellm_core_utils.prompt_templates.factory import (+ get_tool_calls_from_response,+ )++ response: Final = {+ "choices": [+ {+ "message": {+ "tool_calls": [+ {+ "id": "call_1",+ "function": {+ "name": "Read",+ "arguments": "redacted-by-litellm",+ },+ }+ ]+ }+ }+ ]+ }++ with caplog.at_level(logging.WARNING, logger="LiteLLM"):+ tool_calls: Final = get_tool_calls_from_response(response)++ assert tool_calls == [{"id": "call_1", "name": "Read", "arguments": {}}]+ assert "Failed to parse tool call arguments" not in caplog.text+++def test_get_tool_calls_from_response_warns_for_malformed_arguments(caplog):+ from litellm.litellm_core_utils.prompt_templates.factory import (+ get_tool_calls_from_response,+ )++ response: Final = {+ "choices": [+ {+ "message": {+ "tool_calls": [+ {+ "id": "call_1",+ "function": {+ "name": "Read",+ "arguments": "not-json",+ },+ }+ ]+ }+ }+ ]+ }++ with caplog.at_level(logging.WARNING, logger="LiteLLM"):+ tool_calls: Final = get_tool_calls_from_response(response)++ assert tool_calls == [{"id": "call_1", "name": "Read", "arguments": {}}]+ assert "Failed to parse tool call arguments" in caplog.text++def test_group_tool_exchanges_pairs_assistant_with_its_tool_rows():from litellm.litellm_core_utils.prompt_templates.factory import group_tool_exchanges