fix(langchain): token detach exceptions in async contexts #1317
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
Fixes token detachment exceptions in async contexts in
CallbackHandler.pyby modifying_detach_observation()and introducing_get_parent_observation()for improved observation handling._detach_observation()inCallbackHandler.pyto use_RUNTIME_CONTEXT.detach()and handle exceptions._get_parent_observation()to retrieve parent observation, used inon_chain_start(),on_tool_start(),on_retriever_start(), and__on_llm_action().self.client.start_observation()withself._get_parent_observation(parent_run_id).start_observation()inon_chain_start(),on_tool_start(),on_retriever_start(), and__on_llm_action()._RUNTIME_CONTEXTfromopentelemetry.contextandLangfusefromlangfuse._client.client.This description was created by
for 2122c91. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
Updated On: 2025-09-01 14:56:28 UTC
This PR fixes critical issues with LangChain async callback handling in the Langfuse Python SDK, specifically addressing token detach exceptions and observation nesting problems in async contexts like LangGraph.
The changes introduce two key improvements to the
CallbackHandler.pyfile:Proper Parent-Child Observation Relationships: A new
_get_parent_observation()helper method is added that correctly resolves parent observations for nested tracing scenarios. Instead of always attaching child observations to the root client, this method checks if a parent run ID exists in theself.runsdictionary and returns the appropriate parent observation. This ensures that traces maintain proper hierarchical structure rather than appearing as flat, disconnected observations.Safer Context Token Detaching: The token detaching logic is enhanced with exception handling that directly uses
_RUNTIME_CONTEXT.detach()instead of relying on OpenTelemetry's context management. This prevents exceptions that commonly occur in async scenarios where context tokens are created in one async task/thread but detached in another.These changes integrate with Langfuse's existing tracing infrastructure by maintaining compatibility with all observation types (
LangfuseAgent,LangfuseChain,LangfuseGeneration, etc.) while fixing the async context issues that were causing runtime errors. The solution preserves tracing data integrity while eliminating exceptions that were disrupting async LangChain workflows.PR Description Notes:
Confidence score: 4/5
Context used:
Rule - Open a GitHub issue or discussion first before submitting PRs to explain the rationale and necessity of the proposed changes, as required by the contributing guide. (link)