pydantic_ai.ag_ui
Provides an AG-UI protocol adapter for the Pydantic AI agent.
This package provides seamless integration between pydantic-ai agents and ag-ui for building interactive AI applications with streaming event-based communication.
SSE_CONTENT_TYPE
module-attribute
SSE_CONTENT_TYPE = 'text/event-stream'
Content type header value for Server-Sent Events (SSE).
OnCompleteFunc
module-attribute
OnCompleteFunc: TypeAlias = (
Callable[[AgentRunResult[Any]], None]
| Callable[[AgentRunResult[Any]], Awaitable[None]]
| Callable[[AgentRunResult[Any]], AsyncIterator[EventT]]
)
Callback function type that receives the AgentRunResult of the completed run. Can be sync, async, or an async generator of protocol-specific events.
StateDeps
dataclass
Bases: Generic[StateT]
Dependency type that holds state.
This class is used to manage the state of an agent run. It allows setting
the state of the agent run with a specific type of state model, which must
be a subclass of BaseModel.
The state is set using the state setter by the Adapter when the run starts.
Implements the StateHandler protocol.
Source code in pydantic_ai_slim/pydantic_ai/ui/_adapter.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
StateHandler
Bases: Protocol
Protocol for state handlers in agent runs. Requires the class to be a dataclass with a state field.
Source code in pydantic_ai_slim/pydantic_ai/ui/_adapter.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
AGUIApp
Bases: Generic[AgentDepsT, OutputDataT], Starlette
ASGI application for running Pydantic AI agents with AG-UI protocol support.
Source code in pydantic_ai_slim/pydantic_ai/ui/ag_ui/app.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
__init__
deprecated
__init__(
agent: AbstractAgent[AgentDepsT, OutputDataT],
*,
ag_ui_version: str = DEFAULT_AG_UI_VERSION,
preserve_file_data: bool = False,
output_type: OutputSpec[Any] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | None
) = None,
conversation_id: str | None = None,
model: Model | KnownModelName | str | None = None,
deps: AgentDepsT = None,
model_settings: ModelSettings | None = None,
usage_limits: UsageLimits | None = None,
usage: RunUsage | None = None,
infer_name: bool = True,
toolsets: (
Sequence[AbstractToolset[AgentDepsT]] | None
) = None,
capabilities: (
Sequence[AbstractCapability[AgentDepsT]] | None
) = None,
on_complete: OnCompleteFunc[Any] | None = None,
debug: bool = False,
routes: Sequence[BaseRoute] | None = None,
middleware: Sequence[Middleware] | None = None,
exception_handlers: (
Mapping[Any, ExceptionHandler] | None
) = None,
on_startup: Sequence[Callable[[], Any]] | None = None,
on_shutdown: Sequence[Callable[[], Any]] | None = None,
lifespan: Lifespan[Self] | None = None,
**_deprecated_kwargs: Any
) -> None
Deprecated
AGUIApp is deprecated and will be removed in 2.0. Replace:
app = AGUIApp(agent)
with a bare Starlette app:
from starlette.applications import Starlette
from starlette.routing import Route
from pydantic_ai.ui.ag_ui import AGUIAdapter
async def run_agent(request):
return await AGUIAdapter.dispatch_request(request, agent=agent)
app = Starlette(routes=[Route("/", run_agent, methods=["POST"])])
See https://ai.pydantic.dev/ui/ag-ui/#migrating-from-deprecated-apis for full before/after examples.
An ASGI application that handles every request by running the agent and streaming the response.
Note that the deps will be the same for each request, with the exception of the frontend state that's
injected into the state field of a deps object that implements the StateHandler protocol.
To provide different deps for each request (e.g. based on the authenticated user),
use AGUIAdapter.run_stream() or
AGUIAdapter.dispatch_request() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
AbstractAgent[AgentDepsT, OutputDataT]
|
The agent to run. |
required |
ag_ui_version
|
str
|
AG-UI protocol version controlling thinking/reasoning event format. |
DEFAULT_AG_UI_VERSION
|
preserve_file_data
|
bool
|
Whether to preserve agent-generated files and uploaded files
in AG-UI message conversion. See |
False
|
output_type
|
OutputSpec[Any] | None
|
Custom output type to use for this run, |
None
|
message_history
|
Sequence[ModelMessage] | None
|
History of the conversation so far. |
None
|
deferred_tool_results
|
DeferredToolResults | None
|
Optional results for deferred tool calls in the message history. |
None
|
conversation_id
|
str | None
|
ID of the conversation this run belongs to. Pass |
None
|
model
|
Model | KnownModelName | str | None
|
Optional model to use for this run, required if |
None
|
deps
|
AgentDepsT
|
Optional dependencies to use for this run. |
None
|
model_settings
|
ModelSettings | None
|
Optional settings to use for this model's request. |
None
|
usage_limits
|
UsageLimits | None
|
Optional limits on model request count or token usage. |
None
|
usage
|
RunUsage | None
|
Optional usage to start with, useful for resuming a conversation or agents used in tools. |
None
|
infer_name
|
bool
|
Whether to try to infer the agent name from the call frame if it's not set. |
True
|
toolsets
|
Sequence[AbstractToolset[AgentDepsT]] | None
|
Optional additional toolsets for this run. |
None
|
capabilities
|
Sequence[AbstractCapability[AgentDepsT]] | None
|
Optional additional capabilities for every
run handled by this app, merged with the agent's configured capabilities. Use
|
None
|
on_complete
|
OnCompleteFunc[Any] | None
|
Optional callback function called when the agent run completes successfully.
The callback receives the completed |
None
|
debug
|
bool
|
Boolean indicating if debug tracebacks should be returned on errors. |
False
|
routes
|
Sequence[BaseRoute] | None
|
A list of routes to serve incoming HTTP and WebSocket requests. |
None
|
middleware
|
Sequence[Middleware] | None
|
A list of middleware to run for every request. A starlette application will always
automatically include two middleware classes. |
None
|
exception_handlers
|
Mapping[Any, ExceptionHandler] | None
|
A mapping of either integer status codes, or exception class types onto
callables which handle the exceptions. Exception handler callables should be of the form
|
None
|
on_startup
|
Sequence[Callable[[], Any]] | None
|
A list of callables to run on application startup. Startup handler callables do not take any arguments, and may be either standard functions, or async functions. |
None
|
on_shutdown
|
Sequence[Callable[[], Any]] | None
|
A list of callables to run on application shutdown. Shutdown handler callables do not take any arguments, and may be either standard functions, or async functions. |
None
|
lifespan
|
Lifespan[Self] | None
|
A lifespan context function, which can be used to perform startup and shutdown tasks.
This is a newer style that replaces the |
None
|
Source code in pydantic_ai_slim/pydantic_ai/ui/ag_ui/app.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
handle_ag_ui_request
async
handle_ag_ui_request(
agent: AbstractAgent[AgentDepsT, Any],
request: Request,
*,
ag_ui_version: str = DEFAULT_AG_UI_VERSION,
preserve_file_data: bool = False,
output_type: OutputSpec[Any] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | None
) = None,
conversation_id: str | None = None,
model: Model | KnownModelName | str | None = None,
deps: AgentDepsT = None,
model_settings: ModelSettings | None = None,
usage_limits: UsageLimits | None = None,
usage: RunUsage | None = None,
metadata: AgentMetadata[AgentDepsT] | None = None,
infer_name: bool = True,
toolsets: (
Sequence[AbstractToolset[AgentDepsT]] | None
) = None,
on_complete: OnCompleteFunc[BaseEvent] | None = None,
manage_system_prompt: Literal[
"server", "client"
] = "server",
allowed_file_url_schemes: frozenset[str] = frozenset(
{"http", "https"}
)
) -> Response
Handle an AG-UI request by running the agent and returning a streaming response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
AbstractAgent[AgentDepsT, Any]
|
The agent to run. |
required |
request
|
Request
|
The Starlette request (e.g. from FastAPI) containing the AG-UI run input. |
required |
ag_ui_version
|
str
|
AG-UI protocol version controlling thinking/reasoning event format. |
DEFAULT_AG_UI_VERSION
|
preserve_file_data
|
bool
|
Whether to preserve agent-generated files and uploaded files
in AG-UI message conversion. See |
False
|
output_type
|
OutputSpec[Any] | None
|
Custom output type to use for this run, |
None
|
message_history
|
Sequence[ModelMessage] | None
|
History of the conversation so far. |
None
|
deferred_tool_results
|
DeferredToolResults | None
|
Optional results for deferred tool calls in the message history. |
None
|
conversation_id
|
str | None
|
ID of the conversation this run belongs to. Pass |
None
|
model
|
Model | KnownModelName | str | None
|
Optional model to use for this run, required if |
None
|
deps
|
AgentDepsT
|
Optional dependencies to use for this run. |
None
|
model_settings
|
ModelSettings | None
|
Optional settings to use for this model's request. |
None
|
usage_limits
|
UsageLimits | None
|
Optional limits on model request count or token usage. |
None
|
usage
|
RunUsage | None
|
Optional usage to start with, useful for resuming a conversation or agents used in tools. |
None
|
metadata
|
AgentMetadata[AgentDepsT] | None
|
Optional metadata to attach to this run. Accepts a dictionary or a callable taking
|
None
|
infer_name
|
bool
|
Whether to try to infer the agent name from the call frame if it's not set. |
True
|
toolsets
|
Sequence[AbstractToolset[AgentDepsT]] | None
|
Optional additional toolsets for this run. |
None
|
on_complete
|
OnCompleteFunc[BaseEvent] | None
|
Optional callback function called when the agent run completes successfully.
The callback receives the completed |
None
|
manage_system_prompt
|
Literal['server', 'client']
|
Who owns the system prompt. See |
'server'
|
allowed_file_url_schemes
|
frozenset[str]
|
URL schemes allowed for file URL parts from the client. See
|
frozenset({'http', 'https'})
|
Returns:
| Type | Description |
|---|---|
Response
|
A streaming Starlette response with AG-UI protocol events. |
Source code in pydantic_ai_slim/pydantic_ai/ag_ui.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
run_ag_ui
run_ag_ui(
agent: AbstractAgent[AgentDepsT, Any],
run_input: RunAgentInput,
accept: str = SSE_CONTENT_TYPE,
*,
ag_ui_version: str = DEFAULT_AG_UI_VERSION,
preserve_file_data: bool = False,
output_type: OutputSpec[Any] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | None
) = None,
conversation_id: str | None = None,
model: Model | KnownModelName | str | None = None,
deps: AgentDepsT = None,
model_settings: ModelSettings | None = None,
usage_limits: UsageLimits | None = None,
usage: RunUsage | None = None,
metadata: AgentMetadata[AgentDepsT] | None = None,
infer_name: bool = True,
toolsets: (
Sequence[AbstractToolset[AgentDepsT]] | None
) = None,
on_complete: OnCompleteFunc[BaseEvent] | None = None,
manage_system_prompt: Literal[
"server", "client"
] = "server",
allowed_file_url_schemes: frozenset[str] = frozenset(
{"http", "https"}
)
) -> AsyncIterator[str]
Run the agent with the AG-UI run input and stream AG-UI protocol events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
AbstractAgent[AgentDepsT, Any]
|
The agent to run. |
required |
run_input
|
RunAgentInput
|
The AG-UI run input containing thread_id, run_id, messages, etc. |
required |
accept
|
str
|
The accept header value for the run. |
SSE_CONTENT_TYPE
|
ag_ui_version
|
str
|
AG-UI protocol version controlling thinking/reasoning event format. |
DEFAULT_AG_UI_VERSION
|
preserve_file_data
|
bool
|
Whether to preserve agent-generated files and uploaded files
in AG-UI message conversion. See |
False
|
output_type
|
OutputSpec[Any] | None
|
Custom output type to use for this run, |
None
|
message_history
|
Sequence[ModelMessage] | None
|
History of the conversation so far. |
None
|
deferred_tool_results
|
DeferredToolResults | None
|
Optional results for deferred tool calls in the message history. |
None
|
conversation_id
|
str | None
|
ID of the conversation this run belongs to. Pass |
None
|
model
|
Model | KnownModelName | str | None
|
Optional model to use for this run, required if |
None
|
deps
|
AgentDepsT
|
Optional dependencies to use for this run. |
None
|
model_settings
|
ModelSettings | None
|
Optional settings to use for this model's request. |
None
|
usage_limits
|
UsageLimits | None
|
Optional limits on model request count or token usage. |
None
|
usage
|
RunUsage | None
|
Optional usage to start with, useful for resuming a conversation or agents used in tools. |
None
|
metadata
|
AgentMetadata[AgentDepsT] | None
|
Optional metadata to attach to this run. Accepts a dictionary or a callable taking
|
None
|
infer_name
|
bool
|
Whether to try to infer the agent name from the call frame if it's not set. |
True
|
toolsets
|
Sequence[AbstractToolset[AgentDepsT]] | None
|
Optional additional toolsets for this run. |
None
|
on_complete
|
OnCompleteFunc[BaseEvent] | None
|
Optional callback function called when the agent run completes successfully.
The callback receives the completed |
None
|
manage_system_prompt
|
Literal['server', 'client']
|
Who owns the system prompt. See |
'server'
|
allowed_file_url_schemes
|
frozenset[str]
|
URL schemes allowed for file URL parts from the client. See
|
frozenset({'http', 'https'})
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[str]
|
Streaming event chunks encoded as strings according to the accept header value. |
Source code in pydantic_ai_slim/pydantic_ai/ag_ui.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | |