|
| 1 | +#Agent User Interaction (AG-UI) Protocol |
| 2 | + |
| 3 | +The[Agent User Interaction (AG-UI) Protocol](https://docs.ag-ui.com/introduction) is an open standard introduced by the |
| 4 | +[CopilotKit](https://webflow.copilotkit.ai/blog/introducing-ag-ui-the-protocol-where-agents-meet-users) |
| 5 | +team that standardises how frontend applications communicate with AI agents, with support for streaming, frontend tools, shared state, and custom events. |
| 6 | + |
| 7 | +Any Pydantic AI agent can be exposed as an AG-UI server using the[`Agent.to_ag_ui()`][pydantic_ai.Agent.to_ag_ui] convenience method. |
| 8 | + |
| 9 | +!!! note |
| 10 | + The AG-UI integration was originally built by the team at[Rocket Science](https://www.rocketscience.gg/) and contributed in collaboration with the Pydantic AI and CopilotKit teams. Thanks Rocket Science! |
| 11 | + |
| 12 | +##Installation |
| 13 | + |
| 14 | +The only dependencies are: |
| 15 | + |
| 16 | +-[ag-ui-protocol](https://docs.ag-ui.com/introduction): to provide the AG-UI types and encoder |
| 17 | +-[starlette](https://www.starlette.io): to expose the AG-UI server as an[ASGI application](https://asgi.readthedocs.io/en/latest/) |
| 18 | + |
| 19 | +You can install Pydantic AI with the`ag-ui` extra to ensure you have all the |
| 20 | +required AG-UI dependencies: |
| 21 | + |
| 22 | +```bash |
| 23 | +pip/uv-add'pydantic-ai-slim[ag-ui]' |
| 24 | +``` |
| 25 | + |
| 26 | +To run the examples you'll also need: |
| 27 | + |
| 28 | +-[uvicorn](https://www.uvicorn.org/) or another ASGI compatible server |
| 29 | + |
| 30 | +```bash |
| 31 | +pip/uv-add uvicorn |
| 32 | +``` |
| 33 | + |
| 34 | +##Quick start |
| 35 | + |
| 36 | +To expose a Pydantic AI agent as an AG-UI server, you can use the[`Agent.to_ag_ui()`][pydantic_ai.Agent.to_ag_ui] method: |
| 37 | + |
| 38 | +```py {title="agent_to_ag_ui.py" py="3.10" hl_lines="4"} |
| 39 | +from pydantic_aiimport Agent |
| 40 | + |
| 41 | +agent= Agent('openai:gpt-4.1',instructions='Be fun!') |
| 42 | +app= agent.to_ag_ui() |
| 43 | +``` |
| 44 | + |
| 45 | +Since`app` is an ASGI application, it can be used with any ASGI server: |
| 46 | + |
| 47 | +```shell |
| 48 | +uvicorn agent_to_ag_ui:app --host 0.0.0.0 --port 9000 |
| 49 | +``` |
| 50 | + |
| 51 | +This will expose the agent as an AG-UI server, and your frontend can start sending requests to it. |
| 52 | + |
| 53 | +The`to_ag_ui()` method accepts the same arguments as the[`Agent.iter()`][pydantic_ai.agent.Agent.iter] method as well as arguments that let you configure the[Starlette](https://www.starlette.io)-based ASGI app. |
| 54 | + |
| 55 | +##Design |
| 56 | + |
| 57 | +The Pydantic AI AG-UI integration supports all features of the spec: |
| 58 | + |
| 59 | +-[Events](https://docs.ag-ui.com/concepts/events) |
| 60 | +-[Messages](https://docs.ag-ui.com/concepts/messages) |
| 61 | +-[State Management](https://docs.ag-ui.com/concepts/state) |
| 62 | +-[Tools](https://docs.ag-ui.com/concepts/tools) |
| 63 | + |
| 64 | +The app receives messages in the form of a |
| 65 | +[`RunAgentInput`](https://docs.ag-ui.com/sdk/js/core/types#runagentinput) |
| 66 | +which describes the details of a request being passed to the agent including |
| 67 | +messages and state. These are then converted to Pydantic AI types and passed to the |
| 68 | +agent which then process the request. |
| 69 | + |
| 70 | +Events from the agent, including tool calls, are converted to AG-UI events and |
| 71 | +streamed back to the caller as Server-Sent Events (SSE). |
| 72 | + |
| 73 | +A user request may require multiple round trips between client UI and Pydantic AI |
| 74 | +server, depending on the tools and events needed. |
| 75 | + |
| 76 | +##Features |
| 77 | + |
| 78 | +###State management |
| 79 | + |
| 80 | +The adapter provides full support for |
| 81 | +[AG-UI state management](https://docs.ag-ui.com/concepts/state), which enables |
| 82 | +real-time synchronization between agents and frontend applications. |
| 83 | + |
| 84 | +In the example below we have document state which is shared between the UI and |
| 85 | +server using the[`StateDeps`][pydantic_ai.ag_ui.StateDeps] which implements the |
| 86 | +[`StateHandler`][pydantic_ai.ag_ui.StateHandler] protocol that can be used to automatically |
| 87 | +decode state contained in[`RunAgentInput.state`](https://docs.ag-ui.com/sdk/js/core/types#runagentinput) |
| 88 | +when processing requests. |
| 89 | + |
| 90 | +```python {title="ag_ui_state.py" py="3.10"} |
| 91 | +from pydanticimport BaseModel |
| 92 | + |
| 93 | +from pydantic_aiimport Agent |
| 94 | +from pydantic_ai.ag_uiimport StateDeps |
| 95 | + |
| 96 | + |
| 97 | +classDocumentState(BaseModel): |
| 98 | +"""State for the document being written.""" |
| 99 | + |
| 100 | + document:str='' |
| 101 | + |
| 102 | + |
| 103 | +agent= Agent( |
| 104 | +'openai:gpt-4.1', |
| 105 | +instructions='Be fun!', |
| 106 | +deps_type=StateDeps[DocumentState], |
| 107 | +) |
| 108 | +app= agent.to_ag_ui(deps=StateDeps(DocumentState())) |
| 109 | +``` |
| 110 | + |
| 111 | +Since`app` is an ASGI application, it can be used with any ASGI server: |
| 112 | + |
| 113 | +```bash |
| 114 | +uvicorn ag_ui_state:app --host 0.0.0.0 --port 9000 |
| 115 | +``` |
| 116 | + |
| 117 | +###Tools |
| 118 | + |
| 119 | +AG-UI frontend tools are seamlessly provided to the Pydantic AI agent, enabling rich |
| 120 | +user experiences with frontend user interfaces. |
| 121 | + |
| 122 | +###Events |
| 123 | + |
| 124 | +Pydantic AI tools can send |
| 125 | +[AG-UI events](https://docs.ag-ui.com/concepts/events) simply by defining a tool |
| 126 | +which returns a (subclass of) |
| 127 | +[`BaseEvent`](https://docs.ag-ui.com/sdk/python/core/events#baseevent), which allows |
| 128 | +for custom events and state updates. |
| 129 | + |
| 130 | +```python {title="ag_ui_tool_events.py" py="3.10"} |
| 131 | +from ag_ui.coreimport CustomEvent, EventType, StateSnapshotEvent |
| 132 | +from pydanticimport BaseModel |
| 133 | + |
| 134 | +from pydantic_aiimport Agent, RunContext |
| 135 | +from pydantic_ai.ag_uiimport StateDeps |
| 136 | + |
| 137 | + |
| 138 | +classDocumentState(BaseModel): |
| 139 | +"""State for the document being written.""" |
| 140 | + |
| 141 | + document:str='' |
| 142 | + |
| 143 | + |
| 144 | +agent= Agent( |
| 145 | +'openai:gpt-4.1', |
| 146 | +instructions='Be fun!', |
| 147 | +deps_type=StateDeps[DocumentState], |
| 148 | +) |
| 149 | +app= agent.to_ag_ui(deps=StateDeps(DocumentState())) |
| 150 | + |
| 151 | + |
| 152 | +@agent.tool |
| 153 | +defupdate_state(ctx: RunContext[StateDeps[DocumentState]]) -> StateSnapshotEvent: |
| 154 | +return StateSnapshotEvent( |
| 155 | +type=EventType.STATE_SNAPSHOT, |
| 156 | +snapshot=ctx.deps.state, |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +@agent.tool_plain |
| 161 | +defcustom_events() -> list[CustomEvent]: |
| 162 | +return [ |
| 163 | + CustomEvent( |
| 164 | +type=EventType.CUSTOM, |
| 165 | +name='count', |
| 166 | +value=1, |
| 167 | + ), |
| 168 | + CustomEvent( |
| 169 | +type=EventType.CUSTOM, |
| 170 | +name='count', |
| 171 | +value=2, |
| 172 | + ), |
| 173 | + ] |
| 174 | +``` |
| 175 | + |
| 176 | +Since`app` is an ASGI application, it can be used with any ASGI server: |
| 177 | + |
| 178 | +```bash |
| 179 | +uvicorn ag_ui_tool_events:app --host 0.0.0.0 --port 9000 |
| 180 | +``` |
| 181 | + |
| 182 | +##Examples |
| 183 | + |
| 184 | +For more examples of how to use[`to_ag_ui()`][pydantic_ai.Agent.to_ag_ui] see |
| 185 | +[`pydantic_ai_examples.ag_ui`](https://github.com/pydantic/pydantic-ai/tree/main/examples/pydantic_ai_examples/ag_ui), |
| 186 | +which includes a server for use with the |
| 187 | +[AG-UI Dojo](https://docs.ag-ui.com/tutorials/debugging#the-ag-ui-dojo). |