Repository navigation
fix(langgraph): close an open reasoning span before the run finishes - #2891
gourangasatapathyvit wants to merge 6 commits into
Conversation
A reasoning span was closed only when the next model chunk carried no
reasoning. If a model stream stopped right after a reasoning chunk (for
example a dropped connection that the node tolerates, so the run still
completes), the span stayed open and RUN_FINISHED went out with it. AG-UI
1.0 clients fail the run on that ("reasoning spans are still active").
Drain every lane's open reasoning span (signature, REASONING_MESSAGE_END,
REASONING_END) before the subagent drain at the end of the run, so a
subagent's reasoning closes before its SUBAGENT_FINISHED. The in-stream
close now shares the same helper.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
Hi @ranst91 @contextablemark — could one of you take a look when you have a moment? This closes a reasoning span that is still open when the run finishes (agent.py, with a new test test_reasoning_drain_at_run_end.py). I'm happy to adjust anything. Thanks! |
|
Apologies for the delayed response. Nothing blocking from me on this one.
One thing for the tests: most of them call @contextablemark, this one looks ready for a maintainer review. |
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@NathanTarbert thanks for the review. Added the interrupt-path end-to-end test ( The TS adapter part is covered in the next comment. |
Mirror of the Python adapter fix: a stream that stops right after a reasoning chunk left the span open at RUN_FINISHED. Drain it at run end (success and interrupt paths), sharing one close helper with the in-stream close. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@NathanTarbert I took the TS LangGraph adapter gap in this same PR rather than a follow-up (c847189).
@contextablemark the PR now covers both the Python and TS adapters and is ready for maintainer review. |
Problem
ag_ui_langgraphcloses a reasoning span only when the next model chunk carries no reasoning (REASONING_MESSAGE_END+REASONING_END). If a model stream stops right after a reasoning chunk and the run still completes, nothing closes the span andRUN_FINISHEDis emitted with it open.One way to get there: a node or middleware that tolerates a failed model call (it catches a dropped stream and returns). A reasoning chunk was already streamed, no later chunk or model-end arrives, and the graph finishes normally.
The 1.0 spec requires everything the run opened to be closed before
RUN_FINISHED(docs/spec/1.0/events/lifecycle.mdx,events/reasoning.mdx), and the 1.0 TypeScript client enforces it (verify.ts: "Cannot send 'RUN_FINISHED' while reasoning spans are still active"). So the run fails on the client even though the agent itself finished.Reproduction
A graph node whose model streams one thinking chunk and then raises, with the node swallowing the error. Before this change the event stream was:
After:
Note: a model stream that simply ends after reasoning is already closed by the model-end event, so the ordinary reasoning-only case is not affected. This is only the abnormal-stream case.
Change
_drain_reasoning()closes every lane's open reasoning process at the end of the run, beforedrain_subagents, so a subagent's reasoning closes before itsSUBAGENT_FINISHED. Events are attributed to the owning subagent lane. The accumulated signature is emitted first, as in the normal close._close_reasoning_process(); the existing close block now calls it (no behaviour change there).force_close_streaming_messageand this adapter's owndrain_subagents.Tests
tests/test_reasoning_drain_at_run_end.py(10 tests): per-lane unit tests (message-then-span order, signature first, idempotent, nothing open, root + subagent lanes attributed to their own subagent) and an end-to-end run throughagent.run()for the dropped-stream scenario above. The end-to-end test fails without the change (noREASONING_END) and passes with it. Fullintegrations/langgraph/pythonsuite: 812 passed.I found this while upgrading an app to AG-UI 1.0 and checking which of our streams could trip the stricter client. Happy to open an issue first if you'd prefer that for a change this small.