Fix premature end of langchain agent span #1305
Closed
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.
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