feat(langchain): add model, chain, agent, and streaming callback handlers [4/5]#4453
Open
nagkumar91 wants to merge 4 commits intoopen-telemetry:mainfrom
Open
feat(langchain): add model, chain, agent, and streaming callback handlers [4/5]#4453nagkumar91 wants to merge 4 commits intoopen-telemetry:mainfrom
nagkumar91 wants to merge 4 commits intoopen-telemetry:mainfrom
Conversation
…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>
This was referenced Apr 16, 2026
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
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:
on_chat_model_start,on_llm_start,on_llm_end,on_llm_error— enhanced with provider inference, server address extraction, model parameter captureon_chain_start,on_chain_end,on_chain_error— with operation classification (invoke_agent vs invoke_workflow), LangGraph support, ignored-run managementon_agent_action,on_agent_finish— tool call recording, finish reason captureon_llm_new_token— time-to-first-chunk and time-per-output-chunk metricson_tool_start,on_tool_end,on_tool_error— tool name, arguments, results with content redactionon_retriever_start,on_retriever_end,on_retriever_error— query and document capture__init__.py(wiring)_SpanManagerwith a tracer using schema URLV1_37_0EventEmitterwith the logger providerOpenTelemetryLangChainCallbackHandlerconftest.py(test infrastructure)Tests
test_chain_callbacks.py— chain start/end/error, operation classification, LangGraph goto handlingtest_agent_lifecycle.py— agent action/finish, tool call recordingtest_streaming_metrics.py— TTFC/TPOC histogram recording, concurrent streamingtest_llm_call.py— updated for new callback handler signaturePR Series
This is PR 4 of 5 breaking down #4389:
Depends on: #4452 (merge first)
Type of change
Checklist