pydantic_ai.durable_exec
TemporalAgent
Bases: WrapperAgent[AgentDepsT, OutputDataT]
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_agent.py
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 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 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 266 267 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 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 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 | |
__init__
__init__(
wrapped: AbstractAgent[AgentDepsT, OutputDataT],
*,
name: str | None = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
activity_config: ActivityConfig | None = None,
model_activity_config: ActivityConfig | None = None,
toolset_activity_config: (
dict[str, ActivityConfig] | None
) = None,
tool_activity_config: (
dict[
str, dict[str, ActivityConfig | Literal[False]]
]
| None
) = None,
run_context_type: type[
TemporalRunContext[AgentDepsT]
] = TemporalRunContext[AgentDepsT],
temporalize_toolset_func: Callable[
[
AbstractToolset[AgentDepsT],
str,
ActivityConfig,
dict[str, ActivityConfig | Literal[False]],
type[AgentDepsT],
type[TemporalRunContext[AgentDepsT]],
],
AbstractToolset[AgentDepsT],
] = temporalize_toolset
)
Wrap an agent to enable it to be used inside a Temporal workflow, by automatically offloading model requests, tool calls, and MCP server communication to Temporal activities.
After wrapping, the original agent can still be used as normal outside of the Temporal workflow, but any changes to its model or toolsets after wrapping will not be reflected in the durable agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wrapped
|
AbstractAgent[AgentDepsT, OutputDataT]
|
The agent to wrap. |
required |
name
|
str | None
|
Optional unique agent name to use in the Temporal activities' names. If not provided, the agent's |
None
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use instead of the one set on the wrapped agent. |
None
|
activity_config
|
ActivityConfig | None
|
The base Temporal activity config to use for all activities. If no config is provided, a |
None
|
model_activity_config
|
ActivityConfig | None
|
The Temporal activity config to use for model request activities. This is merged with the base activity config. |
None
|
toolset_activity_config
|
dict[str, ActivityConfig] | None
|
The Temporal activity config to use for get-tools and call-tool activities for specific toolsets identified by ID. This is merged with the base activity config. |
None
|
tool_activity_config
|
dict[str, dict[str, ActivityConfig | Literal[False]]] | None
|
The Temporal activity config to use for specific tool call activities identified by toolset ID and tool name.
This is merged with the base and toolset-specific activity configs.
If a tool does not use IO, you can specify |
None
|
run_context_type
|
type[TemporalRunContext[AgentDepsT]]
|
The |
TemporalRunContext[AgentDepsT]
|
temporalize_toolset_func
|
Callable[[AbstractToolset[AgentDepsT], str, ActivityConfig, dict[str, ActivityConfig | Literal[False]], type[AgentDepsT], type[TemporalRunContext[AgentDepsT]]], AbstractToolset[AgentDepsT]]
|
Optional function to use to prepare "leaf" toolsets (i.e. those that implement their own tool listing and calling) for Temporal by wrapping them in a |
temporalize_toolset
|
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_agent.py
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 184 185 186 187 188 189 190 191 | |
run
async
run(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[OutputDataT]
run(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[RunOutputDataT]
run(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
**_deprecated_kwargs: Never
) -> AgentRunResult[Any]
Run the agent with a user prompt in async mode.
This method builds an internal agent graph (using system prompts, tools and result schemas) and then runs the graph to completion. The result of the run is returned.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
async def main():
agent_run = await agent.run('What is the capital of France?')
print(agent_run.output)
#> The capital of France is Paris.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use for this run. |
None
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AgentRunResult[Any]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_agent.py
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 | |
run_sync
run_sync(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[OutputDataT]
run_sync(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[RunOutputDataT]
run_sync(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
**_deprecated_kwargs: Never
) -> AgentRunResult[Any]
Synchronously run the agent with a user prompt.
This is a convenience method that wraps self.run with loop.run_until_complete(...).
You therefore can't use this method inside async code or if there's an active event loop.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
result_sync = agent.run_sync('What is the capital of Italy?')
print(result_sync.output)
#> The capital of Italy is Rome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use for this run. |
None
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AgentRunResult[Any]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_agent.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 | |
run_stream
async
run_stream(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AbstractAsyncContextManager[
StreamedRunResult[AgentDepsT, OutputDataT]
]
run_stream(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AbstractAsyncContextManager[
StreamedRunResult[AgentDepsT, RunOutputDataT]
]
run_stream(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
**_deprecated_kwargs: Never
) -> AsyncIterator[StreamedRunResult[AgentDepsT, Any]]
Run the agent with a user prompt in async mode, returning a streamed response.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
async def main():
async with agent.run_stream('What is the capital of the UK?') as response:
print(await response.get_output())
#> The capital of the UK is London.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use for this run. It will receive all the events up until the final result is found, which you can then read or stream from inside the context manager. |
None
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[StreamedRunResult[AgentDepsT, Any]]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_agent.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | |
run_stream_events
run_stream_events(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AsyncIterator[
AgentStreamEvent | AgentRunResultEvent[OutputDataT]
]
run_stream_events(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AsyncIterator[
AgentStreamEvent | AgentRunResultEvent[RunOutputDataT]
]
run_stream_events(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AsyncIterator[
AgentStreamEvent | AgentRunResultEvent[Any]
]
Run the agent with a user prompt in async mode and stream events from the run.
This is a convenience method that wraps self.run and
uses the event_stream_handler kwarg to get a stream of events from the run.
Example:
from pydantic_ai import Agent, AgentRunResultEvent, AgentStreamEvent
agent = Agent('openai:gpt-4o')
async def main():
events: list[AgentStreamEvent | AgentRunResultEvent] = []
async for event in agent.run_stream_events('What is the capital of France?'):
events.append(event)
print(events)
'''
[
PartStartEvent(index=0, part=TextPart(content='The capital of ')),
FinalResultEvent(tool_name=None, tool_call_id=None),
PartDeltaEvent(index=0, delta=TextPartDelta(content_delta='France is Paris. ')),
AgentRunResultEvent(
result=AgentRunResult(output='The capital of France is Paris. ')
),
]
'''
Arguments are the same as for self.run,
except that event_stream_handler is now allowed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[AgentStreamEvent | AgentRunResultEvent[Any]]
|
An async iterable of stream events |
AsyncIterator[AgentStreamEvent | AgentRunResultEvent[Any]]
|
run result. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_agent.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 | |
iter
async
iter(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
toolsets: (
Sequence[AbstractToolset[AgentDepsT]] | None
) = None,
**_deprecated_kwargs: Never
) -> AbstractAsyncContextManager[
AgentRun[AgentDepsT, OutputDataT]
]
iter(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
**_deprecated_kwargs: Never
) -> AbstractAsyncContextManager[
AgentRun[AgentDepsT, RunOutputDataT]
]
iter(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
**_deprecated_kwargs: Never
) -> AsyncIterator[AgentRun[AgentDepsT, Any]]
A contextmanager which can be used to iterate over the agent graph's nodes as they are executed.
This method builds an internal agent graph (using system prompts, tools and output schemas) and then returns an
AgentRun object. The AgentRun can be used to async-iterate over the nodes of the graph as they are
executed. This is the API to use if you want to consume the outputs coming from each LLM model response, or the
stream of events coming from the execution of tools.
The AgentRun also provides methods to access the full message history, new messages, and usage statistics,
and the final result of the run once it has completed.
For more details, see the documentation of AgentRun.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
async def main():
nodes = []
async with agent.iter('What is the capital of France?') as agent_run:
async for node in agent_run:
nodes.append(node)
print(nodes)
'''
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
system_prompt_dynamic_functions={},
),
ModelRequestNode(
request=ModelRequest(
parts=[
UserPromptPart(
content='What is the capital of France?',
timestamp=datetime.datetime(...),
)
]
)
),
CallToolsNode(
model_response=ModelResponse(
parts=[TextPart(content='The capital of France is Paris.')],
usage=RequestUsage(input_tokens=56, output_tokens=7),
model_name='gpt-4o',
timestamp=datetime.datetime(...),
)
),
End(data=FinalResult(output='The capital of France is Paris.')),
]
'''
print(agent_run.result.output)
#> The capital of France is Paris.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[AgentRun[AgentDepsT, Any]]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_agent.py
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 | |
override
override(
*,
name: str | Unset = UNSET,
deps: AgentDepsT | Unset = UNSET,
model: Model | KnownModelName | str | Unset = UNSET,
toolsets: (
Sequence[AbstractToolset[AgentDepsT]] | Unset
) = UNSET,
tools: (
Sequence[
Tool[AgentDepsT]
| ToolFuncEither[AgentDepsT, ...]
]
| Unset
) = UNSET,
instructions: Instructions[AgentDepsT] | Unset = UNSET
) -> Iterator[None]
Context manager to temporarily override agent name, dependencies, model, toolsets, tools, or instructions.
This is particularly useful when testing. You can find an example of this here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | Unset
|
The name to use instead of the name passed to the agent constructor and agent run. |
UNSET
|
deps
|
AgentDepsT | Unset
|
The dependencies to use instead of the dependencies passed to the agent run. |
UNSET
|
model
|
Model | KnownModelName | str | Unset
|
The model to use instead of the model passed to the agent run. |
UNSET
|
toolsets
|
Sequence[AbstractToolset[AgentDepsT]] | Unset
|
The toolsets to use instead of the toolsets passed to the agent constructor and agent run. |
UNSET
|
tools
|
Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] | Unset
|
The tools to use instead of the tools registered with the agent. |
UNSET
|
instructions
|
Instructions[AgentDepsT] | Unset
|
The instructions to use instead of the instructions registered with the agent. |
UNSET
|
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_agent.py
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 | |
LogfirePlugin
Bases: Plugin
Temporal client plugin for Logfire.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_logfire.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
TemporalRunContext
Bases: RunContext[AgentDepsT]
The RunContext subclass to use to serialize and deserialize the run context for use inside a Temporal activity.
By default, only the deps, retries, tool_call_id, tool_name, tool_call_approved, retry, max_retries and run_step attributes will be available.
To make another attribute available, create a TemporalRunContext subclass with a custom serialize_run_context class method that returns a dictionary that includes the attribute and pass it to TemporalAgent.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_run_context.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
serialize_run_context
classmethod
serialize_run_context(
ctx: RunContext[Any],
) -> dict[str, Any]
Serialize the run context to a dict[str, Any].
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_run_context.py
36 37 38 39 40 41 42 43 44 45 46 47 | |
deserialize_run_context
classmethod
deserialize_run_context(
ctx: dict[str, Any], deps: AgentDepsT
) -> TemporalRunContext[AgentDepsT]
Deserialize the run context from a dict[str, Any].
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_run_context.py
49 50 51 52 | |
PydanticAIPlugin
Bases: Plugin, Plugin
Temporal client and worker plugin for Pydantic AI.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/__init__.py
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 | |
AgentPlugin
Bases: Plugin
Temporal worker plugin for a specific Pydantic AI agent.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/temporal/__init__.py
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 | |
DBOSAgent
Bases: WrapperAgent[AgentDepsT, OutputDataT], DBOSConfiguredInstance
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_agent.py
38 39 40 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 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 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 266 267 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 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 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 | |
__init__
__init__(
wrapped: AbstractAgent[AgentDepsT, OutputDataT],
*,
name: str | None = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
mcp_step_config: StepConfig | None = None,
model_step_config: StepConfig | None = None
)
Wrap an agent to enable it with DBOS durable workflows, by automatically offloading model requests, tool calls, and MCP server communication to DBOS steps.
After wrapping, the original agent can still be used as normal outside of the DBOS workflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wrapped
|
AbstractAgent[AgentDepsT, OutputDataT]
|
The agent to wrap. |
required |
name
|
str | None
|
Optional unique agent name to use as the DBOS configured instance name. If not provided, the agent's |
None
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use instead of the one set on the wrapped agent. |
None
|
mcp_step_config
|
StepConfig | None
|
The base DBOS step config to use for MCP server steps. If no config is provided, use the default settings of DBOS. |
None
|
model_step_config
|
StepConfig | None
|
The DBOS step config to use for model request steps. If no config is provided, use the default settings of DBOS. |
None
|
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_agent.py
40 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 184 185 186 | |
run
async
run(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[OutputDataT]
run(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[RunOutputDataT]
run(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
**_deprecated_kwargs: Never
) -> AgentRunResult[Any]
Run the agent with a user prompt in async mode.
This method builds an internal agent graph (using system prompts, tools and result schemas) and then runs the graph to completion. The result of the run is returned.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
async def main():
agent_run = await agent.run('What is the capital of France?')
print(agent_run.output)
#> The capital of France is Paris.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AgentRunResult[Any]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_agent.py
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 | |
run_sync
run_sync(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[OutputDataT]
run_sync(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[RunOutputDataT]
run_sync(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
**_deprecated_kwargs: Never
) -> AgentRunResult[Any]
Synchronously run the agent with a user prompt.
This is a convenience method that wraps self.run with loop.run_until_complete(...).
You therefore can't use this method inside async code or if there's an active event loop.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
result_sync = agent.run_sync('What is the capital of Italy?')
print(result_sync.output)
#> The capital of Italy is Rome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AgentRunResult[Any]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_agent.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | |
run_stream
async
run_stream(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AbstractAsyncContextManager[
StreamedRunResult[AgentDepsT, OutputDataT]
]
run_stream(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AbstractAsyncContextManager[
StreamedRunResult[AgentDepsT, RunOutputDataT]
]
run_stream(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
**_deprecated_kwargs: Never
) -> AsyncIterator[StreamedRunResult[AgentDepsT, Any]]
Run the agent with a user prompt in async mode, returning a streamed response.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
async def main():
async with agent.run_stream('What is the capital of the UK?') as response:
print(await response.get_output())
#> The capital of the UK is London.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use for this run. It will receive all the events up until the final result is found, which you can then read or stream from inside the context manager. |
None
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[StreamedRunResult[AgentDepsT, Any]]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_agent.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
run_stream_events
run_stream_events(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AsyncIterator[
AgentStreamEvent | AgentRunResultEvent[OutputDataT]
]
run_stream_events(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AsyncIterator[
AgentStreamEvent | AgentRunResultEvent[RunOutputDataT]
]
run_stream_events(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AsyncIterator[
AgentStreamEvent | AgentRunResultEvent[Any]
]
Run the agent with a user prompt in async mode and stream events from the run.
This is a convenience method that wraps self.run and
uses the event_stream_handler kwarg to get a stream of events from the run.
Example:
from pydantic_ai import Agent, AgentRunResultEvent, AgentStreamEvent
agent = Agent('openai:gpt-4o')
async def main():
events: list[AgentStreamEvent | AgentRunResultEvent] = []
async for event in agent.run_stream_events('What is the capital of France?'):
events.append(event)
print(events)
'''
[
PartStartEvent(index=0, part=TextPart(content='The capital of ')),
FinalResultEvent(tool_name=None, tool_call_id=None),
PartDeltaEvent(index=0, delta=TextPartDelta(content_delta='France is Paris. ')),
AgentRunResultEvent(
result=AgentRunResult(output='The capital of France is Paris. ')
),
]
'''
Arguments are the same as for self.run,
except that event_stream_handler is now allowed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[AgentStreamEvent | AgentRunResultEvent[Any]]
|
An async iterable of stream events |
AsyncIterator[AgentStreamEvent | AgentRunResultEvent[Any]]
|
run result. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_agent.py
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 | |
iter
async
iter(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
**_deprecated_kwargs: Never
) -> AbstractAsyncContextManager[
AgentRun[AgentDepsT, OutputDataT]
]
iter(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
**_deprecated_kwargs: Never
) -> AbstractAsyncContextManager[
AgentRun[AgentDepsT, RunOutputDataT]
]
iter(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
**_deprecated_kwargs: Never
) -> AsyncIterator[AgentRun[AgentDepsT, Any]]
A contextmanager which can be used to iterate over the agent graph's nodes as they are executed.
This method builds an internal agent graph (using system prompts, tools and output schemas) and then returns an
AgentRun object. The AgentRun can be used to async-iterate over the nodes of the graph as they are
executed. This is the API to use if you want to consume the outputs coming from each LLM model response, or the
stream of events coming from the execution of tools.
The AgentRun also provides methods to access the full message history, new messages, and usage statistics,
and the final result of the run once it has completed.
For more details, see the documentation of AgentRun.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
async def main():
nodes = []
async with agent.iter('What is the capital of France?') as agent_run:
async for node in agent_run:
nodes.append(node)
print(nodes)
'''
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
system_prompt_dynamic_functions={},
),
ModelRequestNode(
request=ModelRequest(
parts=[
UserPromptPart(
content='What is the capital of France?',
timestamp=datetime.datetime(...),
)
]
)
),
CallToolsNode(
model_response=ModelResponse(
parts=[TextPart(content='The capital of France is Paris.')],
usage=RequestUsage(input_tokens=56, output_tokens=7),
model_name='gpt-4o',
timestamp=datetime.datetime(...),
)
),
End(data=FinalResult(output='The capital of France is Paris.')),
]
'''
print(agent_run.result.output)
#> The capital of France is Paris.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[AgentRun[AgentDepsT, Any]]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_agent.py
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 | |
override
override(
*,
name: str | Unset = UNSET,
deps: AgentDepsT | Unset = UNSET,
model: Model | KnownModelName | str | Unset = UNSET,
toolsets: (
Sequence[AbstractToolset[AgentDepsT]] | Unset
) = UNSET,
tools: (
Sequence[
Tool[AgentDepsT]
| ToolFuncEither[AgentDepsT, ...]
]
| Unset
) = UNSET,
instructions: Instructions[AgentDepsT] | Unset = UNSET
) -> Iterator[None]
Context manager to temporarily override agent name, dependencies, model, toolsets, tools, or instructions.
This is particularly useful when testing. You can find an example of this here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | Unset
|
The name to use instead of the name passed to the agent constructor and agent run. |
UNSET
|
deps
|
AgentDepsT | Unset
|
The dependencies to use instead of the dependencies passed to the agent run. |
UNSET
|
model
|
Model | KnownModelName | str | Unset
|
The model to use instead of the model passed to the agent run. |
UNSET
|
toolsets
|
Sequence[AbstractToolset[AgentDepsT]] | Unset
|
The toolsets to use instead of the toolsets passed to the agent constructor and agent run. |
UNSET
|
tools
|
Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] | Unset
|
The tools to use instead of the tools registered with the agent. |
UNSET
|
instructions
|
Instructions[AgentDepsT] | Unset
|
The instructions to use instead of the instructions registered with the agent. |
UNSET
|
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_agent.py
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 | |
DBOSMCPServer
Bases: WrapperToolset[AgentDepsT], ABC
A wrapper for MCPServer that integrates with DBOS, turning call_tool and get_tools to DBOS steps.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_mcp_server.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 | |
DBOSModel
Bases: WrapperModel
A wrapper for Model that integrates with DBOS, turning request and request_stream to DBOS steps.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_model.py
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 | |
StepConfig
Bases: TypedDict
Configuration for a step in the DBOS workflow.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_utils.py
4 5 6 7 8 9 10 | |
PrefectAgent
Bases: WrapperAgent[AgentDepsT, OutputDataT]
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_agent.py
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 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 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 266 267 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 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 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 | |
__init__
__init__(
wrapped: AbstractAgent[AgentDepsT, OutputDataT],
*,
name: str | None = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
mcp_task_config: TaskConfig | None = None,
model_task_config: TaskConfig | None = None,
tool_task_config: TaskConfig | None = None,
tool_task_config_by_name: (
dict[str, TaskConfig | None] | None
) = None,
event_stream_handler_task_config: (
TaskConfig | None
) = None,
prefectify_toolset_func: Callable[
[
AbstractToolset[AgentDepsT],
TaskConfig,
TaskConfig,
dict[str, TaskConfig | None],
],
AbstractToolset[AgentDepsT],
] = prefectify_toolset
)
Wrap an agent to enable it with Prefect durable flows, by automatically offloading model requests, tool calls, and MCP server communication to Prefect tasks.
After wrapping, the original agent can still be used as normal outside of the Prefect flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wrapped
|
AbstractAgent[AgentDepsT, OutputDataT]
|
The agent to wrap. |
required |
name
|
str | None
|
Optional unique agent name to use as the Prefect flow name prefix. If not provided, the agent's |
None
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use instead of the one set on the wrapped agent. |
None
|
mcp_task_config
|
TaskConfig | None
|
The base Prefect task config to use for MCP server tasks. If no config is provided, use the default settings of Prefect. |
None
|
model_task_config
|
TaskConfig | None
|
The Prefect task config to use for model request tasks. If no config is provided, use the default settings of Prefect. |
None
|
tool_task_config
|
TaskConfig | None
|
The default Prefect task config to use for tool calls. If no config is provided, use the default settings of Prefect. |
None
|
tool_task_config_by_name
|
dict[str, TaskConfig | None] | None
|
Per-tool task configuration. Keys are tool names, values are TaskConfig or None (None disables task wrapping for that tool). |
None
|
event_stream_handler_task_config
|
TaskConfig | None
|
The Prefect task config to use for the event stream handler task. If no config is provided, use the default settings of Prefect. |
None
|
prefectify_toolset_func
|
Callable[[AbstractToolset[AgentDepsT], TaskConfig, TaskConfig, dict[str, TaskConfig | None]], AbstractToolset[AgentDepsT]]
|
Optional function to use to prepare toolsets for Prefect by wrapping them in a |
prefectify_toolset
|
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_agent.py
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 | |
run
async
run(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[OutputDataT]
run(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[RunOutputDataT]
run(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
**_deprecated_kwargs: Never
) -> AgentRunResult[Any]
Run the agent with a user prompt in async mode.
This method builds an internal agent graph (using system prompts, tools and result schemas) and then runs the graph to completion. The result of the run is returned.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
async def main():
agent_run = await agent.run('What is the capital of France?')
print(agent_run.output)
#> The capital of France is Paris.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use for this run. |
None
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AgentRunResult[Any]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_agent.py
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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
run_sync
run_sync(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[OutputDataT]
run_sync(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AgentRunResult[RunOutputDataT]
run_sync(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
**_deprecated_kwargs: Never
) -> AgentRunResult[Any]
Synchronously run the agent with a user prompt.
This is a convenience method that wraps self.run with loop.run_until_complete(...).
You therefore can't use this method inside async code or if there's an active event loop.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
result_sync = agent.run_sync('What is the capital of Italy?')
print(result_sync.output)
#> The capital of Italy is Rome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use for this run. |
None
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AgentRunResult[Any]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_agent.py
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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |
run_stream
async
run_stream(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AbstractAsyncContextManager[
StreamedRunResult[AgentDepsT, OutputDataT]
]
run_stream(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None
) -> AbstractAsyncContextManager[
StreamedRunResult[AgentDepsT, RunOutputDataT]
]
run_stream(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None,
event_stream_handler: (
EventStreamHandler[AgentDepsT] | None
) = None,
**_deprecated_kwargs: Never
) -> AsyncIterator[StreamedRunResult[AgentDepsT, Any]]
Run the agent with a user prompt in async mode, returning a streamed response.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
async def main():
async with agent.run_stream('What is the capital of the UK?') as response:
print(await response.get_output())
#> The capital of the UK is London.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
event_stream_handler
|
EventStreamHandler[AgentDepsT] | None
|
Optional event stream handler to use for this run. It will receive all the events up until the final result is found, which you can then read or stream from inside the context manager. |
None
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[StreamedRunResult[AgentDepsT, Any]]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_agent.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | |
run_stream_events
run_stream_events(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AsyncIterator[
AgentStreamEvent | AgentRunResultEvent[OutputDataT]
]
run_stream_events(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AsyncIterator[
AgentStreamEvent | AgentRunResultEvent[RunOutputDataT]
]
run_stream_events(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AsyncIterator[
AgentStreamEvent | AgentRunResultEvent[Any]
]
Run the agent with a user prompt in async mode and stream events from the run.
This is a convenience method that wraps self.run and
uses the event_stream_handler kwarg to get a stream of events from the run.
Example:
from pydantic_ai import Agent, AgentRunResultEvent, AgentStreamEvent
agent = Agent('openai:gpt-4o')
async def main():
events: list[AgentStreamEvent | AgentRunResultEvent] = []
async for event in agent.run_stream_events('What is the capital of France?'):
events.append(event)
print(events)
'''
[
PartStartEvent(index=0, part=TextPart(content='The capital of ')),
FinalResultEvent(tool_name=None, tool_call_id=None),
PartDeltaEvent(index=0, delta=TextPartDelta(content_delta='France is Paris. ')),
AgentRunResultEvent(
result=AgentRunResult(output='The capital of France is Paris. ')
),
]
'''
Arguments are the same as for self.run,
except that event_stream_handler is now allowed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[AgentStreamEvent | AgentRunResultEvent[Any]]
|
An async iterable of stream events |
AsyncIterator[AgentStreamEvent | AgentRunResultEvent[Any]]
|
run result. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_agent.py
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 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 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 | |
iter
async
iter(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AbstractAsyncContextManager[
AgentRun[AgentDepsT, OutputDataT]
]
iter(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AbstractAsyncContextManager[
AgentRun[AgentDepsT, RunOutputDataT]
]
iter(
user_prompt: str | Sequence[UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT] | None = None,
message_history: Sequence[ModelMessage] | None = None,
deferred_tool_results: (
DeferredToolResults | 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,
builtin_tools: (
Sequence[AbstractBuiltinTool] | None
) = None
) -> AsyncIterator[AgentRun[AgentDepsT, Any]]
A contextmanager which can be used to iterate over the agent graph's nodes as they are executed.
This method builds an internal agent graph (using system prompts, tools and output schemas) and then returns an
AgentRun object. The AgentRun can be used to async-iterate over the nodes of the graph as they are
executed. This is the API to use if you want to consume the outputs coming from each LLM model response, or the
stream of events coming from the execution of tools.
The AgentRun also provides methods to access the full message history, new messages, and usage statistics,
and the final result of the run once it has completed.
For more details, see the documentation of AgentRun.
Example:
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
async def main():
nodes = []
async with agent.iter('What is the capital of France?') as agent_run:
async for node in agent_run:
nodes.append(node)
print(nodes)
'''
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
system_prompt_dynamic_functions={},
),
ModelRequestNode(
request=ModelRequest(
parts=[
UserPromptPart(
content='What is the capital of France?',
timestamp=datetime.datetime(...),
)
]
)
),
CallToolsNode(
model_response=ModelResponse(
parts=[TextPart(content='The capital of France is Paris.')],
usage=RequestUsage(input_tokens=56, output_tokens=7),
model_name='gpt-4o',
timestamp=datetime.datetime(...),
)
),
End(data=FinalResult(output='The capital of France is Paris.')),
]
'''
print(agent_run.result.output)
#> The capital of France is Paris.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str | Sequence[UserContent] | None
|
User input to start/continue the conversation. |
None
|
output_type
|
OutputSpec[RunOutputDataT] | 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
|
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
|
builtin_tools
|
Sequence[AbstractBuiltinTool] | None
|
Optional additional builtin tools for this run. |
None
|
Returns:
| Type | Description |
|---|---|
AsyncIterator[AgentRun[AgentDepsT, Any]]
|
The result of the run. |
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_agent.py
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 | |
override
override(
*,
name: str | Unset = UNSET,
deps: AgentDepsT | Unset = UNSET,
model: Model | KnownModelName | str | Unset = UNSET,
toolsets: (
Sequence[AbstractToolset[AgentDepsT]] | Unset
) = UNSET,
tools: (
Sequence[
Tool[AgentDepsT]
| ToolFuncEither[AgentDepsT, ...]
]
| Unset
) = UNSET,
instructions: Instructions[AgentDepsT] | Unset = UNSET
) -> Iterator[None]
Context manager to temporarily override agent dependencies, model, toolsets, tools, or instructions.
This is particularly useful when testing. You can find an example of this here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | Unset
|
The name to use instead of the name passed to the agent constructor and agent run. |
UNSET
|
deps
|
AgentDepsT | Unset
|
The dependencies to use instead of the dependencies passed to the agent run. |
UNSET
|
model
|
Model | KnownModelName | str | Unset
|
The model to use instead of the model passed to the agent run. |
UNSET
|
toolsets
|
Sequence[AbstractToolset[AgentDepsT]] | Unset
|
The toolsets to use instead of the toolsets passed to the agent constructor and agent run. |
UNSET
|
tools
|
Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] | Unset
|
The tools to use instead of the tools registered with the agent. |
UNSET
|
instructions
|
Instructions[AgentDepsT] | Unset
|
The instructions to use instead of the instructions registered with the agent. |
UNSET
|
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_agent.py
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 | |
PrefectFunctionToolset
Bases: PrefectWrapperToolset[AgentDepsT]
A wrapper for FunctionToolset that integrates with Prefect, turning tool calls into Prefect tasks.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_function_toolset.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
call_tool
async
call_tool(
name: str,
tool_args: dict[str, Any],
ctx: RunContext[AgentDepsT],
tool: ToolsetTool[AgentDepsT],
) -> Any
Call a tool, wrapped as a Prefect task with a descriptive name.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_function_toolset.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
PrefectMCPServer
Bases: PrefectWrapperToolset[AgentDepsT], ABC
A wrapper for MCPServer that integrates with Prefect, turning call_tool and get_tools into Prefect tasks.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_mcp_server.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
call_tool
async
call_tool(
name: str,
tool_args: dict[str, Any],
ctx: RunContext[AgentDepsT],
tool: ToolsetTool[AgentDepsT],
) -> ToolResult
Call an MCP tool, wrapped as a Prefect task with a descriptive name.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_mcp_server.py
50 51 52 53 54 55 56 57 58 59 60 | |
PrefectModel
Bases: WrapperModel
A wrapper for Model that integrates with Prefect, turning request and request_stream into Prefect tasks.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_model.py
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 | |
request
async
request(
messages: list[ModelMessage],
model_settings: ModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> ModelResponse
Make a model request, wrapped as a Prefect task when in a flow.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_model.py
113 114 115 116 117 118 119 120 121 122 | |
request_stream
async
request_stream(
messages: list[ModelMessage],
model_settings: ModelSettings | None,
model_request_parameters: ModelRequestParameters,
run_context: RunContext[Any] | None = None,
) -> AsyncIterator[StreamedResponse]
Make a streaming model request.
When inside a Prefect flow, the stream is consumed within a task and a non-streaming response is returned. When not in a flow, behaves normally.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_model.py
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 | |
TaskConfig
Bases: TypedDict
Configuration for a task in Prefect.
These options are passed to the @task decorator.
Source code in pydantic_ai_slim/pydantic_ai/durable_exec/prefect/_types.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
retry_delay_seconds
instance-attribute
Delay between retries in seconds. Can be a single value or a list for custom backoff.
timeout_seconds
instance-attribute
timeout_seconds: float
Maximum time in seconds for the task to complete.
cache_policy
instance-attribute
cache_policy: CachePolicy
Prefect cache policy for the task.
result_storage
instance-attribute
result_storage: ResultStorage
Prefect result storage for the task. Should be a storage block or a block slug like s3-bucket/my-storage.