Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Sep 25, 2025

Important

Sets self.run_inline to True in LangchainCallbackHandler.__init__() to maintain async context during tool executions.

  • Behavior:
    • Sets self.run_inline to True in LangchainCallbackHandler.__init__() to maintain async context during tool executions.

This description was created by Ellipsis 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 to True 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

  • This PR is safe to merge with minimal risk - it's a targeted fix for async context handling
  • Single line addition that aligns with LangChain's callback pattern for maintaining execution context. Builds on previous async fixes and follows the simpler solution principle from custom instructions. No breaking changes or complex logic introduced.
  • No files require special attention

Important Files Changed

File Analysis

Filename Β Β Β Β  Β  Score Β Β Β Β  Β  Overview
langfuse/langchain/CallbackHandler.py 4/5 Added run_inline=True to maintain async context in tool executions - minimal safe change

Sequence Diagram

sequenceDiagram
    participant App as Application
    participant LCH as LangchainCallbackHandler
    participant LC as LangChain Framework
    participant Tool as Tool Execution
    participant Ctx as Async Context

    App->>LCH: Initialize (run_inline=True)
    Note over LCH: self.run_inline = True<br/>maintains context behavior

    App->>LC: Execute tool chain
    LC->>LCH: on_tool_start()
    LCH->>Ctx: _attach_observation()
    Note over Ctx: Sets span in context with token

    LC->>Tool: Execute tool in async context
    Note over Tool,Ctx: run_inline=True ensures<br/>async context is maintained

    Tool-->>LC: Tool result
    LC->>LCH: on_tool_end()
    LCH->>Ctx: _detach_observation()
    Note over Ctx: Safely detaches context token<br/>even in async scenarios

    LCH-->>App: Tracing data captured
Loading

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 Agent Settings | Greptile

@hassiebp hassiebp merged commit d0fa566 into main Sep 25, 2025
11 checks passed
@hassiebp hassiebp deleted the fix-async-context-prop-langchain branch September 25, 2025 11:34
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.

1 participant