Skip to content

Add LangChain instrumentation with OTel GenAI semantic convention sup…#1620

Draft
jimilp7 wants to merge 4 commits intopydantic:mainfrom
introspection-org:add-langchain-instrumentation
Draft

Add LangChain instrumentation with OTel GenAI semantic convention sup…#1620
jimilp7 wants to merge 4 commits intopydantic:mainfrom
introspection-org:add-langchain-instrumentation

Conversation

@jimilp7
Copy link
Contributor

@jimilp7 jimilp7 commented Jan 9, 2026

This PR adds a new logfire.instrument_langchain() method that provides callback-based instrumentation for LangChain, capturing the complete execution hierarchy.

Features

  • Full execution hierarchy: Chains, tools, retrievers, and LLM calls
  • Tool definitions: Captures gen_ai.tool.definitions from LLM invocations
  • OTel GenAI messages: Input/output messages in OTel format
  • System instructions: Extracted and formatted per OTel spec
  • Conversation tracking: Via thread_id -> gen_ai.conversation.id
  • SpanKind.CLIENT: LLM calls use CLIENT kind for external service calls

Usage:

import logfire
from langgraph.prebuilt import create_react_agent

logfire.configure()
logfire.instrument_langchain()

agent = create_react_agent(model, tools)
agent.invoke({"messages": [...]})

…port

## Summary

  This PR adds a new `logfire.instrument_langchain()` method that provides callback-based instrumentation for LangChain, capturing the complete execution hierarchy.

  ### Features
  - **Full execution hierarchy**: Chains, tools, retrievers, and LLM calls
  - **Tool definitions**: Captures gen_ai.tool.definitions from LLM invocations
  - **OTel GenAI messages**: Input/output messages in OTel format
  - **System instructions**: Extracted and formatted per OTel spec
  - **Conversation tracking**: Via thread_id -> gen_ai.conversation.id
  - **SpanKind.CLIENT**: LLM calls use CLIENT kind for external service calls

  ### Files Changed
  - `logfire/_internal/integrations/langchain.py` (new) - Callback handler
  - `logfire/_internal/main.py` - _span_kind support + instrument_langchain method
  - `logfire/__init__.py` - Export instrument_langchain
  - `logfire/_internal/exporters/processor_wrapper.py` - Enhanced LangSmith span transformation

  ### Design Decision
  LangChain instrumentation is self-contained with inline GenAI semantic convention constants,
  keeping it completely separate from OpenAI/Anthropic instrumentations per reviewer request.
@jimilp7 jimilp7 force-pushed the add-langchain-instrumentation branch from d6de8de to ebbba2f Compare January 9, 2026 23:41
@codecov
Copy link

codecov bot commented Jan 10, 2026

Codecov Report

❌ Patch coverage is 32.76836% with 238 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
logfire/_internal/integrations/langchain.py 25.00% 209 Missing and 1 partial ⚠️
logfire/_internal/exporters/processor_wrapper.py 49.01% 20 Missing and 6 partials ⚠️
logfire/_internal/main.py 89.47% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@jimilp7
Copy link
Contributor Author

jimilp7 commented Jan 10, 2026

@alexmojaki I'd love to get your initial thoughts on this approach for langchain, if it makes sense to you, I can proceed with creating tests. Basically, langgraph native instrumentation does not follow semconv, so we use callback-based approach to manually instrument/process into semconv.

@jimilp7
Copy link
Contributor Author

jimilp7 commented Jan 16, 2026

@alexmojaki Any updates on this?

@alexmojaki I'd love to get your initial thoughts on this approach for langchain, if it makes sense to you, I can proceed with creating tests. Basically, langgraph native instrumentation does not follow semconv, so we use callback-based approach to manually instrument/process into semconv.

@alexmojaki
Copy link
Contributor

I'm reluctant to increase the amount of stuff to maintain. Do you think it's possible to get this into the openinference package we use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants