Skip to content

feat(langchain): add model, chain, agent, and streaming callback handlers [4/5]#4453

Open
nagkumar91 wants to merge 4 commits intoopen-telemetry:mainfrom
nagkumar91:langchain/pr4-callback-handler-core
Open

feat(langchain): add model, chain, agent, and streaming callback handlers [4/5]#4453
nagkumar91 wants to merge 4 commits intoopen-telemetry:mainfrom
nagkumar91:langchain/pr4-callback-handler-core

Conversation

@nagkumar91
Copy link
Copy Markdown
Contributor

Description

Wire all foundation modules into the callback handler and implement the core callback methods for LLM model, chain, agent, and streaming operations.

Changes

callback_handler.py (major enhancement)

All new callback implementations following GenAI semantic conventions:

  • Model callbacks: on_chat_model_start, on_llm_start, on_llm_end, on_llm_error — enhanced with provider inference, server address extraction, model parameter capture
  • Chain callbacks: on_chain_start, on_chain_end, on_chain_error — with operation classification (invoke_agent vs invoke_workflow), LangGraph support, ignored-run management
  • Agent callbacks: on_agent_action, on_agent_finish — tool call recording, finish reason capture
  • Streaming: on_llm_new_token — time-to-first-chunk and time-per-output-chunk metrics
  • Tool callbacks: on_tool_start, on_tool_end, on_tool_error — tool name, arguments, results with content redaction
  • Retriever callbacks: on_retriever_start, on_retriever_end, on_retriever_error — query and document capture
  • W3C propagation: Automatic trace context extraction from metadata/inputs

__init__.py (wiring)

  • Creates _SpanManager with a tracer using schema URL V1_37_0
  • Creates EventEmitter with the logger provider
  • Passes both to OpenTelemetryLangChainCallbackHandler

conftest.py (test infrastructure)

  • Updated fixtures for enhanced callback testing

Tests

  • test_chain_callbacks.py — chain start/end/error, operation classification, LangGraph goto handling
  • test_agent_lifecycle.py — agent action/finish, tool call recording
  • test_streaming_metrics.py — TTFC/TPOC histogram recording, concurrent streaming
  • test_llm_call.py — updated for new callback handler signature

PR Series

This is PR 4 of 5 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 — model, chain, agent, streaming (this PR)
  5. Tool, retriever callbacks + E2E tests

Depends on: #4452 (merge first)

Type of change

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

Checklist

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

nagkumar91 and others added 4 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant