From 3e7c35fc75b99e5e3af0b7eaa22d41ea6993b79c Mon Sep 17 00:00:00 2001 From: yucheng Date: Fri, 18 Sep 2026 23:33:42 +0000 Subject: [PATCH] fix(guardrails): keep structured tool outputs when a guardrail rewrites the turn Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../responses/guardrail_translation/handler.py | 5 ++++- .../test_openai_responses_guardrail_handler.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/litellm/llms/openai/responses/guardrail_translation/handler.py b/litellm/llms/openai/responses/guardrail_translation/handler.py index 943338d09c7..3f3535f3abc 100644 --- a/litellm/llms/openai/responses/guardrail_translation/handler.py +++ b/litellm/llms/openai/responses/guardrail_translation/handler.py @@ -251,8 +251,11 @@ def _rewritten_input_item(item: Mapping[str, object], rewritten: object) -> Mapp field: Final = _item_rewrite_field(item) if field is None or not isinstance(rewritten, Mapping): return None + original: Final = item.get(field) + if isinstance(original, Mapping): + return item rewritten_content: Final = rewritten.get("content") - if isinstance(item.get(field), str) and isinstance(rewritten_content, str): + if isinstance(original, str) and isinstance(rewritten_content, str): return {**item, field: rewritten_content} # mutable-ok: request input items must stay JSON-plain dicts rewritten_row: Final = cast("AllMessageValues", rewritten) # cast-ok: guardrails hand back chat-shaped rows converted_items, _ = LiteLLMResponsesTransformationHandler().convert_chat_completion_messages_to_responses_api( diff --git a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py index b914c80ad60..26c4c3dd9f5 100644 --- a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py +++ b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py @@ -3645,6 +3645,23 @@ class TestResponsesScopingFlags: assert result["input"][1]["output"] == screenshot assert guardrail.seen == [] + @pytest.mark.asyncio + async def test_structured_messages_rewrite_of_a_screenshot_output_keeps_the_screenshot(self): + handler = OpenAIResponsesHandler() + screenshot = {"type": "computer_screenshot", "image_url": "data:image/png;base64,QUJD"} + request = { + "model": "gpt-5.4", + "input": [ + {"role": "user", "content": "Click the button"}, + {"type": "computer_call_output", "call_id": "call_s1", "output": screenshot}, + ], + } + + result = await handler.process_input_messages(data=request, guardrail_to_apply=ToolOutputRewriteGuardrail()) + + assert result["input"][1] == {"type": "computer_call_output", "call_id": "call_s1", "output": screenshot} + assert result["input"][0] == {"role": "user", "content": "Click the button"} + @pytest.mark.asyncio async def test_skip_tool_hides_custom_tool_output_items(self): handler = OpenAIResponsesHandler()