Repository navigation
fix(langgraph): Create invoke_agent span for stream() and astream() - #8267
Closed
sentry-junior[bot] wants to merge 1 commit into
Closed
sentry-junior[bot] wants to merge 1 commit into
sentry-junior[bot] wants to merge 1 commit into
Conversation
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>
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%). Changed files with executable lines (1)
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 -6Generated by Coverage Action |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7871
Repro: getsentry/repro#74
Reported by @4ktLuffy.
Cause and fix
LanggraphIntegrationonly patchedPregel.invokeandPregel.ainvoke. The comment said streaming runs through invoke, but LangGraph does the opposite:invoke()consumesstream(), andainvoke()consumesastream(). So a graph run withgraph.stream(...)orgraph.astream(...)got nogen_ai.invoke_agentspan.This PR also wraps
Pregel.streamandPregel.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 defaultvaluesmode. Closing a stream early ends the span without marking it as an error. AContextVarholds the graph whoseinvoke()/ainvoke()is running, and the stream wrappers skip that inner call, soinvoke()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_spancontext manager, so the two new wrappers can reuse it. The existing invoke tests cover that refactor unchanged.Validation
test_pregel_stream_and_invoke_create_one_invoke_agent_span(stream / astream / invoke / ainvoke, with and without span streaming) andtest_pregel_stream_closed_early. Without the fix, the 6 stream/astream cases fail with 0invoke_agentspans. With the fix, they pass.tests/integrations/langgraphpasses onpy3.12-langgraph-v1.2.20,py3.13-langgraph-v0.6.11, andpy3.12-langgraph-latest(156 passed).py3.14-commonwithtests/tracingpasses.ruff check,ruff format, andmypy sentry_sdkare clean.invoke(),stream(), andastream()now creates oneinvoke_agent my_agentspan.For reviewers
stream()doesn't setgen_ai.response.streaming. I kept it out of scope so the span matches whatinvoke()creates.via @dingsdax.
--
View Session [Sentry]