$npx -y skills add OthmanAdi/openui-forge --skill openui-forge-pythonOpenUI generative UI with Python FastAPI backend. OpenAI and Anthropic SDK variants.
| 1 | # OpenUI Forge — Python |
| 2 | |
| 3 | Build generative UI apps with a React frontend + Python FastAPI backend. Streams OpenAI-compatible NDJSON. |
| 4 | |
| 5 | ## Activation Triggers |
| 6 | |
| 7 | - "openui python", "openui fastapi", "openui flask" |
| 8 | - "generative ui python", "python streaming ui backend" |
| 9 | |
| 10 | ## Prerequisites |
| 11 | |
| 12 | - Node.js >= 22 (24 LTS recommended) + React >= 18.3.1 (19+ recommended) (frontend) |
| 13 | - Python >= 3.10 (backend) |
| 14 | - `OPENAI_API_KEY` or `ANTHROPIC_API_KEY` set |
| 15 | |
| 16 | ## Quick Start |
| 17 | |
| 18 | 1. Create the React frontend and install OpenUI deps: |
| 19 | ```bash |
| 20 | npm install @openuidev/react-ui @openuidev/react-headless @openuidev/react-lang lucide-react zod |
| 21 | ``` |
| 22 | 2. Generate the system prompt from your component library: |
| 23 | ```bash |
| 24 | npx @openuidev/cli generate ./src/lib/library.ts --out backend/system-prompt.txt |
| 25 | ``` |
| 26 | 3. Set up the Python backend (see Full Code below) |
| 27 | 4. Run both: frontend on `:3000`, backend on `:8000` |
| 28 | |
| 29 | ## Full Code |
| 30 | |
| 31 | ### Backend: `backend/requirements.txt` |
| 32 | |
| 33 | ``` |
| 34 | fastapi>=0.115.0 |
| 35 | uvicorn>=0.24.0 |
| 36 | openai>=2.0 |
| 37 | anthropic>=0.111.0 |
| 38 | python-dotenv>=1.0.0 |
| 39 | ``` |
| 40 | |
| 41 | > The Python >= 3.10 floor comes from fastapi/uvicorn/python-dotenv; openai and anthropic themselves need only Python 3.9. |
| 42 | |
| 43 | ### Backend (OpenAI): `backend/main.py` |
| 44 | |
| 45 | ```python |
| 46 | import os |
| 47 | from pathlib import Path |
| 48 | from dotenv import load_dotenv |
| 49 | from fastapi import FastAPI, Request |
| 50 | from fastapi.middleware.cors import CORSMiddleware |
| 51 | from fastapi.responses import StreamingResponse |
| 52 | from openai import AsyncOpenAI |
| 53 | |
| 54 | load_dotenv() |
| 55 | app = FastAPI() |
| 56 | app.add_middleware( |
| 57 | CORSMiddleware, |
| 58 | allow_origins=["http://localhost:3000"], |
| 59 | allow_methods=["POST"], |
| 60 | allow_headers=["*"], |
| 61 | ) |
| 62 | |
| 63 | # AsyncOpenAI keeps the request from blocking the event loop during streaming. |
| 64 | client = AsyncOpenAI() |
| 65 | SYSTEM_PROMPT = Path("system-prompt.txt").read_text() |
| 66 | |
| 67 | @app.post("/api/chat") |
| 68 | async def chat(request: Request): |
| 69 | body = await request.json() |
| 70 | messages = [{"role": "system", "content": SYSTEM_PROMPT}] + body["messages"] |
| 71 | |
| 72 | async def generate(): |
| 73 | response = await client.chat.completions.create( |
| 74 | model=os.getenv("OPENAI_MODEL", "gpt-5.5"), |
| 75 | stream=True, |
| 76 | messages=messages, |
| 77 | ) |
| 78 | async for chunk in response: |
| 79 | data = chunk.model_dump_json() |
| 80 | yield f"data: {data}\n\n" |
| 81 | yield "data: [DONE]\n\n" |
| 82 | |
| 83 | return StreamingResponse(generate(), media_type="text/event-stream") |
| 84 | ``` |
| 85 | |
| 86 | ### Backend (Anthropic variant): `backend/main_anthropic.py` |
| 87 | |
| 88 | ```python |
| 89 | import os, json, time |
| 90 | from pathlib import Path |
| 91 | from dotenv import load_dotenv |
| 92 | from fastapi import FastAPI, Request |
| 93 | from fastapi.middleware.cors import CORSMiddleware |
| 94 | from fastapi.responses import StreamingResponse |
| 95 | from anthropic import AsyncAnthropic |
| 96 | |
| 97 | load_dotenv() |
| 98 | app = FastAPI() |
| 99 | app.add_middleware( |
| 100 | CORSMiddleware, |
| 101 | allow_origins=["http://localhost:3000"], |
| 102 | allow_methods=["POST"], |
| 103 | allow_headers=["*"], |
| 104 | ) |
| 105 | |
| 106 | # AsyncAnthropic mirrors AsyncOpenAI so the stream does not block the loop. |
| 107 | client = AsyncAnthropic() |
| 108 | SYSTEM_PROMPT = Path("system-prompt.txt").read_text() |
| 109 | |
| 110 | @app.post("/api/chat") |
| 111 | async def chat(request: Request): |
| 112 | body = await request.json() |
| 113 | stream_id = f"chatcmpl-{int(time.time())}" |
| 114 | |
| 115 | async def generate(): |
| 116 | async with client.messages.stream( |
| 117 | model=os.getenv("ANTHROPIC_MODEL", "claude-sonnet-4-6"), |
| 118 | max_tokens=4096, |
| 119 | system=SYSTEM_PROMPT, |
| 120 | messages=body["messages"], |
| 121 | ) as stream: |
| 122 | async for text in stream.text_stream: |
| 123 | chunk = {"id": stream_id, "object": "chat.completion.chunk", |
| 124 | "choices": [{"index": 0, "delta": {"content": text}, "finish_reason": None}]} |
| 125 | yield f"data: {json.dumps(chunk)}\n\n" |
| 126 | done = {"id": stream_id, "object": "chat.completion.chunk", |
| 127 | "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]} |
| 128 | yield f"data: {json.dumps(done)}\n\n" |
| 129 | yield "data: [DONE]\n\n" |
| 130 | |
| 131 | return StreamingResponse(generate(), media_type="text/event-stream") |
| 132 | ``` |
| 133 | |
| 134 | ### Frontend: `app/chat/page.tsx` (or `src/Chat.tsx` for Vite) |
| 135 | |
| 136 | ```tsx |
| 137 | "use client"; |
| 138 | import { FullScreen } from "@openuidev/react-ui"; |
| 139 | import { openuiChatLibrary } from "@openuidev/react-ui/genui-lib"; |
| 140 | import { |
| 141 | openAIAdapter, |
| 142 | openAIMessageFormat, |
| 143 | } from "@openuidev/react-headless"; |
| 144 | |
| 145 | export default function ChatPage() { |
| 146 | return ( |
| 147 | <FullScreen |
| 148 | componentLibrary={openuiChatLibrary} |
| 149 | streamProtocol={openAIAdapter()} |
| 150 | messageFormat={openAIMessageFormat} |
| 151 | apiUrl="http://localhost:8000/api/chat" |
| 152 | /> |
| 153 | ); |
| 154 | } |
| 155 | ``` |
| 156 | |
| 157 | > The Python backend emits SSE (`data: {json}\n\n`). Pair it with `openAIAdapter()` on the frontend. `openAIReadableStreamAdapter()` is for NDJSON (no `data:` prefix) and will silently produce no output here. |
| 158 | |
| 159 | ## System Prompt Generation |
| 160 | |
| 161 | Generate once, copy to backend directory: |