Skip to content

langchain: link chat models to the prompt that built their input - #939

Draft
theshubhamgoel wants to merge 3 commits into
open-telemetry:mainfrom
theshubhamgoel:otel/prompt-template-full
Draft

theshubhamgoel wants to merge 3 commits into
open-telemetry:mainfrom
theshubhamgoel:otel/prompt-template-full

Conversation

@theshubhamgoel

@theshubhamgoel theshubhamgoel commented Oct 21, 2026 •

Copy link
Copy Markdown

Description

Links each LangChain chat span to the prompt template whose PromptValue it received, so prompts formatted outside a chain, or several prompts under one parent, get gen_ai.prompt.* attributes. It also sets gen_ai.prompt.version from prompt_version or lc_hub_commit_hash metadata, takes the name from lc_hub_repo, and detects every BasePromptTemplate subclass by run_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, main gets 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 by model.invoke(messages) isn't covered: no prompt value reaches the model.
  • This adds wrappers on BaseChatModel.invoke/ainvoke/stream/astream.
  • It bundles three changes (version and Hub name, detection, linking). I'm happy to split them.

Fixes #940

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested?

  • New 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 input
  • New invocation manager tests for the prompt value registry
  • py313-test-instrumentation-genai-langchain-latest and py310-test-instrumentation-genai-langchain-oldest: all pass except the 8 Anthropic cassette tests, which also fail on main
  • precommit and typecheck

Checklist

See CONTRIBUTING.md
for the style guide, changelog guidance, and more.

  • Followed the style guidelines of this project
  • Changelog updated if the change requires an entry
  • Unit tests added
  • Documentation updated

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
@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@opentelemetry-pr-dashboard

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-10-21 07:19 UTC

Move out of draft to request review.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

This branch has not been deployed

No deployments
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.

[opentelemetry-instrumentation-genai-langchain] Link chat spans to the prompt template that built their input, and add gen_ai.prompt.version

1 participant