fix(langchain): allow prompt linking with langchain v1 create_agent #1481
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 prompt linking in LangChain v1 by adding parent-child run ID mapping and modifying prompt search logic in
CallbackHandler.py, and fixes adefaultdictsyntax error._child_to_parent_run_id_mapinCallbackHandler.pyto track parent-child run ID relationships.__on_llm_actioninCallbackHandler.pyto walk up the parent chain for registered prompts._reset()inCallbackHandler.pyto clear mappings when top-level runs complete.collections.defaultdictimport to the top ofCallbackHandler.py.CallbackHandler.pyby removing UUID truncation.defaultdict(None)syntax error inCallbackHandler.pyby replacing it withlambda: None.This description was created by
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_agentfunction by tracking parent-child relationships between runs and walking up the parent chain to find registered prompts.Key Changes:
_child_to_parent_run_id_mapto track parent-child run ID relationships__on_llm_actionto walk up the parent chain when searching for registered prompts_reset()method to clear the mapping when top-level runs completecollections.defaultdictimport to top of module per project styleCritical Issue Found:
defaultdict(None)on lines 136 and 1016 is incorrect syntax -Noneis not callable and will raiseTypeErrorwhen the defaultdict tries to create default values for missing keysConfidence Score: 2/5
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.Important Files Changed
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 mappingsContext used:
dashboard- Move imports to the top of the module instead of placing them within functions or methods. (source)