$npx -y skills add OthmanAdi/openui-forge --skill openui-forge-langchainOpenUI generative UI with LangChain/LangGraph backend. Supports ChatOpenAI and ChatAnthropic.
| 1 | # OpenUI Forge — LangChain |
| 2 | |
| 3 | Build generative UI apps with OpenUI + LangChain. Stream from ChatOpenAI or ChatAnthropic, convert to OpenAI NDJSON. |
| 4 | |
| 5 | ## Activation Triggers |
| 6 | |
| 7 | - "openui langchain", "openui langgraph", "openui langsmith" |
| 8 | - "generative ui langchain", "langchain streaming ui" |
| 9 | |
| 10 | ## Prerequisites |
| 11 | |
| 12 | - Node.js >= 22 (24 LTS recommended), React >= 18.3.1 (19+ recommended) |
| 13 | - `OPENAI_API_KEY` or `ANTHROPIC_API_KEY` set |
| 14 | - Next.js project (App Router recommended) |
| 15 | |
| 16 | ## Quick Start |
| 17 | |
| 18 | 1. Install dependencies (pick one or both LLM providers): |
| 19 | ```bash |
| 20 | npm install @openuidev/react-ui @openuidev/react-headless @openuidev/react-lang lucide-react zod @langchain/openai @langchain/core |
| 21 | # For Anthropic: npm install @langchain/anthropic |
| 22 | ``` |
| 23 | 2. Add the CSS import to `app/layout.tsx`: |
| 24 | ```tsx |
| 25 | import "@openuidev/react-ui/components.css"; |
| 26 | ``` |
| 27 | 3. Create the API route and frontend page below |
| 28 | 4. Run `npm run dev` and test |
| 29 | |
| 30 | ## Full Code |
| 31 | |
| 32 | ### Backend (OpenAI): `app/api/chat/route.ts` |
| 33 | |
| 34 | ```typescript |
| 35 | import { openuiChatLibrary } from "@openuidev/react-ui/genui-lib"; |
| 36 | import { ChatOpenAI } from "@langchain/openai"; |
| 37 | import { HumanMessage, SystemMessage, AIMessage } from "@langchain/core/messages"; |
| 38 | |
| 39 | const model = new ChatOpenAI({ model: process.env.OPENAI_MODEL ?? "gpt-5.5", streaming: true }); |
| 40 | |
| 41 | export async function POST(req: Request) { |
| 42 | const { messages } = await req.json(); |
| 43 | |
| 44 | const systemPrompt = openuiChatLibrary.prompt({ |
| 45 | preamble: "You are a helpful assistant that generates interactive UIs.", |
| 46 | }); |
| 47 | |
| 48 | const lcMessages = [ |
| 49 | new SystemMessage(systemPrompt), |
| 50 | ...messages.map((m: { role: string; content: string }) => |
| 51 | m.role === "user" ? new HumanMessage(m.content) : new AIMessage(m.content) |
| 52 | ), |
| 53 | ]; |
| 54 | |
| 55 | const stream = await model.stream(lcMessages); |
| 56 | const encoder = new TextEncoder(); |
| 57 | const id = `chatcmpl-${Date.now()}`; |
| 58 | |
| 59 | const readableStream = new ReadableStream({ |
| 60 | async start(controller) { |
| 61 | for await (const chunk of stream) { |
| 62 | const text = typeof chunk.content === "string" ? chunk.content : ""; |
| 63 | if (!text) continue; |
| 64 | const payload = { |
| 65 | id, |
| 66 | object: "chat.completion.chunk", |
| 67 | choices: [{ index: 0, delta: { content: text }, finish_reason: null }], |
| 68 | }; |
| 69 | controller.enqueue(encoder.encode(`data: ${JSON.stringify(payload)}\n\n`)); |
| 70 | } |
| 71 | const done = { |
| 72 | id, |
| 73 | object: "chat.completion.chunk", |
| 74 | choices: [{ index: 0, delta: {}, finish_reason: "stop" }], |
| 75 | }; |
| 76 | controller.enqueue(encoder.encode(`data: ${JSON.stringify(done)}\n\n`)); |
| 77 | controller.enqueue(encoder.encode("data: [DONE]\n\n")); |
| 78 | controller.close(); |
| 79 | }, |
| 80 | }); |
| 81 | |
| 82 | return new Response(readableStream, { |
| 83 | headers: { "Content-Type": "text/event-stream" }, |
| 84 | }); |
| 85 | } |
| 86 | ``` |
| 87 | |
| 88 | ### Backend (Anthropic variant): `app/api/chat/route.ts` |
| 89 | |
| 90 | Replace the model initialization and import: |
| 91 | |
| 92 | ```typescript |
| 93 | import { ChatAnthropic } from "@langchain/anthropic"; |
| 94 | |
| 95 | const model = new ChatAnthropic({ |
| 96 | model: process.env.ANTHROPIC_MODEL ?? "claude-sonnet-4-6", |
| 97 | maxTokens: 4096, |
| 98 | streaming: true, |
| 99 | }); |
| 100 | ``` |
| 101 | |
| 102 | Everything else (message mapping, stream conversion, response) stays identical. |
| 103 | |
| 104 | ### Frontend: `app/chat/page.tsx` |
| 105 | |
| 106 | ```tsx |
| 107 | "use client"; |
| 108 | import { FullScreen } from "@openuidev/react-ui"; |
| 109 | import { openuiChatLibrary } from "@openuidev/react-ui/genui-lib"; |
| 110 | import { |
| 111 | openAIAdapter, |
| 112 | openAIMessageFormat, |
| 113 | } from "@openuidev/react-headless"; |
| 114 | |
| 115 | export default function ChatPage() { |
| 116 | return ( |
| 117 | <FullScreen |
| 118 | componentLibrary={openuiChatLibrary} |
| 119 | streamProtocol={openAIAdapter()} |
| 120 | messageFormat={openAIMessageFormat} |
| 121 | apiUrl="/api/chat" |
| 122 | /> |
| 123 | ); |
| 124 | } |
| 125 | ``` |
| 126 | |
| 127 | > The backend emits SSE (`data: {json}\n\n`). Pair it with `openAIAdapter()` on the frontend. (`langGraphAdapter` is also exported from `@openuidev/react-headless` if you stream LangGraph events natively rather than converting to OpenAI shape.) |
| 128 | |
| 129 | ## Component Creation |
| 130 | |
| 131 | ```tsx |
| 132 | import { defineComponent } from "@openuidev/react-lang"; |
| 133 | import { z } from "zod"; |
| 134 | |
| 135 | export const MetricCard = defineComponent({ |
| 136 | name: "MetricCard", |
| 137 | description: "Displays a metric with label, value, and optional trend", |
| 138 | props: z.object({ |
| 139 | label: z.string().describe("Metric name"), |
| 140 | value: z.number().describe("Current metric value"), |
| 141 | trend: z.enum(["up", "down", "flat"]).optional().describe("Trend direction"), |
| 142 | }), |
| 143 | component: ({ props }) => ( |
| 144 | <div style={{ padding: 16, border: "1px solid #e5e7eb", borderRadius: 8 }}> |
| 145 | <div style={{ fontSize: 14, color: "#6b7280" }}>{props.label}</div> |
| 146 | <div style={{ fontSize: 24, fontWeight: 700 }}>{props.value}</div> |
| 147 | {props.trend && <span>{props.trend === "up" ? "+" : props.trend === "down" ? "-" : "="}</span>} |
| 148 | </div> |
| 149 | ), |
| 150 | }); |
| 151 | ``` |
| 152 | |
| 153 | ## System Prompt Generation |
| 154 | |
| 155 | ```bash |
| 156 | npx @openuidev/cli generate ./ |