Generative UI

Agent-driven UI via structured output, AG-UI streaming, and optional A2UI declarative rendering

python
from praisonaiagents import Agent

agent = Agent(name="ui-assistant", instructions="Reply with concise text suitable for UI cards.")
agent.start("Show me a summary card for my project")

Agents can drive rich user interfaces — from plain text streaming to typed JSON schemas to live React components — by picking the right output tier for your client.

The user chats with the agent; generative UI renders cards, forms, or charts alongside the text reply.

Diagram

CLI / logs

API / custom frontend

CopilotKit / React

Flutter / cross-platform

Agent

Which client?

Tier 0: Markdown stream

Tier 1: Structured output

Tier 2: AG-UI stream

Tier 3: A2UI declarative

Text chunks

Pydantic / JSON

SSE event stream

JSON UI components

How It Works

Diagram
GenerativeUiAgentUserGenerativeUiAgentUserRequestProcessResultResponse

Quick Start

  1. Tier 1 — Structured output (recommended for APIs)
    python
    from pydantic import BaseModel
    from praisonaiagents import Agent
    
    class Dashboard(BaseModel):
        title: str
        summary: str
        metrics: list[dict]
    
    agent = Agent(
        name="Analyst",
        instructions="Summarise sales data into a dashboard.",
        output_pydantic=Dashboard
    )
    
    data = agent.start("Summarise Q1 sales from the attached report.")
    print(data.title)
    print(data.metrics)

    Your frontend receives a typed object — map Dashboard fields to whatever React, Vue, or mobile components you already have.

  2. Tier 2 — AG-UI (CopilotKit / React)
    python
    from praisonaiagents import Agent, AGUI
    from fastapi import FastAPI
    
    agent = Agent(
        name="Assistant",
        instructions="Help users with research.",
    )
    agui = AGUI(agent=agent)
    
    app = FastAPI()
    app.include_router(agui.get_router())

    Run: uvicorn main:app --port 8002

    The agent streams text deltas and tool call events over Server-Sent Events to any AG-UI compatible frontend (CopilotKit, custom SSE client).


How It Works

Diagram
Generative UIAgentUserGenerative UIAgentUserRequestProcess requestResultResponse

Tier decision flow

Diagram

No

Yes

No

Yes

Yes

No – React / web

Do you need live streaming?

Do you have a custom frontend?

Cross-platform native UI?

Tier 0: stdout / markdown stream

Tier 1: output_pydantic or output_json

Tier 3: A2UI — pip install praisonaiagents a2ui

Tier 2: AGUI + FastAPI

Tier 0 — Markdown streaming

Diagram

Agent

stream=True

CLI / chat widget

python
from praisonaiagents import Agent

agent = Agent(name="Bot", instructions="Answer questions.")
for chunk in agent.start("Tell me about AI.", stream=True):
    print(chunk, end="", flush=True)

Tier 1 — Structured output

Diagram

Agent

output_pydantic

JSON object

Frontend components

Use output_pydantic=YourModel or output_json=True on the agent. The model formats its response to fit the schema, and you receive a validated Python object.

Tier 2 — AG-UI stream

Diagram

Agent

AGUI

SSE event stream

CopilotKit / React

AGUI wraps any agent and exposes a POST /agui endpoint that emits text delta and tool call events per the AG-UI protocol. Recoverable failures and progress (retry, model fallback, tool progress, todo updates) arrive as custom events named praisonai.*, and mid-stream provider errors surface as run_error. See the AGUI API reference.

Tier 3 — A2UI declarative

Diagram

Agent

a2ui SDK

Declarative UI JSON

Flutter / React renderers

Requires pip install praisonaiagents[a2ui]. The agent emits A2UI JSON; platform-specific renderers (React, Flutter, Lit) turn it into native UI components.


Configuration Options

Tier 1 — Structured output parameters on Agent:

ParameterTypeDescription
output_pydantictype[BaseModel]Pydantic model class — response is validated and returned as an instance
output_jsonboolReturn raw JSON dict instead of a model instance

Tier 2 — AGUI constructor:

ParameterTypeDefaultDescription
agentAgent—Single agent to expose
agentsAgents—Multi-agent workflow to expose
namestragent nameName for the endpoint
descriptionstragent roleDescription for OpenAPI docs
prefixstr""URL prefix for the router
tagslist[str]["AGUI"]OpenAPI tags
AGUI TypeScript Reference

TypeScript AG-UI configuration

A2UI Protocol

A2UI declarative UI protocol details


Common Patterns

Return a typed dashboard from a research agent:

python
from pydantic import BaseModel
from praisonaiagents import Agent

class ResearchReport(BaseModel):
    title: str
    summary: str
    key_points: list[str]
    sources: list[str]

agent = Agent(
    name="Researcher",
    instructions="Research topics and return structured reports.",
    output_pydantic=ResearchReport
)

report = agent.start("Research the impact of AI on healthcare.")
for point in report.key_points:
    print(f"• {point}")

Expose a multi-agent workflow via AG-UI:

python
from praisonaiagents import Agent, Agents, AGUI
from fastapi import FastAPI

researcher = Agent(name="Researcher", instructions="Research topics.")
writer = Agent(name="Writer", instructions="Write articles.")

workflow = Agents(agents=[researcher, writer])
agui = AGUI(agents=workflow)

app = FastAPI()
app.include_router(agui.get_router())

Stream output to the terminal:

python
from praisonaiagents import Agent

agent = Agent(name="Assistant", instructions="Be helpful.")
for chunk in agent.start("Write a poem about space.", stream=True):
    print(chunk, end="", flush=True)

Best Practices

Choose the simplest tier that meets your needs

Tier 1 covers 90% of use cases — a typed Pydantic response is easy to test, validate, and map to UI components. Only reach for Tier 2 or 3 when you genuinely need live streaming or cross-platform native rendering.

Define narrow Pydantic models for Tier 1

Broad schemas like dict defeat the purpose of structured output. Model exactly the fields your frontend needs — this makes validation strict and LLM prompting more reliable.

Do not reimplement A2UI types

Use praisonaiagents[a2ui] for Tier 3 — do not create your own A2UI JSON structures. The official SDK ensures schema compliance across renderer versions.

Renderers live outside PraisonAI core

For Tier 3, the React, Flutter, and Lit renderers are maintained in the Google A2UI repository. PraisonAI provides the agent side; you choose and update renderers independently.


Related