Bring your own SDK
Read this page when you already use an SDK or framework, or when your code has structured data to show directly.
Code on this page is fragments; the complete example is a whole file.
DyUI doesn’t need to own your model calls. It parses text, so anything that streams text can render components. Two steps:
- Add
app.catalog.prompt()to your system prompt. - Yield the raw text deltas. DyUI finds the
dyuifences while they stream.
import os
from openai import AsyncOpenAI
client = AsyncOpenAI(base_url="https://integrate.api.nvidia.com/v1", api_key=os.environ["NVIDIA_API_KEY"])
@app.on_messageasync def reply(msg: dyui.Message, chat: dyui.Chat): system = "You are a data analyst.\n\n" + app.catalog.prompt() stream = await client.chat.completions.create( model="moonshotai/kimi-k2.6", messages=[{"role": "system", "content": system}, *chat.history], stream=True, ) async for chunk in stream: delta = chunk.choices[0].delta.content if chunk.choices else None if delta: yield deltaThe same applies to any framework (LlamaIndex, Haystack, DSPy, your own HTTP client): put the prompt in, and stream the text out.
Rendering components from code
Section titled “Rendering components from code”When your code already has structured data, skip the model and yield components directly:
rows = await db.fetch("select city, temp from weather")yield ui.Table(["City", "Temp"], [[r.city, r.temp] for r in rows])Validating JSON yourself
Section titled “Validating JSON yourself”If your framework produces component objects (for example, from your own tool calls), yield the dict. DyUI validates it like model output:
yield {"ui": "stat", "label": "Revenue", "value": "$48.2k", "delta": "+12%"}app.catalog.json_schema() returns {name: JSON schema}, and app.catalog.tool_schemas("openai" | "anthropic") returns ready-to-send render_<name> tool definitions if you want to run your own structured loop.
Complete example
Section titled “Complete example”The raw openai client against NVIDIA’s endpoint, with app.catalog.prompt() in the system prompt and the raw text yielded.
"""Use your own SDK client. DyUI only needs raw text: it parses ```dyui fences itself."""import osfrom functools import lru_cache
import dyuifrom openai import AsyncOpenAI
@lru_cache(maxsize=1)def client() -> AsyncOpenAI: # Any OpenAI-compatible endpoint. Created on first use, so the app starts without a key. return AsyncOpenAI(base_url=os.getenv("DYUI_BASE_URL", "https://integrate.api.nvidia.com/v1"), api_key=os.environ["NVIDIA_API_KEY"])
MODEL = os.getenv("DYUI_MODEL", "moonshotai/kimi-k2.6") # the endpoint model id, no dyui provider prefix
app = dyui.App(title="BYO SDK", description="Raw OpenAI client, DyUI rendering.")
@app.on_messageasync def reply(msg: dyui.Message, chat: dyui.Chat): system = "You are a helpful assistant.\n\n" + app.catalog.prompt() stream = await client().chat.completions.create( model=MODEL, messages=[{"role": "system", "content": system}, *chat.history], stream=True, ) async for chunk in stream: if chunk.choices and chunk.choices[0].delta.content: yield chunk.choices[0].delta.content # raw text deltas; fences become components-
Save it as
bring_your_own_sdk.pyin a new, empty file, or download it. -
Put
NVIDIA_API_KEY=nvapi-...in a.envfile next to it (the example reads it withos.environwhen the first message arrives). -
Run it:
Terminal window pip install dyui openaidyui run bring_your_own_sdk.py -
Open http://localhost:8000 and send a message.
See also
Section titled “See also”- Generative UI: the fence format your model writes.
- Models & providers: let DyUI make the call instead.