Skip to content

Conversation

@hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Sep 1, 2025

Important

Fixes token detachment exceptions in async contexts in CallbackHandler.py by modifying _detach_observation() and introducing _get_parent_observation() for improved observation handling.

  • Behavior:
    • Fixes token detachment exceptions in async contexts by modifying _detach_observation() in CallbackHandler.py to use _RUNTIME_CONTEXT.detach() and handle exceptions.
    • Introduces _get_parent_observation() to retrieve parent observation, used in on_chain_start(), on_tool_start(), on_retriever_start(), and __on_llm_action().
  • Functions:
    • Replaces self.client.start_observation() with self._get_parent_observation(parent_run_id).start_observation() in on_chain_start(), on_tool_start(), on_retriever_start(), and __on_llm_action().
  • Imports:
    • Adds import for _RUNTIME_CONTEXT from opentelemetry.context and Langfuse from langfuse._client.client.

This description was created by Ellipsis 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.py file:

  1. 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 the self.runs dictionary and returns the appropriate parent observation. This ensures that traces maintain proper hierarchical structure rather than appearing as flat, disconnected observations.

  2. 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:

  • The PR description is missing entirely (null), which makes it difficult to understand the context and rationale for these changes without examining the code diff directly.

Confidence score: 4/5

  • This PR addresses well-defined async context issues with targeted fixes that maintain backward compatibility
  • Score reflects solid technical approach but lacks comprehensive testing evidence and proper PR documentation
  • Pay close attention to the new parent observation resolution logic and exception handling in async contexts

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)

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, no comments

Edit Code Review Bot Settings | Greptile

@hassiebp hassiebp merged commit 29d75d5 into main Sep 1, 2025
10 checks passed
@hassiebp hassiebp deleted the fix-langgraph-async-nesting branch September 1, 2025 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: "Failed to detach context" error using async with langchain CallbackHandler

2 participants