AG-UI Protocol Python SDK
AG-UI (Agent-User Interaction Protocol) is an open, lightweight, event-based protocol that standardizes how AI agents connect to user-facing applications. The `ag-ui-protocol` Python SDK provides strongly-typed data structures and event encoding for building AG-UI compatible agent servers. It's built on Pydantic for robust validation and automatic camelCase serialization for seamless frontend integration, and is currently at version 0.1.15. The library has a continuous release cadence, reflecting ongoing development in the AG-UI ecosystem.
Warnings
- breaking The AG-UI protocol is under active development. While efforts are made to use versioned schemas, specific event types, data models, or API structures may change in future releases, potentially requiring updates to integrated agents and UIs.
- gotcha AG-UI servers should not be directly exposed to untrusted clients (e.g., browsers, mobile apps) due to security risks. A trusted frontend server should mediate communication to validate and control the construction of AG-UI protocol messages, preventing malicious client input.
- gotcha Integrating AG-UI with existing agent frameworks or custom tooling often requires explicit configuration for features like tool exposure and event translation. Automatic handling might not be sufficient or desired. For instance, in `ag-ui-adk`, client tools are not automatically added to the root agent's toolset and require explicit `AGUIToolset` addition.
- gotcha Effective state management in AG-UI requires understanding when to use `STATE_SNAPSHOT` (full state replacement) versus `STATE_DELTA` (incremental JSON Patch updates). Incorrect usage can lead to inefficient data transfer or inconsistent state between agent and UI.
Install
-
pip install ag-ui-protocol
Imports
- RunAgentInput
from ag_ui.core import RunAgentInput
- AgentEventEncoder
from ag_ui.encoder import AgentEventEncoder
- TextMessageContent
from ag_ui.core import TextMessageContent
Quickstart
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from ag_ui.core import RunAgentInput, TextMessageContent, RunFinished, AgentEvent
from ag_ui.encoder import AgentEventEncoder
import uvicorn
import asyncio
import json
import os
app = FastAPI(title="AG-UI Endpoint Example")
@app.post("/awp")
async def my_endpoint(input_data: RunAgentInput):
async def event_generator():
# Initial greeting event
yield AgentEventEncoder.encode(TextMessageContent(text="Hello from AG-UI!"))
# Echo the last text message from the input
if input_data.messages:
last_message = input_data.messages[-1]
# Ensure the last message is a TextMessageContent for echoing
if isinstance(last_message, TextMessageContent):
yield AgentEventEncoder.encode(TextMessageContent(text=f"You said: {last_message.text}"))
else:
yield AgentEventEncoder.encode(TextMessageContent(text="Received a non-text message type."))
else:
yield AgentEventEncoder.encode(TextMessageContent(text="No messages provided in input."))
# Simulate some asynchronous work
await asyncio.sleep(0.5)
yield AgentEventEncoder.encode(TextMessageContent(text="Performing some agent actions..."))
await asyncio.sleep(0.5)
# End the run with a RunFinished event
yield AgentEventEncoder.encode(RunFinished())
# Use StreamingResponse for Server-Sent Events (SSE)
return StreamingResponse(event_generator(), media_type="text/event-stream")
if __name__ == "__main__":
# To run this, you'll need fastapi and uvicorn installed:
# pip install "fastapi[all]" uvicorn
# Then, run this script directly:
# python your_script_name.py
# Or, if saved as main.py:
# uvicorn main:app --host 0.0.0.0 --port 8000
uvicorn.run(app, host="0.0.0.0", port=8000)