Skip to content

LangGraph

Use the LangGraph adapter when a LangGraph graph or agent writes your replies: it streams the run into the chat, and emit() sends components from inside any node or tool.

Code on this page is fragments; the complete example is a whole file.

Terminal window
pip install "dyui[langgraph]" # installs langgraph>=1.0

dyui.integrations.langgraph has two functions:

  • stream(graph, input, config=None, **kwargs) runs the graph with stream_mode=["messages", "custom"] and yields what DyUI understands: AI message chunks as text (parsed for dyui fences), thinking blocks as Reasoning, and components you emit().
  • emit(item, writer=None, *, config=None), called inside any node or tool, sends a component (or a {"ui": ...} dict) straight to the chat.

A sketch of a two-node research graph (search and llm stand for your own search function and chat model):

import dyui
from dyui import ui
from dyui.integrations.langgraph import emit, stream as lg_stream
from langchain_core.messages import SystemMessage
from langchain_core.runnables import RunnableConfig
from langgraph.graph import END, START, MessagesState, StateGraph
app = dyui.App(title="Research Agent")
async def research(state: MessagesState, config: RunnableConfig):
emit(ui.Step("Searching sources", "running", id="search"), config=config)
results = await search(state["messages"][-1].content)
emit(ui.Step("Searching sources", "done", id="search"), config=config)
emit(ui.Sources(*[ui.Source(r.title, r.url) for r in results]), config=config)
return {"messages": []}
async def answer(state: MessagesState):
system = SystemMessage("Answer from the sources.\n\n" + app.catalog.prompt())
return {"messages": [await llm.ainvoke([system, *state["messages"]])]}
builder = StateGraph(MessagesState)
builder.add_node("research", research)
builder.add_node("answer", answer)
builder.add_edge(START, "research")
builder.add_edge("research", "answer")
builder.add_edge("answer", END)
graph = builder.compile()
@app.on_message
async def reply(msg: dyui.Message, chat: dyui.Chat):
async for part in lg_stream(graph, {"messages": chat.history},
config={"configurable": {"thread_id": chat.thread_id}}):
yield part

On Python 3.11+, emit(item) finds the current LangGraph run by itself. On Python 3.10, LangGraph can’t propagate its run context into nodes, so pass the run explicitly. These forms work on every version, so prefer them in code that others will run:

from langchain_core.runnables import RunnableConfig
from langchain_core.tools import tool
from langgraph.prebuilt import ToolRuntime
async def search(state, config: RunnableConfig): # a graph node
emit(ui.Table(["Title", "URL"], rows), config=config)
@tool
def forecast(city: str, runtime: ToolRuntime) -> str: # a tool
emit(ui.Chart("line", [ui.Series(city, [18, 20, 17])]), writer=runtime)
return "18–20°C"

writer= accepts a LangGraph StreamWriter, or a Runtime / ToolRuntime (its stream writer and config are used). If emit() can’t find a run, for example outside a graph or on 3.10 without config/writer, it logs a warning and drops the component rather than raising. It does the same when langgraph isn’t installed.

  • Add app.catalog.prompt() to the system prompt of the node that writes the final answer, and the model’s components render like any other.
  • chat.history is a list of {"role", "content"} dicts, which LangGraph’s MessagesState accepts directly. If your graph has a checkpointer that already remembers the thread, send only the new message: {"messages": [{"role": "user", "content": msg.text}]}.
  • An emitted {"ui": ...} dict is validated like model output, so it can’t be the developer-only html. Emit a ui.Html(...) instance instead.
  • Tool-call chunks without text are skipped. A complete AIMessage is only shown if it wasn’t already streamed.

A prebuilt ReAct agent whose tool emits a chart while the model streams its answer.

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
  1. Save it as langgraph_agent.py in a new, empty file, or download it.

  2. Put OPENAI_API_KEY=sk-... in a .env file next to it (LangChain’s OpenAI model reads it; set DYUI_LANGCHAIN_MODEL for another <provider>:<model>).

  3. Run it:

    Terminal window
    pip install "dyui[langgraph]" langchain langchain-openai
    dyui run langgraph_agent.py
  4. Open http://localhost:8000 and send a message.

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.