Repository navigation
langchain: link chat models to the prompt that built their input - #939
Draft
theshubhamgoel wants to merge 3 commits into
Draft
theshubhamgoel wants to merge 3 commits into
theshubhamgoel wants to merge 3 commits into
Conversation
A prompt run publishes its template to its parent run, and the next chat model under that parent takes it. A prompt formatted outside any chain has no parent, and a second prompt under the same parent is cleared by the first model call, so those chat spans carry no prompt attributes. The handler now records the prompt value each prompt run returns, and BaseChatModel invoke, ainvoke, stream and astream look up their input and key its template by the run id they choose. The run-position link stays as the fallback. Also set gen_ai.prompt.version from prompt_version or lc_hub_commit_hash metadata, take the name from lc_hub_repo, and detect prompts by run_type so every BasePromptTemplate subclass is covered. Assisted-by: Claude Opus 5.5
Pull request dashboard statusWaiting on the author · refreshed 2026-10-21 07:19 UTC Move out of draft to request review. Status above doesn't look right?
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Links each LangChain chat span to the prompt template whose
PromptValueit received, so prompts formatted outside a chain, or several prompts under one parent, getgen_ai.prompt.*attributes. It also setsgen_ai.prompt.versionfromprompt_versionorlc_hub_commit_hashmetadata, takes the name fromlc_hub_repo, and detects everyBasePromptTemplatesubclass byrun_type.The run-position link from #730 has no parent to publish to in the first case. In the second, the first model call clears the other prompt. In the 17 calls in #940,
maingets 12 right and never sets a version; this change gets all 17. The run-position link stays as the fallback.Known gaps:
prompt.format_messages(...)followed bymodel.invoke(messages)isn't covered: no prompt value reaches the model.BaseChatModel.invoke/ainvoke/stream/astream.Fixes #940
Type of change
How has this been tested?
tests/test_prompt_template.py: a prompt outside a chain (invoke, stream, ainvoke, astream), two prompts under one parent, Hub metadata, a template subclass, and plain inputpy313-test-instrumentation-genai-langchain-latestandpy310-test-instrumentation-genai-langchain-oldest: all pass except the 8 Anthropic cassette tests, which also fail onmainprecommitandtypecheckChecklist
See CONTRIBUTING.md
for the style guide, changelog guidance, and more.