Skip to content

fix(langgraph): close an open reasoning span before the run finishes - #2891

Open
gourangasatapathyvit wants to merge 6 commits into
ag-ui-protocol:mainfrom
gourangasatapathyvit:fix/langgraph-close-open-reasoning-at-run-end
Open

gourangasatapathyvit wants to merge 6 commits into
ag-ui-protocol:mainfrom
gourangasatapathyvit:fix/langgraph-close-open-reasoning-at-run-end

Conversation

@gourangasatapathyvit

Copy link
Copy Markdown

Problem

ag_ui_langgraph closes 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 and RUN_FINISHED is 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:

RUN_STARTED, STEP_STARTED, REASONING_START, REASONING_MESSAGE_START,
REASONING_MESSAGE_CONTENT, STATE_SNAPSHOT, STEP_FINISHED, STATE_SNAPSHOT,
MESSAGES_SNAPSHOT, RUN_FINISHED          <- span never closed

After:

... REASONING_MESSAGE_CONTENT, STATE_SNAPSHOT, REASONING_MESSAGE_END,
REASONING_END, STEP_FINISHED, STATE_SNAPSHOT, MESSAGES_SNAPSHOT, RUN_FINISHED

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, before drain_subagents, so a subagent's reasoning closes before its SUBAGENT_FINISHED. Events are attributed to the owning subagent lane. The accumulated signature is emitted first, as in the normal close.
  • The in-stream close and the drain share one helper, _close_reasoning_process(); the existing close block now calls it (no behaviour change there).
  • Same pattern as the ADK integration's force_close_streaming_message and this adapter's own drain_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 through agent.run() for the dropped-stream scenario above. The end-to-end test fails without the change (no REASONING_END) and passes with it. Full integrations/langgraph/python suite: 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.

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>
@gourangasatapathyvit
gourangasatapathyvit requested a review from a team as a code owner October 1, 2026 10:44
@gourangasatapathyvit

Copy link
Copy Markdown
Author

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!

@NathanTarbert

Copy link
Copy Markdown
Contributor

Hey @gourangasatapathyvit,

Apologies for the delayed response. Nothing blocking from me on this one.

_drain_reasoning runs before the subagent drain and before RUN_FINISHED on both the success and interrupt paths. It iterates over a copy of the lanes, so it can safely run more than once. Pulling _close_reasoning_process out doesn't change the in-stream path.

One thing for the tests: most of them call _drain_reasoning() directly, so test_open_span_is_closed_before_run_finished is the only one that would catch the call being removed from the run. One more end-to-end case, maybe on the interrupt path, would pin that down. The TS LangGraph adapter has the same gap at run end, if you'd like to take that on as a follow-up.

@contextablemark, this one looks ready for a maintainer review.

@gourangasatapathyvit

gourangasatapathyvit commented Nov 13, 2026 •

Copy link
Copy Markdown
Author

One thing for the tests: most of them call _drain_reasoning() directly, so test_open_span_is_closed_before_run_finished is the only one that would catch the call being removed from the run. One more end-to-end case, maybe on the interrupt path, would pin that down.

@NathanTarbert thanks for the review. Added the interrupt-path end-to-end test (TestInterruptAfterDroppedReasoningStream in test_reasoning_drain_at_run_end.py, e92eacf): a node streams a thinking chunk, swallows the dropped stream, then calls interrupt(), and the test asserts the reasoning span closes before RUN_FINISHED(outcome=interrupt). It fails if the _drain_reasoning() call is removed from the run, so it pins that down. Full langgraph python suite passes.

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

gourangasatapathyvit commented Nov 13, 2026 •

Copy link
Copy Markdown
Author

The TS LangGraph adapter has the same gap at run end, if you'd like to take that on as a follow-up.

@NathanTarbert I took the TS LangGraph adapter gap in this same PR rather than a follow-up (c847189).

  • integrations/langgraph/typescript/src/agent.ts: the in-stream reasoning close is now a shared closeReasoningProcess() helper (no behaviour change there), and a new drainReasoning() closes any span still open at the end of the run, before the final step close and RUN_FINISHED. It covers both the normal and the interrupt finish.
  • src/reasoning-drain-at-run-end.test.ts: two end-to-end runs where the stream stops right after a thinking chunk, one finishing normally and one ending on an interrupt. Both fail if the drainReasoning() call is removed.
  • TS suite: 916 passed, tsc --noEmit clean.

@contextablemark the PR now covers both the Python and TS adapters and is ready for maintainer review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants