Skip to content

feat(langchain): add provider inference, message formatting, and W3C propagation utilities [2/5]#4451

Open
nagkumar91 wants to merge 2 commits intoopen-telemetry:mainfrom
nagkumar91:langchain/pr2-utils-and-formatting
Open

feat(langchain): add provider inference, message formatting, and W3C propagation utilities [2/5]#4451
nagkumar91 wants to merge 2 commits intoopen-telemetry:mainfrom
nagkumar91:langchain/pr2-utils-and-formatting

Conversation

@nagkumar91
Copy link
Copy Markdown
Contributor

Description

Add utility modules for the LangChain instrumentor that handle provider inference, message serialization, and W3C trace context propagation.

New Modules

utils.py

  • Provider name inference from LangChain callback data using multiple strategies: ls_provider metadata hint, base URL patterns, class name matching, and endpoint field analysis
  • Server address/port extraction from invocation params and serialized kwargs
  • W3C trace context header extraction and propagation support (traceparent/tracestate) from metadata, inputs, and nested containers

message_formatting.py

  • Converts LangChain messages into the compact JSON format expected by GenAI semantic conventions (gen_ai.input_messages, gen_ai.output_messages, gen_ai.system_instructions, gen_ai.tool_definitions)
  • Content-redaction support: when record_content=False, text content, tool arguments, and results are replaced with "[redacted]"
  • Handles message role mapping, tool-call extraction, document formatting, and multi-part content

Tests

  • test_utils.py — provider inference for Azure, OpenAI, Bedrock, Anthropic, Google; server address/port extraction; trace header extraction
  • test_w3c_propagation.py — W3C context propagation, nested header lookup, edge cases

PR Series

This is PR 2 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 (this PR)
  3. Span manager enhancements + Event emitter
  4. Callback handler — model, chain, agent callbacks + wiring
  5. Callback handler — tool, retriever callbacks + E2E tests

Depends on: #4450 (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 2 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>
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