You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,8 +50,8 @@ Designed to give your IDE or AI coding agent as much context as possible for aut
50
50
5.**Powerful Evals**:
51
51
Enables you to systematically test and [evaluate](https://ai.pydantic.dev/evals) the performance and accuracy of the agentic systems you build, and monitor the performance over time in Pydantic Logfire.
52
52
53
-
6.**MCP, A2A, and AG-UI**:
54
-
Integrates the [Model Context Protocol](https://ai.pydantic.dev/mcp/client), [Agent2Agent](https://ai.pydantic.dev/a2a), and [AG-UI](https://ai.pydantic.dev/ag-ui) standards to give your agent access to external tools and data, let it interoperate with other agents, and build interactive applications with streaming event-based communication.
53
+
6.**MCP, A2A, and UI**:
54
+
Integrates the [Model Context Protocol](https://ai.pydantic.dev/mcp/overview), [Agent2Agent](https://ai.pydantic.dev/a2a), and various [UI event stream](https://ai.pydantic.dev/ui/overview) standards to give your agent access to external tools and data, let it interoperate with other agents, and build interactive applications with streaming event-based communication.
55
55
56
56
7.**Human-in-the-Loop Tool Approval**:
57
57
Easily lets you flag that certain tool calls [require approval](https://ai.pydantic.dev/deferred-tools#human-in-the-loop-tool-approval) before they can proceed, possibly depending on tool call arguments, conversation history, or user preferences.
Copy file name to clipboardExpand all lines: docs/index.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,8 +25,8 @@ Designed to give your IDE or AI coding agent as much context as possible for aut
25
25
5.**Powerful Evals**:
26
26
Enables you to systematically test and [evaluate](evals.md) the performance and accuracy of the agentic systems you build, and monitor the performance over time in Pydantic Logfire.
27
27
28
-
6.**MCP, A2A, and AG-UI**:
29
-
Integrates the [Model Context Protocol](mcp/client.md), [Agent2Agent](a2a.md), and [AG-UI](ag-ui.md) standards to give your agent access to external tools and data, let it interoperate with other agents, and build interactive applications with streaming event-based communication.
28
+
6.**MCP, A2A, and UI**:
29
+
Integrates the [Model Context Protocol](mcp/overview.md), [Agent2Agent](a2a.md), and various [UI event stream](ui/overview.md) standards to give your agent access to external tools and data, let it interoperate with other agents, and build interactive applications with streaming event-based communication.
30
30
31
31
7.**Human-in-the-Loop Tool Approval**:
32
32
Easily lets you flag that certain tool calls [require approval](deferred-tools.md#human-in-the-loop-tool-approval) before they can proceed, possibly depending on tool call arguments, conversation history, or user preferences.
1. You can also use the [`AGUIAdapter.from_request()`][pydantic_ai.ui.ag_ui.AGUIAdapter.from_request] class method to build an adapter directly from a request.
81
-
2. You can also use the [`AGUIAdapter.run_stream_native()`][pydantic_ai.ui.ag_ui.AGUIAdapter.run_stream_native]method to run the agent and return a stream of Pydantic AI events instead of AG-UI events. These can then be transformed into AG-UI events using the [`AGUIAdapter.transform_stream()`][pydantic_ai.ui.ag_ui.AGUIAdapter.transform_stream] method.
82
-
3.The [`AGUIAdapter.encode_stream()`][pydantic_ai.ui.ag_ui.AGUIAdapter.encode_stream]method encodes the stream of AG-UI events as strings according to the accept header value. You can also use the [`AGUIAdapter.streaming_response()`][pydantic_ai.ui.ag_ui.AGUIAdapter.streaming_response] method to generate a streaming response directly from the AG-UI event stream returned by `run_stream()`.
82
+
1.[`AGUIAdapter.build_run_input()`][pydantic_ai.ui.ag_ui.AGUIAdapter.build_run_input] takes the request body as bytes and returns an AG-UI [`RunAgentInput`](https://docs.ag-ui.com/sdk/python/core/types#runagentinput) object. You can also use the [`AGUIAdapter.from_request()`][pydantic_ai.ui.ag_ui.AGUIAdapter.from_request] class method to build an adapter directly from a request.
83
+
2.[`AGUIAdapter.run_stream()`][pydantic_ai.ui.ag_ui.AGUIAdapter.run_stream] runs the agent and returns a stream of AG-UI events. It supports the same optional arguments as [`Agent.run_stream_events()`](../agents.md#running-agents), including `deps`. You can also use [`AGUIAdapter.run_stream_native()`][pydantic_ai.ui.ag_ui.AGUIAdapter.run_stream_native] to run the agent and return a stream of Pydantic AI events instead, which can then be transformed into AG-UI events using [`AGUIAdapter.transform_stream()`][pydantic_ai.ui.ag_ui.AGUIAdapter.transform_stream].
84
+
3.[`AGUIAdapter.encode_stream()`][pydantic_ai.ui.ag_ui.AGUIAdapter.encode_stream] encodes the stream of AG-UI events as strings according to the accept header value. You can also use [`AGUIAdapter.streaming_response()`][pydantic_ai.ui.ag_ui.AGUIAdapter.streaming_response] to generate a streaming response directly from the AG-UI event stream returned by `run_stream()`.
83
85
84
86
Since `app` is an ASGI application, it can be used with any ASGI server:
85
87
@@ -167,7 +169,7 @@ The integration provides full support for
167
169
real-time synchronization between agents and frontend applications.
168
170
169
171
In the example below we have document state which is shared between the UI and
170
-
server using the [`StateDeps`][pydantic_ai.ag_ui.StateDeps][dependencies type](./dependencies.md) that can be used to automatically
172
+
server using the [`StateDeps`][pydantic_ai.ag_ui.StateDeps][dependencies type](../dependencies.md) that can be used to automatically
171
173
validate state contained in [`RunAgentInput.state`](https://docs.ag-ui.com/sdk/js/core/types#runagentinput) using a Pydantic `BaseModel` specified as a generic parameter.
172
174
173
175
!!! note "Custom dependencies type with AG-UI state"
@@ -182,7 +184,7 @@ from pydantic import BaseModel
182
184
183
185
from pydantic_ai import Agent
184
186
from pydantic_ai.ui import StateDeps
185
-
from pydantic_ai.ui.ag_ui import AGUIApp
187
+
from pydantic_ai.ui.ag_ui.appimport AGUIApp
186
188
187
189
188
190
classDocumentState(BaseModel):
@@ -213,7 +215,7 @@ user experiences with frontend user interfaces.
213
215
### Events
214
216
215
217
Pydantic AI tools can send [AG-UI events](https://docs.ag-ui.com/concepts/events) simply by returning a
216
-
[`ToolReturn`](tools-advanced.md#advanced-tool-returns) object with a
218
+
[`ToolReturn`](../tools-advanced.md#advanced-tool-returns) object with a
217
219
[`BaseEvent`](https://docs.ag-ui.com/sdk/python/core/events#baseevent) (or a list of events) as `metadata`,
218
220
which allows for custom events and state updates.
219
221
@@ -223,7 +225,7 @@ from pydantic import BaseModel
223
225
224
226
from pydantic_ai import Agent, RunContext, ToolReturn
If you're building a chat app or other interactive frontend for an AI agent, your backend will need to receive agent run input (like a chat message or full message history) from the frontend, and will need to stream the [agent's events](../agents.md#streaming-all-events) (like text, thinking, and tool calls) to the frontend so that the user knows what's happening in real time.
4
+
5
+
While your frontend could use Pydantic AI's [`ModelRequest`s](../message-history.md) and [`AgentStreamEvent`s][pydantic_ai.messages.AgentStreamEvent] directly, you'll typically want to use a UI event stream protocol that's natively supported by your frontend framework.
6
+
7
+
Pydantic AI natively supports two UI event stream protocols:
8
+
9
+
-[Agent User Interaction (AG-UI) Protocol](./ag-ui.md)
10
+
-[Vercel AI Data Stream Protocol](./vercel-ai.md)
11
+
12
+
These integrations are implemented as subclasses of the abstract [`UIAdapter`][pydantic_ai.ui.UIAdapter] class, so they also serve as a reference for integrating with other UI event stream protocols.
13
+
14
+
## Usage
15
+
16
+
The protocol-specific [`UIAdapter`][pydantic_ai.ui.UIAdapter] subclass (i.e. [`AGUIAdapter`][pydantic_ai.ui.ag_ui.AGUIAdapter] or [`VercelAIAdapter`][pydantic_ai.ui.vercel_ai.VercelAIAdapter]) is responsible for transforming agent run input received from the frontend into arguments for [`Agent.run_stream_events()`](../agents.md#running-agents), running the agent, and then transforming Pydantic AI events into protocol-specific events. The event stream transformation is handled by a protocol-specific [`UIEventStream`][pydantic_ai.ui.UIEventStream] subclass, but you typically won't use this directly.
17
+
18
+
If you're using a Starlette-based web framework like FastAPI, you can use the [`UIAdapter.dispatch_request()`][pydantic_ai.ui.UIAdapter.dispatch_request] class method from an endpoint function to directly handle a request and return a streaming response of protocol-specific events. Besides the request, this method takes the agent and supports the same optional arguments as [`Agent.run_stream_events()`](../agents.md#running-agents), including `deps`.
19
+
20
+
!!! note
21
+
These examples use the `VercelAIAdapter`, but the same patterns apply to all `UIAdapter` subclasses.
22
+
23
+
```py {title="dispatch_request.py"}
24
+
from fastapi import FastAPI
25
+
from starlette.requests import Request
26
+
from starlette.responses import Response
27
+
28
+
from pydantic_ai import Agent
29
+
from pydantic_ai.ui.vercel_ai import VercelAIAdapter
If you're using a web framework not based on Starlette (e.g. Django or Flask) or want fine-grained control over the input or output, you can create and use a `UIAdapter` instance directly.
41
+
42
+
!!! note
43
+
This example uses FastAPI, but can be modified to work with any web framework.
44
+
45
+
```py {title="run_stream.py"}
46
+
import json
47
+
from http import HTTPStatus
48
+
49
+
from fastapi import FastAPI
50
+
from fastapi.requests import Request
51
+
from fastapi.responses import Response, StreamingResponse
52
+
from pydantic import ValidationError
53
+
54
+
from pydantic_ai import Agent
55
+
from pydantic_ai.ui importSSE_CONTENT_TYPE
56
+
from pydantic_ai.ui.vercel_ai import VercelAIAdapter
1.[`UIAdapter.build_run_input()`][pydantic_ai.ui.UIAdapter.build_run_input] takes the request body as bytes and returns a protocol-specific run input object. You can also use the [`UIAdapter.from_request()`][pydantic_ai.ui.UIAdapter.from_request] class method to build an adapter directly from a request.
83
+
2.[`UIAdapter.run_stream()`][pydantic_ai.ui.UIAdapter.run_stream] runs the agent and returns a stream of protocol-specific events. It supports the same optional arguments as [`Agent.run_stream_events()`](../agents.md#running-agents), including `deps`. You can also use [`UIAdapter.run_stream_native()`][pydantic_ai.ui.UIAdapter.run_stream_native] to run the agent and return a stream of Pydantic AI events instead, which can then be transformed into protocol-specific events using [`UIAdapter.transform_stream()`][pydantic_ai.ui.UIAdapter.transform_stream].
84
+
3.[`UIAdapter.encode_stream()`][pydantic_ai.ui.UIAdapter.encode_stream] encodes the stream of protocol-specific events as strings according to the accept header value. You can also use [`UIAdapter.streaming_response()`][pydantic_ai.ui.UIAdapter.streaming_response] to generate a streaming response directly from the protocol-specific event stream returned by `run_stream()`.
0 commit comments