Module 29: Introduction to UI Integration
Theoryโ
Figure: Integrating AI agents into modern multi-platform frontends (React, WebSockets, Streamlit, and conversational UI).
Why UI Integration Mattersโ
While agents can be run from the command line or via an API, their full potential is unlocked when they are integrated into a user interface. A UI makes your agent accessible to non-technical users and enables rich, interactive experiences.
The UI Integration Landscapeโ
The ADK is flexible and supports several approaches for UI integration, each suited for different use cases:
| Approach | Best For | Key Features |
|---|---|---|
| AG-UI Protocol | Modern web apps (React/Next.js) | Pre-built components, official support |
| Native ADK API | Custom frameworks (Vue, Angular, Vanilla JS) | Full control, zero frontend SDK lock-in |
| Direct Python | Data apps (Streamlit, Gradio) | In-process, no HTTP overhead |
| Messaging Platforms | Team bots (Slack, Teams) | Native platform UX |
| Event-Driven | High-scale, async workflows | Decoupled, scalable (Pub/Sub) |
Understanding the AG-UI Protocolโ
AG-UI (Agent-Generative UI) is an open protocol for agent-user interaction, developed through an official partnership between the Google ADK and CopilotKit teams. It provides a standardized, event-based way for AI agents to communicate with web UIs.
The Stack:
Figure: Fullstack AG-UI architecture bridging frontend React/Next.js components with backend ADK FastAPI agents via bidirectional streaming.
By using the AG-UI protocol and the associated libraries, you can quickly build a production-ready chat interface with minimal front-end code, as the pre-built components handle the complexities of state management and streaming.
Under the Hood: Native ADK API & /run_sse Streamingโ
A note on this module's scope: the lab in this module builds directly against the Native ADK API (uv run adk api_server) instead of AG-UI โ on purpose. Writing a raw fetch + Server-Sent Events (SSE) client by hand teaches you the exact wire mechanics that higher-level frameworks like AG-UI and CopilotKit abstract away:
- Explicit Session Creation (
POST /apps/{app_name}/users/{user_id}/sessions): Before calling/run_sse, the client creates a session via REST, passing{"session_id": "..."}in the request body (or{}to let the server generate one). - Enabling Token Streaming (
"streaming": true): In ADK'sRunAgentRequestschema,streamingdefaults tofalse. Including"streaming": truein your/run_sseJSON payload activatesStreamingMode.SSEso tokens arrive incrementally as the model generates them rather than in a single block at the end of the turn. - Partial Chunks vs. Final Aggregated Event (
event.partial): During an active SSE stream, ADK emits incrementalEventobjects with"partial": true, followed at the end of the turn by one final aggregatedEvent(wherepartialis omitted/false) containing the full accumulated response text. A well-behaved UI appendspartial === truechunks live and skips re-appending the final aggregated text so responses are never duplicated! - Line Buffering Across TCP Chunks:
ReadableStreamDefaultReader.read()yields arbitrary network byte slices that can split adata: {...}\n\nline mid-JSON. Buffering incomplete trailing text acrossreader.read()iterations preventsJSON.parsecrashes on large events.
Once you understand these mechanics, adopting AG-UI for a production React/Next.js project is as simple as dropping in @copilotkit/react-core โ see the CopilotKit + ADK docs to explore that path.
When consuming ADK's /run_sse endpoint with 'streaming': true, why must a custom frontend inspect eventData.partial before appending text?
Key Takeawaysโ
- Integrating an agent with a UI makes it accessible to a wider audience and enables richer interactions.
- The ADK supports multiple UI integration strategies, including the official AG-UI Protocol, the native ADK API, direct Python integration, and messaging platforms.
- The AG-UI Protocol, developed in partnership with CopilotKit, is Google's officially supported path for production React/Next.js applications, providing pre-built components that handle streaming and state for you.
- When building custom clients against
uv run adk api_serverand/run_sse, pass"streaming": true, buffer SSE lines across network chunks, and distinguishpartial: truedeltas from the final aggregatedEvent. - Static Asset Isolation: Never run a static file server (like
python3 -m http.server) inside your agent package directory, or it will expose.envcredentials over HTTP. Keep frontend files in a separatefrontend/directory. - Session ID Persistence: Generating a new
sessionIdon every page load loses conversational memory on refresh; persistsessionIdinlocalStorageor an HTTP-only session cookie in real apps.