Fix premature end of langchain agent span#1305
Conversation
Signed-off-by: qnnn <qiunan@cmbchina.com>
|
@qnnn - thanks for your contribution! Could you please open an issue with / share a minimum repro example of a chain that is broken with the current SDK and will be fixed with this PR? |
Sure! I’ve opened issue #8718 and added a minimal reproducible unit test there. Please take a look. This PR removes the span update operations in |
|
Thanks for your contribution, added in #1312 |
LangChain sends the same
run_idforagent_action,agent_finish, andchain_endduring an agent interaction.Therefore, only the
chain_endcallback is used to end the agent span to avoid it being prematurely ended.Important
Removes
on_agent_actionandon_agent_finishmethods fromCallbackHandler.pyto prevent premature span termination, relying solely onon_chain_endto end spans.on_agent_actionandon_agent_finishmethods fromCallbackHandler.py.on_chain_endnow solely ends the agent span, preventing premature span termination.AgentActionandAgentFinishfromCallbackHandler.py.This description was created by
for 2781062. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR fixes a critical issue in the LangChain callback handler where agent spans were being prematurely terminated. The root cause was that LangChain reuses the same
run_idacross multiple callback events (agent_action,agent_finish, andchain_end) during a single agent interaction, causing spans to be ended multiple times.The solution removes two callback methods (
on_agent_actionandon_agent_finish) from theCallbackHandlerclass inlangfuse/langchain/CallbackHandler.py. These methods were calling.end()on spans when they received their respective events, but since all three events share the samerun_id, this resulted in spans being closed before the agent interaction was actually complete.By eliminating these intermediate callbacks and relying solely on
on_chain_endto handle span termination, the change ensures that agent spans remain active throughout the entire agent execution lifecycle and are only closed when the agent's work is truly finished. The PR also removes the now-unused imports forAgentActionandAgentFinishfromlangchain.schema.agent.This change integrates well with the existing codebase architecture, as the callback handler already has a robust span management system through the
self.runsdictionary that tracks active spans by theirrun_id. The fix aligns with LangChain's callback model wherechain_endrepresents the definitive completion of an agent's execution cycle.Confidence score: 4/5