Skip to content

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.

Terminal window
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 part

DyUI 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.

async for part in dyui.llm.stream("openai/gpt-5", chat.history, components=app.catalog, ui_mode="tools"):
yield part

DyUI 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.

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 part

AsyncAzureOpenAI reads AZURE_OPENAI_API_KEY itself.

app.py
"""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.py
Try 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_message
async 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
  1. Save it as app.py in a new, empty file, or download app.py.

  2. Put OPENAI_API_KEY=sk-... in a .env file next to it.

  3. Run it:

    Terminal window
    pip install "dyui[openai]"
    dyui run app.py
  4. Open http://localhost:8000 and send the starter. Add DYUI_UI_MODE=tools to .env and restart to try structured mode.

DyUI Docs assistant

Answers come from the DyUI docs. gpt-oss-120b via Groq. Press Escape or the close button to return to the page.