pydantic-ai-slim 0.8.1__py3-none-any.whl → 1.0.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/__init__.py +28 -2
- pydantic_ai/_a2a.py +1 -1
- pydantic_ai/_agent_graph.py +323 -156
- pydantic_ai/_function_schema.py +5 -5
- pydantic_ai/_griffe.py +2 -1
- pydantic_ai/_otel_messages.py +2 -2
- pydantic_ai/_output.py +31 -35
- pydantic_ai/_parts_manager.py +7 -5
- pydantic_ai/_run_context.py +3 -1
- pydantic_ai/_system_prompt.py +2 -2
- pydantic_ai/_tool_manager.py +32 -28
- pydantic_ai/_utils.py +14 -26
- pydantic_ai/ag_ui.py +82 -51
- pydantic_ai/agent/__init__.py +70 -9
- pydantic_ai/agent/abstract.py +35 -4
- pydantic_ai/agent/wrapper.py +6 -0
- pydantic_ai/builtin_tools.py +2 -2
- pydantic_ai/common_tools/duckduckgo.py +4 -2
- pydantic_ai/durable_exec/temporal/__init__.py +4 -2
- pydantic_ai/durable_exec/temporal/_agent.py +93 -11
- pydantic_ai/durable_exec/temporal/_function_toolset.py +53 -6
- pydantic_ai/durable_exec/temporal/_logfire.py +1 -1
- pydantic_ai/durable_exec/temporal/_mcp_server.py +2 -1
- pydantic_ai/durable_exec/temporal/_model.py +2 -2
- pydantic_ai/durable_exec/temporal/_run_context.py +2 -1
- pydantic_ai/durable_exec/temporal/_toolset.py +2 -1
- pydantic_ai/exceptions.py +45 -2
- pydantic_ai/format_prompt.py +2 -2
- pydantic_ai/mcp.py +15 -27
- pydantic_ai/messages.py +149 -42
- pydantic_ai/models/__init__.py +6 -4
- pydantic_ai/models/anthropic.py +9 -16
- pydantic_ai/models/bedrock.py +50 -56
- pydantic_ai/models/cohere.py +3 -3
- pydantic_ai/models/fallback.py +2 -2
- pydantic_ai/models/function.py +25 -23
- pydantic_ai/models/gemini.py +12 -13
- pydantic_ai/models/google.py +18 -4
- pydantic_ai/models/groq.py +126 -38
- pydantic_ai/models/huggingface.py +4 -4
- pydantic_ai/models/instrumented.py +35 -16
- pydantic_ai/models/mcp_sampling.py +3 -1
- pydantic_ai/models/mistral.py +6 -6
- pydantic_ai/models/openai.py +35 -40
- pydantic_ai/models/test.py +24 -4
- pydantic_ai/output.py +27 -32
- pydantic_ai/profiles/__init__.py +3 -3
- pydantic_ai/profiles/groq.py +1 -1
- pydantic_ai/profiles/openai.py +25 -4
- pydantic_ai/providers/__init__.py +4 -0
- pydantic_ai/providers/anthropic.py +2 -3
- pydantic_ai/providers/bedrock.py +3 -2
- pydantic_ai/providers/google_vertex.py +2 -1
- pydantic_ai/providers/groq.py +21 -2
- pydantic_ai/providers/litellm.py +134 -0
- pydantic_ai/result.py +144 -41
- pydantic_ai/retries.py +52 -31
- pydantic_ai/run.py +12 -5
- pydantic_ai/tools.py +127 -23
- pydantic_ai/toolsets/__init__.py +4 -1
- pydantic_ai/toolsets/_dynamic.py +4 -4
- pydantic_ai/toolsets/abstract.py +18 -2
- pydantic_ai/toolsets/approval_required.py +32 -0
- pydantic_ai/toolsets/combined.py +7 -12
- pydantic_ai/toolsets/{deferred.py → external.py} +11 -5
- pydantic_ai/toolsets/filtered.py +1 -1
- pydantic_ai/toolsets/function.py +58 -21
- pydantic_ai/toolsets/wrapper.py +2 -1
- pydantic_ai/usage.py +44 -8
- {pydantic_ai_slim-0.8.1.dist-info → pydantic_ai_slim-1.0.0.dist-info}/METADATA +8 -9
- pydantic_ai_slim-1.0.0.dist-info/RECORD +121 -0
- pydantic_ai_slim-0.8.1.dist-info/RECORD +0 -119
- {pydantic_ai_slim-0.8.1.dist-info → pydantic_ai_slim-1.0.0.dist-info}/WHEEL +0 -0
- {pydantic_ai_slim-0.8.1.dist-info → pydantic_ai_slim-1.0.0.dist-info}/entry_points.txt +0 -0
- {pydantic_ai_slim-0.8.1.dist-info → pydantic_ai_slim-1.0.0.dist-info}/licenses/LICENSE +0 -0
pydantic_ai/result.py
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
from __future__ import annotations as _annotations
|
|
2
2
|
|
|
3
|
-
from collections.abc import AsyncIterator, Awaitable, Callable
|
|
3
|
+
from collections.abc import AsyncIterator, Awaitable, Callable, Iterable
|
|
4
4
|
from copy import copy
|
|
5
5
|
from dataclasses import dataclass, field
|
|
6
6
|
from datetime import datetime
|
|
7
|
-
from typing import Generic, cast
|
|
7
|
+
from typing import Generic, cast, overload
|
|
8
8
|
|
|
9
9
|
from pydantic import ValidationError
|
|
10
10
|
from typing_extensions import TypeVar, deprecated
|
|
11
11
|
|
|
12
|
-
from pydantic_ai._tool_manager import ToolManager
|
|
13
|
-
|
|
14
12
|
from . import _utils, exceptions, messages as _messages, models
|
|
15
13
|
from ._output import (
|
|
16
14
|
OutputDataT_inv,
|
|
@@ -22,11 +20,14 @@ from ._output import (
|
|
|
22
20
|
ToolOutputSchema,
|
|
23
21
|
)
|
|
24
22
|
from ._run_context import AgentDepsT, RunContext
|
|
23
|
+
from ._tool_manager import ToolManager
|
|
25
24
|
from .messages import ModelResponseStreamEvent
|
|
26
25
|
from .output import (
|
|
26
|
+
DeferredToolRequests,
|
|
27
27
|
OutputDataT,
|
|
28
28
|
ToolOutput,
|
|
29
29
|
)
|
|
30
|
+
from .run import AgentRunResult
|
|
30
31
|
from .usage import RunUsage, UsageLimits
|
|
31
32
|
|
|
32
33
|
__all__ = (
|
|
@@ -41,7 +42,7 @@ T = TypeVar('T')
|
|
|
41
42
|
"""An invariant TypeVar."""
|
|
42
43
|
|
|
43
44
|
|
|
44
|
-
@dataclass
|
|
45
|
+
@dataclass(kw_only=True)
|
|
45
46
|
class AgentStream(Generic[AgentDepsT, OutputDataT]):
|
|
46
47
|
_raw_stream_response: models.StreamedResponse
|
|
47
48
|
_output_schema: OutputSchema[OutputDataT]
|
|
@@ -155,12 +156,12 @@ class AgentStream(Generic[AgentDepsT, OutputDataT]):
|
|
|
155
156
|
return await self._tool_manager.handle_call(
|
|
156
157
|
tool_call, allow_partial=allow_partial, wrap_validation_errors=False
|
|
157
158
|
)
|
|
158
|
-
elif
|
|
159
|
-
if not self._output_schema.
|
|
159
|
+
elif deferred_tool_requests := _get_deferred_tool_requests(message.parts, self._tool_manager):
|
|
160
|
+
if not self._output_schema.allows_deferred_tools:
|
|
160
161
|
raise exceptions.UserError(
|
|
161
|
-
'A deferred tool call was present, but `
|
|
162
|
+
'A deferred tool call was present, but `DeferredToolRequests` is not among output types. To resolve this, add `DeferredToolRequests` to the list of output types for this agent.'
|
|
162
163
|
)
|
|
163
|
-
return cast(OutputDataT,
|
|
164
|
+
return cast(OutputDataT, deferred_tool_requests)
|
|
164
165
|
elif isinstance(self._output_schema, TextOutputSchema):
|
|
165
166
|
text = '\n\n'.join(x.content for x in message.parts if isinstance(x, _messages.TextPart))
|
|
166
167
|
|
|
@@ -233,15 +234,17 @@ class AgentStream(Generic[AgentDepsT, OutputDataT]):
|
|
|
233
234
|
return self._agent_stream_iterator
|
|
234
235
|
|
|
235
236
|
|
|
236
|
-
@dataclass
|
|
237
|
+
@dataclass(init=False)
|
|
237
238
|
class StreamedRunResult(Generic[AgentDepsT, OutputDataT]):
|
|
238
239
|
"""Result of a streamed run that returns structured data via a tool call."""
|
|
239
240
|
|
|
240
241
|
_all_messages: list[_messages.ModelMessage]
|
|
241
242
|
_new_message_index: int
|
|
242
243
|
|
|
243
|
-
_stream_response: AgentStream[AgentDepsT, OutputDataT]
|
|
244
|
-
_on_complete: Callable[[], Awaitable[None]]
|
|
244
|
+
_stream_response: AgentStream[AgentDepsT, OutputDataT] | None = None
|
|
245
|
+
_on_complete: Callable[[], Awaitable[None]] | None = None
|
|
246
|
+
|
|
247
|
+
_run_result: AgentRunResult[OutputDataT] | None = None
|
|
245
248
|
|
|
246
249
|
is_complete: bool = field(default=False, init=False)
|
|
247
250
|
"""Whether the stream has all been received.
|
|
@@ -253,6 +256,39 @@ class StreamedRunResult(Generic[AgentDepsT, OutputDataT]):
|
|
|
253
256
|
[`get_output`][pydantic_ai.result.StreamedRunResult.get_output] completes.
|
|
254
257
|
"""
|
|
255
258
|
|
|
259
|
+
@overload
|
|
260
|
+
def __init__(
|
|
261
|
+
self,
|
|
262
|
+
all_messages: list[_messages.ModelMessage],
|
|
263
|
+
new_message_index: int,
|
|
264
|
+
stream_response: AgentStream[AgentDepsT, OutputDataT] | None,
|
|
265
|
+
on_complete: Callable[[], Awaitable[None]] | None,
|
|
266
|
+
) -> None: ...
|
|
267
|
+
|
|
268
|
+
@overload
|
|
269
|
+
def __init__(
|
|
270
|
+
self,
|
|
271
|
+
all_messages: list[_messages.ModelMessage],
|
|
272
|
+
new_message_index: int,
|
|
273
|
+
*,
|
|
274
|
+
run_result: AgentRunResult[OutputDataT],
|
|
275
|
+
) -> None: ...
|
|
276
|
+
|
|
277
|
+
def __init__(
|
|
278
|
+
self,
|
|
279
|
+
all_messages: list[_messages.ModelMessage],
|
|
280
|
+
new_message_index: int,
|
|
281
|
+
stream_response: AgentStream[AgentDepsT, OutputDataT] | None = None,
|
|
282
|
+
on_complete: Callable[[], Awaitable[None]] | None = None,
|
|
283
|
+
run_result: AgentRunResult[OutputDataT] | None = None,
|
|
284
|
+
) -> None:
|
|
285
|
+
self._all_messages = all_messages
|
|
286
|
+
self._new_message_index = new_message_index
|
|
287
|
+
|
|
288
|
+
self._stream_response = stream_response
|
|
289
|
+
self._on_complete = on_complete
|
|
290
|
+
self._run_result = run_result
|
|
291
|
+
|
|
256
292
|
def all_messages(self, *, output_tool_return_content: str | None = None) -> list[_messages.ModelMessage]:
|
|
257
293
|
"""Return the history of _messages.
|
|
258
294
|
|
|
@@ -340,9 +376,15 @@ class StreamedRunResult(Generic[AgentDepsT, OutputDataT]):
|
|
|
340
376
|
Returns:
|
|
341
377
|
An async iterable of the response data.
|
|
342
378
|
"""
|
|
343
|
-
|
|
344
|
-
yield output
|
|
345
|
-
|
|
379
|
+
if self._run_result is not None:
|
|
380
|
+
yield self._run_result.output
|
|
381
|
+
await self._marked_completed()
|
|
382
|
+
elif self._stream_response is not None:
|
|
383
|
+
async for output in self._stream_response.stream_output(debounce_by=debounce_by):
|
|
384
|
+
yield output
|
|
385
|
+
await self._marked_completed(self._stream_response.get())
|
|
386
|
+
else:
|
|
387
|
+
raise ValueError('No stream response or run result provided') # pragma: no cover
|
|
346
388
|
|
|
347
389
|
async def stream_text(self, *, delta: bool = False, debounce_by: float | None = 0.1) -> AsyncIterator[str]:
|
|
348
390
|
"""Stream the text result as an async iterable.
|
|
@@ -357,9 +399,20 @@ class StreamedRunResult(Generic[AgentDepsT, OutputDataT]):
|
|
|
357
399
|
Debouncing is particularly important for long structured responses to reduce the overhead of
|
|
358
400
|
performing validation as each token is received.
|
|
359
401
|
"""
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
402
|
+
if self._run_result is not None: # pragma: no cover
|
|
403
|
+
# We can't really get here, as `_run_result` is only set in `run_stream` when `CallToolsNode` produces `DeferredToolRequests` output
|
|
404
|
+
# as a result of a tool function raising `CallDeferred` or `ApprovalRequired`.
|
|
405
|
+
# That'll change if we ever support something like `raise EndRun(output: OutputT)` where `OutputT` could be `str`.
|
|
406
|
+
if not isinstance(self._run_result.output, str):
|
|
407
|
+
raise exceptions.UserError('stream_text() can only be used with text responses')
|
|
408
|
+
yield self._run_result.output
|
|
409
|
+
await self._marked_completed()
|
|
410
|
+
elif self._stream_response is not None:
|
|
411
|
+
async for text in self._stream_response.stream_text(delta=delta, debounce_by=debounce_by):
|
|
412
|
+
yield text
|
|
413
|
+
await self._marked_completed(self._stream_response.get())
|
|
414
|
+
else:
|
|
415
|
+
raise ValueError('No stream response or run result provided') # pragma: no cover
|
|
363
416
|
|
|
364
417
|
@deprecated('`StreamedRunResult.stream_structured` is deprecated, use `stream_responses` instead.')
|
|
365
418
|
async def stream_structured(
|
|
@@ -381,20 +434,34 @@ class StreamedRunResult(Generic[AgentDepsT, OutputDataT]):
|
|
|
381
434
|
Returns:
|
|
382
435
|
An async iterable of the structured response message and whether that is the last message.
|
|
383
436
|
"""
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
yield
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
437
|
+
if self._run_result is not None:
|
|
438
|
+
model_response = cast(_messages.ModelResponse, self.all_messages()[-1])
|
|
439
|
+
yield model_response, True
|
|
440
|
+
await self._marked_completed()
|
|
441
|
+
elif self._stream_response is not None:
|
|
442
|
+
# if the message currently has any parts with content, yield before streaming
|
|
443
|
+
async for msg in self._stream_response.stream_responses(debounce_by=debounce_by):
|
|
444
|
+
yield msg, False
|
|
445
|
+
|
|
446
|
+
msg = self._stream_response.get()
|
|
447
|
+
yield msg, True
|
|
448
|
+
|
|
449
|
+
await self._marked_completed(msg)
|
|
450
|
+
else:
|
|
451
|
+
raise ValueError('No stream response or run result provided') # pragma: no cover
|
|
392
452
|
|
|
393
453
|
async def get_output(self) -> OutputDataT:
|
|
394
454
|
"""Stream the whole response, validate and return it."""
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
455
|
+
if self._run_result is not None:
|
|
456
|
+
output = self._run_result.output
|
|
457
|
+
await self._marked_completed()
|
|
458
|
+
return output
|
|
459
|
+
elif self._stream_response is not None:
|
|
460
|
+
output = await self._stream_response.get_output()
|
|
461
|
+
await self._marked_completed(self._stream_response.get())
|
|
462
|
+
return output
|
|
463
|
+
else:
|
|
464
|
+
raise ValueError('No stream response or run result provided') # pragma: no cover
|
|
398
465
|
|
|
399
466
|
def usage(self) -> RunUsage:
|
|
400
467
|
"""Return the usage of the whole run.
|
|
@@ -402,28 +469,45 @@ class StreamedRunResult(Generic[AgentDepsT, OutputDataT]):
|
|
|
402
469
|
!!! note
|
|
403
470
|
This won't return the full usage until the stream is finished.
|
|
404
471
|
"""
|
|
405
|
-
|
|
472
|
+
if self._run_result is not None:
|
|
473
|
+
return self._run_result.usage()
|
|
474
|
+
elif self._stream_response is not None:
|
|
475
|
+
return self._stream_response.usage()
|
|
476
|
+
else:
|
|
477
|
+
raise ValueError('No stream response or run result provided') # pragma: no cover
|
|
406
478
|
|
|
407
479
|
def timestamp(self) -> datetime:
|
|
408
480
|
"""Get the timestamp of the response."""
|
|
409
|
-
|
|
481
|
+
if self._run_result is not None:
|
|
482
|
+
return self._run_result.timestamp()
|
|
483
|
+
elif self._stream_response is not None:
|
|
484
|
+
return self._stream_response.timestamp()
|
|
485
|
+
else:
|
|
486
|
+
raise ValueError('No stream response or run result provided') # pragma: no cover
|
|
410
487
|
|
|
411
488
|
@deprecated('`validate_structured_output` is deprecated, use `validate_response_output` instead.')
|
|
412
489
|
async def validate_structured_output(
|
|
413
490
|
self, message: _messages.ModelResponse, *, allow_partial: bool = False
|
|
414
491
|
) -> OutputDataT:
|
|
415
|
-
return await self.
|
|
492
|
+
return await self.validate_response_output(message, allow_partial=allow_partial)
|
|
416
493
|
|
|
417
494
|
async def validate_response_output(
|
|
418
495
|
self, message: _messages.ModelResponse, *, allow_partial: bool = False
|
|
419
496
|
) -> OutputDataT:
|
|
420
497
|
"""Validate a structured result message."""
|
|
421
|
-
|
|
498
|
+
if self._run_result is not None:
|
|
499
|
+
return self._run_result.output
|
|
500
|
+
elif self._stream_response is not None:
|
|
501
|
+
return await self._stream_response.validate_response_output(message, allow_partial=allow_partial)
|
|
502
|
+
else:
|
|
503
|
+
raise ValueError('No stream response or run result provided') # pragma: no cover
|
|
422
504
|
|
|
423
|
-
async def _marked_completed(self, message: _messages.ModelResponse) -> None:
|
|
505
|
+
async def _marked_completed(self, message: _messages.ModelResponse | None = None) -> None:
|
|
424
506
|
self.is_complete = True
|
|
425
|
-
|
|
426
|
-
|
|
507
|
+
if message is not None:
|
|
508
|
+
self._all_messages.append(message)
|
|
509
|
+
if self._on_complete is not None:
|
|
510
|
+
await self._on_complete()
|
|
427
511
|
|
|
428
512
|
|
|
429
513
|
@dataclass(repr=False)
|
|
@@ -432,8 +516,10 @@ class FinalResult(Generic[OutputDataT]):
|
|
|
432
516
|
|
|
433
517
|
output: OutputDataT
|
|
434
518
|
"""The final result data."""
|
|
519
|
+
|
|
435
520
|
tool_name: str | None = None
|
|
436
521
|
"""Name of the final output tool; `None` if the output came from unstructured text content."""
|
|
522
|
+
|
|
437
523
|
tool_call_id: str | None = None
|
|
438
524
|
"""ID of the tool call that produced the final output; `None` if the output came from unstructured text content."""
|
|
439
525
|
|
|
@@ -454,9 +540,26 @@ def _get_usage_checking_stream_response(
|
|
|
454
540
|
|
|
455
541
|
return _usage_checking_iterator()
|
|
456
542
|
else:
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
543
|
+
return aiter(stream_response)
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
def _get_deferred_tool_requests(
|
|
547
|
+
parts: Iterable[_messages.ModelResponsePart], tool_manager: ToolManager[AgentDepsT]
|
|
548
|
+
) -> DeferredToolRequests | None:
|
|
549
|
+
"""Get the deferred tool requests from the model response parts."""
|
|
550
|
+
approvals: list[_messages.ToolCallPart] = []
|
|
551
|
+
calls: list[_messages.ToolCallPart] = []
|
|
552
|
+
|
|
553
|
+
for part in parts:
|
|
554
|
+
if isinstance(part, _messages.ToolCallPart):
|
|
555
|
+
tool_def = tool_manager.get_tool_def(part.tool_name)
|
|
556
|
+
if tool_def is not None: # pragma: no branch
|
|
557
|
+
if tool_def.kind == 'unapproved':
|
|
558
|
+
approvals.append(part)
|
|
559
|
+
elif tool_def.kind == 'external':
|
|
560
|
+
calls.append(part)
|
|
561
|
+
|
|
562
|
+
if not calls and not approvals:
|
|
563
|
+
return None
|
|
461
564
|
|
|
462
|
-
|
|
565
|
+
return DeferredToolRequests(calls=calls, approvals=approvals)
|
pydantic_ai/retries.py
CHANGED
|
@@ -13,6 +13,8 @@ The module includes:
|
|
|
13
13
|
|
|
14
14
|
from __future__ import annotations
|
|
15
15
|
|
|
16
|
+
from types import TracebackType
|
|
17
|
+
|
|
16
18
|
from httpx import (
|
|
17
19
|
AsyncBaseTransport,
|
|
18
20
|
AsyncHTTPTransport,
|
|
@@ -24,17 +26,17 @@ from httpx import (
|
|
|
24
26
|
)
|
|
25
27
|
|
|
26
28
|
try:
|
|
27
|
-
from tenacity import
|
|
29
|
+
from tenacity import RetryCallState, RetryError, retry, wait_exponential
|
|
28
30
|
except ImportError as _import_error:
|
|
29
31
|
raise ImportError(
|
|
30
32
|
'Please install `tenacity` to use the retries utilities, '
|
|
31
33
|
'you can use the `retries` optional group — `pip install "pydantic-ai-slim[retries]"`'
|
|
32
34
|
) from _import_error
|
|
33
35
|
|
|
34
|
-
from collections.abc import Awaitable
|
|
36
|
+
from collections.abc import Awaitable, Callable
|
|
35
37
|
from datetime import datetime, timezone
|
|
36
38
|
from email.utils import parsedate_to_datetime
|
|
37
|
-
from typing import TYPE_CHECKING, Any,
|
|
39
|
+
from typing import TYPE_CHECKING, Any, cast
|
|
38
40
|
|
|
39
41
|
from typing_extensions import TypedDict
|
|
40
42
|
|
|
@@ -134,8 +136,9 @@ class TenacityTransport(BaseTransport):
|
|
|
134
136
|
|
|
135
137
|
Example:
|
|
136
138
|
```python
|
|
137
|
-
from httpx import Client,
|
|
138
|
-
from tenacity import
|
|
139
|
+
from httpx import Client, HTTPStatusError, HTTPTransport
|
|
140
|
+
from tenacity import retry_if_exception_type, stop_after_attempt
|
|
141
|
+
|
|
139
142
|
from pydantic_ai.retries import RetryConfig, TenacityTransport, wait_retry_after
|
|
140
143
|
|
|
141
144
|
transport = TenacityTransport(
|
|
@@ -157,18 +160,7 @@ class TenacityTransport(BaseTransport):
|
|
|
157
160
|
config: RetryConfig,
|
|
158
161
|
wrapped: BaseTransport | None = None,
|
|
159
162
|
validate_response: Callable[[Response], Any] | None = None,
|
|
160
|
-
**kwargs: NoReturn,
|
|
161
163
|
):
|
|
162
|
-
# TODO: Remove the following checks (and **kwargs) during v1 release
|
|
163
|
-
if 'controller' in kwargs: # pragma: no cover
|
|
164
|
-
raise TypeError('The `controller` argument has been renamed to `config`, and now requires a `RetryConfig`.')
|
|
165
|
-
if kwargs: # pragma: no cover
|
|
166
|
-
raise TypeError(f'Unexpected keyword arguments: {", ".join(kwargs)}')
|
|
167
|
-
if isinstance(config, Retrying): # pragma: no cover
|
|
168
|
-
raise ValueError(
|
|
169
|
-
'Passing a Retrying instance is no longer supported; the `config` argument must be a `pydantic_ai.retries.RetryConfig`.'
|
|
170
|
-
)
|
|
171
|
-
|
|
172
164
|
self.config = config
|
|
173
165
|
self.wrapped = wrapped or HTTPTransport()
|
|
174
166
|
self.validate_response = validate_response
|
|
@@ -195,11 +187,30 @@ class TenacityTransport(BaseTransport):
|
|
|
195
187
|
response.request = req
|
|
196
188
|
|
|
197
189
|
if self.validate_response:
|
|
198
|
-
|
|
190
|
+
try:
|
|
191
|
+
self.validate_response(response)
|
|
192
|
+
except Exception:
|
|
193
|
+
response.close()
|
|
194
|
+
raise
|
|
199
195
|
return response
|
|
200
196
|
|
|
201
197
|
return handle_request(request)
|
|
202
198
|
|
|
199
|
+
def __enter__(self) -> TenacityTransport:
|
|
200
|
+
self.wrapped.__enter__()
|
|
201
|
+
return self
|
|
202
|
+
|
|
203
|
+
def __exit__(
|
|
204
|
+
self,
|
|
205
|
+
exc_type: type[BaseException] | None = None,
|
|
206
|
+
exc_value: BaseException | None = None,
|
|
207
|
+
traceback: TracebackType | None = None,
|
|
208
|
+
) -> None:
|
|
209
|
+
self.wrapped.__exit__(exc_type, exc_value, traceback)
|
|
210
|
+
|
|
211
|
+
def close(self) -> None:
|
|
212
|
+
self.wrapped.close() # pragma: no cover
|
|
213
|
+
|
|
203
214
|
|
|
204
215
|
class AsyncTenacityTransport(AsyncBaseTransport):
|
|
205
216
|
"""Asynchronous HTTP transport with tenacity-based retry functionality.
|
|
@@ -224,7 +235,8 @@ class AsyncTenacityTransport(AsyncBaseTransport):
|
|
|
224
235
|
Example:
|
|
225
236
|
```python
|
|
226
237
|
from httpx import AsyncClient, HTTPStatusError
|
|
227
|
-
from tenacity import
|
|
238
|
+
from tenacity import retry_if_exception_type, stop_after_attempt
|
|
239
|
+
|
|
228
240
|
from pydantic_ai.retries import AsyncTenacityTransport, RetryConfig, wait_retry_after
|
|
229
241
|
|
|
230
242
|
transport = AsyncTenacityTransport(
|
|
@@ -245,18 +257,7 @@ class AsyncTenacityTransport(AsyncBaseTransport):
|
|
|
245
257
|
config: RetryConfig,
|
|
246
258
|
wrapped: AsyncBaseTransport | None = None,
|
|
247
259
|
validate_response: Callable[[Response], Any] | None = None,
|
|
248
|
-
**kwargs: NoReturn,
|
|
249
260
|
):
|
|
250
|
-
# TODO: Remove the following checks (and **kwargs) during v1 release
|
|
251
|
-
if 'controller' in kwargs: # pragma: no cover
|
|
252
|
-
raise TypeError('The `controller` argument has been renamed to `config`, and now requires a `RetryConfig`.')
|
|
253
|
-
if kwargs: # pragma: no cover
|
|
254
|
-
raise TypeError(f'Unexpected keyword arguments: {", ".join(kwargs)}')
|
|
255
|
-
if isinstance(config, AsyncRetrying): # pragma: no cover
|
|
256
|
-
raise ValueError(
|
|
257
|
-
'Passing an AsyncRetrying instance is no longer supported; the `config` argument must be a `pydantic_ai.retries.RetryConfig`.'
|
|
258
|
-
)
|
|
259
|
-
|
|
260
261
|
self.config = config
|
|
261
262
|
self.wrapped = wrapped or AsyncHTTPTransport()
|
|
262
263
|
self.validate_response = validate_response
|
|
@@ -283,11 +284,30 @@ class AsyncTenacityTransport(AsyncBaseTransport):
|
|
|
283
284
|
response.request = req
|
|
284
285
|
|
|
285
286
|
if self.validate_response:
|
|
286
|
-
|
|
287
|
+
try:
|
|
288
|
+
self.validate_response(response)
|
|
289
|
+
except Exception:
|
|
290
|
+
await response.aclose()
|
|
291
|
+
raise
|
|
287
292
|
return response
|
|
288
293
|
|
|
289
294
|
return await handle_async_request(request)
|
|
290
295
|
|
|
296
|
+
async def __aenter__(self) -> AsyncTenacityTransport:
|
|
297
|
+
await self.wrapped.__aenter__()
|
|
298
|
+
return self
|
|
299
|
+
|
|
300
|
+
async def __aexit__(
|
|
301
|
+
self,
|
|
302
|
+
exc_type: type[BaseException] | None = None,
|
|
303
|
+
exc_value: BaseException | None = None,
|
|
304
|
+
traceback: TracebackType | None = None,
|
|
305
|
+
) -> None:
|
|
306
|
+
await self.wrapped.__aexit__(exc_type, exc_value, traceback)
|
|
307
|
+
|
|
308
|
+
async def aclose(self) -> None:
|
|
309
|
+
await self.wrapped.aclose()
|
|
310
|
+
|
|
291
311
|
|
|
292
312
|
def wait_retry_after(
|
|
293
313
|
fallback_strategy: Callable[[RetryCallState], float] | None = None, max_wait: float = 300
|
|
@@ -314,7 +334,8 @@ def wait_retry_after(
|
|
|
314
334
|
Example:
|
|
315
335
|
```python
|
|
316
336
|
from httpx import AsyncClient, HTTPStatusError
|
|
317
|
-
from tenacity import
|
|
337
|
+
from tenacity import retry_if_exception_type, stop_after_attempt
|
|
338
|
+
|
|
318
339
|
from pydantic_ai.retries import AsyncTenacityTransport, RetryConfig, wait_retry_after
|
|
319
340
|
|
|
320
341
|
transport = AsyncTenacityTransport(
|
pydantic_ai/run.py
CHANGED
|
@@ -3,9 +3,8 @@ from __future__ import annotations as _annotations
|
|
|
3
3
|
import dataclasses
|
|
4
4
|
from collections.abc import AsyncIterator
|
|
5
5
|
from copy import deepcopy
|
|
6
|
-
from
|
|
7
|
-
|
|
8
|
-
from typing_extensions import Literal
|
|
6
|
+
from datetime import datetime
|
|
7
|
+
from typing import TYPE_CHECKING, Any, Generic, Literal, overload
|
|
9
8
|
|
|
10
9
|
from pydantic_graph import End, GraphRun, GraphRunContext
|
|
11
10
|
|
|
@@ -16,9 +15,11 @@ from . import (
|
|
|
16
15
|
usage as _usage,
|
|
17
16
|
)
|
|
18
17
|
from .output import OutputDataT
|
|
19
|
-
from .result import FinalResult
|
|
20
18
|
from .tools import AgentDepsT
|
|
21
19
|
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from .result import FinalResult
|
|
22
|
+
|
|
22
23
|
|
|
23
24
|
@dataclasses.dataclass(repr=False)
|
|
24
25
|
class AgentRun(Generic[AgentDepsT, OutputDataT]):
|
|
@@ -100,7 +101,7 @@ class AgentRun(Generic[AgentDepsT, OutputDataT]):
|
|
|
100
101
|
def ctx(self) -> GraphRunContext[_agent_graph.GraphAgentState, _agent_graph.GraphAgentDeps[AgentDepsT, Any]]:
|
|
101
102
|
"""The current context of the agent run."""
|
|
102
103
|
return GraphRunContext[_agent_graph.GraphAgentState, _agent_graph.GraphAgentDeps[AgentDepsT, Any]](
|
|
103
|
-
self._graph_run.state, self._graph_run.deps
|
|
104
|
+
state=self._graph_run.state, deps=self._graph_run.deps
|
|
104
105
|
)
|
|
105
106
|
|
|
106
107
|
@property
|
|
@@ -348,3 +349,9 @@ class AgentRunResult(Generic[OutputDataT]):
|
|
|
348
349
|
def usage(self) -> _usage.RunUsage:
|
|
349
350
|
"""Return the usage of the whole run."""
|
|
350
351
|
return self._state.usage
|
|
352
|
+
|
|
353
|
+
def timestamp(self) -> datetime:
|
|
354
|
+
"""Return the timestamp of last response."""
|
|
355
|
+
model_response = self.all_messages()[-1]
|
|
356
|
+
assert isinstance(model_response, _messages.ModelResponse)
|
|
357
|
+
return model_response.timestamp
|