Skip to content

The Open Standard for Generative UI

4.2 KB
Raw Blame
---
title: "LangChain & LangGraph"
---

<Example
  prompt="Compare the weather in London and Sydney right now."
  image="/integrations/examples/langchain-langgraph"
  width={1600}
  height={1025}
  alt="The LangGraph example answering a request to compare the weather in London and Sydney. The agent calls get_weather twice, and OpenUI renders a weather card for each city and a tabbed two-day forecast."
  href="https://github.com/thesysdev/openui/tree/main/examples/agent-frameworks/langgraph-platform"
>

The `langgraph-platform` example runs a LangGraph agent, built with `createDeepAgent`, on a local LangGraph server. Its weather, stock-price and web-search tools return sample data. A Next.js route relays each run to the browser as AG-UI events, and OpenUI renders the agent's answer as components.

```bash title="Terminal"
cd examples/agent-frameworks/langgraph-platform
pnpm install --ignore-workspace
cp .env.example .env   # add your THESYS_API_KEY
pnpm dev               # LangGraph server on :2024, app on :3000
```

</Example>

<Steps>

<Step title="Add the stream transformer" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/langgraph-platform/src/agent/agent.ts">

Give the agent the generated OpenUI system prompt, and add `openUIStreamTransformer` from `@openuidev/langchain/transformer` to the graph's `streamTransformers`. The transformer publishes the run's messages and tool calls as AG-UI events.

```ts title="src/agent/agent.ts"
import { ChatOpenAI } from "@langchain/openai";
import { openUIStreamTransformer } from "@openuidev/langchain/transformer";
import { createDeepAgent } from "deepagents";
// Builds the OpenUI system prompt from your component library.
import { cloudInstructions } from "@/lib/cloud-prompt";
// Your LangChain tools.
import { getStockPrice, getWeather, searchWeb } from "./tools";

// OpenUI Gateway is OpenAI-compatible, so ChatOpenAI can call it.
const model = new ChatOpenAI({
  model: "google/gemini-3.6-flash-free",
  apiKey: process.env.THESYS_API_KEY,
  streaming: true,
  configuration: { baseURL: "https://api.thesys.dev/v1/embed" },
});

export const graph = createDeepAgent({
  model,
  tools: [getWeather, getStockPrice, searchWeb],
  systemPrompt: cloudInstructions(),
  // Publishes the run's message and tool events as AG-UI on "custom:openui".
  streamTransformers: [openUIStreamTransformer],
});
```

</Step>

<Step title="Expose an AG-UI route" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/langgraph-platform/src/app/api/chat/route.ts">

Return `createLangChainStreamResponse(request, options)` from a server route. It starts a run on your LangGraph server and relays the output to the browser as AG-UI server-sent events, so the API key and deployment URL stay on the server.

```ts title="src/app/api/chat/route.ts"
import { createLangChainStreamResponse } from "@openuidev/langchain";

export const runtime = "nodejs";

export async function POST(request: Request) {
  // Converts the AG-UI request to LangChain messages, runs the graph on your
  // LangGraph server, and streams the result back as AG-UI events.
  return createLangChainStreamResponse(request, {
    apiUrl: process.env.LANGGRAPH_API_URL ?? "http://localhost:2024",
    assistantId: process.env.LANGGRAPH_ASSISTANT_ID ?? "agent",
    apiKey: process.env.LANGSMITH_API_KEY,
  });
}
```

</Step>

<Step title="Connect your chat UI" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/langgraph-platform/src/app/page.tsx">

Point `fetchLLM` at the route with `agUIAdapter()`, and pass it to `AgentInterface` for a ready-made chat app or to `ChatProvider` from `@openuidev/react-headless` for your own UI. If your backend returns LangGraph's native named-event stream instead, use `langGraphAdapter()` with `langGraphMessageFormat`.

```tsx title="src/app/page.tsx"
"use client";

import { AgentInterface, agUIAdapter, fetchLLM } from "@openuidev/react-ui";
import { openuiLibrary } from "@openuidev/react-ui/genui-lib";

const llm = fetchLLM({ url: "/api/chat", streamAdapter: agUIAdapter() });

export default function Page() {
  return <AgentInterface llm={llm} componentLibrary={openuiLibrary} />;
}
```

</Step>

</Steps>