From 0a57831828a1ce0a5608906915e854a169f97d40 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:54:15 +0000 Subject: [PATCH] fix(agent): send the json value returned by to_model_output instead of the raw tool result --- src/openrouter_agent/model_result.py | 2 + tests/unit/test_to_model_output_json.py | 59 +++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tests/unit/test_to_model_output_json.py diff --git a/src/openrouter_agent/model_result.py b/src/openrouter_agent/model_result.py index cfc6141..ddc7302 100644 --- a/src/openrouter_agent/model_result.py +++ b/src/openrouter_agent/model_result.py @@ -679,6 +679,8 @@ async def _tool_result_to_output( converted = await maybe_await(converter({"output": result.get("result"), "input": call.arguments})) if isinstance(converted, Mapping) and converted.get("type") == "content": output = converted.get("value", []) + elif isinstance(converted, Mapping) and converted.get("type") == "json": + output = json_dumps(converted.get("value")) else: output = json_dumps(result.get("result")) else: diff --git a/tests/unit/test_to_model_output_json.py b/tests/unit/test_to_model_output_json.py new file mode 100644 index 0000000..d64f180 --- /dev/null +++ b/tests/unit/test_to_model_output_json.py @@ -0,0 +1,59 @@ +from __future__ import annotations + +import json +from typing import Any, Dict + +from openrouter_agent import call_model, tool +from tests._fixtures import QueuedClient, text_response, tool_call_response + +PRIVATE = "private-value-must-not-reach-model" + + +def _lookup_tool(to_model_output: Any) -> Any: + def execute(params: Dict[str, Any], ctx: Any) -> Dict[str, Any]: + return {"public_status": "ok", "private_token": PRIVATE} + + return tool( + name="lookup", + input_schema=dict, + output_schema=dict, + execute=execute, + to_model_output=to_model_output, + ) + + +async def _followup_output(to_model_output: Any) -> Any: + client = QueuedClient([tool_call_response("r1", "lookup"), text_response("r2", "done")]) + await call_model( + client, {"model": "test-model", "input": "hi", "tools": [_lookup_tool(to_model_output)]} + ).get_text() + followup_input = client.requests[1]["input"] + return next(i for i in followup_input if i.get("type") == "function_call_output")["output"] + + +async def test_json_to_model_output_replaces_raw_tool_result() -> None: + def redact(args: Dict[str, Any]) -> Dict[str, Any]: + return {"type": "json", "value": {"public_status": args["output"]["public_status"]}} + + output = await _followup_output(redact) + + assert PRIVATE not in output + assert json.loads(output) == {"public_status": "ok"} + + +async def test_async_json_to_model_output_replaces_raw_tool_result() -> None: + async def redact(args: Dict[str, Any]) -> Dict[str, Any]: + return {"type": "json", "value": {"redacted": True}} + + output = await _followup_output(redact) + + assert PRIVATE not in output + assert json.loads(output) == {"redacted": True} + + +async def test_content_to_model_output_still_replaces_raw_tool_result() -> None: + content = [{"type": "input_text", "text": "summary only"}] + + output = await _followup_output(lambda args: {"type": "content", "value": content}) + + assert output == content