Skip to content

Latest commit

 

History

History

fastapi

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

OpenUI × FastAPI Example

A full-stack example that renders answers with @openuidev/intelligent-ui-library and keeps @openuidev/react-ui for the chat window, with a FastAPI (Python) backend. The LLM streams responses in OpenUI Lang and the React frontend renders them as live components rooted at Col.

This is the first example in the repo using a non-Node.js backend — the same frontend that works with Next.js works here unchanged.

Architecture

┌────────────────────────┐         ┌─────────────────────────┐
│  Vite + React          │  POST   │  FastAPI (Python)       │
│  (port 5173)           │ ──────► │  (port 8000)            │
│                        │         │                         │
│  • AgentInterface UI   │         │  • POST /api/chat       │
│  • openAIReadable-     │  NDJSON │  • OpenUI Cloud         │
│    StreamAdapter()     │ ◄────── │    Chat Completions     │
└────────────────────────┘         └─────────────────────────┘

The Vite dev server proxies /api/* to FastAPI, so CORS is handled transparently.

Project Structure

fastapi/
├── backend/
│   ├── .env.example         # Environment template
│   ├── pyproject.toml       # Python dependencies
│   └── app/
│       └── main.py          # Streaming chat endpoint
└── frontend/
    ├── package.json
    ├── vite.config.js       # Vite + proxy to localhost:8000
    ├── index.html
    └── src/
        ├── library.ts           # Intelligent UI library + promptOptions re-exports
        ├── generated/spec.json  # Generated before dev/build/verify (gitignored)
        ├── main.jsx
        ├── App.jsx              # AgentInterface + library + ThemeProvider
        └── index.css

Getting Started

Prerequisites

1. Configure the backend

cd backend
cp .env.example .env

Add your key to backend/.env:

THESYS_API_KEY=sk-th-...

THESYS_API_KEY is the only required environment variable; no OPENAI_API_KEY is needed. Optionally set OPENUI_MODEL to override the backend's default Cloud model (google/gemini-3.6-flash-free).

2. Start the backend

cd backend
uv run uvicorn app.main:app --reload

Or with pip:

pip install fastapi openai python-dotenv uvicorn
uvicorn app.main:app --reload

The API will be available at http://localhost:8000.

3. Start the frontend

cd frontend
pnpm install --ignore-workspace
pnpm generate
pnpm dev

Open http://localhost:5173.

What's in This Example

backend/app/main.py

A FastAPI endpoint that:

  1. Receives { messages } as JSON (OpenAI Chat Completions messages)
  2. Calls OpenUI Cloud's Chat Completions API (POST /v1/embed/chat/completions) with a system prompt built from frontend/src/generated/spec.json (the Intelligent UI library, generated by pnpm generate in the frontend)
  3. Yields each chunk as NDJSON — the same format the JavaScript SDK's toReadableStream() produces

The prompt stays in Cloud mode. Python reads the generated schema, root and component groups into chatLibrary, forwards the library's examples and additional rules in the sibling systemPromptOptions field, and appends rules for form submission and conditional fields using the current Button and RadioGroup arguments.

frontend/src/App.jsx

Uses fetchLLM with openAIReadableStreamAdapter() and openAIMessageFormat to parse the NDJSON stream from FastAPI.

src/library.ts re-exports library and promptOptions from @openuidev/intelligent-ui-library (pinned to 0.0.2). Before development, builds and verification, pnpm generate runs openui generate src/library.ts --spec --out src/generated/spec.json so the backend and renderer use the same components: Col, Title, Text, Chart and the rest of the new set.

The frontend keeps AgentInterface, the streaming adapters and styles from react-ui. The new library's ThemeProvider wraps the chat and receives the same useSystemThemeMode() value as AgentInterface, so answers follow the system's light/dark preference. Threads and messages persist in browser local storage. Previously saved answers using the old components cannot render with this library; start a new chat to generate answers with the current components.

Learn More

Verify

From frontend/, the verification script builds the client and syntax-checks the Python backend:

pnpm verify