Concepts Components Blog Roadmap
Get Started
On this page

Teaching AI to Speak UI: Agent Development Guide

How to prompt-engineer your LLM to generate valid A2UI JSON. Best practices for schema validation and tool calling.

The Challenge: LLMs are great at poetry, but bad at structured JSON. The Solution: Give them a grammar book (Schema) and a translator (Validation).

This guide shows you how to turn a standard “Chatbot” into a “UI Generator”.

1. The “System Prompt” Strategy

The most reliable way to get A2UI output is to inject the instructions directly into the System Prompt.

The Anatomy of a UI Prompt

You need to tell the LLM three things:

  1. Identity: “You are a helpful assistant…”
  2. Capability: “You can render UI components using the a2ui tool…”
  3. Grammar: “Here is the exact JSON structure you must follow…“
SYSTEM_PROMPT = """
You are a travel assistant.
If the user asks for flights, DO NOT send a text list.
Instead, generate a JSON object using the 'flight-list' schema:

{
  "type": "flight-list",
  "data": { "flights": [...] }
}
"""

2. Tool Use vs. Generation

There are two ways to generate UI:

Define a render_ui tool. The LLM calls the function, and your backend handles the JSON construction.

  • Pros: 100% valid JSON (guaranteed by the SDK).
  • Cons: Slower (extra round trip).

Approach B: Direct Streaming (Fastest)

Ask the LLM to output raw JSON directly in the stream.

  • Pros: Instant “Streaming” UX.
  • Cons: LLM might make syntax errors (e.g., missing comma).

3. Validation is Mandatory

Never trust the AI. It will eventually “hallucinate” a closing bracket. Always validate the output before sending it to the client.

import jsonschema

def validate_and_send(ai_response):
    try:
        jsonschema.validate(instance=ai_response, schema=A2UI_SCHEMA)
        client.send(ai_response)
    except Exception:
        client.send({ "type": "error", "message": "I tried to draw a UI but failed." })

4. Google ADK Example

Using the Google Agent Development Kit, you can bind the schema automatically.

from google.adk.agents import Agent

agent = Agent(
    name="restaurant_booker",
    instruction="Use the `show_restaurant` tool to display results.",
    tools=[show_restaurant_tool] 
)