Principle:Truera Trulens LangChain App Wrapping
| Knowledge Sources | |
|---|---|
| Domains | Observability, LLM_Evaluation |
| Last Updated | 2026-02-14 08:00 GMT |
Overview
An instrumentation pattern that wraps a LangChain Runnable application with automatic tracing, evaluation, and recording capabilities.
Description
LangChain App Wrapping provides transparent instrumentation for LangChain applications without requiring code changes to the underlying chain. The wrapper intercepts calls to the LangChain Runnable, automatically creates OpenTelemetry spans for each component (retrievers, LLMs, output parsers), and runs configured feedback functions on the resulting traces.
This pattern solves the problem of adding observability to existing LangChain applications. Rather than manually instrumenting each component, the wrapper automatically discovers and instruments LangChain components through reflection on the Runnable's internal structure.
Key capabilities:
- Automatic component discovery and instrumentation
- Context manager-based recording for explicit trace boundaries
- Integration with TruLens feedback evaluation pipeline
- Support for both sync and async LangChain invocations
Usage
Use this principle when you have a LangChain application (chain, agent, or Runnable) that you want to evaluate with TruLens. Wrap the application after initializing a TruSession and configuring feedback functions. The wrapper should be used for LangChain-specific applications; for custom Python classes, use TruApp instead.
Theoretical Basis
LangChain App Wrapping implements the Decorator Pattern — it adds behavior (tracing and evaluation) to an existing object without modifying its interface. The wrapped application continues to function identically from the caller's perspective.
Pseudo-code Logic:
# Abstract wrapping pattern
wrapped = Wrapper(
app=langchain_chain,
feedbacks=[metric_1, metric_2],
metadata={"name": "my_app", "version": "v1"}
)
# Recording creates trace boundary
with wrapped as recording:
result = chain.invoke("user query")
# Traces + evaluations are persisted automatically
The instrumentation uses Monkey Patching to intercept method calls on LangChain components, creating OTEL spans for each invocation in the chain's execution graph.