drop response body from vertex/bedrock transformation errors

This commit is contained in:
Michael Riad Zaky
2026-04-28 17:38:42 -07:00
parent fd32f29e39
commit b07e1c0341
4 changed files with 71 additions and 6 deletions
@@ -1942,8 +1942,8 @@ class AmazonConverseConfig(BaseConfig):
completion_response = ConverseResponseBlock(**response.json()) # type: ignore
except 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,
)
@@ -2395,8 +2395,8 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):
completion_response = GenerateContentResponseBody(**raw_response.json()) # type: ignore
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(
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,
@@ -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 msg
@@ -4261,3 +4261,32 @@ def test_sync_streaming_uses_custom_client():
# Verify that gemini_client is in the partial's keywords
assert "gemini_client" in partial_make_sync_call.keywords
assert 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