fix(gettingStartedDocs): Update LangGraph example to use StateGraph API#109551
fix(gettingStartedDocs): Update LangGraph example to use StateGraph API#109551priscilawebdev merged 2 commits intomasterfrom
Conversation
Replace createReactAgent with StateGraph and MessagesAnnotation in the agent monitoring getting started guide. instrumentLangGraph works by wrapping the .compile() method on a StateGraph to intercept the compilation step and inject tracing. This means it must be called before .compile() is invoked. createReactAgent calls .compile() internally and returns the already-compiled graph — there's no hook point between graph construction and compilation. By the time you get the return value, the compile step has already happened, making it too late for instrumentLangGraph to do its job. In short: createReactAgent hides the compile step, but instrumentLangGraph needs to wrap that exact step. They are fundamentally incompatible with the current SDK API. Co-Authored-By: Claude <noreply@anthropic.com>
5d952b3 to
607fa80
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
|
If I'm not reading this entirely wrong, we have an issue in the SDK. The createReactAgent pattern is widespread for agents built with LangGraph and should be a functional example. Stategraphs should be wrapped, no matter where or how they are instantiated. @RulaKhaled is this an inherent limitation or can we do something about the way this is instrumented so that the createReactAgent path is also supported? |
Agree! we should fix that. But for now, let’s have something that actually works as an example until the fix is ready |
shellmayr
left a comment
There was a problem hiding this comment.
Looks good overall - please address the agent comments regarding the end node in the graph before merging 🙏 (unless it's not an issue in practice of course)
Without an edge from the agent node to __end__, the StateGraph has no termination path, causing agent.invoke() to fail at runtime. Co-Authored-By: Claude <noreply@anthropic.com>
yes, the commit should be here in any minute . Thanks for reviewing |
|
@shellmayr We have something in the works for |
Update the LangGraph code snippet in the agent monitoring getting started guide.
instrumentLangGraphworks by wrapping the.compile()method on aStateGraphto intercept the compilation step and inject tracing. This means it must be called before.compile()is invoked.createReactAgentcalls.compile()internally and returns the already-compiled graph — there's no hook point between graph construction and compilation. By the time you get the return value, the compile step has already happened, making it too late forinstrumentLangGraphto do its job.In short:
createReactAgenthides the compile step, butinstrumentLangGraphneeds to wrap that exact step. They are fundamentally incompatible with the current SDK API.The fix replaces
createReactAgentwith the lower-levelStateGraph+MessagesAnnotationpattern, which keeps the compile step explicit and accessible.