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>
This commit is contained in:
yucheng 2026-09-18 23:33:42 +00:00
parent 8c1be20f9c
commit 3e7c35fc75
2 changed files with 21 additions and 1 deletions

View file

@ -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(

View file

@ -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()