Implementation:Truera Trulens TruApp Wrapper
| Knowledge Sources | |
|---|---|
| Domains | Observability, LLM_Evaluation |
| Last Updated | 2026-02-14 08:00 GMT |
Overview
Concrete tool for wrapping arbitrary Python classes with @instrument-decorated methods for tracing and evaluation, provided by the trulens-core library.
Description
The TruApp class is the most flexible recorder in TruLens. It wraps any Python class instance whose methods are decorated with @instrument. TruApp discovers instrumented methods, identifies the main entry point, and provides context manager recording.
TruApp automatically discovers methods decorated with the RECORD_ROOT span type to use as the main method. If no RECORD_ROOT method is found, a main method can be specified explicitly.
Usage
Import and use TruApp when your application is a custom Python class (not a LangChain chain or LangGraph graph). First decorate methods with @instrument, then wrap with TruApp.
Code Reference
Source Location
- Repository: trulens
- File: src/core/trulens/apps/app.py
- Lines: L218-499
Signature
class TruApp(core_app.App):
app: Any
def __init__(
self,
app: Any,
main_method: Optional[Callable] = None,
methods_to_instrument=None,
**kwargs: Any,
):
"""
Args:
app: Any Python class instance to instrument.
main_method: Optional entry point method. Auto-detected from
RECORD_ROOT-decorated methods if not specified.
methods_to_instrument: Optional dict of additional methods to
instrument.
**kwargs: Additional arguments including:
app_name (str): Application name.
app_version (str): Version tag.
feedbacks (List[Feedback]): Feedback functions.
connector (DBConnector): Optional database connector.
"""
Import
from trulens.apps.app import TruApp
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| app | Any | Yes | Python class instance with @instrument-decorated methods |
| main_method | Callable | No | Entry point method (auto-detected if RECORD_ROOT decorated) |
| 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 | TruApp | Instrumented wrapper supporting context manager recording |
Usage Examples
Custom RAG App
from trulens.core.otel.instrument import instrument
from trulens.otel.semconv.trace import SpanAttributes
from trulens.apps.app import TruApp
class MyRAG:
@instrument(span_type=SpanAttributes.SpanType.RETRIEVAL)
def retrieve(self, query: str) -> list:
return self.vector_store.query(query)
@instrument(span_type=SpanAttributes.SpanType.GENERATION)
def generate(self, context: str, query: str) -> str:
return self.llm.invoke(f"{context}\n{query}")
@instrument()
def respond(self, query: str) -> str:
contexts = self.retrieve(query)
return self.generate("\n".join(contexts), query)
my_app = MyRAG()
tru_recorder = TruApp(
my_app,
main_method=my_app.respond,
app_name="Custom_RAG",
app_version="v1",
feedbacks=[f_relevance, f_groundedness],
)
with tru_recorder as recording:
result = my_app.respond("What is TruLens?")