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
Content type header value for Server-Sent Events (SSE).
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/ag_ui.py
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
__init__
__init__(
agent: Agent[AgentDepsT, OutputDataT],
*,
output_type: OutputSpec[Any] | None = None,
model: Model | KnownModelName | str | None = None,
deps: AgentDepsT = None,
model_settings: ModelSettings | None = None,
usage_limits: UsageLimits | None = None,
usage: Usage | None = None,
infer_name: bool = True,
toolsets: (
Sequence[AbstractToolset[AgentDepsT]] | 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[AGUIApp[AgentDepsT, OutputDataT]] | None
) = None
) -> None
An ASGI application that handles every AG-UI request by running the agent.
Note that the deps
will be the same for each request, with the exception of the AG-UI 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 pydantic_ai.ag_ui.run_ag_ui
or
pydantic_ai.ag_ui.handle_ag_ui_request
instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
Agent[AgentDepsT, OutputDataT]
|
The agent to run. |
required |
output_type
|
OutputSpec[Any] | None
|
Custom output type to use for this run, |
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
|
Usage | 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
|
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[AGUIApp[AgentDepsT, OutputDataT]] | 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/ag_ui.py
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
handle_ag_ui_request
async
handle_ag_ui_request(
agent: Agent[AgentDepsT, Any],
request: Request,
*,
output_type: OutputSpec[Any] | None = None,
model: Model | KnownModelName | str | None = None,
deps: AgentDepsT = None,
model_settings: ModelSettings | None = None,
usage_limits: UsageLimits | None = None,
usage: Usage | None = None,
infer_name: bool = True,
toolsets: (
Sequence[AbstractToolset[AgentDepsT]] | None
) = None
) -> Response
Handle an AG-UI request by running the agent and returning a streaming response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
Agent[AgentDepsT, Any]
|
The agent to run. |
required |
request
|
Request
|
The Starlette request (e.g. from FastAPI) containing the AG-UI run input. |
required |
output_type
|
OutputSpec[Any] | None
|
Custom output type to use for this run, |
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
|
Usage | 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
|
Returns:
Type | Description |
---|---|
Response
|
A streaming Starlette response with AG-UI protocol events. |
Source code in pydantic_ai_slim/pydantic_ai/ag_ui.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|
run_ag_ui
async
run_ag_ui(
agent: Agent[AgentDepsT, Any],
run_input: RunAgentInput,
accept: str = SSE_CONTENT_TYPE,
*,
output_type: OutputSpec[Any] | None = None,
model: Model | KnownModelName | str | None = None,
deps: AgentDepsT = None,
model_settings: ModelSettings | None = None,
usage_limits: UsageLimits | None = None,
usage: Usage | None = None,
infer_name: bool = True,
toolsets: (
Sequence[AbstractToolset[AgentDepsT]] | None
) = None
) -> AsyncIterator[str]
Run the agent with the AG-UI run input and stream AG-UI protocol events.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
Agent[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
|
output_type
|
OutputSpec[Any] | None
|
Custom output type to use for this run, |
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
|
Usage | 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
|
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
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
|
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/ag_ui.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
|
state
property
writable
state: State
Get the current state of the agent run.
StateDeps
dataclass
Bases: Generic[StateT]
Provides AG-UI state management.
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/ag_ui.py
618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
|