From a1b5e36b2b3f9d3a7354d60ffc060b9d1c9256ea 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:53:53 +0000 Subject: [PATCH] fix(agent): keep only the last generator yield as output when event_schema is dict With event_schema=dict and a narrower output_schema, the first yield that validated against output_schema was used as the tool result, so an earlier progress event could be sent to the model while the real final yield was emitted only as a preliminary event. Hold an output-shaped yield as a candidate and emit it as preliminary once another yield arrives. --- src/openrouter_agent/tool_executor.py | 41 ++++++++++++-------- tests/unit/test_tool_factory_and_executor.py | 24 ++++++++++++ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/openrouter_agent/tool_executor.py b/src/openrouter_agent/tool_executor.py index a768529..93617a9 100644 --- a/src/openrouter_agent/tool_executor.py +++ b/src/openrouter_agent/tool_executor.py @@ -94,33 +94,40 @@ async def execute_generator_tool( has_final = False last_value = None has_value = False - broad_overlapping_schemas = fn.get("event_schema") is dict and fn.get("output_schema") is dict - pending_broad_event = None + broad_event_schema = fn.get("event_schema") is dict + pending_output = None + has_pending_output = False + + async def emit_preliminary(event: Any) -> None: + preliminary.append(event) + if on_preliminary_result is not None: + await maybe_await(on_preliminary_result(tool_call.id, event)) + async for value in produced: - if broad_overlapping_schemas: - if pending_broad_event is not None: - preliminary.append(pending_broad_event) - if on_preliminary_result is not None: - await maybe_await(on_preliminary_result(tool_call.id, pending_broad_event)) - pending_broad_event = value - has_value = True - last_value = value - continue has_value = True last_value = value + if broad_event_schema: + # A dict event schema accepts every yield, so only the last yield can be the output. + if has_pending_output: + await emit_preliminary(pending_output) + has_pending_output = False + if _try_validate(fn.get("output_schema"), value): + pending_output = value + has_pending_output = True + else: + await emit_preliminary(value) + continue matches_output = _try_validate(fn.get("output_schema"), value) matches_event = _try_validate(fn.get("event_schema"), value) - if matches_output and (not matches_event or fn.get("event_schema") is dict) and not has_final: + if matches_output and not matches_event and not has_final: final = validate_schema(fn.get("output_schema"), value) has_final = True continue if fn.get("event_schema") is not None: value = validate_schema(fn.get("event_schema"), value) - preliminary.append(value) - if on_preliminary_result is not None: - await maybe_await(on_preliminary_result(tool_call.id, value)) - if broad_overlapping_schemas and pending_broad_event is not None: - final = validate_schema(fn.get("output_schema"), pending_broad_event) + await emit_preliminary(value) + if has_pending_output: + final = validate_schema(fn.get("output_schema"), pending_output) has_final = True if not has_final: if not has_value: diff --git a/tests/unit/test_tool_factory_and_executor.py b/tests/unit/test_tool_factory_and_executor.py index 2d53b96..0903f61 100644 --- a/tests/unit/test_tool_factory_and_executor.py +++ b/tests/unit/test_tool_factory_and_executor.py @@ -80,6 +80,30 @@ async def execute(params: Input, ctx): assert result["preliminary_results"] == [{"progress": 0.5}] +async def test_generator_tool_with_dict_event_schema_keeps_earlier_output_shaped_yields_preliminary() -> None: + class Out(BaseModel): + data: str + + async def execute(params, ctx): + yield {"phase": "progress", "data": "progress-only"} + yield {"data": "final"} + + generated = tool(name="gen_overlap", input_schema=dict, event_schema=dict, output_schema=Out, execute=execute) + prelim = [] + + result = await execute_tool( + generated, + ParsedToolCall(id="call_4", name="gen_overlap", arguments={}), + on_preliminary_result=lambda call_id, value: prelim.append((call_id, value)), + ) + + assert result is not None + assert "error" not in result + assert result["result"].data == "final" + assert prelim == [("call_4", {"phase": "progress", "data": "progress-only"})] + assert result["preliminary_results"] == [{"phase": "progress", "data": "progress-only"}] + + async def test_generator_tool_with_overlapping_dict_schemas_keeps_last_yield_as_output() -> None: async def execute(params, ctx): yield {"progress": 0.5}