Implementation:Truera Trulens TruChain Wrapper
| Knowledge Sources | |
|---|---|
| Domains | Observability, LLM_Evaluation |
| Last Updated | 2026-02-14 08:00 GMT |
Overview
Concrete tool for wrapping and instrumenting LangChain Runnable applications provided by the trulens-apps-langchain package.
Description
The TruChain class is a recorder for LangChain applications. It wraps a LangChain Runnable (chain, agent, or pipeline) and automatically instruments its components for tracing and evaluation. TruChain discovers LangChain components through the Runnable's internal structure and applies OTEL instrumentation to capture spans for retrievers, LLMs, and output parsers.
TruChain inherits from the base App class and adds LangChain-specific component discovery and instrumentation logic.
Usage
Import and use TruChain when you have a LangChain application that you want to evaluate. Wrap the chain after defining feedback functions. Use the context manager pattern to record traces.
Code Reference
Source Location
- Repository: trulens
- File: src/apps/langchain/trulens/apps/langchain/tru_chain.py
- Lines: L584-885
Signature
class TruChain(core_app.App):
app: Runnable
"""The langchain app to be instrumented."""
def __init__(
self,
app: Runnable,
main_method: Optional[Callable] = None,
**kwargs: Dict[str, Any],
):
"""
Args:
app: A LangChain Runnable (chain, agent, pipeline).
main_method: Optional callable for the main entry point.
**kwargs: Additional arguments including:
app_name (str): Application name for identification.
app_version (str): Version tag.
feedbacks (List[Feedback]): Feedback functions for evaluation.
connector (DBConnector): Optional database connector.
"""
Import
from trulens.apps.langchain import TruChain
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| app | Runnable | Yes | LangChain Runnable (chain, agent, pipeline) |
| main_method | Callable | No | Optional entry point method |
| app_name | str | No | Application name (via kwargs) |
| app_version | str | No | Version tag (via kwargs) |
| feedbacks | List[Feedback] | No | Feedback functions for evaluation (via kwargs) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | TruChain | Instrumented wrapper supporting context manager recording |
Usage Examples
Basic LangChain RAG Wrapping
from trulens.apps.langchain import TruChain
from trulens.core.session import TruSession
session = TruSession()
# Assuming rag_chain is a LangChain Runnable
tru_recorder = TruChain(
rag_chain,
app_name="RAG_App",
app_version="v1",
feedbacks=[f_answer_relevance, f_context_relevance, f_groundedness],
)
# Record a trace
with tru_recorder as recording:
result = rag_chain.invoke("What is LangChain?")
# Access recording
record = recording.get()
print(record.main_output)