@@ -32,33 +32,88 @@ def _check_langgraph_installed():
3232class LangGraphAgentAdapter (AgentAdapter ):
3333 """An AgentAdapter for LangGraph CompiledGraph agents.
3434
35- Requires langgraph to be installed.
35+ This adapter integrates LangGraph's compiled graphs with MASEval's benchmarking framework,
36+ converting LangChain/LangGraph message types to OpenAI-compatible MessageHistory format.
37+ It preserves tool calls, tool responses, multi-modal content, and supports both stateless
38+ and stateful (checkpointed) graph execution.
39+
40+ LangGraph graphs can operate in two modes:
41+
42+ - **Stateless**: Messages from invoke() result are cached in the adapter for access
43+ - **Stateful**: With checkpointer and thread_id, messages are fetched from persistent state
44+
45+ The adapter automatically handles both modes, preferring persistent state when available
46+ and falling back to cached results for stateless graphs.
47+
48+ How to use:
49+ 1. **Create a LangGraph graph** with state and nodes
50+ 2. **Compile the graph** (optionally with checkpointer for state persistence)
51+ 3. **Wrap with LangGraphAgentAdapter** to enable MASEval integration
52+ 4. **Use in benchmarks** or call directly for testing
53+ 5. **Access traces and config** for analysis and debugging
54+
55+ Example workflow:
56+ ```python
57+ from maseval.interface.agents.langgraph import LangGraphAgentAdapter
58+ from langgraph.graph import StateGraph, MessagesState
59+ from langgraph.checkpoint.memory import MemorySaver
60+
61+ # Define your graph
62+ def chatbot(state: MessagesState):
63+ # Your agent logic
64+ return {"messages": [response]}
65+
66+ # Build graph
67+ graph = StateGraph(MessagesState)
68+ graph.add_node("chatbot", chatbot)
69+ graph.set_entry_point("chatbot")
70+ graph.set_finish_point("chatbot")
71+
72+ # Compile (stateless)
73+ compiled_graph = graph.compile()
74+ agent_adapter = LangGraphAgentAdapter(compiled_graph, "agent_name")
75+
76+ # Or compile with checkpointer (stateful)
77+ memory = MemorySaver()
78+ compiled_graph = graph.compile(checkpointer=memory)
79+ config = {"configurable": {"thread_id": "session_1"}}
80+ agent_adapter = LangGraphAgentAdapter(
81+ compiled_graph,
82+ "agent_name",
83+ config=config
84+ )
3685
37- This adapter converts LangChain/LangGraph message types to MASEval's
38- OpenAI-compatible MessageHistory format. It preserves tool calls, tool
39- responses, and multi-modal content.
86+ # Run agent
87+ result = agent_adapter.run("What's the weather?")
4088
41- LangGraph graphs can be stateless or stateful (with checkpointer). This
42- adapter supports both modes:
43- - Stateless: Messages from invoke() result are cached in adapter
44- - Stateful: Messages fetched from graph state if config/thread_id provided
89+ # Access message history in OpenAI format
90+ for msg in agent_adapter.get_messages():
91+ print(f"{msg['role']}: {msg['content']}")
4592
46- Example:
47- ```python
48- from maseval.interface.agents.langgraph import LangGraphAgentAdapter
49- from langgraph.graph import StateGraph
93+ # Gather execution traces
94+ traces = agent_adapter.gather_traces()
95+ if 'total_tokens' in traces:
96+ print(f"Total tokens: {traces['total_tokens']}")
5097
51- # Create a LangGraph graph
52- graph = StateGraph(...)
53- compiled_graph = graph.compile()
98+ # Use in benchmark
99+ benchmark = MyBenchmark(agent_data={"agent": agent_adapter})
100+ results = benchmark.run(tasks)
101+ ```
54102
55- agent_adapter = LangGraphAgentAdapter(compiled_graph, "agent_name")
56- result = agent_adapter.run("What's the weather?")
103+ For stateful graphs, the adapter preserves conversation context across multiple
104+ calls using the same thread_id, enabling multi-turn interactions.
57105
58- # Access message history
59- for msg in agent_adapter.get_messages():
60- print(msg['role'], msg['content'])
61- ```
106+ Message Format:
107+ LangGraph uses LangChain message types. The adapter converts to `maseval` / OpenAI format.
108+
109+ Tool calls are preserved with metadata and converted to OpenAI's tool call format.
110+
111+ Token Usage:
112+ If LangChain messages include `usage_metadata`, the adapter automatically extracts
113+ and aggregates token counts. This is available for models that provide usage information.
114+
115+ Requires:
116+ langgraph to be installed: `pip install maseval[langgraph]`
62117 """
63118
64119 def __init__ (self , agent_instance , name : str , callbacks = None , config = None ):
0 commit comments