Skip to content

fix(langgraph): Create invoke_agent span for stream() and astream() - #8267

Closed
sentry-junior[bot] wants to merge 1 commit into
masterfrom
fix/7871-langgraph-stream-span
Closed

sentry-junior[bot] wants to merge 1 commit into
masterfrom
fix/7871-langgraph-stream-span

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #7871
Repro: getsentry/repro#74

Reported by @4ktLuffy.

Cause and fix

LanggraphIntegration only patched Pregel.invoke and Pregel.ainvoke. The comment said streaming runs through invoke, but LangGraph does the opposite: invoke() consumes stream(), and ainvoke() consumes astream(). So a graph run with graph.stream(...) or graph.astream(...) got no gen_ai.invoke_agent span.

This PR also wraps Pregel.stream and Pregel.astream. The span starts on the first iteration and ends when the stream is exhausted or closed. Response data comes from the last chunk, the final state in the default values mode. Closing a stream early ends the span without marking it as an error. A ContextVar holds the graph whose invoke()/ainvoke() is running, and the stream wrappers skip that inner call, so invoke() still creates exactly one span.

The span setup that was copied four times in invoke/ainvoke (streaming and static branches) is now one _invoke_agent_span context manager, so the two new wrappers can reuse it. The existing invoke tests cover that refactor unchanged.

Validation

  • New tests test_pregel_stream_and_invoke_create_one_invoke_agent_span (stream / astream / invoke / ainvoke, with and without span streaming) and test_pregel_stream_closed_early. Without the fix, the 6 stream/astream cases fail with 0 invoke_agent spans. With the fix, they pass.
  • tests/integrations/langgraph passes on py3.12-langgraph-v1.2.20, py3.13-langgraph-v0.6.11, and py3.12-langgraph-latest (156 passed). py3.14-common with tests/tracing passes.
  • ruff check, ruff format, and mypy sentry_sdk are clean.
  • Repro (Reproduction for sentry-python#7871 repro#74) with an editable install of this branch: exit 1 before the fix, exit 0 after, on langgraph 1.2.13 and 0.6.11. Each of invoke(), stream(), and astream() now creates one invoke_agent my_agent span.

For reviewers

  • The issue author offered to open their own PR with a fix (in the issue body, about 5 weeks ago), but no PR was opened. If you'd rather take their PR, close this one.
  • stream() doesn't set gen_ai.response.streaming. I kept it out of scope so the span matches what invoke() creates.

via @dingsdax.

--

View Session [Sentry]

Pregel.invoke and Pregel.ainvoke run through Pregel.stream and
Pregel.astream, not the other way round, so graphs run with stream() or
astream() got no invoke_agent span. Wrap the stream methods too, and skip
the inner stream call of an invoke so it still creates exactly one span.

Fixes #7871

Co-Authored-By: Johannes Daxboeck <114897+dingsdax@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 139282 passed | ⏭️ 7861 skipped | Total: 147143 | Pass Rate: 94.66% | Execution Time: 458m 56s

All tests are passing successfully.

✅ Patch coverage is 98.92% (92 of 93 changed executable lines covered; target 80%).
Project statement coverage is 90.22% (unchanged from base (26ef571) to head (71cefa2)).

Changed files with executable lines (1)
File Patch coverage Changed executable lines
sentry_sdk/integrations/langgraph.py 98.92% 92/93 covered; missed: 259; partial branches: 58, 60, 230, 243, 258
Coverage diff
@@            Coverage Diff             @@
##        master     #8267       +/-##
==========================================
  Coverage    90.22%    90.22%        —%
==========================================
  Files          203       203         —
  Tracked lines     27335     27345       +10
  Branches     10246     10232       -14
==========================================
+ Hits         24661     24671       +10
  Misses        2674      2674         —
+ Partials      1558      1552        -6

Generated by Coverage Action

@sentry-junior
sentry-junior Bot deleted the fix/7871-langgraph-stream-span branch November 13, 2026 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

junior-automation:repro PR from the Junior SDK repro flow

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LangGraph: graph.stream() and graph.astream() produce no invoke_agent span

1 participant