OpenAI
Use openai/<model id> to stream OpenAI models. This page covers what is specific to OpenAI; every argument of dyui.llm.stream is on Models & providers.
pip install "dyui[openai]"The key goes in OPENAI_API_KEY, for example in a .env file next to app.py; see API keys & environment. Inside your handler:
async for part in dyui.llm.stream("openai/gpt-5", chat.history, system="You are a helpful analyst.", components=app.catalog): yield partDyUI uses the Chat Completions streaming API. system= (plus the catalog) is sent as the first message, and other keyword arguments go to client.chat.completions.create, for example temperature or reasoning_effort. Reasoning fields in the stream (reasoning_content / reasoning) arrive as dyui.Reasoning.
Structured mode
Section titled “Structured mode”async for part in dyui.llm.stream("openai/gpt-5", chat.history, components=app.catalog, ui_mode="tools"): yield partDyUI sends one strict function tool per component, render_<name>. The JSON schemas have additionalProperties: false, and every field is required, with optional ones nullable. DyUI runs the tool loop for you. If the model calls a tool you passed in tools=, the loop stops, because custom tools need their own loop. See Generative UI.
Azure OpenAI and proxies
Section titled “Azure OpenAI and proxies”OPENAI_BASE_URL or base_url= points DyUI at a compatible proxy. For Azure, pass a configured client; the model id is your deployment name:
from openai import AsyncAzureOpenAI
client = AsyncAzureOpenAI(azure_endpoint="https://….openai.azure.com", api_version="2024-10-21")
# Inside your handler:async for part in dyui.llm.stream("openai/my-deployment", chat.history, components=app.catalog, client=client): yield partAsyncAzureOpenAI reads AZURE_OPENAI_API_KEY itself.
Complete example
Section titled “Complete example”"""An OpenAI model that answers with DyUI components.
Needs: pip install "dyui[openai]" and OPENAI_API_KEY in a .env file next to this one.Save as app.py, then run: dyui run app.pyTry structured mode with: DYUI_UI_MODE=tools dyui run app.py"""import os
import dyui
MODEL = os.getenv("DYUI_MODEL", "openai/gpt-5")UI_MODE = os.getenv("DYUI_UI_MODE", "fence") # "tools" = one strict render_<name> tool per component
app = dyui.App(title="Analyst", starters=["Compare three cloud providers in a table"])
@app.on_messageasync def reply(msg: dyui.Message, chat: dyui.Chat): async for part in dyui.llm.stream( MODEL, chat.history, system="You are a helpful analyst.", components=app.catalog, ui_mode=UI_MODE, ): yield part-
Save it as
app.pyin a new, empty file, or download app.py. -
Put
OPENAI_API_KEY=sk-...in a.envfile next to it. -
Run it:
Terminal window pip install "dyui[openai]"dyui run app.py -
Open http://localhost:8000 and send the starter. Add
DYUI_UI_MODE=toolsto.envand restart to try structured mode.
See also
Section titled “See also”- Models & providers: every argument, rate limits.
- Data analyst: fence and tools mode side by side.