Add LangChain instrumentation with OTel GenAI semantic convention sup…#1620
Add LangChain instrumentation with OTel GenAI semantic convention sup…#1620jimilp7 wants to merge 4 commits intopydantic:mainfrom
Conversation
…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.
d6de8de to
ebbba2f
Compare
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
|
@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 Any updates on this?
|
|
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? |
This PR adds a new
logfire.instrument_langchain()method that provides callback-based instrumentation for LangChain, capturing the complete execution hierarchy.Features
Usage: