fix(langchain): maintain async context in tool executions #1380
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
Sets
self.run_inline
toTrue
inLangchainCallbackHandler.__init__()
to maintain async context during tool executions.self.run_inline
toTrue
inLangchainCallbackHandler.__init__()
to maintain async context during tool executions.This description was created by
for c32d3dc. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Overview
Updated On: 2025-09-25 11:30:00 UTC
Summary
This PR adds a single line
self.run_inline = True
to the LangchainCallbackHandler constructor. This change maintains proper async context during tool executions in LangChain integrations.The
run_inline
attribute is a LangChain BaseCallbackHandler property that controls whether callbacks execute inline with the main execution flow rather than being deferred. Setting it toTrue
ensures that the OpenTelemetry context tokens created in_attach_observation()
remain valid when tool callbacks are invoked, preventing context detach errors in async scenarios.This builds on previous async context fixes (commit 29d75d5) that improved error handling during context detachment. The change is minimal, safe, and directly addresses async context management issues without affecting synchronous execution paths.
Confidence Score: 4/5
Important Files Changed
File Analysis
Sequence Diagram