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
drop response body from vertex/bedrock transformation errors
Michael Riad Zakyb07e1c034183
diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.pyindex a27153365d2..61a7d4c08db 100644--- a/litellm/llms/bedrock/chat/converse_transformation.py+++ b/litellm/llms/bedrock/chat/converse_transformation.py@@ -1942,8 +1942,8 @@ class AmazonConverseConfig(BaseConfig):completion_response = ConverseResponseBlock(**response.json()) # type: ignoreexcept Exception as e:raise BedrockError(- message="Received={}, Error converting to valid response block={}. File an issue if litellm error - https://github.com/BerriAI/litellm/issues".format(- response.text, str(e)+ message="Error converting to valid response block={}. File an issue if litellm error - https://github.com/BerriAI/litellm/issues".format(+ str(e)),status_code=422,)diff --git a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.pyindex a90e05919fb..474ddb402a1 100644--- a/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py+++ b/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py@@ -2395,8 +2395,8 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):completion_response = GenerateContentResponseBody(**raw_response.json()) # type: ignoreexcept Exception as e:raise VertexAIError(- message="Received={}, Error converting to valid response block={}. File an issue if litellm error - https://github.com/BerriAI/litellm/issues".format(- raw_response.text, str(e)+ message="Error converting to valid response block={}. File an issue if litellm error - https://github.com/BerriAI/litellm/issues".format(+ str(e)),status_code=422,headers=raw_response.headers,@@ -2530,8 +2530,8 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):except Exception as e:raise VertexAIError(- message="Received={}, Error converting to valid response block={}. File an issue if litellm error - https://github.com/BerriAI/litellm/issues".format(- completion_response, str(e)+ message="Error converting to valid response block={}. File an issue if litellm error - https://github.com/BerriAI/litellm/issues".format(+ str(e)),status_code=422,headers=raw_response.headers,diff --git a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.pyindex 8e53e57f1e0..633aed38a08 100644--- a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py+++ b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py@@ -4146,3 +4146,39 @@ def test_transform_response_finish_reason_stop_when_json_mode_filters_all_tools(# finish_reason must be "stop", not "tool_calls"assert result.choices[0].finish_reason == "stop"+++def test_transform_response_does_not_leak_body_on_parse_failure():+ from litellm.llms.bedrock.common_utils import BedrockError++ leaky_body = {"output": {"message": {"content": [{"text": "secret content"}]}}}++ class MockResponse:+ def json(self):+ return leaky_body++ @property+ def text(self):+ return json.dumps(leaky_body)++ with patch(+ "litellm.llms.bedrock.chat.converse_transformation.ConverseResponseBlock",+ side_effect=KeyError("missing required field"),+ ):+ with pytest.raises(BedrockError) as exc_info:+ AmazonConverseConfig()._transform_response(+ model="bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0",+ response=MockResponse(),+ model_response=ModelResponse(),+ stream=False,+ logging_obj=None,+ optional_params={},+ api_key=None,+ data=None,+ messages=[],+ encoding=None,+ )++ msg = str(exc_info.value)+ assert "secret content" not in msg+ assert "Error converting to valid response block" in msgdiff --git a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.pyindex 0118b39ddd1..353d19b0198 100644--- a/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py+++ b/tests/test_litellm/llms/vertex_ai/gemini/test_vertex_and_google_ai_studio_gemini.py@@ -4261,3 +4261,32 @@ def test_sync_streaming_uses_custom_client():# Verify that gemini_client is in the partial's keywordsassert "gemini_client" in partial_make_sync_call.keywordsassert partial_make_sync_call.keywords["gemini_client"] is mock_client+++def test_transform_response_does_not_leak_body_on_parse_failure():+ leaky_body = {"candidates": [{"content": {"parts": [{"text": "secret content"}]}}]}+ raw_response = MagicMock()+ raw_response.json.return_value = leaky_body+ raw_response.text = json.dumps(leaky_body)+ raw_response.headers = {}++ with patch(+ "litellm.llms.vertex_ai.gemini.vertex_and_google_ai_studio_gemini.GenerateContentResponseBody",+ side_effect=KeyError("missing required field"),+ ):+ with pytest.raises(VertexAIError) as exc_info:+ VertexGeminiConfig().transform_response(+ model="gemini-pro",+ raw_response=raw_response,+ model_response=ModelResponse(),+ logging_obj=MagicMock(),+ request_data={},+ messages=[],+ optional_params={},+ litellm_params={},+ encoding=None,+ )++ msg = str(exc_info.value)+ assert "secret content" not in msg+ assert "Error converting to valid response block" in msg