11from __future__ import annotations
22
33from collections .abc import Callable , Mapping , Sequence
4- from typing import Any , Generic , Self
4+ from typing import Any , Generic
5+
6+ from typing_extensions import Self
57
68from .. import DeferredToolResults
79from ..agent import AbstractAgent
10+ from ..builtin_tools import AbstractBuiltinTool
811from ..messages import ModelMessage
912from ..models import KnownModelName , Model
1013from ..output import OutputDataT , OutputSpec
1114from ..settings import ModelSettings
1215from ..tools import AgentDepsT
1316from ..toolsets import AbstractToolset
1417from ..usage import RunUsage , UsageLimits
15- from .adapter import UIAdapter
18+ from .adapter import OnCompleteFunc , UIAdapter
1619
1720try :
1821 from starlette .applications import Starlette
@@ -36,7 +39,7 @@ def __init__(
3639 adapter_type : type [UIAdapter [Any , Any , Any , AgentDepsT , OutputDataT ]],
3740 agent : AbstractAgent [AgentDepsT , OutputDataT ],
3841 * ,
39- # Agent.iter parameters.
42+ # UIAdapter.dispatch_request parameters
4043 output_type : OutputSpec [Any ] | None = None ,
4144 message_history : Sequence [ModelMessage ] | None = None ,
4245 deferred_tool_results : DeferredToolResults | None = None ,
@@ -47,7 +50,9 @@ def __init__(
4750 usage : RunUsage | None = None ,
4851 infer_name : bool = True ,
4952 toolsets : Sequence [AbstractToolset [AgentDepsT ]] | None = None ,
50- # Starlette parameters.
53+ builtin_tools : Sequence [AbstractBuiltinTool ] | None = None ,
54+ on_complete : OnCompleteFunc [Any ] | None = None ,
55+ # Starlette parameters
5156 debug : bool = False ,
5257 routes : Sequence [BaseRoute ] | None = None ,
5358 middleware : Sequence [Middleware ] | None = None ,
@@ -58,12 +63,11 @@ def __init__(
5863 ) -> None :
5964 """An ASGI application that handles every request by running the agent and streaming the response.
6065
61- # TODO (DouweM): Docstring
62- Note that the `deps` will be the same for each request, with the exception of the AG-UI state that's
63- injected into the `state` field of a `deps` object that implements the [`StateHandler`][pydantic_ai.ag_ui.StateHandler] protocol.
66+ Note that the `deps` will be the same for each request, with the exception of the frontend state that's
67+ injected into the `state` field of a `deps` object that implements the [`StateHandler`][pydantic_ai.ui.StateHandler] protocol.
6468 To provide different `deps` for each request (e.g. based on the authenticated user),
65- use [`pydantic_ai.ag_ui.run_ag_ui `][pydantic_ai.ag_ui.run_ag_ui ] or
66- [`pydantic_ai.ag_ui.handle_ag_ui_request `][pydantic_ai.ag_ui.handle_ag_ui_request ] instead.
69+ use [`UIAdapter.run_stream() `][pydantic_ai.ui.UIAdapter.run_stream ] or
70+ [`UIAdapter.dispatch_request() `][pydantic_ai.ui.UIAdapter.dispatch_request ] instead.
6771
6872 Args:
6973 adapter_type: The type of the UI adapter to use.
@@ -81,6 +85,9 @@ def __init__(
8185 usage: Optional usage to start with, useful for resuming a conversation or agents used in tools.
8286 infer_name: Whether to try to infer the agent name from the call frame if it's not set.
8387 toolsets: Optional additional toolsets for this run.
88+ builtin_tools: Optional additional builtin tools for this run.
89+ on_complete: Optional callback function called when the agent run completes successfully.
90+ The callback receives the completed [`AgentRunResult`][pydantic_ai.agent.AgentRunResult] and can access `all_messages()` and other result data.
8491
8592 debug: Boolean indicating if debug tracebacks should be returned on errors.
8693 routes: A list of routes to serve incoming HTTP and WebSocket requests.
@@ -125,6 +132,8 @@ async def run_agent(request: Request) -> Response:
125132 usage = usage ,
126133 infer_name = infer_name ,
127134 toolsets = toolsets ,
135+ builtin_tools = builtin_tools ,
136+ on_complete = on_complete ,
128137 )
129138
130139 self .router .add_route ('/' , run_agent , methods = ['POST' ])
0 commit comments