Loading...
Loading...
Expose an AG2 `Agent` over the AG-UI protocol so a frontend (CopilotKit, custom React/Next.js, or any AG-UI client) can stream responses (text and reasoning), render tool calls, sync shared state, and track sub-task steps. Wraps the agent with `AGUIStream(agent)` and mounts it in FastAPI via `stream.dispatch(...)` or `stream.build_asgi()`. Use when the user wants a web frontend in front of an AG2 agent rather than a CLI / script.
npx skill4agent add ag2ai/ag2-skills ag2-ag-ui| Feature | Status |
|---|---|
Run lifecycle ( | ✓ |
Streaming text events ( | ✓ |
Reasoning events ( | ✓ |
Backend tool lifecycle ( | ✓ |
Frontend-tool dispatch ( | ✓ |
Shared-state snapshots ( | ✓ |
Sub-task steps ( | ✓ |
| Human input checkpoints surfaced as AG-UI events | ✗ — no human-input event family; |
pip install "ag2[ag-ui]"Required. Run this install before delivering the code. If you cannot run commands, state the exactcommand.pip install
from fastapi import FastAPI, Header
from fastapi.responses import StreamingResponse
from ag2 import Agent
from ag2.ag_ui import AGUIStream, RunAgentInput
from ag2.config import OpenAIConfig
agent = Agent(
name="support_bot",
prompt="You help users with billing questions.",
config=OpenAIConfig(model="gpt-4o-mini"),
)
stream = AGUIStream(agent)
app = FastAPI()
@app.post("/chat")
async def run_agent(message: RunAgentInput, accept: str | None = Header(None)) -> StreamingResponse:
return StreamingResponse(
stream.dispatch(message, accept=accept),
media_type=accept or "text/event-stream",
)uvicorn run_ag_ui:app --reload --port 8000curl -N -X POST http://127.0.0.1:8000/chat \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-d '{"thread_id":"t1","run_id":"r1","messages":[{"id":"m1","role":"user","content":"Hello"}],"state":{},"context":[],"tools":[]}'build_asgi()from ag2.ag_ui import AGUIStream
from fastapi import FastAPI
app = FastAPI()
stream = AGUIStream(agent)
app.mount("/chat", stream.build_asgi())# Option A — CopilotKit bootstrap
npx copilotkit@latest create -f ag2
# Option B — clone the reference starter
# https://github.com/ag2ai/ag2-copilotkit-starterag2-copilotkit-starter/
├── agent-py/ # Python backend (AG2 + AG-UI)
└── ui-react/ # React + CopilotKit frontend/chatui-react/app/api/copilotkit/route.tsimport { HttpAgent } from "@ag-ui/client";
import { CopilotRuntime, ExperimentalEmptyAdapter, copilotRuntimeNextJSAppRouterEndpoint } from "@copilotkit/runtime";
import { NextRequest } from "next/server";
const agent = new HttpAgent({ url: "http://localhost:8008/chat" });
const runtime = new CopilotRuntime({ agents: { weather_agent: agent } });
export async function POST(req: NextRequest) {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
runtime,
serviceAdapter: new ExperimentalEmptyAdapter(),
endpoint: "/api/copilotkit",
});
return handleRequest(req);
}import { CopilotKit } from "@copilotkit/react-core";
import "@copilotkit/react-ui/styles.css";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<CopilotKit agent="weather_agent" runtimeUrl="/api/copilotkit">
{children}
</CopilotKit>
</body>
</html>
);
}<CopilotChat />useCopilotAction({...})POSTOPTIONS/api/copilotkit/chatContent-Type: text/event-streamuseCopilotActionname@toolwebsite/docs/user-guide/ag-ui/index.mdxAGUIStreamwebsite/docs/user-guide/ag-ui/copilotkit-quickstart.mdxag-uipip install "ag2[ag-ui]"from ag2.ag_ui import AGUIStreamCORSMiddlewareContent-Typecurl -NuseCopilotAction({ name: "..." })@toolAGUIStreamConversableAgentag2.Agent