Skip to content

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.

  • pip install "dyui[langgraph]" langchain langchain-openai
  • An API key, in a .env file (step 2).
  1. Save the code below as langgraph_agent.py in a new, empty file, or download langgraph_agent.py.

  2. Put your key in a .env file next to it: OPENAI_API_KEY=sk-.... LangChain’s model reads it; set DYUI_LANGCHAIN_MODEL to another <provider>:<model> (with that provider’s LangChain package and key) to switch.

  3. Run it:

    Terminal window
    pip install "dyui[langgraph]" langchain langchain-openai
    dyui run langgraph_agent.py
  4. Open http://localhost:8000. Ask Compare the weather in Paris and Rome.

  • 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_stream turns the run into DyUI parts: text chunks, reasoning and emitted components.
langgraph_agent.py
"""A LangGraph agent whose tool emits DyUI components while the model streams text."""
import os
from functools import lru_cache
import dyui
from dyui import ui
from dyui.integrations.langgraph import emit, stream as lg_stream
from langchain_core.tools import tool
from langgraph.checkpoint.memory import InMemorySaver
from 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"])
@tool
def 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_message
async 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 part
  • 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.

DyUI Docs assistant

Answers come from the DyUI docs. gpt-oss-120b via Groq. Press Escape or the close button to return to the page.