fix(bedrock): skip passthrough output write-back when guardrail returns no texts

This commit is contained in:
mateo-berri
2026-06-12 10:10:50 +00:00
parent 0a186774f8
commit b4fb2b2e16
2 changed files with 21 additions and 1 deletions
@@ -497,6 +497,7 @@ class BedrockPassthroughGuardrailHandler(BaseTranslation):
)
guardrailed_texts = guardrailed_inputs.get("texts", [])
_write_back_texts(guardrailed_texts, holders)
if guardrailed_texts:
_write_back_texts(guardrailed_texts, holders)
return response
@@ -597,6 +597,25 @@ class TestBedrockPassthroughGuardrailHandlerOutput:
assert result["output"]["message"]["content"][0]["text"] == "[MASKED]"
assert result["stopReason"] == "end_turn"
@pytest.mark.asyncio
async def test_response_guardrail_returning_no_texts_preserves_output(self, monkeypatch):
"""A guardrail that returns no texts must leave the response untouched and
not warn, mirroring the request path's empty-result guard."""
handler = BedrockPassthroughGuardrailHandler()
response = self._converse_response("Model reply")
guardrail = _make_guardrail({"texts": []})
warnings = []
monkeypatch.setattr(
"litellm.llms.bedrock.passthrough.guardrail_translation.handler.verbose_proxy_logger.warning",
lambda *args, **kwargs: warnings.append(args),
)
result = await handler.process_output_response(response=response, guardrail_to_apply=guardrail)
assert not warnings
assert result["output"]["message"]["content"][0]["text"] == "Model reply"
@pytest.mark.asyncio
async def test_response_reasoning_and_tooluse_extracted_and_masked(self):
"""Model output hidden in reasoningContent.reasoningText.text and