LangGraph agent
You’ll build a prebuilt LangGraph ReAct agent with one tool. While the model writes its answer, the tool calls emit(ui.Chart(...)), and the chart appears in the reply as soon as the tool runs.
Prerequisites
Section titled “Prerequisites”pip install "dyui[langgraph]" langchain langchain-openai- An API key, in a
.envfile (step 2).
Save and run
Section titled “Save and run”-
Save the code below as
langgraph_agent.pyin a new, empty file, or download langgraph_agent.py. -
Put your key in a
.envfile next to it:OPENAI_API_KEY=sk-.... LangChain’s model reads it; setDYUI_LANGCHAIN_MODELto another<provider>:<model>(with that provider’s LangChain package and key) to switch. -
Run it:
Terminal window pip install "dyui[langgraph]" langchain langchain-openaidyui run langgraph_agent.py -
Open http://localhost:8000. Ask Compare the weather in Paris and Rome.
What to look at
Section titled “What to look at”emit()works from inside tools and nodes. There’s nothing to return or thread through state.- The checkpointer owns the history, so the handler sends only the new message, plus the thread id DyUI gives the conversation.
lg_streamturns the run into DyUI parts: text chunks, reasoning and emitted components.
The code
Section titled “The code”"""A LangGraph agent whose tool emits DyUI components while the model streams text."""import osfrom functools import lru_cache
import dyuifrom dyui import uifrom dyui.integrations.langgraph import emit, stream as lg_streamfrom langchain_core.tools import toolfrom langgraph.checkpoint.memory import InMemorySaverfrom langgraph.prebuilt import ToolRuntime, create_react_agent
MODEL = os.getenv("DYUI_LANGCHAIN_MODEL", "openai:gpt-5") # pip install langchain langchain-openai
app = dyui.App(title="Weather Agent", starters=["Compare the weather in Paris and Rome"])
@tooldef forecast(city: str, runtime: ToolRuntime) -> str: """Get a 3-day temperature forecast for a city.""" temps = {"paris": [18, 20, 17], "rome": [24, 26, 25]}.get(city.lower(), [20, 20, 20]) emit(ui.Chart(kind="line", title=f"{city}: next 3 days", unit="C", labels=["Mon", "Tue", "Wed"], series=[{"name": city, "data": temps}]), writer=runtime) # the injected ToolRuntime (portable to Python 3.10) return f"{city}: {temps} C"
@lru_cache(maxsize=1)def graph(): # built on first use, so the app starts without an API key return create_react_agent(MODEL, tools=[forecast], checkpointer=InMemorySaver(), prompt="You are a weather assistant. Be brief.")
@app.on_messageasync def reply(msg: dyui.Message, chat: dyui.Chat): config = {"configurable": {"thread_id": chat.thread_id}} async for part in lg_stream(graph(), {"messages": [{"role": "user", "content": msg.text}]}, config=config): yield partExtend it
Section titled “Extend it”- Add a second tool that emits a
ui.Table. - Show tool calls as live steps by emitting
ui.Step(..., "running", id=...)and then"done"with the same id.