pydantic-ai-slim 1.9.0__py3-none-any.whl → 1.12.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pydantic_ai/_agent_graph.py +18 -14
- pydantic_ai/_output.py +20 -105
- pydantic_ai/_run_context.py +8 -2
- pydantic_ai/_tool_manager.py +30 -11
- pydantic_ai/_utils.py +18 -0
- pydantic_ai/agent/__init__.py +34 -32
- pydantic_ai/agent/abstract.py +155 -3
- pydantic_ai/agent/wrapper.py +5 -0
- pydantic_ai/common_tools/duckduckgo.py +1 -1
- pydantic_ai/durable_exec/dbos/_agent.py +28 -0
- pydantic_ai/durable_exec/prefect/_agent.py +25 -0
- pydantic_ai/durable_exec/temporal/_agent.py +25 -0
- pydantic_ai/durable_exec/temporal/_function_toolset.py +23 -73
- pydantic_ai/durable_exec/temporal/_mcp_server.py +30 -30
- pydantic_ai/durable_exec/temporal/_run_context.py +9 -3
- pydantic_ai/durable_exec/temporal/_toolset.py +67 -3
- pydantic_ai/mcp.py +4 -4
- pydantic_ai/messages.py +11 -2
- pydantic_ai/models/__init__.py +80 -35
- pydantic_ai/models/anthropic.py +27 -8
- pydantic_ai/models/bedrock.py +3 -3
- pydantic_ai/models/cohere.py +5 -3
- pydantic_ai/models/fallback.py +25 -4
- pydantic_ai/models/function.py +8 -0
- pydantic_ai/models/gemini.py +3 -3
- pydantic_ai/models/google.py +25 -22
- pydantic_ai/models/groq.py +5 -3
- pydantic_ai/models/huggingface.py +3 -3
- pydantic_ai/models/instrumented.py +29 -13
- pydantic_ai/models/mistral.py +6 -4
- pydantic_ai/models/openai.py +15 -6
- pydantic_ai/models/outlines.py +21 -12
- pydantic_ai/models/wrapper.py +1 -1
- pydantic_ai/output.py +3 -2
- pydantic_ai/profiles/openai.py +5 -2
- pydantic_ai/providers/anthropic.py +2 -2
- pydantic_ai/providers/openrouter.py +3 -0
- pydantic_ai/result.py +159 -4
- pydantic_ai/tools.py +12 -10
- pydantic_ai/ui/_adapter.py +2 -2
- pydantic_ai/ui/_event_stream.py +4 -4
- pydantic_ai/ui/ag_ui/_event_stream.py +11 -2
- pydantic_ai/ui/ag_ui/app.py +8 -1
- {pydantic_ai_slim-1.9.0.dist-info → pydantic_ai_slim-1.12.0.dist-info}/METADATA +9 -7
- {pydantic_ai_slim-1.9.0.dist-info → pydantic_ai_slim-1.12.0.dist-info}/RECORD +48 -48
- {pydantic_ai_slim-1.9.0.dist-info → pydantic_ai_slim-1.12.0.dist-info}/WHEEL +0 -0
- {pydantic_ai_slim-1.9.0.dist-info → pydantic_ai_slim-1.12.0.dist-info}/entry_points.txt +0 -0
- {pydantic_ai_slim-1.9.0.dist-info → pydantic_ai_slim-1.12.0.dist-info}/licenses/LICENSE +0 -0
pydantic_ai/agent/__init__.py
CHANGED
|
@@ -8,7 +8,7 @@ from asyncio import Lock
|
|
|
8
8
|
from collections.abc import AsyncIterator, Awaitable, Callable, Iterator, Sequence
|
|
9
9
|
from contextlib import AbstractAsyncContextManager, AsyncExitStack, asynccontextmanager, contextmanager
|
|
10
10
|
from contextvars import ContextVar
|
|
11
|
-
from typing import TYPE_CHECKING, Any, ClassVar,
|
|
11
|
+
from typing import TYPE_CHECKING, Any, ClassVar, overload
|
|
12
12
|
|
|
13
13
|
from opentelemetry.trace import NoOpTracer, use_span
|
|
14
14
|
from pydantic.json_schema import GenerateJsonSchema
|
|
@@ -39,7 +39,6 @@ from .._tool_manager import ToolManager
|
|
|
39
39
|
from ..builtin_tools import AbstractBuiltinTool
|
|
40
40
|
from ..models.instrumented import InstrumentationSettings, InstrumentedModel, instrument_model
|
|
41
41
|
from ..output import OutputDataT, OutputSpec
|
|
42
|
-
from ..profiles import ModelProfile
|
|
43
42
|
from ..run import AgentRun, AgentRunResult
|
|
44
43
|
from ..settings import ModelSettings, merge_model_settings
|
|
45
44
|
from ..tools import (
|
|
@@ -133,7 +132,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
133
132
|
_instrument_default: ClassVar[InstrumentationSettings | bool] = False
|
|
134
133
|
|
|
135
134
|
_deps_type: type[AgentDepsT] = dataclasses.field(repr=False)
|
|
136
|
-
_output_schema: _output.
|
|
135
|
+
_output_schema: _output.OutputSchema[OutputDataT] = dataclasses.field(repr=False)
|
|
137
136
|
_output_validators: list[_output.OutputValidator[AgentDepsT, OutputDataT]] = dataclasses.field(repr=False)
|
|
138
137
|
_instructions: list[str | _system_prompt.SystemPromptFunc[AgentDepsT]] = dataclasses.field(repr=False)
|
|
139
138
|
_system_prompts: tuple[str, ...] = dataclasses.field(repr=False)
|
|
@@ -238,7 +237,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
238
237
|
output_type: The type of the output data, used to validate the data returned by the model,
|
|
239
238
|
defaults to `str`.
|
|
240
239
|
instructions: Instructions to use for this agent, you can also register instructions via a function with
|
|
241
|
-
[`instructions`][pydantic_ai.Agent.instructions].
|
|
240
|
+
[`instructions`][pydantic_ai.Agent.instructions] or pass additional, temporary, instructions when executing a run.
|
|
242
241
|
system_prompt: Static system prompts to use for this agent, you can also register system
|
|
243
242
|
prompts via a function with [`system_prompt`][pydantic_ai.Agent.system_prompt].
|
|
244
243
|
deps_type: The type used for dependency injection, this parameter exists solely to allow you to fully
|
|
@@ -303,11 +302,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
303
302
|
|
|
304
303
|
_utils.validate_empty_kwargs(_deprecated_kwargs)
|
|
305
304
|
|
|
306
|
-
|
|
307
|
-
self.model.profile.default_structured_output_mode if isinstance(self.model, models.Model) else None
|
|
308
|
-
)
|
|
309
|
-
|
|
310
|
-
self._output_schema = _output.OutputSchema[OutputDataT].build(output_type, default_mode=default_output_mode)
|
|
305
|
+
self._output_schema = _output.OutputSchema[OutputDataT].build(output_type)
|
|
311
306
|
self._output_validators = []
|
|
312
307
|
|
|
313
308
|
self._instructions = self._normalize_instructions(instructions)
|
|
@@ -418,6 +413,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
418
413
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
419
414
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
420
415
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
416
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
421
417
|
deps: AgentDepsT = None,
|
|
422
418
|
model_settings: ModelSettings | None = None,
|
|
423
419
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -436,6 +432,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
436
432
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
437
433
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
438
434
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
435
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
439
436
|
deps: AgentDepsT = None,
|
|
440
437
|
model_settings: ModelSettings | None = None,
|
|
441
438
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -450,10 +447,11 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
450
447
|
self,
|
|
451
448
|
user_prompt: str | Sequence[_messages.UserContent] | None = None,
|
|
452
449
|
*,
|
|
453
|
-
output_type: OutputSpec[
|
|
450
|
+
output_type: OutputSpec[Any] | None = None,
|
|
454
451
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
455
452
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
456
453
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
454
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
457
455
|
deps: AgentDepsT = None,
|
|
458
456
|
model_settings: ModelSettings | None = None,
|
|
459
457
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -527,6 +525,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
527
525
|
message_history: History of the conversation so far.
|
|
528
526
|
deferred_tool_results: Optional results for deferred tool calls in the message history.
|
|
529
527
|
model: Optional model to use for this run, required if `model` was not set when creating the agent.
|
|
528
|
+
instructions: Optional additional instructions to use for this run.
|
|
530
529
|
deps: Optional dependencies to use for this run.
|
|
531
530
|
model_settings: Optional settings to use for this model's request.
|
|
532
531
|
usage_limits: Optional limits on model request count or token usage.
|
|
@@ -545,18 +544,18 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
545
544
|
del model
|
|
546
545
|
|
|
547
546
|
deps = self._get_deps(deps)
|
|
548
|
-
output_schema = self._prepare_output_schema(output_type
|
|
547
|
+
output_schema = self._prepare_output_schema(output_type)
|
|
549
548
|
|
|
550
549
|
output_type_ = output_type or self.output_type
|
|
551
550
|
|
|
552
551
|
# We consider it a user error if a user tries to restrict the result type while having an output validator that
|
|
553
552
|
# may change the result type from the restricted type to something else. Therefore, we consider the following
|
|
554
553
|
# typecast reasonable, even though it is possible to violate it with otherwise-type-checked code.
|
|
555
|
-
output_validators =
|
|
554
|
+
output_validators = self._output_validators
|
|
556
555
|
|
|
557
556
|
output_toolset = self._output_toolset
|
|
558
557
|
if output_schema != self._output_schema or output_validators:
|
|
559
|
-
output_toolset =
|
|
558
|
+
output_toolset = output_schema.toolset
|
|
560
559
|
if output_toolset:
|
|
561
560
|
output_toolset.max_retries = self._max_result_retries
|
|
562
561
|
output_toolset.output_validators = output_validators
|
|
@@ -580,7 +579,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
580
579
|
model_settings = merge_model_settings(merged_settings, model_settings)
|
|
581
580
|
usage_limits = usage_limits or _usage.UsageLimits()
|
|
582
581
|
|
|
583
|
-
instructions_literal, instructions_functions = self._get_instructions()
|
|
582
|
+
instructions_literal, instructions_functions = self._get_instructions(additional_instructions=instructions)
|
|
584
583
|
|
|
585
584
|
async def get_instructions(run_context: RunContext[AgentDepsT]) -> str | None:
|
|
586
585
|
parts = [
|
|
@@ -588,11 +587,6 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
588
587
|
*[await func.run(run_context) for func in instructions_functions],
|
|
589
588
|
]
|
|
590
589
|
|
|
591
|
-
model_profile = model_used.profile
|
|
592
|
-
if isinstance(output_schema, _output.PromptedOutputSchema):
|
|
593
|
-
instructions = output_schema.instructions(model_profile.prompted_output_template)
|
|
594
|
-
parts.append(instructions)
|
|
595
|
-
|
|
596
590
|
parts = [p for p in parts if p]
|
|
597
591
|
if not parts:
|
|
598
592
|
return None
|
|
@@ -605,7 +599,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
605
599
|
instrumentation_settings = None
|
|
606
600
|
tracer = NoOpTracer()
|
|
607
601
|
|
|
608
|
-
graph_deps = _agent_graph.GraphAgentDeps[AgentDepsT,
|
|
602
|
+
graph_deps = _agent_graph.GraphAgentDeps[AgentDepsT, OutputDataT](
|
|
609
603
|
user_deps=deps,
|
|
610
604
|
prompt=user_prompt,
|
|
611
605
|
new_message_index=len(message_history) if message_history else 0,
|
|
@@ -1330,9 +1324,15 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
1330
1324
|
|
|
1331
1325
|
def _get_instructions(
|
|
1332
1326
|
self,
|
|
1327
|
+
additional_instructions: Instructions[AgentDepsT] = None,
|
|
1333
1328
|
) -> tuple[str | None, list[_system_prompt.SystemPromptRunner[AgentDepsT]]]:
|
|
1334
1329
|
override_instructions = self._override_instructions.get()
|
|
1335
|
-
|
|
1330
|
+
if override_instructions:
|
|
1331
|
+
instructions = override_instructions.value
|
|
1332
|
+
else:
|
|
1333
|
+
instructions = self._instructions.copy()
|
|
1334
|
+
if additional_instructions is not None:
|
|
1335
|
+
instructions.extend(self._normalize_instructions(additional_instructions))
|
|
1336
1336
|
|
|
1337
1337
|
literal_parts: list[str] = []
|
|
1338
1338
|
functions: list[_system_prompt.SystemPromptRunner[AgentDepsT]] = []
|
|
@@ -1408,21 +1408,23 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
1408
1408
|
|
|
1409
1409
|
return toolsets
|
|
1410
1410
|
|
|
1411
|
+
@overload
|
|
1412
|
+
def _prepare_output_schema(self, output_type: None) -> _output.OutputSchema[OutputDataT]: ...
|
|
1413
|
+
|
|
1414
|
+
@overload
|
|
1411
1415
|
def _prepare_output_schema(
|
|
1412
|
-
self, output_type: OutputSpec[RunOutputDataT]
|
|
1413
|
-
) -> _output.OutputSchema[RunOutputDataT]:
|
|
1416
|
+
self, output_type: OutputSpec[RunOutputDataT]
|
|
1417
|
+
) -> _output.OutputSchema[RunOutputDataT]: ...
|
|
1418
|
+
|
|
1419
|
+
def _prepare_output_schema(self, output_type: OutputSpec[Any] | None) -> _output.OutputSchema[Any]:
|
|
1414
1420
|
if output_type is not None:
|
|
1415
1421
|
if self._output_validators:
|
|
1416
1422
|
raise exceptions.UserError('Cannot set a custom run `output_type` when the agent has output validators')
|
|
1417
|
-
schema = _output.OutputSchema
|
|
1418
|
-
output_type, default_mode=model_profile.default_structured_output_mode
|
|
1419
|
-
)
|
|
1423
|
+
schema = _output.OutputSchema.build(output_type)
|
|
1420
1424
|
else:
|
|
1421
|
-
schema = self._output_schema
|
|
1422
|
-
|
|
1423
|
-
schema.raise_if_unsupported(model_profile)
|
|
1425
|
+
schema = self._output_schema
|
|
1424
1426
|
|
|
1425
|
-
return schema
|
|
1427
|
+
return schema
|
|
1426
1428
|
|
|
1427
1429
|
async def __aenter__(self) -> Self:
|
|
1428
1430
|
"""Enter the agent context.
|
|
@@ -1492,7 +1494,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
1492
1494
|
|
|
1493
1495
|
@dataclasses.dataclass(init=False)
|
|
1494
1496
|
class _AgentFunctionToolset(FunctionToolset[AgentDepsT]):
|
|
1495
|
-
output_schema: _output.
|
|
1497
|
+
output_schema: _output.OutputSchema[Any]
|
|
1496
1498
|
|
|
1497
1499
|
def __init__(
|
|
1498
1500
|
self,
|
|
@@ -1500,7 +1502,7 @@ class _AgentFunctionToolset(FunctionToolset[AgentDepsT]):
|
|
|
1500
1502
|
*,
|
|
1501
1503
|
max_retries: int = 1,
|
|
1502
1504
|
id: str | None = None,
|
|
1503
|
-
output_schema: _output.
|
|
1505
|
+
output_schema: _output.OutputSchema[Any],
|
|
1504
1506
|
):
|
|
1505
1507
|
self.output_schema = output_schema
|
|
1506
1508
|
super().__init__(tools, max_retries=max_retries, id=id)
|
pydantic_ai/agent/abstract.py
CHANGED
|
@@ -12,7 +12,6 @@ import anyio
|
|
|
12
12
|
from typing_extensions import Self, TypeIs, TypeVar
|
|
13
13
|
|
|
14
14
|
from pydantic_graph import End
|
|
15
|
-
from pydantic_graph._utils import get_event_loop
|
|
16
15
|
|
|
17
16
|
from .. import (
|
|
18
17
|
_agent_graph,
|
|
@@ -132,6 +131,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
132
131
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
133
132
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
134
133
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
134
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
135
135
|
deps: AgentDepsT = None,
|
|
136
136
|
model_settings: ModelSettings | None = None,
|
|
137
137
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -151,6 +151,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
151
151
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
152
152
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
153
153
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
154
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
154
155
|
deps: AgentDepsT = None,
|
|
155
156
|
model_settings: ModelSettings | None = None,
|
|
156
157
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -169,6 +170,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
169
170
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
170
171
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
171
172
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
173
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
172
174
|
deps: AgentDepsT = None,
|
|
173
175
|
model_settings: ModelSettings | None = None,
|
|
174
176
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -202,6 +204,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
202
204
|
message_history: History of the conversation so far.
|
|
203
205
|
deferred_tool_results: Optional results for deferred tool calls in the message history.
|
|
204
206
|
model: Optional model to use for this run, required if `model` was not set when creating the agent.
|
|
207
|
+
instructions: Optional additional instructions to use for this run.
|
|
205
208
|
deps: Optional dependencies to use for this run.
|
|
206
209
|
model_settings: Optional settings to use for this model's request.
|
|
207
210
|
usage_limits: Optional limits on model request count or token usage.
|
|
@@ -225,6 +228,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
225
228
|
message_history=message_history,
|
|
226
229
|
deferred_tool_results=deferred_tool_results,
|
|
227
230
|
model=model,
|
|
231
|
+
instructions=instructions,
|
|
228
232
|
deps=deps,
|
|
229
233
|
model_settings=model_settings,
|
|
230
234
|
usage_limits=usage_limits,
|
|
@@ -251,6 +255,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
251
255
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
252
256
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
253
257
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
258
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
254
259
|
deps: AgentDepsT = None,
|
|
255
260
|
model_settings: ModelSettings | None = None,
|
|
256
261
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -270,6 +275,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
270
275
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
271
276
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
272
277
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
278
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
273
279
|
deps: AgentDepsT = None,
|
|
274
280
|
model_settings: ModelSettings | None = None,
|
|
275
281
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -288,6 +294,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
288
294
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
289
295
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
290
296
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
297
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
291
298
|
deps: AgentDepsT = None,
|
|
292
299
|
model_settings: ModelSettings | None = None,
|
|
293
300
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -320,6 +327,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
320
327
|
message_history: History of the conversation so far.
|
|
321
328
|
deferred_tool_results: Optional results for deferred tool calls in the message history.
|
|
322
329
|
model: Optional model to use for this run, required if `model` was not set when creating the agent.
|
|
330
|
+
instructions: Optional additional instructions to use for this run.
|
|
323
331
|
deps: Optional dependencies to use for this run.
|
|
324
332
|
model_settings: Optional settings to use for this model's request.
|
|
325
333
|
usage_limits: Optional limits on model request count or token usage.
|
|
@@ -335,13 +343,14 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
335
343
|
if infer_name and self.name is None:
|
|
336
344
|
self._infer_name(inspect.currentframe())
|
|
337
345
|
|
|
338
|
-
return get_event_loop().run_until_complete(
|
|
346
|
+
return _utils.get_event_loop().run_until_complete(
|
|
339
347
|
self.run(
|
|
340
348
|
user_prompt,
|
|
341
349
|
output_type=output_type,
|
|
342
350
|
message_history=message_history,
|
|
343
351
|
deferred_tool_results=deferred_tool_results,
|
|
344
352
|
model=model,
|
|
353
|
+
instructions=instructions,
|
|
345
354
|
deps=deps,
|
|
346
355
|
model_settings=model_settings,
|
|
347
356
|
usage_limits=usage_limits,
|
|
@@ -362,6 +371,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
362
371
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
363
372
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
364
373
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
374
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
365
375
|
deps: AgentDepsT = None,
|
|
366
376
|
model_settings: ModelSettings | None = None,
|
|
367
377
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -381,6 +391,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
381
391
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
382
392
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
383
393
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
394
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
384
395
|
deps: AgentDepsT = None,
|
|
385
396
|
model_settings: ModelSettings | None = None,
|
|
386
397
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -400,6 +411,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
400
411
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
401
412
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
402
413
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
414
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
403
415
|
deps: AgentDepsT = None,
|
|
404
416
|
model_settings: ModelSettings | None = None,
|
|
405
417
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -440,6 +452,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
440
452
|
message_history: History of the conversation so far.
|
|
441
453
|
deferred_tool_results: Optional results for deferred tool calls in the message history.
|
|
442
454
|
model: Optional model to use for this run, required if `model` was not set when creating the agent.
|
|
455
|
+
instructions: Optional additional instructions to use for this run.
|
|
443
456
|
deps: Optional dependencies to use for this run.
|
|
444
457
|
model_settings: Optional settings to use for this model's request.
|
|
445
458
|
usage_limits: Optional limits on model request count or token usage.
|
|
@@ -469,6 +482,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
469
482
|
deferred_tool_results=deferred_tool_results,
|
|
470
483
|
model=model,
|
|
471
484
|
deps=deps,
|
|
485
|
+
instructions=instructions,
|
|
472
486
|
model_settings=model_settings,
|
|
473
487
|
usage_limits=usage_limits,
|
|
474
488
|
usage=usage,
|
|
@@ -581,6 +595,133 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
581
595
|
if not yielded:
|
|
582
596
|
raise exceptions.AgentRunError('Agent run finished without producing a final result') # pragma: no cover
|
|
583
597
|
|
|
598
|
+
@overload
|
|
599
|
+
def run_stream_sync(
|
|
600
|
+
self,
|
|
601
|
+
user_prompt: str | Sequence[_messages.UserContent] | None = None,
|
|
602
|
+
*,
|
|
603
|
+
output_type: None = None,
|
|
604
|
+
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
605
|
+
deferred_tool_results: DeferredToolResults | None = None,
|
|
606
|
+
model: models.Model | models.KnownModelName | str | None = None,
|
|
607
|
+
deps: AgentDepsT = None,
|
|
608
|
+
model_settings: ModelSettings | None = None,
|
|
609
|
+
usage_limits: _usage.UsageLimits | None = None,
|
|
610
|
+
usage: _usage.RunUsage | None = None,
|
|
611
|
+
infer_name: bool = True,
|
|
612
|
+
toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None,
|
|
613
|
+
builtin_tools: Sequence[AbstractBuiltinTool] | None = None,
|
|
614
|
+
event_stream_handler: EventStreamHandler[AgentDepsT] | None = None,
|
|
615
|
+
) -> result.StreamedRunResultSync[AgentDepsT, OutputDataT]: ...
|
|
616
|
+
|
|
617
|
+
@overload
|
|
618
|
+
def run_stream_sync(
|
|
619
|
+
self,
|
|
620
|
+
user_prompt: str | Sequence[_messages.UserContent] | None = None,
|
|
621
|
+
*,
|
|
622
|
+
output_type: OutputSpec[RunOutputDataT],
|
|
623
|
+
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
624
|
+
deferred_tool_results: DeferredToolResults | None = None,
|
|
625
|
+
model: models.Model | models.KnownModelName | str | None = None,
|
|
626
|
+
deps: AgentDepsT = None,
|
|
627
|
+
model_settings: ModelSettings | None = None,
|
|
628
|
+
usage_limits: _usage.UsageLimits | None = None,
|
|
629
|
+
usage: _usage.RunUsage | None = None,
|
|
630
|
+
infer_name: bool = True,
|
|
631
|
+
toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None,
|
|
632
|
+
builtin_tools: Sequence[AbstractBuiltinTool] | None = None,
|
|
633
|
+
event_stream_handler: EventStreamHandler[AgentDepsT] | None = None,
|
|
634
|
+
) -> result.StreamedRunResultSync[AgentDepsT, RunOutputDataT]: ...
|
|
635
|
+
|
|
636
|
+
def run_stream_sync(
|
|
637
|
+
self,
|
|
638
|
+
user_prompt: str | Sequence[_messages.UserContent] | None = None,
|
|
639
|
+
*,
|
|
640
|
+
output_type: OutputSpec[RunOutputDataT] | None = None,
|
|
641
|
+
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
642
|
+
deferred_tool_results: DeferredToolResults | None = None,
|
|
643
|
+
model: models.Model | models.KnownModelName | str | None = None,
|
|
644
|
+
deps: AgentDepsT = None,
|
|
645
|
+
model_settings: ModelSettings | None = None,
|
|
646
|
+
usage_limits: _usage.UsageLimits | None = None,
|
|
647
|
+
usage: _usage.RunUsage | None = None,
|
|
648
|
+
infer_name: bool = True,
|
|
649
|
+
toolsets: Sequence[AbstractToolset[AgentDepsT]] | None = None,
|
|
650
|
+
builtin_tools: Sequence[AbstractBuiltinTool] | None = None,
|
|
651
|
+
event_stream_handler: EventStreamHandler[AgentDepsT] | None = None,
|
|
652
|
+
) -> result.StreamedRunResultSync[AgentDepsT, Any]:
|
|
653
|
+
"""Run the agent with a user prompt in sync streaming mode.
|
|
654
|
+
|
|
655
|
+
This is a convenience method that wraps [`run_stream()`][pydantic_ai.agent.AbstractAgent.run_stream] with `loop.run_until_complete(...)`.
|
|
656
|
+
You therefore can't use this method inside async code or if there's an active event loop.
|
|
657
|
+
|
|
658
|
+
This method builds an internal agent graph (using system prompts, tools and output schemas) and then
|
|
659
|
+
runs the graph until the model produces output matching the `output_type`, for example text or structured data.
|
|
660
|
+
At this point, a streaming run result object is yielded from which you can stream the output as it comes in,
|
|
661
|
+
and -- once this output has completed streaming -- get the complete output, message history, and usage.
|
|
662
|
+
|
|
663
|
+
As this method will consider the first output matching the `output_type` to be the final output,
|
|
664
|
+
it will stop running the agent graph and will not execute any tool calls made by the model after this "final" output.
|
|
665
|
+
If you want to always run the agent graph to completion and stream events and output at the same time,
|
|
666
|
+
use [`agent.run()`][pydantic_ai.agent.AbstractAgent.run] with an `event_stream_handler` or [`agent.iter()`][pydantic_ai.agent.AbstractAgent.iter] instead.
|
|
667
|
+
|
|
668
|
+
Example:
|
|
669
|
+
```python
|
|
670
|
+
from pydantic_ai import Agent
|
|
671
|
+
|
|
672
|
+
agent = Agent('openai:gpt-4o')
|
|
673
|
+
|
|
674
|
+
def main():
|
|
675
|
+
response = agent.run_stream_sync('What is the capital of the UK?')
|
|
676
|
+
print(response.get_output())
|
|
677
|
+
#> The capital of the UK is London.
|
|
678
|
+
```
|
|
679
|
+
|
|
680
|
+
Args:
|
|
681
|
+
user_prompt: User input to start/continue the conversation.
|
|
682
|
+
output_type: Custom output type to use for this run, `output_type` may only be used if the agent has no
|
|
683
|
+
output validators since output validators would expect an argument that matches the agent's output type.
|
|
684
|
+
message_history: History of the conversation so far.
|
|
685
|
+
deferred_tool_results: Optional results for deferred tool calls in the message history.
|
|
686
|
+
model: Optional model to use for this run, required if `model` was not set when creating the agent.
|
|
687
|
+
deps: Optional dependencies to use for this run.
|
|
688
|
+
model_settings: Optional settings to use for this model's request.
|
|
689
|
+
usage_limits: Optional limits on model request count or token usage.
|
|
690
|
+
usage: Optional usage to start with, useful for resuming a conversation or agents used in tools.
|
|
691
|
+
infer_name: Whether to try to infer the agent name from the call frame if it's not set.
|
|
692
|
+
toolsets: Optional additional toolsets for this run.
|
|
693
|
+
builtin_tools: Optional additional builtin tools for this run.
|
|
694
|
+
event_stream_handler: Optional handler for events from the model's streaming response and the agent's execution of tools to use for this run.
|
|
695
|
+
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.
|
|
696
|
+
Note that it does _not_ receive any events after the final result is found.
|
|
697
|
+
|
|
698
|
+
Returns:
|
|
699
|
+
The result of the run.
|
|
700
|
+
"""
|
|
701
|
+
if infer_name and self.name is None:
|
|
702
|
+
self._infer_name(inspect.currentframe())
|
|
703
|
+
|
|
704
|
+
async def _consume_stream():
|
|
705
|
+
async with self.run_stream(
|
|
706
|
+
user_prompt,
|
|
707
|
+
output_type=output_type,
|
|
708
|
+
message_history=message_history,
|
|
709
|
+
deferred_tool_results=deferred_tool_results,
|
|
710
|
+
model=model,
|
|
711
|
+
deps=deps,
|
|
712
|
+
model_settings=model_settings,
|
|
713
|
+
usage_limits=usage_limits,
|
|
714
|
+
usage=usage,
|
|
715
|
+
infer_name=infer_name,
|
|
716
|
+
toolsets=toolsets,
|
|
717
|
+
builtin_tools=builtin_tools,
|
|
718
|
+
event_stream_handler=event_stream_handler,
|
|
719
|
+
) as stream_result:
|
|
720
|
+
yield stream_result
|
|
721
|
+
|
|
722
|
+
async_result = _utils.get_event_loop().run_until_complete(anext(_consume_stream()))
|
|
723
|
+
return result.StreamedRunResultSync(async_result)
|
|
724
|
+
|
|
584
725
|
@overload
|
|
585
726
|
def run_stream_events(
|
|
586
727
|
self,
|
|
@@ -590,6 +731,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
590
731
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
591
732
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
592
733
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
734
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
593
735
|
deps: AgentDepsT = None,
|
|
594
736
|
model_settings: ModelSettings | None = None,
|
|
595
737
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -608,6 +750,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
608
750
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
609
751
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
610
752
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
753
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
611
754
|
deps: AgentDepsT = None,
|
|
612
755
|
model_settings: ModelSettings | None = None,
|
|
613
756
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -625,6 +768,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
625
768
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
626
769
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
627
770
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
771
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
628
772
|
deps: AgentDepsT = None,
|
|
629
773
|
model_settings: ModelSettings | None = None,
|
|
630
774
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -674,6 +818,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
674
818
|
message_history: History of the conversation so far.
|
|
675
819
|
deferred_tool_results: Optional results for deferred tool calls in the message history.
|
|
676
820
|
model: Optional model to use for this run, required if `model` was not set when creating the agent.
|
|
821
|
+
instructions: Optional additional instructions to use for this run.
|
|
677
822
|
deps: Optional dependencies to use for this run.
|
|
678
823
|
model_settings: Optional settings to use for this model's request.
|
|
679
824
|
usage_limits: Optional limits on model request count or token usage.
|
|
@@ -698,6 +843,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
698
843
|
message_history=message_history,
|
|
699
844
|
deferred_tool_results=deferred_tool_results,
|
|
700
845
|
model=model,
|
|
846
|
+
instructions=instructions,
|
|
701
847
|
deps=deps,
|
|
702
848
|
model_settings=model_settings,
|
|
703
849
|
usage_limits=usage_limits,
|
|
@@ -714,6 +860,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
714
860
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
715
861
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
716
862
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
863
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
717
864
|
deps: AgentDepsT = None,
|
|
718
865
|
model_settings: ModelSettings | None = None,
|
|
719
866
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -739,6 +886,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
739
886
|
message_history=message_history,
|
|
740
887
|
deferred_tool_results=deferred_tool_results,
|
|
741
888
|
model=model,
|
|
889
|
+
instructions=instructions,
|
|
742
890
|
deps=deps,
|
|
743
891
|
model_settings=model_settings,
|
|
744
892
|
usage_limits=usage_limits,
|
|
@@ -767,6 +915,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
767
915
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
768
916
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
769
917
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
918
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
770
919
|
deps: AgentDepsT = None,
|
|
771
920
|
model_settings: ModelSettings | None = None,
|
|
772
921
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -785,6 +934,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
785
934
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
786
935
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
787
936
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
937
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
788
938
|
deps: AgentDepsT = None,
|
|
789
939
|
model_settings: ModelSettings | None = None,
|
|
790
940
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -804,6 +954,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
804
954
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
805
955
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
806
956
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
957
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
807
958
|
deps: AgentDepsT = None,
|
|
808
959
|
model_settings: ModelSettings | None = None,
|
|
809
960
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -877,6 +1028,7 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
877
1028
|
message_history: History of the conversation so far.
|
|
878
1029
|
deferred_tool_results: Optional results for deferred tool calls in the message history.
|
|
879
1030
|
model: Optional model to use for this run, required if `model` was not set when creating the agent.
|
|
1031
|
+
instructions: Optional additional instructions to use for this run.
|
|
880
1032
|
deps: Optional dependencies to use for this run.
|
|
881
1033
|
model_settings: Optional settings to use for this model's request.
|
|
882
1034
|
usage_limits: Optional limits on model request count or token usage.
|
|
@@ -1217,6 +1369,6 @@ class AbstractAgent(Generic[AgentDepsT, OutputDataT], ABC):
|
|
|
1217
1369
|
agent.to_cli_sync(prog_name='assistant')
|
|
1218
1370
|
```
|
|
1219
1371
|
"""
|
|
1220
|
-
return get_event_loop().run_until_complete(
|
|
1372
|
+
return _utils.get_event_loop().run_until_complete(
|
|
1221
1373
|
self.to_cli(deps=deps, prog_name=prog_name, message_history=message_history)
|
|
1222
1374
|
)
|
pydantic_ai/agent/wrapper.py
CHANGED
|
@@ -76,6 +76,7 @@ class WrapperAgent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
76
76
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
77
77
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
78
78
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
79
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
79
80
|
deps: AgentDepsT = None,
|
|
80
81
|
model_settings: ModelSettings | None = None,
|
|
81
82
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -94,6 +95,7 @@ class WrapperAgent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
94
95
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
95
96
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
96
97
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
98
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
97
99
|
deps: AgentDepsT = None,
|
|
98
100
|
model_settings: ModelSettings | None = None,
|
|
99
101
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -112,6 +114,7 @@ class WrapperAgent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
112
114
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
113
115
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
114
116
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
117
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
115
118
|
deps: AgentDepsT = None,
|
|
116
119
|
model_settings: ModelSettings | None = None,
|
|
117
120
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -185,6 +188,7 @@ class WrapperAgent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
185
188
|
message_history: History of the conversation so far.
|
|
186
189
|
deferred_tool_results: Optional results for deferred tool calls in the message history.
|
|
187
190
|
model: Optional model to use for this run, required if `model` was not set when creating the agent.
|
|
191
|
+
instructions: Optional additional instructions to use for this run.
|
|
188
192
|
deps: Optional dependencies to use for this run.
|
|
189
193
|
model_settings: Optional settings to use for this model's request.
|
|
190
194
|
usage_limits: Optional limits on model request count or token usage.
|
|
@@ -202,6 +206,7 @@ class WrapperAgent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
202
206
|
message_history=message_history,
|
|
203
207
|
deferred_tool_results=deferred_tool_results,
|
|
204
208
|
model=model,
|
|
209
|
+
instructions=instructions,
|
|
205
210
|
deps=deps,
|
|
206
211
|
model_settings=model_settings,
|
|
207
212
|
usage_limits=usage_limits,
|