Repository navigation
feat(server-utils): Add onSpanStart/onSpanEnd hooks to the LangChain and LangGraph integrations - #26629
Merged
Conversation
…and LangGraph integrations Co-Authored-By: Francesco Gringl-Novy <2411343+mydea@users.noreply.github.com>
Contributor
size-limit report 📦Compared: 441ea95 (PR test merge using develop at c744751) Against: c744751 (develop) · base run #67442 ✅ Baseline matches: this diff contains only this PR's changes.
|
Contributor
Optional E2E testsSome optional checks were not successful. They do not block the merge. 6 not successful, 58 successful. See the run.
Successful checks
|
mydea
marked this pull request as ready for review
November 23, 2026 08:46
javascript-sdk-gitflow
Bot
requested review from
RulaKhaled,
andreiborza and
timfish
November 23, 2026 08:47
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 5400c06. Configure here.
51 tasks done
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.

What
langChainIntegration(withcreateLangChainCallbackHandlerandinstrumentLangChainEmbeddings) andlangGraphIntegration(withinstrumentStateGraph/instrumentCreateReactAgent) take theonSpanStart/onSpanEndhooks from #26586. Both hooks get the same call info thatrecordInputs/recordOutputsget.onSpanEndruns before every span end.Why
This is a follow-up to #26586, which added the hooks to OpenAI.
Decisions:
responseis what the end callback or call got, unchanged. For LangChain that is theLLMResultfor chat/LLM runs, the chain outputs for chain anddecideruns, the tool output for tools, and the vectors for embeddings. For LangGraph it is the final graph state: whatinvoke()resolved with, or the root run's output forstream(). That output is a single final object, the same one the SDK uses for its response attributes. It isundefinedwhen the caller stops reading early.runIdnext to the span, sohandle*End/handle*Errorpass the start hook's object to the end hook. A run that is evicted because too many runs are open gets{}.invoke_agentspan. In a graph run, LangGraph's own callback handler creates the chat spans and its tool wrapper creates the tool spans, and neither runs hooks. So those spans get no hooks from either integration, even though their recording follows the graph's decision. A LangChain handler that was already on the callbacks before the graph started still runs its own hooks.Open question: should chat/tool spans inside a graph run
langChainIntegration's hooks? Today the LangGraph integration has no access to them. Doing it means looking up the other integration's options, so I left it out.via @mydea.
--
View Session [Sentry]