-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy path__init__.py
More file actions
2649 lines (2314 loc) · 121 KB
/
__init__.py
File metadata and controls
2649 lines (2314 loc) · 121 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 asyncio
import contextvars
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 pathlib import Path
from typing import TYPE_CHECKING, Any, ClassVar, Literal, cast, 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 pydantic_ai._spec import load_from_registry
from .. import (
_agent_graph,
_instructions,
_output,
_system_prompt,
_utils,
concurrency as _concurrency,
exceptions,
messages as _messages,
models,
usage as _usage,
)
from .._agent_graph import (
CallToolsNode,
EndStrategy,
HistoryProcessor,
ModelRequestNode,
UserPromptNode,
build_run_context,
capture_run_messages,
)
from .._instructions import AgentInstructions
from .._output import OutputToolset
from .._template import TemplateStr, validate_from_spec_args
from .._tool_manager import ParallelExecutionMode, ToolManager
from ..builtin_tools import AbstractBuiltinTool
from ..capabilities import AbstractCapability, CombinedCapability
from ..capabilities.builtin_tool import BuiltinTool as BuiltinToolCap
from ..capabilities.history_processor import HistoryProcessor as HistoryProcessorCap
from ..models.instrumented import InstrumentationSettings, InstrumentedModel, instrument_model
from ..output import OutputDataT, OutputSpec, StructuredDict
from ..run import AgentRun, AgentRunResult
from ..settings import ModelSettings, merge_model_settings
from ..tools import (
AgentBuiltinTool,
AgentDepsT,
ArgsValidatorFunc,
BuiltinToolFunc,
DeferredToolResults,
DocstringFormat,
GenerateToolJsonSchema,
RunContext,
Tool,
ToolFuncContext,
ToolFuncEither,
ToolFuncPlain,
ToolParams,
ToolPrepareFunc,
ToolsPrepareFunc,
)
from ..toolsets import AbstractToolset, AgentToolset
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,
AgentModelSettings,
EventStreamHandler,
RunOutputDataT,
)
from .spec import AgentSpec, get_capability_registry
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',
'ParallelExecutionMode',
'WrapperAgent',
'AbstractAgent',
'EventStreamHandler',
'AgentInstructions',
'AgentModelSettings',
'BuiltinToolFunc',
)
T = TypeVar('T')
S = TypeVar('S')
NoneType = type(None)
@dataclasses.dataclass
class _ResolvedSpec:
"""Result of resolving an AgentSpec for use at run/override time."""
capability: CombinedCapability[Any] | None
instructions: list[str | _system_prompt.SystemPromptFunc[Any]]
model: str | None
model_settings: ModelSettings | None
metadata: dict[str, Any] | None
name: str | 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-5.2')
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
_description: TemplateStr[AgentDepsT] | 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: AgentModelSettings[AgentDepsT] | None
"""Optional model request settings to use for this agent's runs, by default.
Can be a static `ModelSettings` dict or a callable that takes a
[`RunContext`][pydantic_ai.tools.RunContext] and returns `ModelSettings`.
Callables are called before each model request, allowing dynamic per-step settings.
Note, if `model_settings` is also provided at run time, those settings will be merged
on top of the agent-level settings, with the run-level 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)
_concurrency_limiter: _concurrency.AbstractConcurrencyLimiter | 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: AgentInstructions[AgentDepsT] = None,
system_prompt: str | Sequence[str] = (),
deps_type: type[AgentDepsT] = NoneType,
name: str | None = None,
description: TemplateStr[AgentDepsT] | str | None = None,
model_settings: AgentModelSettings[AgentDepsT] | 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[AgentBuiltinTool[AgentDepsT]] = (),
prepare_tools: ToolsPrepareFunc[AgentDepsT] | None = None,
prepare_output_tools: ToolsPrepareFunc[AgentDepsT] | None = None,
toolsets: Sequence[AgentToolset[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,
max_concurrency: _concurrency.AnyConcurrencyLimit = None,
capabilities: Sequence[AbstractCapability[AgentDepsT]] | 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: AgentInstructions[AgentDepsT] = None,
system_prompt: str | Sequence[str] = (),
deps_type: type[AgentDepsT] = NoneType,
name: str | None = None,
description: TemplateStr[AgentDepsT] | str | None = None,
model_settings: AgentModelSettings[AgentDepsT] | 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[AgentBuiltinTool[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,
max_concurrency: _concurrency.AnyConcurrencyLimit = None,
capabilities: Sequence[AbstractCapability[AgentDepsT]] | None = None,
) -> None: ...
def __init__(
self,
model: models.Model | models.KnownModelName | str | None = None,
*,
output_type: OutputSpec[OutputDataT] = str,
instructions: AgentInstructions[AgentDepsT] = None,
system_prompt: str | Sequence[str] = (),
deps_type: type[AgentDepsT] = NoneType,
name: str | None = None,
description: TemplateStr[AgentDepsT] | str | None = None,
model_settings: AgentModelSettings[AgentDepsT] | 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[AgentBuiltinTool[AgentDepsT]] = (),
prepare_tools: ToolsPrepareFunc[AgentDepsT] | None = None,
prepare_output_tools: ToolsPrepareFunc[AgentDepsT] | None = None,
toolsets: Sequence[AgentToolset[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,
max_concurrency: _concurrency.AnyConcurrencyLimit = None,
capabilities: Sequence[AbstractCapability[AgentDepsT]] | 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.
description: A human-readable description of the agent, attached to the agent run span as
`gen_ai.agent.description` when instrumentation is enabled.
model_settings: Optional model request settings to use for this agent's runs, by default.
Can be a static `ModelSettings` dict or a callable that takes a
[`RunContext`][pydantic_ai.tools.RunContext] and returns `ModelSettings`.
Callables are called before each model request, allowing dynamic per-step settings.
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).
max_concurrency: Optional limit on concurrent agent runs. Can be an integer for simple limiting,
a [`ConcurrencyLimit`][pydantic_ai.ConcurrencyLimit] for advanced configuration with backpressure,
a [`ConcurrencyLimiter`][pydantic_ai.ConcurrencyLimiter] for sharing limits across
multiple agents, or None (default) for no limiting. When the limit is reached, additional calls
to `run()` or `iter()` will wait until a slot becomes available.
capabilities: Optional list of [capabilities](https://ai.pydantic.dev/capabilities/) to configure the agent with.
Custom capabilities can be created by subclassing
[`AbstractCapability`][pydantic_ai.capabilities.AbstractCapability].
"""
if model is None or defer_model_check:
self._model = model
else:
self._model = models.infer_model(model)
self._name = name
self._description = description
self.end_strategy = end_strategy
self.history_processors: list[HistoryProcessor[AgentDepsT]] = list(history_processors or [])
capabilities = list(capabilities or [])
for history_processor in self.history_processors:
capabilities.append(HistoryProcessorCap(history_processor))
for builtin_tool in builtin_tools:
capabilities.append(BuiltinToolCap(builtin_tool))
self._root_capability = CombinedCapability(capabilities)
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 = _instructions.normalize_instructions(instructions)
self._cap_instructions = _instructions.normalize_instructions(self._root_capability.get_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._cap_builtin_tools = list(self._root_capability.get_builtin_tools())
self._cap_model_settings = self._root_capability.get_model_settings()
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,
)
# Agent-direct toolsets
agent_toolsets = list(toolsets or [])
self._dynamic_toolsets = [
DynamicToolset[AgentDepsT](toolset_func=toolset)
for toolset in agent_toolsets
if not isinstance(toolset, AbstractToolset)
]
self._user_toolsets = [toolset for toolset in agent_toolsets if isinstance(toolset, AbstractToolset)]
# Capability-contributed toolsets (stored separately for per-run re-extraction)
cap_toolset = self._root_capability.get_toolset()
self._cap_toolsets: list[AgentToolset[AgentDepsT]] = [cap_toolset] if cap_toolset is not None else []
self._event_stream_handler = event_stream_handler
self._concurrency_limiter = _concurrency.normalize_to_limiter(max_concurrency)
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._override_model_settings: ContextVar[_utils.Option[AgentModelSettings[AgentDepsT]]] = ContextVar(
'_override_model_settings', default=None
)
self._override_root_capability: ContextVar[_utils.Option[CombinedCapability[AgentDepsT]]] = ContextVar(
'_override_root_capability', default=None
)
self._enter_lock = Lock()
self._entered_count = 0
self._exit_stack = None
@overload
@classmethod
def from_spec(
cls,
spec: dict[str, Any] | AgentSpec,
*,
custom_capability_types: Sequence[type[AbstractCapability[Any]]] = (),
model: models.Model | models.KnownModelName | str | None = None,
output_type: OutputSpec[Any] = str,
instructions: AgentInstructions[Any] = None,
system_prompt: str | Sequence[str] = (),
name: str | None = None,
description: TemplateStr[Any] | str | None = None,
model_settings: ModelSettings | None = None,
retries: int | None = None,
validation_context: Any = None,
output_retries: int | None = None,
tools: Sequence[Tool[Any] | ToolFuncEither[Any, ...]] = (),
builtin_tools: Sequence[AgentBuiltinTool[Any]] = (),
prepare_tools: ToolsPrepareFunc[Any] | None = None,
prepare_output_tools: ToolsPrepareFunc[Any] | None = None,
toolsets: Sequence[AgentToolset[Any]] | None = None,
defer_model_check: bool = False,
end_strategy: EndStrategy | None = None,
instrument: InstrumentationSettings | bool | None = None,
metadata: AgentMetadata[Any] | None = None,
history_processors: Sequence[HistoryProcessor[Any]] | None = None,
event_stream_handler: EventStreamHandler[Any] | None = None,
tool_timeout: float | None = None,
max_concurrency: _concurrency.AnyConcurrencyLimit = None,
capabilities: Sequence[AbstractCapability[Any]] | None = None,
) -> Agent[None, str]: ...
@overload
@classmethod
def from_spec(
cls,
spec: dict[str, Any] | AgentSpec,
*,
deps_type: type[T],
custom_capability_types: Sequence[type[AbstractCapability[Any]]] = (),
model: models.Model | models.KnownModelName | str | None = None,
output_type: OutputSpec[Any] = str,
instructions: AgentInstructions[Any] = None,
system_prompt: str | Sequence[str] = (),
name: str | None = None,
description: TemplateStr[Any] | str | None = None,
model_settings: ModelSettings | None = None,
retries: int | None = None,
validation_context: Any = None,
output_retries: int | None = None,
tools: Sequence[Tool[Any] | ToolFuncEither[Any, ...]] = (),
builtin_tools: Sequence[AgentBuiltinTool[Any]] = (),
prepare_tools: ToolsPrepareFunc[Any] | None = None,
prepare_output_tools: ToolsPrepareFunc[Any] | None = None,
toolsets: Sequence[AgentToolset[Any]] | None = None,
defer_model_check: bool = False,
end_strategy: EndStrategy | None = None,
instrument: InstrumentationSettings | bool | None = None,
metadata: AgentMetadata[Any] | None = None,
history_processors: Sequence[HistoryProcessor[Any]] | None = None,
event_stream_handler: EventStreamHandler[Any] | None = None,
tool_timeout: float | None = None,
max_concurrency: _concurrency.AnyConcurrencyLimit = None,
capabilities: Sequence[AbstractCapability[Any]] | None = None,
) -> Agent[T, str]: ...
@classmethod
def from_spec(
cls,
spec: dict[str, Any] | AgentSpec,
*,
deps_type: type[Any] = type(None),
custom_capability_types: Sequence[type[AbstractCapability[Any]]] = (),
model: models.Model | models.KnownModelName | str | None = None,
output_type: OutputSpec[Any] = str,
instructions: AgentInstructions[Any] = None,
system_prompt: str | Sequence[str] = (),
name: str | None = None,
description: TemplateStr[Any] | str | None = None,
model_settings: ModelSettings | None = None,
retries: int | None = None,
validation_context: Any = None,
output_retries: int | None = None,
tools: Sequence[Tool[Any] | ToolFuncEither[Any, ...]] = (),
builtin_tools: Sequence[AgentBuiltinTool[Any]] = (),
prepare_tools: ToolsPrepareFunc[Any] | None = None,
prepare_output_tools: ToolsPrepareFunc[Any] | None = None,
toolsets: Sequence[AgentToolset[Any]] | None = None,
defer_model_check: bool = False,
end_strategy: EndStrategy | None = None,
instrument: InstrumentationSettings | bool | None = None,
metadata: AgentMetadata[Any] | None = None,
history_processors: Sequence[HistoryProcessor[Any]] | None = None,
event_stream_handler: EventStreamHandler[Any] | None = None,
tool_timeout: float | None = None,
max_concurrency: _concurrency.AnyConcurrencyLimit = None,
capabilities: Sequence[AbstractCapability[Any]] | None = None,
) -> Agent[Any, Any]:
"""Construct an Agent from a spec dict or `AgentSpec`.
This allows defining agents declaratively in YAML/JSON/dict form.
Keyword arguments supplement the spec: scalar spec fields (like `name`,
`retries`) are used as defaults that explicit arguments override, while
`capabilities` from both sources are merged.
Args:
spec: The agent specification, either a dict or an `AgentSpec` instance.
deps_type: The type of the dependencies for the agent. When provided,
template strings in capabilities (e.g. `"Hello {{name}}"`) are
compiled and validated against this type.
custom_capability_types: Additional capability classes to make available
beyond the built-in defaults.
model: Override the model from the spec.
output_type: The type of the output data, defaults to `str`.
instructions: Instructions for the agent.
system_prompt: Static system prompts.
name: The agent name, overrides spec `name` if provided.
description: The agent description, overrides spec `description` if provided.
model_settings: Model request settings.
retries: Default retries for tool calls and output validation, overrides spec `retries` if provided.
validation_context: Pydantic validation context for tool arguments and outputs.
output_retries: Max retries for output validation, overrides spec `output_retries` if provided.
tools: Tools to register with the agent.
builtin_tools: Builtin tools for the agent.
prepare_tools: Custom function to prepare tool definitions.
prepare_output_tools: Custom function to prepare output tool definitions.
toolsets: Toolsets to register with the agent.
defer_model_check: Defer model evaluation until first run.
end_strategy: Strategy for tool calls alongside a final result, overrides spec `end_strategy` if provided.
instrument: Instrumentation settings, overrides spec `instrument` if provided.
metadata: Metadata to store with each run, overrides spec `metadata` if provided.
history_processors: Processors for message history.
event_stream_handler: Handler for streaming events.
tool_timeout: Default timeout for tool execution, overrides spec `tool_timeout` if provided.
max_concurrency: Limit on concurrent agent runs.
capabilities: Additional capabilities merged with those from the spec.
Returns:
A new Agent instance.
"""
validated_spec, template_context = _validate_spec(spec, deps_type)
effective_output_type: OutputSpec[Any]
if output_type is not str:
effective_output_type = output_type
elif validated_spec.output_schema is not None:
effective_output_type = StructuredDict(validated_spec.output_schema)
else:
effective_output_type = str
# Merge instructions from spec and arg
merged_instructions = _instructions.normalize_instructions(validated_spec.instructions)
merged_instructions.extend(_instructions.normalize_instructions(instructions))
all_capabilities = _capabilities_from_spec(validated_spec, custom_capability_types, template_context)
if capabilities:
all_capabilities.extend(capabilities)
effective_model = model or validated_spec.model
if effective_model is None:
raise exceptions.UserError(
'`model` must be provided either in the spec or as a keyword argument to `from_spec()`.'
)
return Agent(
model=effective_model,
output_type=effective_output_type,
instructions=merged_instructions or None,
system_prompt=system_prompt,
deps_type=deps_type,
name=name or validated_spec.name,
description=description or validated_spec.description,
model_settings=merge_model_settings(
cast(ModelSettings, validated_spec.model_settings) if validated_spec.model_settings else None,
model_settings,
),
retries=retries if retries is not None else validated_spec.retries,
validation_context=validation_context,
output_retries=output_retries if output_retries is not None else validated_spec.output_retries,
tools=tools,
builtin_tools=builtin_tools,
prepare_tools=prepare_tools,
prepare_output_tools=prepare_output_tools,
toolsets=toolsets,
defer_model_check=defer_model_check,
end_strategy=end_strategy if end_strategy is not None else validated_spec.end_strategy,
instrument=instrument if instrument is not None else validated_spec.instrument,
metadata=metadata if metadata is not None else validated_spec.metadata,
history_processors=history_processors,
event_stream_handler=event_stream_handler,
tool_timeout=tool_timeout if tool_timeout is not None else validated_spec.tool_timeout,
max_concurrency=max_concurrency,
capabilities=all_capabilities,
)
@overload
@classmethod
def from_file(
cls,
path: Path | str,
*,
fmt: Literal['yaml', 'json'] | None = None,
custom_capability_types: Sequence[type[AbstractCapability[Any]]] = (),
model: models.Model | models.KnownModelName | str | None = None,
output_type: OutputSpec[Any] = str,
instructions: AgentInstructions[Any] = None,
system_prompt: str | Sequence[str] = (),
name: str | None = None,
description: TemplateStr[Any] | str | None = None,
model_settings: ModelSettings | None = None,
retries: int | None = None,
validation_context: Any = None,
output_retries: int | None = None,
tools: Sequence[Tool[Any] | ToolFuncEither[Any, ...]] = (),
builtin_tools: Sequence[AgentBuiltinTool[Any]] = (),
prepare_tools: ToolsPrepareFunc[Any] | None = None,
prepare_output_tools: ToolsPrepareFunc[Any] | None = None,
toolsets: Sequence[AgentToolset[Any]] | None = None,
defer_model_check: bool = False,
end_strategy: EndStrategy | None = None,
instrument: InstrumentationSettings | bool | None = None,
metadata: AgentMetadata[Any] | None = None,
history_processors: Sequence[HistoryProcessor[Any]] | None = None,
event_stream_handler: EventStreamHandler[Any] | None = None,
tool_timeout: float | None = None,
max_concurrency: _concurrency.AnyConcurrencyLimit = None,
capabilities: Sequence[AbstractCapability[Any]] | None = None,
) -> Agent[None, str]: ...
@overload
@classmethod
def from_file(
cls,
path: Path | str,
*,
fmt: Literal['yaml', 'json'] | None = None,
deps_type: type[T],
custom_capability_types: Sequence[type[AbstractCapability[Any]]] = (),
model: models.Model | models.KnownModelName | str | None = None,
output_type: OutputSpec[Any] = str,
instructions: AgentInstructions[Any] = None,
system_prompt: str | Sequence[str] = (),
name: str | None = None,
description: TemplateStr[Any] | str | None = None,
model_settings: ModelSettings | None = None,
retries: int | None = None,
validation_context: Any = None,
output_retries: int | None = None,
tools: Sequence[Tool[Any] | ToolFuncEither[Any, ...]] = (),
builtin_tools: Sequence[AgentBuiltinTool[Any]] = (),
prepare_tools: ToolsPrepareFunc[Any] | None = None,
prepare_output_tools: ToolsPrepareFunc[Any] | None = None,
toolsets: Sequence[AgentToolset[Any]] | None = None,
defer_model_check: bool = False,
end_strategy: EndStrategy | None = None,
instrument: InstrumentationSettings | bool | None = None,
metadata: AgentMetadata[Any] | None = None,
history_processors: Sequence[HistoryProcessor[Any]] | None = None,
event_stream_handler: EventStreamHandler[Any] | None = None,
tool_timeout: float | None = None,
max_concurrency: _concurrency.AnyConcurrencyLimit = None,
capabilities: Sequence[AbstractCapability[Any]] | None = None,
) -> Agent[T, str]: ...
@classmethod
def from_file(
cls,
path: Path | str,
*,
fmt: Literal['yaml', 'json'] | None = None,
deps_type: type[Any] = type(None),
custom_capability_types: Sequence[type[AbstractCapability[Any]]] = (),
model: models.Model | models.KnownModelName | str | None = None,
output_type: OutputSpec[Any] = str,
instructions: AgentInstructions[Any] = None,
system_prompt: str | Sequence[str] = (),
name: str | None = None,
description: TemplateStr[Any] | str | None = None,
model_settings: ModelSettings | None = None,
retries: int | None = None,
validation_context: Any = None,
output_retries: int | None = None,
tools: Sequence[Tool[Any] | ToolFuncEither[Any, ...]] = (),
builtin_tools: Sequence[AgentBuiltinTool[Any]] = (),
prepare_tools: ToolsPrepareFunc[Any] | None = None,
prepare_output_tools: ToolsPrepareFunc[Any] | None = None,
toolsets: Sequence[AgentToolset[Any]] | None = None,
defer_model_check: bool = False,
end_strategy: EndStrategy | None = None,
instrument: InstrumentationSettings | bool | None = None,
metadata: AgentMetadata[Any] | None = None,
history_processors: Sequence[HistoryProcessor[Any]] | None = None,
event_stream_handler: EventStreamHandler[Any] | None = None,
tool_timeout: float | None = None,
max_concurrency: _concurrency.AnyConcurrencyLimit = None,
capabilities: Sequence[AbstractCapability[Any]] | None = None,
) -> Agent[Any, Any]:
"""Construct an Agent from a YAML or JSON spec file.
This is a convenience method equivalent to
`Agent.from_spec(AgentSpec.from_file(path), ...)`.
The file format is inferred from the extension (`.yaml`/`.yml` or `.json`)
unless overridden with the `fmt` argument.
All other arguments are forwarded to [`from_spec`][pydantic_ai.agent.Agent.from_spec].
"""
spec = AgentSpec.from_file(path, fmt=fmt)
return cls.from_spec(
spec,
deps_type=deps_type,
custom_capability_types=custom_capability_types,
model=model,
output_type=output_type,
instructions=instructions,
system_prompt=system_prompt,
name=name,
description=description,
model_settings=model_settings,
retries=retries,
validation_context=validation_context,
output_retries=output_retries,
tools=tools,
builtin_tools=builtin_tools,
prepare_tools=prepare_tools,
prepare_output_tools=prepare_output_tools,
toolsets=toolsets,
defer_model_check=defer_model_check,
end_strategy=end_strategy,
instrument=instrument,
metadata=metadata,
history_processors=history_processors,
event_stream_handler=event_stream_handler,
tool_timeout=tool_timeout,
max_concurrency=max_concurrency,
capabilities=capabilities,
)
@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 description(self) -> str | None:
"""A human-readable description of the agent.
If the description is a TemplateStr, returns the raw template source.
The rendered description is available at runtime via OTel span attributes.
"""
if self._description is None:
return None
return str(self._description)
@description.setter
def description(self, value: TemplateStr[AgentDepsT] | str | None) -> None:
"""Set the description of the agent."""
self._description = 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: AgentInstructions[AgentDepsT] = None,
deps: AgentDepsT = None,
model_settings: AgentModelSettings[AgentDepsT] | 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[AgentBuiltinTool[AgentDepsT]] | None = None,
spec: dict[str, Any] | AgentSpec | 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: AgentInstructions[AgentDepsT] = None,
deps: AgentDepsT = None,
model_settings: AgentModelSettings[AgentDepsT] | 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[AgentBuiltinTool[AgentDepsT]] | None = None,
spec: dict[str, Any] | AgentSpec | None = None,
) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, RunOutputDataT]]: ...
@asynccontextmanager
async def iter( # noqa: C901
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: AgentInstructions[AgentDepsT] = None,
deps: AgentDepsT = None,
model_settings: AgentModelSettings[AgentDepsT] | 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[AgentBuiltinTool[AgentDepsT]] | None = None,
spec: dict[str, Any] | AgentSpec | 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-5.2')
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(