fix(langchain): do not end span on agent callbacks #1312
Merged
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.
Important
Improved handling of agent callbacks in
CallbackHandler.pyby removing exceptions for missing run IDs and preventing premature span endings.run_idinon_retriever_error,on_agent_action,on_agent_finish,on_chain_end, andon_llm_endinCallbackHandler.py.on_agent_actionandon_agent_finishto update spans without ending them.on_llm_endto handle response extraction and span updates without checking forrun_idexistence.on_agent_finishto clarify handling of run ID overlap between agent finish and chain end.This description was created by
for 136242c. 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 integration's callback handler related to agent execution flow. The changes address premature span ending and improve robustness when handling LangChain's agent callbacks.
Key Changes Made:
Removed premature span ending: The
.end()calls have been removed from bothon_agent_actionandon_agent_finishmethods. This prevents spans from being terminated too early in the agent execution lifecycle.Improved error handling: Replaced strict dictionary access (
self.runs[run_id]) with graceful lookups (self.runs.get(run_id, None)) throughout the callback handler to handle cases where run IDs might not be present.Eliminated strict validation: Removed several exception-raising checks when run IDs are not found, making the handler more resilient to unexpected callback ordering or missing runs.
Clarified execution flow: Added documentation explaining that LangChain sends the same run ID for both agent finish and chain end events, so cleanup is properly deferred to the chain end callback.
The root issue stems from LangChain's agent implementation sending duplicate run IDs across different callback events, which was causing double cleanup attempts and premature span termination. By deferring cleanup to the appropriate chain end callback and using defensive programming practices, this fix ensures proper observation lifecycle management within the Langfuse tracing system.
This change integrates with the existing callback system by maintaining the same interface while making the internal handling more robust against LangChain's complex execution patterns.
Confidence score: 4/5
CallbackHandler.pyto ensure agent callback flows work correctly in complex LangChain scenarios