Skip to content

fix(langchain): trace all model callbacks - #1024

Open
amarrtech wants to merge 2 commits into
open-telemetry:mainfrom
amarrtech:fix-langchain-missing-inference-spans
Open

amarrtech wants to merge 2 commits into
open-telemetry:mainfrom
amarrtech:fix-langchain-missing-inference-spans

Conversation

@amarrtech

Copy link
Copy Markdown

Fixes #1023. Adds text_completion inference spans for on_llm_start callbacks and preserves chat inference spans when the request model name is unavailable. Text-completion inputs and outputs are captured through the shared GenAI content models, and existing agent-span expectations now include the previously missing child inference spans.

Tests:

  • uv run tox -e py311-test-instrumentation-genai-langchain-latest
  • uv run tox -e py311-test-instrumentation-genai-langchain-oldest
  • uv run tox -e typecheck
  • uv run tox -e precommit
  • uv run tox -e changelog-preview

Implement text-completion inference spans and keep chat spans when model names are unavailable.

Assisted-by: OpenAI Codex
Signed-off-by: Amrinder Randhawa <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 21:32
@amarrtech
amarrtech requested a review from a team as a code owner October 31, 2026 21:32
@linux-foundation-easycla

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

Copy link
Copy Markdown

CLA Not Signed

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on the author · refreshed 2026-11-03 19:18 UTC

Two things need attention:

  • Required checks are failing — investigate the failures.
  • 1 review item — respond to each (e.g. link a commit, explain why not, ask a follow-up):
    • Inline threads: 1
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.
  • Should this be with reviewers? Comment /dashboard route:reviewers to route it to them.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

BaseLLM completion spans are not propagated as the current execution context, causing incorrect provider-span parenting, and available text-completion token counts are omitted from usage attributes and metrics.

2 open issues
What changed in this PR

This PR addresses #1023 by extending LangChain callback instrumentation to trace text-completion calls and chat calls without a detectable model name.

Changes:

  • Adds text_completion spans and captures completion content through shared GenAI models.
  • Preserves chat spans when the request model name is unavailable.
  • Adds callback tests and updates agent-span expectations to include newly traced child inference spans.
File Description
instrumentation/​opentelemetry-instrumentation-genai-langchain/​tests/​test_operation_mapping.py Updates expected inference spans and parent relationships.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​tests/​test_callback_handler.py Tests sync/async completions and nameless chat callbacks.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​tests/​test_agent_classification_corpus.py Includes child chat spans in agent expectations.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​tests/​conftest.py Maps completion operations to duration metrics.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​src/​opentelemetry/​instrumentation/​genai/​langchain/​operation_mapping.py Adds the semantic-convention completion operation constant.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​src/​opentelemetry/​instrumentation/​genai/​langchain/​callback_handler.py Shares model-start handling and captures text-generation results.
instrumentation/​opentelemetry-instrumentation-genai-langchain/​.changelog/​1024.fixed Records the expanded inference-span coverage.

Review effort: Balanced

Comment on lines +517 to +520
self._on_model_start(
serialized,
input_messages=input_messages,
operation_name=OperationName.TEXT_COMPLETION,
Comment on lines +828 to +829
if finish_reason:
finish_reasons.append(finish_reason)
Propagate text-completion invocation context through sync, async, and streaming BaseLLM boundaries. Record token usage reported in LLMResult output without overriding message-level usage.

Assisted-by: OpenAI Codex
Signed-off-by: Amrinder Randhawa <[email protected]>

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

Development

Successfully merging this pull request may close these issues.

[opentelemetry-instrumentation-genai-langchain] Some model calls produce no inference span

2 participants