Skip to content

fix(server-utils): Instrument LangGraph stream executions - #23615

Open
psh4607 wants to merge 5 commits into
getsentry:developfrom
psh4607:fix/JS-1858/langgraph-stream-instrumentation
Open

fix(server-utils): Instrument LangGraph stream executions#23615
psh4607 wants to merge 5 commits into
getsentry:developfrom
psh4607:fix/JS-1858/langgraph-stream-instrumentation

Conversation

@psh4607

@psh4607 psh4607 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

LangGraph CompiledGraph.stream() now creates the same gen_ai.invoke_agent parent span as invoke() and keeps it active until stream consumption finishes. The shared wrapper is used by manual server-runtime instrumentation and diagnostics-channel auto-instrumentation.

Root cause

The compile and create-agent wrappers only patched invoke(). stream() returned an async iterable without an agent span, so callback-created child spans became roots. Ending a span when stream() resolves would also be too early because graph work continues while the stream is consumed.

The implementation preserves the original stream object, observes async-iterator and ReadableStream consumption paths, forwards chunks unchanged, records accumulated response attributes when enabled, and ends the span on completion, cancellation, or error. It also suppresses LangGraph's internal invoke() to stream() call per compiled graph so existing invoke instrumentation does not emit duplicate agent spans.

JS-1858

Fixes #19626

@psh4607
psh4607 force-pushed the fix/JS-1858/langgraph-stream-instrumentation branch from 35cd712 to e500300 Compare August 26, 2026 04:17
Handle ReadableStream consumption and record accumulated stream responses.
@psh4607
psh4607 force-pushed the fix/JS-1858/langgraph-stream-instrumentation branch from f632689 to bbd119d Compare August 26, 2026 04:20
@psh4607
psh4607 marked this pull request as ready for review August 26, 2026 04:27
@psh4607
psh4607 requested review from a team as code owners August 26, 2026 04:27
@psh4607
psh4607 requested review from JPeer264 and isaacs and removed request for a team August 26, 2026 04:27
Comment thread packages/server-utils/src/ai/langgraph/streaming.ts Outdated
Comment thread packages/server-utils/src/ai/langgraph/streaming.ts
Comment thread packages/server-utils/src/ai/langgraph/streaming.ts
Record source chunks and complete spans across pipeTo and pipeThrough consumption.

Co-Authored-By: OpenAI Codex <codex@openai.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 794a336. Configure here.

Comment thread packages/server-utils/src/ai/langgraph/streaming.ts
Throw for locked pipeThrough inputs and forward asynchronous pipeline failures through the returned readable.

Co-Authored-By: OpenAI Codex <codex@openai.com>
Comment thread packages/server-utils/src/ai/langgraph/streaming.ts
Preserve the native TypeError for locked pipeThrough inputs without reporting an internal stream failure or ending the active span.

Co-Authored-By: OpenAI Codex <codex@openai.com>
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.

instrumentLangGraph does not instrument stream(), only invoke()

1 participant