-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy path__init__.py
More file actions
1746 lines (1491 loc) · 76.4 KB
/
__init__.py
File metadata and controls
1746 lines (1491 loc) · 76.4 KB
1
2
3
4
5
6
7
8
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
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
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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations as _annotations
import dataclasses
import inspect
import json
import warnings
from asyncio import Lock
from collections.abc import AsyncIterator, Awaitable, Callable, Iterator, Sequence
from contextlib import AbstractAsyncContextManager, AsyncExitStack, asynccontextmanager, contextmanager
from contextvars import ContextVar
from typing import TYPE_CHECKING, Any, ClassVar, overload
from opentelemetry.trace import NoOpTracer, use_span
from pydantic.json_schema import GenerateJsonSchema
from typing_extensions import Self, TypeVar, deprecated
from pydantic_ai._instrumentation import DEFAULT_INSTRUMENTATION_VERSION, InstrumentationNames
from .. import (
_agent_graph,
_output,
_system_prompt,
_utils,
exceptions,
messages as _messages,
models,
usage as _usage,
)
from .._agent_graph import (
CallToolsNode,
EndStrategy,
HistoryProcessor,
ModelRequestNode,
UserPromptNode,
build_run_context,
capture_run_messages,
)
from .._output import OutputToolset
from .._tool_manager import ToolManager
from ..builtin_tools import AbstractBuiltinTool
from ..models.instrumented import InstrumentationSettings, InstrumentedModel, instrument_model
from ..output import OutputDataT, OutputSpec
from ..run import AgentRun, AgentRunResult
from ..settings import ModelSettings, merge_model_settings
from ..tools import (
AgentDepsT,
BuiltinToolFunc,
DeferredToolResults,
DocstringFormat,
GenerateToolJsonSchema,
RunContext,
Tool,
ToolFuncContext,
ToolFuncEither,
ToolFuncPlain,
ToolParams,
ToolPrepareFunc,
ToolsPrepareFunc,
)
from ..toolsets import AbstractToolset
from ..toolsets._dynamic import (
DynamicToolset,
ToolsetFunc,
)
from ..toolsets.combined import CombinedToolset
from ..toolsets.function import FunctionToolset
from ..toolsets.prepared import PreparedToolset
from .abstract import AbstractAgent, AgentMetadata, EventStreamHandler, Instructions, RunOutputDataT
from .wrapper import WrapperAgent
if TYPE_CHECKING:
from starlette.applications import Starlette
from pydantic_graph import GraphRunContext
from ..builtin_tools import AbstractBuiltinTool
from ..mcp import MCPServer
from ..ui._web import ModelsParam
__all__ = (
'Agent',
'AgentRun',
'AgentRunResult',
'capture_run_messages',
'EndStrategy',
'CallToolsNode',
'ModelRequestNode',
'UserPromptNode',
'InstrumentationSettings',
'WrapperAgent',
'AbstractAgent',
'EventStreamHandler',
)
T = TypeVar('T')
S = TypeVar('S')
NoneType = type(None)
@dataclasses.dataclass(init=False)
class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
"""Class for defining "agents" - a way to have a specific type of "conversation" with an LLM.
Agents are generic in the dependency type they take [`AgentDepsT`][pydantic_ai.tools.AgentDepsT]
and the output type they return, [`OutputDataT`][pydantic_ai.output.OutputDataT].
By default, if neither generic parameter is customised, agents have type `Agent[None, str]`.
Minimal usage example:
```python
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o')
result = agent.run_sync('What is the capital of France?')
print(result.output)
#> The capital of France is Paris.
```
"""
_model: models.Model | models.KnownModelName | str | None
_name: str | None
end_strategy: EndStrategy
"""The strategy for handling multiple tool calls when a final result is found.
- `'early'` (default): Output tools are executed first. Once a valid final result is found, remaining function and output tool calls are skipped
- `'exhaustive'`: Output tools are executed first, then all function tools are executed. The first valid output tool result becomes the final output
"""
model_settings: ModelSettings | None
"""Optional model request settings to use for this agents's runs, by default.
Note, if `model_settings` is provided by `run`, `run_sync`, or `run_stream`, those settings will
be merged with this value, with the runtime argument taking priority.
"""
_output_type: OutputSpec[OutputDataT]
instrument: InstrumentationSettings | bool | None
"""Options to automatically instrument with OpenTelemetry."""
_instrument_default: ClassVar[InstrumentationSettings | bool] = False
_metadata: AgentMetadata[AgentDepsT] | None = dataclasses.field(repr=False)
_deps_type: type[AgentDepsT] = dataclasses.field(repr=False)
_output_schema: _output.OutputSchema[OutputDataT] = dataclasses.field(repr=False)
_output_validators: list[_output.OutputValidator[AgentDepsT, OutputDataT]] = dataclasses.field(repr=False)
_instructions: list[str | _system_prompt.SystemPromptFunc[AgentDepsT]] = dataclasses.field(repr=False)
_system_prompts: tuple[str, ...] = dataclasses.field(repr=False)
_system_prompt_functions: list[_system_prompt.SystemPromptRunner[AgentDepsT]] = dataclasses.field(repr=False)
_system_prompt_dynamic_functions: dict[str, _system_prompt.SystemPromptRunner[AgentDepsT]] = dataclasses.field(
repr=False
)
_function_toolset: FunctionToolset[AgentDepsT] = dataclasses.field(repr=False)
_output_toolset: OutputToolset[AgentDepsT] | None = dataclasses.field(repr=False)
_user_toolsets: list[AbstractToolset[AgentDepsT]] = dataclasses.field(repr=False)
_prepare_tools: ToolsPrepareFunc[AgentDepsT] | None = dataclasses.field(repr=False)
_prepare_output_tools: ToolsPrepareFunc[AgentDepsT] | None = dataclasses.field(repr=False)
_max_result_retries: int = dataclasses.field(repr=False)
_max_tool_retries: int = dataclasses.field(repr=False)
_tool_timeout: float | None = dataclasses.field(repr=False)
_validation_context: Any | Callable[[RunContext[AgentDepsT]], Any] = dataclasses.field(repr=False)
_event_stream_handler: EventStreamHandler[AgentDepsT] | None = dataclasses.field(repr=False)
_enter_lock: Lock = dataclasses.field(repr=False)
_entered_count: int = dataclasses.field(repr=False)
_exit_stack: AsyncExitStack | None = dataclasses.field(repr=False)
@overload
def __init__(
self,
model: models.Model | models.KnownModelName | str | None = None,
*,
output_type: OutputSpec[OutputDataT] = str,
instructions: Instructions[AgentDepsT] = None,
system_prompt: str | Sequence[str] = (),
deps_type: type[AgentDepsT] = NoneType,
name: str | None = None,
model_settings: ModelSettings | None = None,
retries: int = 1,
validation_context: Any | Callable[[RunContext[AgentDepsT]], Any] = None,
output_retries: int | None = None,
tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] = (),
builtin_tools: Sequence[AbstractBuiltinTool | BuiltinToolFunc[AgentDepsT]] = (),
prepare_tools: ToolsPrepareFunc[AgentDepsT] | None = None,
prepare_output_tools: ToolsPrepareFunc[AgentDepsT] | None = None,
toolsets: Sequence[AbstractToolset[AgentDepsT] | ToolsetFunc[AgentDepsT]] | None = None,
defer_model_check: bool = False,
end_strategy: EndStrategy = 'early',
instrument: InstrumentationSettings | bool | None = None,
metadata: AgentMetadata[AgentDepsT] | None = None,
history_processors: Sequence[HistoryProcessor[AgentDepsT]] | None = None,
event_stream_handler: EventStreamHandler[AgentDepsT] | None = None,
tool_timeout: float | None = None,
) -> None: ...
@overload
@deprecated('`mcp_servers` is deprecated, use `toolsets` instead.')
def __init__(
self,
model: models.Model | models.KnownModelName | str | None = None,
*,
output_type: OutputSpec[OutputDataT] = str,
instructions: Instructions[AgentDepsT] = None,
system_prompt: str | Sequence[str] = (),
deps_type: type[AgentDepsT] = NoneType,
name: str | None = None,
model_settings: ModelSettings | None = None,
retries: int = 1,
validation_context: Any | Callable[[RunContext[AgentDepsT]], Any] = None,
output_retries: int | None = None,
tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] = (),
builtin_tools: Sequence[AbstractBuiltinTool | BuiltinToolFunc[AgentDepsT]] = (),
prepare_tools: ToolsPrepareFunc[AgentDepsT] | None = None,
prepare_output_tools: ToolsPrepareFunc[AgentDepsT] | None = None,
mcp_servers: Sequence[MCPServer] = (),
defer_model_check: bool = False,
end_strategy: EndStrategy = 'early',
instrument: InstrumentationSettings | bool | None = None,
metadata: AgentMetadata[AgentDepsT] | None = None,
history_processors: Sequence[HistoryProcessor[AgentDepsT]] | None = None,
event_stream_handler: EventStreamHandler[AgentDepsT] | None = None,
tool_timeout: float | None = None,
) -> None: ...
def __init__(
self,
model: models.Model | models.KnownModelName | str | None = None,
*,
output_type: OutputSpec[OutputDataT] = str,
instructions: Instructions[AgentDepsT] = None,
system_prompt: str | Sequence[str] = (),
deps_type: type[AgentDepsT] = NoneType,
name: str | None = None,
model_settings: ModelSettings | None = None,
retries: int = 1,
validation_context: Any | Callable[[RunContext[AgentDepsT]], Any] = None,
output_retries: int | None = None,
tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] = (),
builtin_tools: Sequence[AbstractBuiltinTool | BuiltinToolFunc[AgentDepsT]] = (),
prepare_tools: ToolsPrepareFunc[AgentDepsT] | None = None,
prepare_output_tools: ToolsPrepareFunc[AgentDepsT] | None = None,
toolsets: Sequence[AbstractToolset[AgentDepsT] | ToolsetFunc[AgentDepsT]] | None = None,
defer_model_check: bool = False,
end_strategy: EndStrategy = 'early',
instrument: InstrumentationSettings | bool | None = None,
metadata: AgentMetadata[AgentDepsT] | None = None,
history_processors: Sequence[HistoryProcessor[AgentDepsT]] | None = None,
event_stream_handler: EventStreamHandler[AgentDepsT] | None = None,
tool_timeout: float | None = None,
**_deprecated_kwargs: Any,
):
"""Create an agent.
Args:
model: The default model to use for this agent, if not provided,
you must provide the model when calling it. We allow `str` here since the actual list of allowed models changes frequently.
output_type: The type of the output data, used to validate the data returned by the model,
defaults to `str`.
instructions: Instructions to use for this agent, you can also register instructions via a function with
[`instructions`][pydantic_ai.agent.Agent.instructions] or pass additional, temporary, instructions when executing a run.
system_prompt: Static system prompts to use for this agent, you can also register system
prompts via a function with [`system_prompt`][pydantic_ai.agent.Agent.system_prompt].
deps_type: The type used for dependency injection, this parameter exists solely to allow you to fully
parameterize the agent, and therefore get the best out of static type checking.
If you're not using deps, but want type checking to pass, you can set `deps=None` to satisfy Pyright
or add a type hint `: Agent[None, <return type>]`.
name: The name of the agent, used for logging. If `None`, we try to infer the agent name from the call frame
when the agent is first run.
model_settings: Optional model request settings to use for this agent's runs, by default.
retries: The default number of retries to allow for tool calls and output validation, before raising an error.
For model request retries, see the [HTTP Request Retries](../retries.md) documentation.
validation_context: Pydantic [validation context](https://docs.pydantic.dev/latest/concepts/validators/#validation-context) used to validate tool arguments and outputs.
output_retries: The maximum number of retries to allow for output validation, defaults to `retries`.
tools: Tools to register with the agent, you can also register tools via the decorators
[`@agent.tool`][pydantic_ai.agent.Agent.tool] and [`@agent.tool_plain`][pydantic_ai.agent.Agent.tool_plain].
builtin_tools: The builtin tools that the agent will use. This depends on the model, as some models may not
support certain tools. If the model doesn't support the builtin tools, an error will be raised.
prepare_tools: Custom function to prepare the tool definition of all tools for each step, except output tools.
This is useful if you want to customize the definition of multiple tools or you want to register
a subset of tools for a given step. See [`ToolsPrepareFunc`][pydantic_ai.tools.ToolsPrepareFunc]
prepare_output_tools: Custom function to prepare the tool definition of all output tools for each step.
This is useful if you want to customize the definition of multiple output tools or you want to register
a subset of output tools for a given step. See [`ToolsPrepareFunc`][pydantic_ai.tools.ToolsPrepareFunc]
toolsets: Toolsets to register with the agent, including MCP servers and functions which take a run context
and return a toolset. See [`ToolsetFunc`][pydantic_ai.toolsets.ToolsetFunc] for more information.
defer_model_check: by default, if you provide a [named][pydantic_ai.models.KnownModelName] model,
it's evaluated to create a [`Model`][pydantic_ai.models.Model] instance immediately,
which checks for the necessary environment variables. Set this to `false`
to defer the evaluation until the first run. Useful if you want to
[override the model][pydantic_ai.agent.Agent.override] for testing.
end_strategy: Strategy for handling tool calls that are requested alongside a final result.
See [`EndStrategy`][pydantic_ai.agent.EndStrategy] for more information.
instrument: Set to True to automatically instrument with OpenTelemetry,
which will use Logfire if it's configured.
Set to an instance of [`InstrumentationSettings`][pydantic_ai.agent.InstrumentationSettings] to customize.
If this isn't set, then the last value set by
[`Agent.instrument_all()`][pydantic_ai.agent.Agent.instrument_all]
will be used, which defaults to False.
See the [Debugging and Monitoring guide](https://ai.pydantic.dev/logfire/) for more info.
metadata: Optional metadata to store with each run.
Provide a dictionary of primitives, or a callable returning one
computed from the [`RunContext`][pydantic_ai.tools.RunContext] on each run.
Metadata is resolved when a run starts and recomputed after a successful run finishes so it
can reflect the final state.
Resolved metadata can be read after the run completes via
[`AgentRun.metadata`][pydantic_ai.agent.AgentRun],
[`AgentRunResult.metadata`][pydantic_ai.agent.AgentRunResult], and
[`StreamedRunResult.metadata`][pydantic_ai.result.StreamedRunResult],
and is attached to the agent run span when instrumentation is enabled.
history_processors: Optional list of callables to process the message history before sending it to the model.
Each processor takes a list of messages and returns a modified list of messages.
Processors can be sync or async and are applied in sequence.
event_stream_handler: Optional handler for events from the model's streaming response and the agent's execution of tools.
tool_timeout: Default timeout in seconds for tool execution. If a tool takes longer than this,
the tool is considered to have failed and a retry prompt is returned to the model (counting towards the retry limit).
Individual tools can override this with their own timeout. Defaults to None (no timeout).
"""
if model is None or defer_model_check:
self._model = model
else:
self._model = models.infer_model(model)
self._name = name
self.end_strategy = end_strategy
self.model_settings = model_settings
self._output_type = output_type
self.instrument = instrument
self._metadata = metadata
self._deps_type = deps_type
if mcp_servers := _deprecated_kwargs.pop('mcp_servers', None):
if toolsets is not None: # pragma: no cover
raise TypeError('`mcp_servers` and `toolsets` cannot be set at the same time.')
warnings.warn('`mcp_servers` is deprecated, use `toolsets` instead', DeprecationWarning)
toolsets = mcp_servers
_utils.validate_empty_kwargs(_deprecated_kwargs)
self._output_schema = _output.OutputSchema[OutputDataT].build(output_type)
self._output_validators = []
self._instructions = self._normalize_instructions(instructions)
self._system_prompts = (system_prompt,) if isinstance(system_prompt, str) else tuple(system_prompt)
self._system_prompt_functions = []
self._system_prompt_dynamic_functions = {}
self._max_result_retries = output_retries if output_retries is not None else retries
self._max_tool_retries = retries
self._tool_timeout = tool_timeout
self._validation_context = validation_context
self._builtin_tools = builtin_tools
self._prepare_tools = prepare_tools
self._prepare_output_tools = prepare_output_tools
self._output_toolset = self._output_schema.toolset
if self._output_toolset:
self._output_toolset.max_retries = self._max_result_retries
self._function_toolset = _AgentFunctionToolset(
tools,
max_retries=self._max_tool_retries,
timeout=self._tool_timeout,
output_schema=self._output_schema,
)
self._dynamic_toolsets = [
DynamicToolset[AgentDepsT](toolset_func=toolset)
for toolset in toolsets or []
if not isinstance(toolset, AbstractToolset)
]
self._user_toolsets = [toolset for toolset in toolsets or [] if isinstance(toolset, AbstractToolset)]
self.history_processors = history_processors or []
self._event_stream_handler = event_stream_handler
self._override_name: ContextVar[_utils.Option[str]] = ContextVar('_override_name', default=None)
self._override_deps: ContextVar[_utils.Option[AgentDepsT]] = ContextVar('_override_deps', default=None)
self._override_model: ContextVar[_utils.Option[models.Model]] = ContextVar('_override_model', default=None)
self._override_toolsets: ContextVar[_utils.Option[Sequence[AbstractToolset[AgentDepsT]]]] = ContextVar(
'_override_toolsets', default=None
)
self._override_tools: ContextVar[
_utils.Option[Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]]]
] = ContextVar('_override_tools', default=None)
self._override_instructions: ContextVar[
_utils.Option[list[str | _system_prompt.SystemPromptFunc[AgentDepsT]]]
] = ContextVar('_override_instructions', default=None)
self._override_metadata: ContextVar[_utils.Option[AgentMetadata[AgentDepsT]]] = ContextVar(
'_override_metadata', default=None
)
self._enter_lock = Lock()
self._entered_count = 0
self._exit_stack = None
@staticmethod
def instrument_all(instrument: InstrumentationSettings | bool = True) -> None:
"""Set the instrumentation options for all agents where `instrument` is not set."""
Agent._instrument_default = instrument
@property
def model(self) -> models.Model | models.KnownModelName | str | None:
"""The default model configured for this agent."""
return self._model
@model.setter
def model(self, value: models.Model | models.KnownModelName | str | None) -> None:
"""Set the default model configured for this agent.
We allow `str` here since the actual list of allowed models changes frequently.
"""
self._model = value
@property
def name(self) -> str | None:
"""The name of the agent, used for logging.
If `None`, we try to infer the agent name from the call frame when the agent is first run.
"""
name_ = self._override_name.get()
return name_.value if name_ else self._name
@name.setter
def name(self, value: str | None) -> None:
"""Set the name of the agent, used for logging."""
self._name = value
@property
def deps_type(self) -> type:
"""The type of dependencies used by the agent."""
return self._deps_type
@property
def output_type(self) -> OutputSpec[OutputDataT]:
"""The type of data output by agent runs, used to validate the data returned by the model, defaults to `str`."""
return self._output_type
@property
def event_stream_handler(self) -> EventStreamHandler[AgentDepsT] | None:
"""Optional handler for events from the model's streaming response and the agent's execution of tools."""
return self._event_stream_handler
def __repr__(self) -> str:
return f'{type(self).__name__}(model={self.model!r}, name={self.name!r}, end_strategy={self.end_strategy!r}, model_settings={self.model_settings!r}, output_type={self.output_type!r}, instrument={self.instrument!r})'
@overload
def iter(
self,
user_prompt: str | Sequence[_messages.UserContent] | None = None,
*,
output_type: None = None,
message_history: Sequence[_messages.ModelMessage] | None = None,
deferred_tool_results: DeferredToolResults | None = None,
model: models.Model | models.KnownModelName | str | None = None,
instructions: Instructions[AgentDepsT] = None,
deps: AgentDepsT = None,
model_settings: ModelSettings | None = None,
usage_limits: _usage.UsageLimits | None = None,
usage: _usage.RunUsage | None = None,
metadata: AgentMetadata[AgentDepsT] | None = None,
infer_name: bool = True,
toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None,
builtin_tools: Sequence[AbstractBuiltinTool | BuiltinToolFunc[AgentDepsT]] | None = None,
) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, OutputDataT]]: ...
@overload
def iter(
self,
user_prompt: str | Sequence[_messages.UserContent] | None = None,
*,
output_type: OutputSpec[RunOutputDataT],
message_history: Sequence[_messages.ModelMessage] | None = None,
deferred_tool_results: DeferredToolResults | None = None,
model: models.Model | models.KnownModelName | str | None = None,
instructions: Instructions[AgentDepsT] = None,
deps: AgentDepsT = None,
model_settings: ModelSettings | None = None,
usage_limits: _usage.UsageLimits | None = None,
usage: _usage.RunUsage | None = None,
metadata: AgentMetadata[AgentDepsT] | None = None,
infer_name: bool = True,
toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None,
builtin_tools: Sequence[AbstractBuiltinTool | BuiltinToolFunc[AgentDepsT]] | None = None,
) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, RunOutputDataT]]: ...
@asynccontextmanager
async def iter(
self,
user_prompt: str | Sequence[_messages.UserContent] | None = None,
*,
output_type: OutputSpec[Any] | None = None,
message_history: Sequence[_messages.ModelMessage] | None = None,
deferred_tool_results: DeferredToolResults | None = None,
model: models.Model | models.KnownModelName | str | None = None,
instructions: Instructions[AgentDepsT] = None,
deps: AgentDepsT = None,
model_settings: ModelSettings | None = None,
usage_limits: _usage.UsageLimits | None = None,
usage: _usage.RunUsage | None = None,
metadata: AgentMetadata[AgentDepsT] | None = None,
infer_name: bool = True,
toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None,
builtin_tools: Sequence[AbstractBuiltinTool | BuiltinToolFunc[AgentDepsT]] | 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:
```python
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(...),
)
],
timestamp=datetime.datetime(...),
run_id='...',
)
),
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(...),
run_id='...',
)
),
End(data=FinalResult(output='The capital of France is Paris.')),
]
'''
print(agent_run.result.output)
#> The capital of France is Paris.
```
Args:
user_prompt: User input to start/continue the conversation.
output_type: Custom output type to use for this run, `output_type` may only be used if the agent has no
output validators since output validators would expect an argument that matches the agent's output type.
message_history: History of the conversation so far.
deferred_tool_results: Optional results for deferred tool calls in the message history.
model: Optional model to use for this run, required if `model` was not set when creating the agent.
instructions: Optional additional instructions to use for this run.
deps: Optional dependencies to use for this run.
model_settings: Optional settings to use for this model's request.
usage_limits: Optional limits on model request count or token usage.
usage: Optional usage to start with, useful for resuming a conversation or agents used in tools.
metadata: Optional metadata to attach to this run. Accepts a dictionary or a callable taking
[`RunContext`][pydantic_ai.tools.RunContext]; merged with the agent's configured metadata.
infer_name: Whether to try to infer the agent name from the call frame if it's not set.
toolsets: Optional additional toolsets for this run.
builtin_tools: Optional additional builtin tools for this run.
Returns:
The result of the run.
"""
if infer_name and self.name is None:
self._infer_name(inspect.currentframe())
# Validate tool_choice - 'required' and list[str] would prevent the agent from ever completing
# because they exclude output tools. These settings are only valid for direct model requests.
# TODO(prepare_model_settings): This validation remains correct for static model_settings.
# The hook CAN return 'required' or list[str] for per-step control (e.g., force tool on
# step 1, then allow completion on step 2+). The hook bypasses this validation because
# it applies dynamically per-request, not statically for the entire run.
if model_settings:
tool_choice = model_settings.get('tool_choice')
if tool_choice == 'required' or isinstance(tool_choice, list):
raise exceptions.UserError(
f'tool_choice={tool_choice!r} is not supported in agent.run() because it prevents '
f'the agent from producing a final response. Use ToolOrOutput to combine specific '
f'tools with output capability, or use model.request() for direct model calls.'
)
model_used = self._get_model(model)
del model
deps = self._get_deps(deps)
output_schema = self._prepare_output_schema(output_type)
output_type_ = output_type or self.output_type
# We consider it a user error if a user tries to restrict the result type while having an output validator that
# may change the result type from the restricted type to something else. Therefore, we consider the following
# typecast reasonable, even though it is possible to violate it with otherwise-type-checked code.
output_validators = self._output_validators
output_toolset = self._output_toolset
if output_schema != self._output_schema or output_validators:
output_toolset = output_schema.toolset
if output_toolset:
output_toolset.max_retries = self._max_result_retries
output_toolset.output_validators = output_validators
toolset = self._get_toolset(output_toolset=output_toolset, additional_toolsets=toolsets)
tool_manager = ToolManager[AgentDepsT](toolset, default_max_retries=self._max_tool_retries)
# Build the graph
graph = _agent_graph.build_agent_graph(self.name, self._deps_type, output_type_)
# Build the initial state
usage = usage or _usage.RunUsage()
state = _agent_graph.GraphAgentState(
message_history=list(message_history) if message_history else [],
usage=usage,
retries=0,
run_step=0,
)
# Merge model settings in order of precedence: run > agent > model
merged_settings = merge_model_settings(model_used.settings, self.model_settings)
model_settings = merge_model_settings(merged_settings, model_settings)
usage_limits = usage_limits or _usage.UsageLimits()
instructions_literal, instructions_functions = self._get_instructions(additional_instructions=instructions)
async def get_instructions(run_context: RunContext[AgentDepsT]) -> str | None:
parts = [
instructions_literal,
*[await func.run(run_context) for func in instructions_functions],
]
parts = [p for p in parts if p]
if not parts:
return None
return '\n\n'.join(parts).strip()
if isinstance(model_used, InstrumentedModel):
instrumentation_settings = model_used.instrumentation_settings
tracer = model_used.instrumentation_settings.tracer
else:
instrumentation_settings = None
tracer = NoOpTracer()
graph_deps = _agent_graph.GraphAgentDeps[AgentDepsT, OutputDataT](
user_deps=deps,
prompt=user_prompt,
new_message_index=len(message_history) if message_history else 0,
model=model_used,
model_settings=model_settings,
usage_limits=usage_limits,
max_result_retries=self._max_result_retries,
end_strategy=self.end_strategy,
output_schema=output_schema,
output_validators=output_validators,
validation_context=self._validation_context,
history_processors=self.history_processors,
builtin_tools=[*self._builtin_tools, *(builtin_tools or [])],
tool_manager=tool_manager,
tracer=tracer,
get_instructions=get_instructions,
instrumentation_settings=instrumentation_settings,
)
user_prompt_node = _agent_graph.UserPromptNode[AgentDepsT](
user_prompt=user_prompt,
deferred_tool_results=deferred_tool_results,
instructions=instructions_literal,
instructions_functions=instructions_functions,
system_prompts=self._system_prompts,
system_prompt_functions=self._system_prompt_functions,
system_prompt_dynamic_functions=self._system_prompt_dynamic_functions,
)
agent_name = self.name or 'agent'
instrumentation_names = InstrumentationNames.for_version(
instrumentation_settings.version if instrumentation_settings else DEFAULT_INSTRUMENTATION_VERSION
)
run_span = tracer.start_span(
instrumentation_names.get_agent_run_span_name(agent_name),
attributes={
'model_name': model_used.model_name if model_used else 'no-model',
'agent_name': agent_name,
'gen_ai.agent.name': agent_name,
'logfire.msg': f'{agent_name} run',
},
)
run_metadata: dict[str, Any] | None = None
try:
async with graph.iter(
inputs=user_prompt_node,
state=state,
deps=graph_deps,
span=use_span(run_span) if run_span.is_recording() else None,
infer_name=False,
) as graph_run:
async with toolset:
agent_run = AgentRun(graph_run)
run_metadata = self._resolve_and_store_metadata(agent_run.ctx, metadata)
try:
yield agent_run
finally:
if agent_run.result is not None:
run_metadata = self._resolve_and_store_metadata(agent_run.ctx, metadata)
else:
run_metadata = graph_run.state.metadata
final_result = agent_run.result
if (
instrumentation_settings
and instrumentation_settings.include_content
and run_span.is_recording()
and final_result is not None
):
run_span.set_attribute(
'final_result',
(
final_result.output
if isinstance(final_result.output, str)
else json.dumps(InstrumentedModel.serialize_any(final_result.output))
),
)
finally:
try:
if instrumentation_settings and run_span.is_recording():
run_span.set_attributes(
self._run_span_end_attributes(
instrumentation_settings,
usage,
state.message_history,
graph_deps.new_message_index,
run_metadata,
)
)
finally:
run_span.end()
def _get_metadata(
self,
ctx: RunContext[AgentDepsT],
additional_metadata: AgentMetadata[AgentDepsT] | None = None,
) -> dict[str, Any] | None:
metadata_override = self._override_metadata.get()
if metadata_override is not None:
return self._resolve_metadata_config(metadata_override.value, ctx)
base_metadata = self._resolve_metadata_config(self._metadata, ctx)
run_metadata = self._resolve_metadata_config(additional_metadata, ctx)
if base_metadata and run_metadata:
return {**base_metadata, **run_metadata}
return run_metadata or base_metadata
def _resolve_metadata_config(
self,
config: AgentMetadata[AgentDepsT] | None,
ctx: RunContext[AgentDepsT],
) -> dict[str, Any] | None:
if config is None:
return None
metadata = config(ctx) if callable(config) else config
return metadata
def _resolve_and_store_metadata(
self,
graph_run_ctx: GraphRunContext[_agent_graph.GraphAgentState, _agent_graph.GraphAgentDeps[AgentDepsT, Any]],
metadata: AgentMetadata[AgentDepsT] | None,
) -> dict[str, Any] | None:
run_context = build_run_context(graph_run_ctx)
resolved_metadata = self._get_metadata(run_context, metadata)
graph_run_ctx.state.metadata = resolved_metadata
return resolved_metadata
def _run_span_end_attributes(
self,
settings: InstrumentationSettings,
usage: _usage.RunUsage,
message_history: list[_messages.ModelMessage],
new_message_index: int,
metadata: dict[str, Any] | None = None,
):
if settings.version == 1:
attrs = {
'all_messages_events': json.dumps(
[InstrumentedModel.event_to_dict(e) for e in settings.messages_to_otel_events(message_history)]
)
}
else:
# Store the last instructions here for convenience
last_instructions = InstrumentedModel._get_instructions(message_history) # pyright: ignore[reportPrivateUsage]
attrs: dict[str, Any] = {
'pydantic_ai.all_messages': json.dumps(settings.messages_to_otel_messages(list(message_history))),
**settings.system_instructions_attributes(last_instructions),
}
# If this agent run was provided with existing history, store an attribute indicating the point at which the
# new messages begin.
if new_message_index > 0:
attrs['pydantic_ai.new_message_index'] = new_message_index
# If the instructions for this agent run were not always the same, store an attribute that indicates that.
# This can signal to an observability UI that different steps in the agent run had different instructions.
# Note: We purposely only look at "new" messages because they are the only ones produced by this agent run.
if any(
(
isinstance(m, _messages.ModelRequest)
and m.instructions is not None
and m.instructions != last_instructions
)
for m in message_history[new_message_index:]
):
attrs['pydantic_ai.variable_instructions'] = True
if metadata is not None:
attrs['metadata'] = json.dumps(InstrumentedModel.serialize_any(metadata))
return {
**usage.opentelemetry_attributes(),
**attrs,
'logfire.json_schema': json.dumps(
{
'type': 'object',
'properties': {
**{k: {'type': 'array'} if isinstance(v, str) else {} for k, v in attrs.items()},
'final_result': {'type': 'object'},
},
}
),
}
@contextmanager
def override(
self,
*,
name: str | _utils.Unset = _utils.UNSET,
deps: AgentDepsT | _utils.Unset = _utils.UNSET,
model: models.Model | models.KnownModelName | str | _utils.Unset = _utils.UNSET,
toolsets: Sequence[AbstractToolset[AgentDepsT]] | _utils.Unset = _utils.UNSET,
tools: Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] | _utils.Unset = _utils.UNSET,
instructions: Instructions[AgentDepsT] | _utils.Unset = _utils.UNSET,
metadata: AgentMetadata[AgentDepsT] | _utils.Unset = _utils.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](../testing.md#overriding-model-via-pytest-fixtures).
Args:
name: The name to use instead of the name passed to the agent constructor and agent run.
deps: The dependencies to use instead of the dependencies passed to the agent run.
model: The model to use instead of the model passed to the agent run.
toolsets: The toolsets to use instead of the toolsets passed to the agent constructor and agent run.
tools: The tools to use instead of the tools registered with the agent.
instructions: The instructions to use instead of the instructions registered with the agent.
metadata: The metadata to use instead of the metadata passed to the agent constructor. When set, any
per-run `metadata` argument is ignored.
"""
if _utils.is_set(name):
name_token = self._override_name.set(_utils.Some(name))
else:
name_token = None
if _utils.is_set(deps):
deps_token = self._override_deps.set(_utils.Some(deps))
else:
deps_token = None
if _utils.is_set(model):
model_token = self._override_model.set(_utils.Some(models.infer_model(model)))
else:
model_token = None
if _utils.is_set(toolsets):
toolsets_token = self._override_toolsets.set(_utils.Some(toolsets))
else:
toolsets_token = None
if _utils.is_set(tools):
tools_token = self._override_tools.set(_utils.Some(tools))
else:
tools_token = None
if _utils.is_set(instructions):
normalized_instructions = self._normalize_instructions(instructions)
instructions_token = self._override_instructions.set(_utils.Some(normalized_instructions))
else:
instructions_token = None
if _utils.is_set(metadata):
metadata_token = self._override_metadata.set(_utils.Some(metadata))
else:
metadata_token = None
try:
yield
finally:
if name_token is not None:
self._override_name.reset(name_token)
if deps_token is not None:
self._override_deps.reset(deps_token)
if model_token is not None:
self._override_model.reset(model_token)
if toolsets_token is not None:
self._override_toolsets.reset(toolsets_token)
if tools_token is not None:
self._override_tools.reset(tools_token)
if instructions_token is not None:
self._override_instructions.reset(instructions_token)
if metadata_token is not None:
self._override_metadata.reset(metadata_token)
@overload
def instructions(
self, func: Callable[[RunContext[AgentDepsT]], str | None], /
) -> Callable[[RunContext[AgentDepsT]], str | None]: ...
@overload
def instructions(
self, func: Callable[[RunContext[AgentDepsT]], Awaitable[str | None]], /
) -> Callable[[RunContext[AgentDepsT]], Awaitable[str | None]]: ...
@overload
def instructions(self, func: Callable[[], str | None], /) -> Callable[[], str | None]: ...
@overload
def instructions(self, func: Callable[[], Awaitable[str | None]], /) -> Callable[[], Awaitable[str | None]]: ...
@overload
def instructions(
self, /
) -> Callable[[_system_prompt.SystemPromptFunc[AgentDepsT]], _system_prompt.SystemPromptFunc[AgentDepsT]]: ...
def instructions(
self,
func: _system_prompt.SystemPromptFunc[AgentDepsT] | None = None,
/,
) -> (
Callable[[_system_prompt.SystemPromptFunc[AgentDepsT]], _system_prompt.SystemPromptFunc[AgentDepsT]]
| _system_prompt.SystemPromptFunc[AgentDepsT]
):
"""Decorator to register an instructions function.
Optionally takes [`RunContext`][pydantic_ai.tools.RunContext] as its only argument.
Can decorate a sync or async functions.
The decorator can be used bare (`agent.instructions`).
Overloads for every possible signature of `instructions` are included so the decorator doesn't obscure
the type of the function.
Example:
```python
from pydantic_ai import Agent, RunContext
agent = Agent('test', deps_type=str)
@agent.instructions
def simple_instructions() -> str:
return 'foobar'
@agent.instructions
async def async_instructions(ctx: RunContext[str]) -> str:
return f'{ctx.deps} is the best'
```
"""
if func is None:
def decorator(
func_: _system_prompt.SystemPromptFunc[AgentDepsT],