Implementation:Truera Trulens StateGraph Compile
| Knowledge Sources | |
|---|---|
| Domains | Agent_Architecture, LLM_Applications |
| Last Updated | 2026-02-14 08:00 GMT |
Overview
External tool for constructing and compiling LangGraph stateful agent workflows, provided by the langgraph library. This is a Wrapper Doc — TruLens instruments but does not define this API.
Description
StateGraph is the primary builder class in LangGraph for defining stateful agent workflows. Users add nodes (processing functions), edges (execution order), and conditional routing logic, then call .compile() to produce a CompiledStateGraph (Pregel) object that can be invoked.
TruLens wraps the compiled result with TruGraph for tracing and evaluation. The StateGraph API itself is external to TruLens.
Usage
Use StateGraph when building multi-step agentic workflows with LangGraph. Define your graph, compile it, then wrap with TruGraph for evaluation.
Code Reference
Source Location
- Repository: langgraph (external)
- File: langgraph/graph/state.py (external)
Signature
class StateGraph:
def __init__(self, state_schema: Type):
"""
Args:
state_schema: Pydantic model or TypedDict defining the graph state.
"""
def add_node(self, name: str, function: Callable) -> StateGraph: ...
def add_edge(self, start: str, end: str) -> StateGraph: ...
def add_conditional_edges(self, source: str, condition: Callable, mapping: Dict) -> StateGraph: ...
def set_entry_point(self, name: str) -> StateGraph: ...
def compile(self) -> CompiledStateGraph: ...
Import
from langgraph.graph import StateGraph
External Reference
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| state_schema | Type | Yes | Pydantic model or TypedDict defining the graph state |
| nodes | Dict[str, Callable] | Yes | Named processing functions added via add_node |
| edges | List[Tuple] | Yes | Connections between nodes via add_edge/add_conditional_edges |
Outputs
| Name | Type | Description |
|---|---|---|
| compile() returns | CompiledStateGraph | Compiled graph (Pregel) ready for invocation and TruGraph wrapping |
Usage Examples
Build and Compile Agent Graph
from langgraph.graph import StateGraph
from typing import TypedDict, List
class AgentState(TypedDict):
messages: List[dict]
def agent_node(state: AgentState) -> dict:
return {"messages": state["messages"] + [{"role": "assistant", "content": "..."}]}
def tool_node(state: AgentState) -> dict:
return {"messages": state["messages"] + [{"role": "tool", "content": "..."}]}
def should_continue(state: AgentState) -> str:
last = state["messages"][-1]
if "tool_call" in last.get("content", ""):
return "continue"
return "end"
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.compile()
# Now wrap with TruGraph for evaluation
from trulens.apps.langgraph import TruGraph
tru_agent = TruGraph(compiled, app_name="Agent_v1", feedbacks=[...])