pydantic-ai-slim 1.9.1__py3-none-any.whl → 1.11.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.
Potentially problematic release.
This version of pydantic-ai-slim might be problematic. Click here for more details.
- pydantic_ai/_run_context.py +8 -2
- pydantic_ai/_tool_manager.py +1 -0
- pydantic_ai/_utils.py +18 -0
- pydantic_ai/agent/__init__.py +13 -3
- pydantic_ai/agent/abstract.py +155 -3
- pydantic_ai/agent/wrapper.py +5 -0
- 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/messages.py +10 -1
- pydantic_ai/models/openai.py +4 -0
- pydantic_ai/profiles/openai.py +5 -2
- pydantic_ai/providers/openrouter.py +3 -0
- pydantic_ai/result.py +159 -4
- pydantic_ai/tools.py +10 -6
- pydantic_ai/ui/_event_stream.py +4 -4
- pydantic_ai/ui/ag_ui/_event_stream.py +11 -2
- {pydantic_ai_slim-1.9.1.dist-info → pydantic_ai_slim-1.11.0.dist-info}/METADATA +8 -6
- {pydantic_ai_slim-1.9.1.dist-info → pydantic_ai_slim-1.11.0.dist-info}/RECORD +26 -26
- {pydantic_ai_slim-1.9.1.dist-info → pydantic_ai_slim-1.11.0.dist-info}/WHEEL +0 -0
- {pydantic_ai_slim-1.9.1.dist-info → pydantic_ai_slim-1.11.0.dist-info}/entry_points.txt +0 -0
- {pydantic_ai_slim-1.9.1.dist-info → pydantic_ai_slim-1.11.0.dist-info}/licenses/LICENSE +0 -0
pydantic_ai/_run_context.py
CHANGED
|
@@ -16,15 +16,19 @@ if TYPE_CHECKING:
|
|
|
16
16
|
from .models import Model
|
|
17
17
|
from .result import RunUsage
|
|
18
18
|
|
|
19
|
+
# TODO (v2): Change the default for all typevars like this from `None` to `object`
|
|
19
20
|
AgentDepsT = TypeVar('AgentDepsT', default=None, contravariant=True)
|
|
20
21
|
"""Type variable for agent dependencies."""
|
|
21
22
|
|
|
23
|
+
RunContextAgentDepsT = TypeVar('RunContextAgentDepsT', default=None, covariant=True)
|
|
24
|
+
"""Type variable for the agent dependencies in `RunContext`."""
|
|
25
|
+
|
|
22
26
|
|
|
23
27
|
@dataclasses.dataclass(repr=False, kw_only=True)
|
|
24
|
-
class RunContext(Generic[
|
|
28
|
+
class RunContext(Generic[RunContextAgentDepsT]):
|
|
25
29
|
"""Information about the current call."""
|
|
26
30
|
|
|
27
|
-
deps:
|
|
31
|
+
deps: RunContextAgentDepsT
|
|
28
32
|
"""Dependencies for the agent."""
|
|
29
33
|
model: Model
|
|
30
34
|
"""The model used in this run."""
|
|
@@ -54,6 +58,8 @@ class RunContext(Generic[AgentDepsT]):
|
|
|
54
58
|
"""The current step in the run."""
|
|
55
59
|
tool_call_approved: bool = False
|
|
56
60
|
"""Whether a tool call that required approval has now been approved."""
|
|
61
|
+
partial_output: bool = False
|
|
62
|
+
"""Whether the output passed to an output validator is partial."""
|
|
57
63
|
|
|
58
64
|
@property
|
|
59
65
|
def last_attempt(self) -> bool:
|
pydantic_ai/_tool_manager.py
CHANGED
|
@@ -147,6 +147,7 @@ class ToolManager(Generic[AgentDepsT]):
|
|
|
147
147
|
tool_call_id=call.tool_call_id,
|
|
148
148
|
retry=self.ctx.retries.get(name, 0),
|
|
149
149
|
max_retries=tool.max_retries,
|
|
150
|
+
partial_output=allow_partial,
|
|
150
151
|
)
|
|
151
152
|
|
|
152
153
|
pyd_allow_partial = 'trailing-strings' if allow_partial else 'off'
|
pydantic_ai/_utils.py
CHANGED
|
@@ -234,6 +234,15 @@ def sync_anext(iterator: Iterator[T]) -> T:
|
|
|
234
234
|
raise StopAsyncIteration() from e
|
|
235
235
|
|
|
236
236
|
|
|
237
|
+
def sync_async_iterator(async_iter: AsyncIterator[T]) -> Iterator[T]:
|
|
238
|
+
loop = get_event_loop()
|
|
239
|
+
while True:
|
|
240
|
+
try:
|
|
241
|
+
yield loop.run_until_complete(anext(async_iter))
|
|
242
|
+
except StopAsyncIteration:
|
|
243
|
+
break
|
|
244
|
+
|
|
245
|
+
|
|
237
246
|
def now_utc() -> datetime:
|
|
238
247
|
return datetime.now(tz=timezone.utc)
|
|
239
248
|
|
|
@@ -489,3 +498,12 @@ def get_union_args(tp: Any) -> tuple[Any, ...]:
|
|
|
489
498
|
return tuple(_unwrap_annotated(arg) for arg in get_args(tp))
|
|
490
499
|
else:
|
|
491
500
|
return ()
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
def get_event_loop():
|
|
504
|
+
try:
|
|
505
|
+
event_loop = asyncio.get_event_loop()
|
|
506
|
+
except RuntimeError: # pragma: lax no cover
|
|
507
|
+
event_loop = asyncio.new_event_loop()
|
|
508
|
+
asyncio.set_event_loop(event_loop)
|
|
509
|
+
return event_loop
|
pydantic_ai/agent/__init__.py
CHANGED
|
@@ -238,7 +238,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
238
238
|
output_type: The type of the output data, used to validate the data returned by the model,
|
|
239
239
|
defaults to `str`.
|
|
240
240
|
instructions: Instructions to use for this agent, you can also register instructions via a function with
|
|
241
|
-
[`instructions`][pydantic_ai.Agent.instructions].
|
|
241
|
+
[`instructions`][pydantic_ai.Agent.instructions] or pass additional, temporary, instructions when executing a run.
|
|
242
242
|
system_prompt: Static system prompts to use for this agent, you can also register system
|
|
243
243
|
prompts via a function with [`system_prompt`][pydantic_ai.Agent.system_prompt].
|
|
244
244
|
deps_type: The type used for dependency injection, this parameter exists solely to allow you to fully
|
|
@@ -418,6 +418,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
418
418
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
419
419
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
420
420
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
421
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
421
422
|
deps: AgentDepsT = None,
|
|
422
423
|
model_settings: ModelSettings | None = None,
|
|
423
424
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -436,6 +437,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
436
437
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
437
438
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
438
439
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
440
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
439
441
|
deps: AgentDepsT = None,
|
|
440
442
|
model_settings: ModelSettings | None = None,
|
|
441
443
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -454,6 +456,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
454
456
|
message_history: Sequence[_messages.ModelMessage] | None = None,
|
|
455
457
|
deferred_tool_results: DeferredToolResults | None = None,
|
|
456
458
|
model: models.Model | models.KnownModelName | str | None = None,
|
|
459
|
+
instructions: Instructions[AgentDepsT] = None,
|
|
457
460
|
deps: AgentDepsT = None,
|
|
458
461
|
model_settings: ModelSettings | None = None,
|
|
459
462
|
usage_limits: _usage.UsageLimits | None = None,
|
|
@@ -527,6 +530,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
527
530
|
message_history: History of the conversation so far.
|
|
528
531
|
deferred_tool_results: Optional results for deferred tool calls in the message history.
|
|
529
532
|
model: Optional model to use for this run, required if `model` was not set when creating the agent.
|
|
533
|
+
instructions: Optional additional instructions to use for this run.
|
|
530
534
|
deps: Optional dependencies to use for this run.
|
|
531
535
|
model_settings: Optional settings to use for this model's request.
|
|
532
536
|
usage_limits: Optional limits on model request count or token usage.
|
|
@@ -580,7 +584,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
580
584
|
model_settings = merge_model_settings(merged_settings, model_settings)
|
|
581
585
|
usage_limits = usage_limits or _usage.UsageLimits()
|
|
582
586
|
|
|
583
|
-
instructions_literal, instructions_functions = self._get_instructions()
|
|
587
|
+
instructions_literal, instructions_functions = self._get_instructions(additional_instructions=instructions)
|
|
584
588
|
|
|
585
589
|
async def get_instructions(run_context: RunContext[AgentDepsT]) -> str | None:
|
|
586
590
|
parts = [
|
|
@@ -1330,9 +1334,15 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
|
|
|
1330
1334
|
|
|
1331
1335
|
def _get_instructions(
|
|
1332
1336
|
self,
|
|
1337
|
+
additional_instructions: Instructions[AgentDepsT] = None,
|
|
1333
1338
|
) -> tuple[str | None, list[_system_prompt.SystemPromptRunner[AgentDepsT]]]:
|
|
1334
1339
|
override_instructions = self._override_instructions.get()
|
|
1335
|
-
|
|
1340
|
+
if override_instructions:
|
|
1341
|
+
instructions = override_instructions.value
|
|
1342
|
+
else:
|
|
1343
|
+
instructions = self._instructions.copy()
|
|
1344
|
+
if additional_instructions is not None:
|
|
1345
|
+
instructions.extend(self._normalize_instructions(additional_instructions))
|
|
1336
1346
|
|
|
1337
1347
|
literal_parts: list[str] = []
|
|
1338
1348
|
functions: list[_system_prompt.SystemPromptRunner[AgentDepsT]] = []
|
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,
|