Skip to content

feat(langchain): add tool, retriever E2E tests and changelog [5/5]#4454

Open
nagkumar91 wants to merge 5 commits intoopen-telemetry:mainfrom
nagkumar91:langchain/pr5-tool-retriever-e2e
Open

feat(langchain): add tool, retriever E2E tests and changelog [5/5]#4454
nagkumar91 wants to merge 5 commits intoopen-telemetry:mainfrom
nagkumar91:langchain/pr5-tool-retriever-e2e

Conversation

@nagkumar91
Copy link
Copy Markdown
Contributor

Description

Add remaining test coverage for the enhanced LangChain tracing and update the changelog.

Tests

test_tool_callbacks.py

  • Tool start/end/error callback handling
  • Tool name, arguments, and result capture with content redaction
  • Tool definitions formatting
  • Event emission for tool calls and results

test_retriever_callbacks.py

  • Retriever start/end/error callback handling
  • Query capture and document serialization
  • Content redaction behavior
  • Retriever event emission

test_e2e_scenarios.py

  • Multi-step agent workflows with tool use
  • Nested chain execution with correct span hierarchy
  • Concurrent agent execution and thread safety
  • Error propagation across callback types
  • LangGraph-style workflows with goto transitions
  • W3C trace context propagation through full pipelines

Changelog

Updated CHANGELOG.md with the enhancement entry for the full series.

PR Series

This is PR 5 of 5 (final) breaking down #4389:

  1. ✅ Foundation modules (feat(langchain): add semconv attributes, operation mapping, and content recording modules [1/5] #4450)
  2. ✅ Provider inference, message formatting, W3C propagation (feat(langchain): add provider inference, message formatting, and W3C propagation utilities [2/5] #4451)
  3. ✅ Span manager enhancements + Event emitter (feat(langchain): enhance span manager and add event emitter [3/5] #4452)
  4. ✅ Callback handler — all callbacks + wiring (feat(langchain): add model, chain, agent, and streaming callback handlers [4/5] #4453)
  5. Tool, retriever, E2E tests + changelog (this PR)

Depends on: #4453 (merge first)

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added

nagkumar91 and others added 5 commits April 16, 2026 14:59
…nt recording modules

Add foundation modules for enhanced LangChain GenAI semantic convention tracing:

- semconv_attributes.py: Per-operation attribute matrix based on OTel GenAI
  semantic conventions. Single source of truth for which attributes apply to
  which operations (chat, text_completion, invoke_agent, execute_tool,
  invoke_workflow, retrieval).

- operation_mapping.py: Callback-to-semconv operation mapping. Maps each
  LangChain callback to the correct GenAI semantic convention operation name.
  Includes heuristic classification for on_chain_start callbacks (agents vs
  workflows vs internal plumbing) and LangGraph marker recognition.

- content_recording.py: Thin integration layer over the shared genai content
  capture utilities. Provides clear APIs for the callback handler to decide
  what content should be recorded on spans and events.

Part 1 of a series breaking down open-telemetry#4389 into smaller reviewable PRs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…propagation utilities

Add utility modules for the LangChain instrumentor:

- utils.py: Provider name inference from LangChain callback data (ls_provider,
  base_url, class name, endpoint fields), server address/port extraction, and
  W3C trace context header extraction and propagation support.

- message_formatting.py: Message, tool, and document serialization with
  content-redaction support. Converts LangChain message objects into the
  compact JSON format expected by OpenTelemetry GenAI semantic conventions.

Part 2 of a series breaking down open-telemetry#4389 into smaller reviewable PRs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Enhance the span lifecycle manager and add event emission for non-LLM
GenAI operations:

- span_manager.py: Enhanced with ignored-run walkthrough (re-parenting
  children of skipped internal runs), per-thread agent stacks for hierarchy
  tracking, token usage accumulation and propagation to parent agent spans,
  and LangGraph Command(goto=...) transition support.

- event_emitter.py: Emits semantic-convention-aligned log-record events for
  tool, agent, and retriever spans. All event emission is gated behind the
  content policy. Uses LogRecord instances linked to the active span context.

Part 3 of a series breaking down open-telemetry#4389 into smaller reviewable PRs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…h wiring

Wire all foundation modules into the callback handler and add comprehensive
callback implementations:

- callback_handler.py: Enhanced with model start/end/error (improved provider
  and server inference), chain start/end/error (with operation classification),
  agent action/finish, LLM new token (streaming chunk timing metrics),
  tool start/end/error, and retriever start/end/error callbacks.

- __init__.py: Wire SpanManager, EventEmitter, and tracer into the
  instrumentor's _instrument method.

- conftest.py: Updated test infrastructure for enhanced callback testing.

Part 4 of a series breaking down open-telemetry#4389 into smaller reviewable PRs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add remaining test coverage for the LangChain enhanced tracing:

- test_tool_callbacks.py: Tool start/end/error callbacks, content redaction,
  tool definitions formatting, event emission
- test_retriever_callbacks.py: Retriever start/end/error callbacks, document
  serialization, query capture, content redaction
- test_e2e_scenarios.py: End-to-end scenarios covering multi-step agent
  workflows, nested chains, concurrent execution, error propagation

Update CHANGELOG.md with the enhancement entry.

Part 5 (final) of a series breaking down open-telemetry#4389 into smaller reviewable PRs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
agent_name: str,
output_messages: Optional[str] = None,
) -> None:
"""Emit a ``gen_ai.agent.end`` event when an agent completes."""
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

where do these conventions come from?

)

with self._lock:
self._spans[rid] = record
Copy link
Copy Markdown
Member

@lmolkova lmolkova Apr 17, 2026

Choose a reason for hiding this comment

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

is there a way to write this instrumentation without accumulating state? can it be monkey-patching as usual that relies on genai-utils instead of relying on callback mechanism from langchain ? callback mechanism can never work properly with distributed tracing (context management, accumulating state, not getting proper signals when things fail or get cancelled). It's unrealistic to build anything long-term based on it and we'll need to rewrite it from scratch at some point.

I'd suggest this is the time to rewrite it to use monkey-patching and abandon any prior art langsmith put into it.

Copy link
Copy Markdown
Member

@lmolkova lmolkova left a comment

Choose a reason for hiding this comment

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

it seems this PR uses some unknown conventions and should be converted to draft for the time being. We also need to discuss if state accumulation and callback is the right path going forward and if we can do something better.

@github-project-automation github-project-automation Bot moved this to Reviewed PRs that need fixes in Python PR digest Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

2 participants