Constructing a LangGraphAgent
Source notebook
Repo path:
gemini/agents/genai-experience-concierge/langgraph-demo/backend/notebooks/langgraph-agent.ipynb· Open on GitHub · intermediate
Wraps a simple LangGraph StateGraph in LangGraphAgent and tests stateful/stateless streaming.
Summary
The notebook teaches how to transform a LangGraph graph into a LangGraphAgent using concierge.langgraph_server. It defines a typed state with an id, builds async SET_ID and REVERSE_ID nodes, and wires them into a StateGraph. It then demonstrates a memory-backed stateful agent with debug, values, and updates stream modes, plus a stateless agent created by disabling the checkpointer.
Key code patterns
Typed graph state
class State(TypedDict, total=False):
id: strDefines the state schema passed into StateGraph.
Command-based node routing
async def set_id(state: State):
new_id = state.get("id")
assert new_id is not None, "must set ID"
return types.Command(update=State(id=new_id), goto="REVERSE_ID")Shows a LangGraph node updating state and routing to the next node.
StateGraph construction
state_graph = graph.StateGraph(state_schema=State)
state_graph.add_node("SET_ID", set_id)
state_graph.add_node("REVERSE_ID", reverse_id)
state_graph.set_entry_point("SET_ID")Builds the graph that LangGraphAgent wraps.
Stateful LangGraphAgent
basic_agent = langgraph_agent.LangGraphAgent(
state_graph=state_graph,
checkpointer_config=schemas.MemoryBackendConfig(),
)Uses a memory checkpointer for stateful threaded requests.
Streaming with thread ID
async for chunk in basic_agent.stream(
input=State(id="hello"),
config={"configurable": {"thread_id": uuid.uuid4().hex}},
stream_mode="updates",
):
print("updates", "->", chunk)Demonstrates async streaming for a stateful agent.
Stateless LangGraphAgent
stateless_basic_agent = langgraph_agent.LangGraphAgent(
state_graph=state_graph,
checkpointer_config=None,
)Disables the checkpointer so the agent works with stateless requests.
Models & APIs used
- SDKs / libraries:
langgraph,concierge.langgraph_server
When to use this
Use this pattern to prototype a LangGraph graph as a LangGraphAgent interactively before deploying it on a server.
Gotchas & caveats
- MemoryBackendConfig requires a configurable thread_id for stateful requests.
- checkpointer_config=None means the agent only works with stateless requests.
- The node functions assert that id is present before updating or reversing it.
- concierge.langgraph_server must be importable in the notebook environment.
- The stream examples use async for, so execution must run in an async-capable context.
Best practices
- Define an explicit TypedDict state schema before constructing the StateGraph.
- Return types.Command from nodes to update state and route execution.
- Use separate stream modes to inspect debug events, full values, and node updates.
- Use unique uuid.uuid4().hex thread IDs for stateful stream calls.
- Build a separate agent with checkpointer_config=None for stateless requests.
Related
- Concepts: Agents & ADK
- Entities: LangGraph
- Area: Gemini Notebooks
- Best practices: Agents & ADK - Best Practices