Implementation:Truera Trulens TruGraph Wrapper
| Knowledge Sources | |
|---|---|
| Domains | Observability, Agent_Evaluation |
| Last Updated | 2026-02-14 08:00 GMT |
Overview
Concrete tool for wrapping and instrumenting compiled LangGraph agents for agent-specific tracing and evaluation, provided by the trulens-apps-langgraph package.
Description
The TruGraph class is a specialized recorder for LangGraph applications. It inherits from TruChain and extends it with LangGraph-specific instrumentation for graph nodes, task functions, and Pregel execution methods. The wrapper automatically discovers and instruments LangGraph internal components.
TruGraph combines both LangChain and LangGraph instrumentation via a CombinedInstrument that merges module, class, and method discovery from both frameworks.
Usage
Import and use TruGraph when evaluating compiled LangGraph agents. Wrap the compiled graph (the result of graph.compile()), not the StateGraph builder. Configure with agent evaluation metrics (tool selection, relevance, groundedness).
Code Reference
Source Location
- Repository: trulens
- File: src/apps/langgraph/trulens/apps/langgraph/tru_graph.py
- Lines: L210-1814 (class L210, __init__ L1586-1658)
Signature
class TruGraph(TruChain):
def __init__(
self,
app: Any,
main_method: Optional[Callable] = None,
**kwargs: Any,
):
"""
Args:
app: A LangGraph application (Pregel, StateGraph, or custom class).
main_method: Optional entry point callable.
**kwargs: Additional arguments including:
app_name (str): Application name.
app_version (str): Version tag.
feedbacks (List[Feedback]): Agent evaluation metrics.
connector (DBConnector): Optional database connector.
"""
Import
from trulens.apps.langgraph import TruGraph
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| app | Any | Yes | Compiled LangGraph (Pregel/CompiledStateGraph) |
| main_method | Callable | No | Optional entry point |
| app_name | str | No | Application name (via kwargs) |
| app_version | str | No | Version tag (via kwargs) |
| feedbacks | List[Feedback] | No | Agent evaluation metrics (via kwargs) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | TruGraph | Instrumented wrapper with graph-aware tracing |
Usage Examples
Wrap Compiled LangGraph Agent
from trulens.apps.langgraph import TruGraph
from trulens.core.session import TruSession
from langgraph.graph import StateGraph
session = TruSession()
# Build and compile agent graph
graph = StateGraph(AgentState)
graph.add_node("agent", agent_node)
graph.add_node("tools", tool_node)
graph.add_conditional_edges("agent", should_continue, {"continue": "tools", "end": "__end__"})
graph.add_edge("tools", "agent")
graph.set_entry_point("agent")
compiled_graph = graph.compile()
# Wrap compiled graph
tru_agent = TruGraph(
compiled_graph,
app_name="ReAct_Agent",
app_version="v1",
feedbacks=[f_tool_selection, f_relevance, f_groundedness],
)
# Record agent execution
with tru_agent as recording:
result = compiled_graph.invoke({"messages": [("user", "What is TruLens?")]})
Related Pages
Implements Principle
Requires Environment
- Environment:Truera_Trulens_Python_Core_Environment
- Environment:Truera_Trulens_LangChain_LangGraph_Environment