fix(langchain): do not end span on agent callbacks#1312
Merged
Conversation
qnnn
reviewed
Aug 27, 2025
| output=action, | ||
| input=kwargs.get("inputs"), | ||
| ).end() | ||
| agent_run.update( |
Contributor
There was a problem hiding this comment.
Hi @hassiebp , I have a question. Should we update the input and output during the agent action or finish process? It’s an intermediate step and may not accurately represent the true input and output of the span.
Contributor
Author
There was a problem hiding this comment.
Could you please try with the latest SDK version and report whether you see unexpected results?
Contributor
There was a problem hiding this comment.
Could you please try with the latest SDK version and report whether you see unexpected results?
This appears to be a different issue that I didn't explain clearly before (or it might not be an issue at all).
- In the agent action callback when setting output, if the tool execution fails, the current output is the action's output, but I expect the output to be empty in such cases.
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.
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