Skip to content

Conversation

@hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Dec 19, 2025

Important

Fixes prompt linking in LangChain v1 by adding parent-child run ID mapping and modifying prompt search logic in CallbackHandler.py, and fixes a defaultdict syntax error.

  • Behavior:
    • Adds _child_to_parent_run_id_map in CallbackHandler.py to track parent-child run ID relationships.
    • Modifies __on_llm_action in CallbackHandler.py to walk up the parent chain for registered prompts.
    • Adds _reset() in CallbackHandler.py to clear mappings when top-level runs complete.
  • Imports:
    • Moves collections.defaultdict import to the top of CallbackHandler.py.
  • Logging:
    • Improves debug logging in CallbackHandler.py by removing UUID truncation.
  • Bug Fix:
    • Fixes defaultdict(None) syntax error in CallbackHandler.py by replacing it with lambda: None.

This description was created by Ellipsis for e61d204. You can customize this summary. It will automatically update as commits are pushed.


Disclaimer: Experimental PR review

Greptile Summary

This PR fixes prompt linking for LangChain v1's create_agent function by tracking parent-child relationships between runs and walking up the parent chain to find registered prompts.

Key Changes:

  • Added _child_to_parent_run_id_map to track parent-child run ID relationships
  • Modified __on_llm_action to walk up the parent chain when searching for registered prompts
  • Added _reset() method to clear the mapping when top-level runs complete
  • Moved collections.defaultdict import to top of module per project style
  • Improved debug logging by removing UUID truncation

Critical Issue Found:

  • defaultdict(None) on lines 136 and 1016 is incorrect syntax - None is not callable and will raise TypeError when the defaultdict tries to create default values for missing keys

Confidence Score: 2/5

  • This PR contains a critical syntax error that will cause runtime failures
  • The defaultdict(None) syntax error on lines 136 and 1016 will raise TypeError when the defaultdict attempts to create default values. This is currently masked by only using .get() which doesn't trigger the default factory, but accessing missing keys directly would fail. The logic for parent-chain traversal appears sound otherwise.
  • langfuse/langchain/CallbackHandler.py requires immediate attention to fix defaultdict initialization

Important Files Changed

Filename Overview
langfuse/langchain/CallbackHandler.py Adds parent-child run ID mapping and tree-walking logic for prompt linking; contains defaultdict(None) bug that should use lambda: None

Sequence Diagram

sequenceDiagram
    participant Agent as LangChain Agent
    participant CH as CallbackHandler
    participant Map as _child_to_parent_run_id_map
    participant PromptMap as prompt_to_parent_run_map
    
    Note over Agent,PromptMap: Agent starts with prompt
    Agent->>CH: on_chain_start(run_id=A, parent_run_id=None)
    CH->>Map: Store A -> None
    CH->>PromptMap: Register prompt for A
    
    Note over Agent,PromptMap: Agent creates intermediate chain
    Agent->>CH: on_chain_start(run_id=B, parent_run_id=A)
    CH->>Map: Store B -> A
    CH->>PromptMap: Propagate prompt from A to B
    
    Note over Agent,PromptMap: LLM invocation happens
    Agent->>CH: __on_llm_action(run_id=C, parent_run_id=B)
    CH->>Map: Store C -> B
    
    Note over CH,PromptMap: Walk up parent chain
    CH->>PromptMap: Check B for prompt
    alt Prompt not found at B
        CH->>Map: Get parent of B (returns A)
        CH->>PromptMap: Check A for prompt
        PromptMap-->>CH: Found prompt!
    end
    
    CH->>PromptMap: Deregister prompt from A
    CH->>Agent: Link generation with prompt
    
    Note over Agent,CH: Top-level run completes
    Agent->>CH: on_chain_end(run_id=A, parent_run_id=None)
    CH->>Map: _reset() - Clear all mappings
Loading

Context used:

  • Rule from dashboard - Move imports to the top of the module instead of placing them within functions or methods. (source)

@hassiebp hassiebp merged commit c700431 into main Dec 19, 2025
12 checks passed
@hassiebp hassiebp deleted the hassieb/lfe-7908-prompt-linking-not-working-with-langchain-v1 branch December 19, 2025 14:30
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.

2 participants