pydantic-ai-slim 0.2.18__tar.gz → 0.2.19__tar.gz
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_slim-0.2.18 → pydantic_ai_slim-0.2.19}/PKG-INFO +4 -4
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/_agent_graph.py +34 -14
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/_function_schema.py +2 -3
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/_output.py +1 -1
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/_system_prompt.py +1 -1
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/_utils.py +28 -3
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/agent.py +13 -3
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/messages.py +4 -5
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/anthropic.py +1 -1
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/gemini.py +1 -3
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/google.py +5 -3
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/__init__.py +23 -17
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/tools.py +1 -2
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/.gitignore +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/LICENSE +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/README.md +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/__init__.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/__main__.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/_a2a.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/_cli.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/_griffe.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/_parts_manager.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/common_tools/__init__.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/common_tools/duckduckgo.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/common_tools/tavily.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/direct.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/exceptions.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/ext/__init__.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/ext/langchain.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/format_as_xml.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/format_prompt.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/mcp.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/__init__.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/bedrock.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/cohere.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/fallback.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/function.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/groq.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/instrumented.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/mistral.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/openai.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/test.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/models/wrapper.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/__init__.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/_json_schema.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/amazon.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/anthropic.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/cohere.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/deepseek.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/google.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/grok.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/meta.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/mistral.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/openai.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/profiles/qwen.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/anthropic.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/azure.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/bedrock.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/cohere.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/deepseek.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/fireworks.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/google.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/google_gla.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/google_vertex.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/grok.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/groq.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/heroku.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/mistral.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/openai.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/openrouter.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/providers/together.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/py.typed +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/result.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/settings.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pydantic_ai/usage.py +0 -0
- {pydantic_ai_slim-0.2.18 → pydantic_ai_slim-0.2.19}/pyproject.toml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pydantic-ai-slim
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.19
|
|
4
4
|
Summary: Agent Framework / shim to use Pydantic with LLMs, slim package
|
|
5
5
|
Author-email: Samuel Colvin <samuel@pydantic.dev>, Marcelo Trylesinski <marcelotryle@gmail.com>, David Montague <david@pydantic.dev>, Alex Hall <alex@pydantic.dev>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -30,11 +30,11 @@ Requires-Dist: exceptiongroup; python_version < '3.11'
|
|
|
30
30
|
Requires-Dist: griffe>=1.3.2
|
|
31
31
|
Requires-Dist: httpx>=0.27
|
|
32
32
|
Requires-Dist: opentelemetry-api>=1.28.0
|
|
33
|
-
Requires-Dist: pydantic-graph==0.2.
|
|
33
|
+
Requires-Dist: pydantic-graph==0.2.19
|
|
34
34
|
Requires-Dist: pydantic>=2.10
|
|
35
35
|
Requires-Dist: typing-inspection>=0.4.0
|
|
36
36
|
Provides-Extra: a2a
|
|
37
|
-
Requires-Dist: fasta2a==0.2.
|
|
37
|
+
Requires-Dist: fasta2a==0.2.19; extra == 'a2a'
|
|
38
38
|
Provides-Extra: anthropic
|
|
39
39
|
Requires-Dist: anthropic>=0.52.0; extra == 'anthropic'
|
|
40
40
|
Provides-Extra: bedrock
|
|
@@ -48,7 +48,7 @@ Requires-Dist: cohere>=5.13.11; (platform_system != 'Emscripten') and extra == '
|
|
|
48
48
|
Provides-Extra: duckduckgo
|
|
49
49
|
Requires-Dist: duckduckgo-search>=7.0.0; extra == 'duckduckgo'
|
|
50
50
|
Provides-Extra: evals
|
|
51
|
-
Requires-Dist: pydantic-evals==0.2.
|
|
51
|
+
Requires-Dist: pydantic-evals==0.2.19; extra == 'evals'
|
|
52
52
|
Provides-Extra: google
|
|
53
53
|
Requires-Dist: google-genai>=1.15.0; extra == 'google'
|
|
54
54
|
Provides-Extra: groq
|
|
@@ -12,18 +12,11 @@ from typing import TYPE_CHECKING, Any, Callable, Generic, Literal, Union, cast
|
|
|
12
12
|
from opentelemetry.trace import Tracer
|
|
13
13
|
from typing_extensions import TypeGuard, TypeVar, assert_never
|
|
14
14
|
|
|
15
|
+
from pydantic_ai._utils import is_async_callable, run_in_executor
|
|
15
16
|
from pydantic_graph import BaseNode, Graph, GraphRunContext
|
|
16
17
|
from pydantic_graph.nodes import End, NodeRunEndT
|
|
17
18
|
|
|
18
|
-
from . import
|
|
19
|
-
_output,
|
|
20
|
-
_system_prompt,
|
|
21
|
-
exceptions,
|
|
22
|
-
messages as _messages,
|
|
23
|
-
models,
|
|
24
|
-
result,
|
|
25
|
-
usage as _usage,
|
|
26
|
-
)
|
|
19
|
+
from . import _output, _system_prompt, exceptions, messages as _messages, models, result, usage as _usage
|
|
27
20
|
from .result import OutputDataT
|
|
28
21
|
from .settings import ModelSettings, merge_model_settings
|
|
29
22
|
from .tools import RunContext, Tool, ToolDefinition, ToolsPrepareFunc
|
|
@@ -39,6 +32,7 @@ __all__ = (
|
|
|
39
32
|
'CallToolsNode',
|
|
40
33
|
'build_run_context',
|
|
41
34
|
'capture_run_messages',
|
|
35
|
+
'HistoryProcessor',
|
|
42
36
|
)
|
|
43
37
|
|
|
44
38
|
|
|
@@ -54,6 +48,11 @@ EndStrategy = Literal['early', 'exhaustive']
|
|
|
54
48
|
DepsT = TypeVar('DepsT')
|
|
55
49
|
OutputT = TypeVar('OutputT')
|
|
56
50
|
|
|
51
|
+
_HistoryProcessorSync = Callable[[list[_messages.ModelMessage]], list[_messages.ModelMessage]]
|
|
52
|
+
_HistoryProcessorAsync = Callable[[list[_messages.ModelMessage]], Awaitable[list[_messages.ModelMessage]]]
|
|
53
|
+
HistoryProcessor = Union[_HistoryProcessorSync, _HistoryProcessorAsync]
|
|
54
|
+
"""A function that processes a list of model messages and returns a list of model messages."""
|
|
55
|
+
|
|
57
56
|
|
|
58
57
|
@dataclasses.dataclass
|
|
59
58
|
class GraphAgentState:
|
|
@@ -93,6 +92,8 @@ class GraphAgentDeps(Generic[DepsT, OutputDataT]):
|
|
|
93
92
|
output_schema: _output.OutputSchema[OutputDataT] | None
|
|
94
93
|
output_validators: list[_output.OutputValidator[DepsT, OutputDataT]]
|
|
95
94
|
|
|
95
|
+
history_processors: Sequence[HistoryProcessor]
|
|
96
|
+
|
|
96
97
|
function_tools: dict[str, Tool[DepsT]] = dataclasses.field(repr=False)
|
|
97
98
|
mcp_servers: Sequence[MCPServer] = dataclasses.field(repr=False)
|
|
98
99
|
default_retries: int
|
|
@@ -327,8 +328,9 @@ class ModelRequestNode(AgentNode[DepsT, NodeRunEndT]):
|
|
|
327
328
|
|
|
328
329
|
model_settings, model_request_parameters = await self._prepare_request(ctx)
|
|
329
330
|
model_request_parameters = ctx.deps.model.customize_request_parameters(model_request_parameters)
|
|
331
|
+
message_history = await _process_message_history(ctx.state.message_history, ctx.deps.history_processors)
|
|
330
332
|
async with ctx.deps.model.request_stream(
|
|
331
|
-
|
|
333
|
+
message_history, model_settings, model_request_parameters
|
|
332
334
|
) as streamed_response:
|
|
333
335
|
self._did_stream = True
|
|
334
336
|
ctx.state.usage.requests += 1
|
|
@@ -350,9 +352,8 @@ class ModelRequestNode(AgentNode[DepsT, NodeRunEndT]):
|
|
|
350
352
|
|
|
351
353
|
model_settings, model_request_parameters = await self._prepare_request(ctx)
|
|
352
354
|
model_request_parameters = ctx.deps.model.customize_request_parameters(model_request_parameters)
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
)
|
|
355
|
+
message_history = await _process_message_history(ctx.state.message_history, ctx.deps.history_processors)
|
|
356
|
+
model_response = await ctx.deps.model.request(message_history, model_settings, model_request_parameters)
|
|
356
357
|
ctx.state.usage.incr(_usage.Usage())
|
|
357
358
|
|
|
358
359
|
return self._finish_handling(ctx, model_response)
|
|
@@ -647,6 +648,7 @@ async def process_function_tools( # noqa C901
|
|
|
647
648
|
# if tool_name is in output_schema, it means we found a output tool but an error occurred in
|
|
648
649
|
# validation, we don't add another part here
|
|
649
650
|
if output_tool_name is not None:
|
|
651
|
+
yield _messages.FunctionToolCallEvent(call)
|
|
650
652
|
if found_used_output_tool:
|
|
651
653
|
content = 'Output tool not used - a final result was already processed.'
|
|
652
654
|
else:
|
|
@@ -657,9 +659,14 @@ async def process_function_tools( # noqa C901
|
|
|
657
659
|
content=content,
|
|
658
660
|
tool_call_id=call.tool_call_id,
|
|
659
661
|
)
|
|
662
|
+
yield _messages.FunctionToolResultEvent(part, tool_call_id=call.tool_call_id)
|
|
660
663
|
output_parts.append(part)
|
|
661
664
|
else:
|
|
662
|
-
|
|
665
|
+
yield _messages.FunctionToolCallEvent(call)
|
|
666
|
+
|
|
667
|
+
part = _unknown_tool(call.tool_name, call.tool_call_id, ctx)
|
|
668
|
+
yield _messages.FunctionToolResultEvent(part, tool_call_id=call.tool_call_id)
|
|
669
|
+
output_parts.append(part)
|
|
663
670
|
|
|
664
671
|
if not calls_to_run:
|
|
665
672
|
return
|
|
@@ -865,3 +872,16 @@ def build_agent_graph(
|
|
|
865
872
|
auto_instrument=False,
|
|
866
873
|
)
|
|
867
874
|
return graph
|
|
875
|
+
|
|
876
|
+
|
|
877
|
+
async def _process_message_history(
|
|
878
|
+
messages: list[_messages.ModelMessage],
|
|
879
|
+
processors: Sequence[HistoryProcessor],
|
|
880
|
+
) -> list[_messages.ModelMessage]:
|
|
881
|
+
"""Process message history through a sequence of processors."""
|
|
882
|
+
for processor in processors:
|
|
883
|
+
if is_async_callable(processor):
|
|
884
|
+
messages = await processor(messages)
|
|
885
|
+
else:
|
|
886
|
+
messages = await run_in_executor(processor, messages)
|
|
887
|
+
return messages
|
|
@@ -5,7 +5,6 @@ This module has to use numerous internal Pydantic APIs and is therefore brittle
|
|
|
5
5
|
|
|
6
6
|
from __future__ import annotations as _annotations
|
|
7
7
|
|
|
8
|
-
import inspect
|
|
9
8
|
from collections.abc import Awaitable
|
|
10
9
|
from dataclasses import dataclass, field
|
|
11
10
|
from inspect import Parameter, signature
|
|
@@ -23,7 +22,7 @@ from typing_extensions import get_origin
|
|
|
23
22
|
from pydantic_ai.tools import RunContext
|
|
24
23
|
|
|
25
24
|
from ._griffe import doc_descriptions
|
|
26
|
-
from ._utils import check_object_json_schema, is_model_like, run_in_executor
|
|
25
|
+
from ._utils import check_object_json_schema, is_async_callable, is_model_like, run_in_executor
|
|
27
26
|
|
|
28
27
|
if TYPE_CHECKING:
|
|
29
28
|
from .tools import DocstringFormat, ObjectJsonSchema
|
|
@@ -214,7 +213,7 @@ def function_schema( # noqa: C901
|
|
|
214
213
|
positional_fields=positional_fields,
|
|
215
214
|
var_positional_field=var_positional_field,
|
|
216
215
|
takes_ctx=takes_ctx,
|
|
217
|
-
is_async=
|
|
216
|
+
is_async=is_async_callable(function),
|
|
218
217
|
function=function,
|
|
219
218
|
)
|
|
220
219
|
|
|
@@ -60,7 +60,7 @@ class OutputValidator(Generic[AgentDepsT, OutputDataT_inv]):
|
|
|
60
60
|
|
|
61
61
|
def __post_init__(self):
|
|
62
62
|
self._takes_ctx = len(inspect.signature(self.function).parameters) > 1
|
|
63
|
-
self._is_async =
|
|
63
|
+
self._is_async = _utils.is_async_callable(self.function)
|
|
64
64
|
|
|
65
65
|
async def validate(
|
|
66
66
|
self,
|
|
@@ -18,7 +18,7 @@ class SystemPromptRunner(Generic[AgentDepsT]):
|
|
|
18
18
|
|
|
19
19
|
def __post_init__(self):
|
|
20
20
|
self._takes_ctx = len(inspect.signature(self.function).parameters) > 0
|
|
21
|
-
self._is_async =
|
|
21
|
+
self._is_async = _utils.is_async_callable(self.function)
|
|
22
22
|
|
|
23
23
|
async def run(self, run_context: RunContext[AgentDepsT]) -> str:
|
|
24
24
|
if self._takes_ctx:
|
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
from __future__ import annotations as _annotations
|
|
2
2
|
|
|
3
3
|
import asyncio
|
|
4
|
+
import functools
|
|
5
|
+
import inspect
|
|
4
6
|
import time
|
|
5
7
|
import uuid
|
|
6
|
-
from collections.abc import AsyncIterable, AsyncIterator, Iterator
|
|
8
|
+
from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Iterator
|
|
7
9
|
from contextlib import asynccontextmanager, suppress
|
|
8
10
|
from dataclasses import dataclass, fields, is_dataclass
|
|
9
11
|
from datetime import datetime, timezone
|
|
10
12
|
from functools import partial
|
|
11
13
|
from types import GenericAlias
|
|
12
|
-
from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar, Union
|
|
14
|
+
from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar, Union, overload
|
|
13
15
|
|
|
14
16
|
from anyio.to_thread import run_sync
|
|
15
17
|
from pydantic import BaseModel, TypeAdapter
|
|
16
18
|
from pydantic.json_schema import JsonSchemaValue
|
|
17
|
-
from typing_extensions import ParamSpec, TypeAlias, TypeGuard, is_typeddict
|
|
19
|
+
from typing_extensions import ParamSpec, TypeAlias, TypeGuard, TypeIs, is_typeddict
|
|
18
20
|
|
|
19
21
|
from pydantic_graph._utils import AbstractSpan
|
|
20
22
|
|
|
@@ -302,3 +304,26 @@ def dataclasses_no_defaults_repr(self: Any) -> str:
|
|
|
302
304
|
|
|
303
305
|
def number_to_datetime(x: int | float) -> datetime:
|
|
304
306
|
return TypeAdapter(datetime).validate_python(x)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
AwaitableCallable = Callable[..., Awaitable[T]]
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
@overload
|
|
313
|
+
def is_async_callable(obj: AwaitableCallable[T]) -> TypeIs[AwaitableCallable[T]]: ...
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
@overload
|
|
317
|
+
def is_async_callable(obj: Any) -> TypeIs[AwaitableCallable[Any]]: ...
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def is_async_callable(obj: Any) -> Any:
|
|
321
|
+
"""Correctly check if a callable is async.
|
|
322
|
+
|
|
323
|
+
This function was copied from Starlette:
|
|
324
|
+
https://github.com/encode/starlette/blob/78da9b9e218ab289117df7d62aee200ed4c59617/starlette/_utils.py#L36-L40
|
|
325
|
+
"""
|
|
326
|
+
while isinstance(obj, functools.partial):
|
|
327
|
+
obj = obj.func
|
|
328
|
+
|
|
329
|
+
return inspect.iscoroutinefunction(obj) or (callable(obj) and inspect.iscoroutinefunction(obj.__call__)) # type: ignore
|
|
@@ -28,6 +28,7 @@ from . import (
|
|
|
28
28
|
result,
|
|
29
29
|
usage as _usage,
|
|
30
30
|
)
|
|
31
|
+
from ._agent_graph import HistoryProcessor
|
|
31
32
|
from .models.instrumented import InstrumentationSettings, InstrumentedModel, instrument_model
|
|
32
33
|
from .result import FinalResult, OutputDataT, StreamedRunResult
|
|
33
34
|
from .settings import ModelSettings, merge_model_settings
|
|
@@ -179,6 +180,7 @@ class Agent(Generic[AgentDepsT, OutputDataT]):
|
|
|
179
180
|
defer_model_check: bool = False,
|
|
180
181
|
end_strategy: EndStrategy = 'early',
|
|
181
182
|
instrument: InstrumentationSettings | bool | None = None,
|
|
183
|
+
history_processors: Sequence[HistoryProcessor] | None = None,
|
|
182
184
|
) -> None: ...
|
|
183
185
|
|
|
184
186
|
@overload
|
|
@@ -208,6 +210,7 @@ class Agent(Generic[AgentDepsT, OutputDataT]):
|
|
|
208
210
|
defer_model_check: bool = False,
|
|
209
211
|
end_strategy: EndStrategy = 'early',
|
|
210
212
|
instrument: InstrumentationSettings | bool | None = None,
|
|
213
|
+
history_processors: Sequence[HistoryProcessor] | None = None,
|
|
211
214
|
) -> None: ...
|
|
212
215
|
|
|
213
216
|
def __init__(
|
|
@@ -232,6 +235,7 @@ class Agent(Generic[AgentDepsT, OutputDataT]):
|
|
|
232
235
|
defer_model_check: bool = False,
|
|
233
236
|
end_strategy: EndStrategy = 'early',
|
|
234
237
|
instrument: InstrumentationSettings | bool | None = None,
|
|
238
|
+
history_processors: Sequence[HistoryProcessor] | None = None,
|
|
235
239
|
**_deprecated_kwargs: Any,
|
|
236
240
|
):
|
|
237
241
|
"""Create an agent.
|
|
@@ -275,6 +279,9 @@ class Agent(Generic[AgentDepsT, OutputDataT]):
|
|
|
275
279
|
[`Agent.instrument_all()`][pydantic_ai.Agent.instrument_all]
|
|
276
280
|
will be used, which defaults to False.
|
|
277
281
|
See the [Debugging and Monitoring guide](https://ai.pydantic.dev/logfire/) for more info.
|
|
282
|
+
history_processors: Optional list of callables to process the message history before sending it to the model.
|
|
283
|
+
Each processor takes a list of messages and returns a modified list of messages.
|
|
284
|
+
Processors can be sync or async and are applied in sequence.
|
|
278
285
|
"""
|
|
279
286
|
if model is None or defer_model_check:
|
|
280
287
|
self.model = model
|
|
@@ -343,6 +350,7 @@ class Agent(Generic[AgentDepsT, OutputDataT]):
|
|
|
343
350
|
self._max_result_retries = output_retries if output_retries is not None else retries
|
|
344
351
|
self._mcp_servers = mcp_servers
|
|
345
352
|
self._prepare_tools = prepare_tools
|
|
353
|
+
self.history_processors = history_processors or []
|
|
346
354
|
for tool in tools:
|
|
347
355
|
if isinstance(tool, Tool):
|
|
348
356
|
self._register_tool(tool)
|
|
@@ -669,10 +677,11 @@ class Agent(Generic[AgentDepsT, OutputDataT]):
|
|
|
669
677
|
if self._instructions is None and not self._instructions_functions:
|
|
670
678
|
return None
|
|
671
679
|
|
|
672
|
-
instructions = self._instructions
|
|
680
|
+
instructions = [self._instructions] if self._instructions else []
|
|
673
681
|
for instructions_runner in self._instructions_functions:
|
|
674
|
-
instructions
|
|
675
|
-
|
|
682
|
+
instructions.append(await instructions_runner.run(run_context))
|
|
683
|
+
concatenated_instructions = '\n'.join(instruction for instruction in instructions if instruction)
|
|
684
|
+
return concatenated_instructions.strip() if concatenated_instructions else None
|
|
676
685
|
|
|
677
686
|
# Copy the function tools so that retry state is agent-run-specific
|
|
678
687
|
# Note that the retry count is reset to 0 when this happens due to the `default=0` and `init=False`.
|
|
@@ -689,6 +698,7 @@ class Agent(Generic[AgentDepsT, OutputDataT]):
|
|
|
689
698
|
end_strategy=self.end_strategy,
|
|
690
699
|
output_schema=output_schema,
|
|
691
700
|
output_validators=output_validators,
|
|
701
|
+
history_processors=self.history_processors,
|
|
692
702
|
function_tools=run_function_tools,
|
|
693
703
|
mcp_servers=self._mcp_servers,
|
|
694
704
|
default_retries=self._default_retries,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
from __future__ import annotations as _annotations
|
|
2
2
|
|
|
3
3
|
import base64
|
|
4
|
-
import uuid
|
|
5
4
|
from abc import ABC, abstractmethod
|
|
6
5
|
from collections.abc import Sequence
|
|
7
6
|
from dataclasses import dataclass, field, replace
|
|
@@ -888,13 +887,13 @@ class FunctionToolCallEvent:
|
|
|
888
887
|
|
|
889
888
|
part: ToolCallPart
|
|
890
889
|
"""The (function) tool call to make."""
|
|
891
|
-
call_id: str = field(init=False)
|
|
892
|
-
"""An ID used for matching details about the call to its result. If present, defaults to the part's tool_call_id."""
|
|
893
890
|
event_kind: Literal['function_tool_call'] = 'function_tool_call'
|
|
894
891
|
"""Event type identifier, used as a discriminator."""
|
|
895
892
|
|
|
896
|
-
|
|
897
|
-
|
|
893
|
+
@property
|
|
894
|
+
def call_id(self) -> str:
|
|
895
|
+
"""An ID used for matching details about the call to its result. If present, defaults to the part's tool_call_id."""
|
|
896
|
+
return self.part.tool_call_id
|
|
898
897
|
|
|
899
898
|
__repr__ = _utils.dataclasses_no_defaults_repr
|
|
900
899
|
|
|
@@ -220,7 +220,7 @@ class AnthropicModel(Model):
|
|
|
220
220
|
extra_headers = model_settings.get('extra_headers', {})
|
|
221
221
|
extra_headers.setdefault('User-Agent', get_user_agent())
|
|
222
222
|
return await self.client.beta.messages.create(
|
|
223
|
-
max_tokens=model_settings.get('max_tokens',
|
|
223
|
+
max_tokens=model_settings.get('max_tokens', 4096),
|
|
224
224
|
system=system_prompt or NOT_GIVEN,
|
|
225
225
|
messages=anthropic_messages,
|
|
226
226
|
model=self._model_name,
|
|
@@ -723,9 +723,7 @@ class _GeminiFunction(TypedDict):
|
|
|
723
723
|
|
|
724
724
|
def _function_from_abstract_tool(tool: ToolDefinition) -> _GeminiFunction:
|
|
725
725
|
json_schema = tool.parameters_json_schema
|
|
726
|
-
f = _GeminiFunction(name=tool.name, description=tool.description)
|
|
727
|
-
if json_schema.get('properties'):
|
|
728
|
-
f['parameters'] = json_schema
|
|
726
|
+
f = _GeminiFunction(name=tool.name, description=tool.description, parameters=json_schema)
|
|
729
727
|
return f
|
|
730
728
|
|
|
731
729
|
|
|
@@ -469,9 +469,11 @@ def _process_response_from_parts(
|
|
|
469
469
|
|
|
470
470
|
def _function_declaration_from_tool(tool: ToolDefinition) -> FunctionDeclarationDict:
|
|
471
471
|
json_schema = tool.parameters_json_schema
|
|
472
|
-
f = FunctionDeclarationDict(
|
|
473
|
-
|
|
474
|
-
|
|
472
|
+
f = FunctionDeclarationDict(
|
|
473
|
+
name=tool.name,
|
|
474
|
+
description=tool.description,
|
|
475
|
+
parameters=json_schema, # type: ignore
|
|
476
|
+
)
|
|
475
477
|
return f
|
|
476
478
|
|
|
477
479
|
|
|
@@ -48,68 +48,74 @@ class Provider(ABC, Generic[InterfaceClient]):
|
|
|
48
48
|
return None # pragma: no cover
|
|
49
49
|
|
|
50
50
|
|
|
51
|
-
def
|
|
52
|
-
"""
|
|
51
|
+
def infer_provider_class(provider: str) -> type[Provider[Any]]: # noqa: C901
|
|
52
|
+
"""Infers the provider class from the provider name."""
|
|
53
53
|
if provider == 'openai':
|
|
54
54
|
from .openai import OpenAIProvider
|
|
55
55
|
|
|
56
|
-
return OpenAIProvider
|
|
56
|
+
return OpenAIProvider
|
|
57
57
|
elif provider == 'deepseek':
|
|
58
58
|
from .deepseek import DeepSeekProvider
|
|
59
59
|
|
|
60
|
-
return DeepSeekProvider
|
|
60
|
+
return DeepSeekProvider
|
|
61
61
|
elif provider == 'openrouter':
|
|
62
62
|
from .openrouter import OpenRouterProvider
|
|
63
63
|
|
|
64
|
-
return OpenRouterProvider
|
|
64
|
+
return OpenRouterProvider
|
|
65
65
|
elif provider == 'azure':
|
|
66
66
|
from .azure import AzureProvider
|
|
67
67
|
|
|
68
|
-
return AzureProvider
|
|
68
|
+
return AzureProvider
|
|
69
69
|
elif provider == 'google-vertex':
|
|
70
70
|
from .google_vertex import GoogleVertexProvider
|
|
71
71
|
|
|
72
|
-
return GoogleVertexProvider
|
|
72
|
+
return GoogleVertexProvider
|
|
73
73
|
elif provider == 'google-gla':
|
|
74
74
|
from .google_gla import GoogleGLAProvider
|
|
75
75
|
|
|
76
|
-
return GoogleGLAProvider
|
|
76
|
+
return GoogleGLAProvider
|
|
77
77
|
# NOTE: We don't test because there are many ways the `boto3.client` can retrieve the credentials.
|
|
78
78
|
elif provider == 'bedrock':
|
|
79
79
|
from .bedrock import BedrockProvider
|
|
80
80
|
|
|
81
|
-
return BedrockProvider
|
|
81
|
+
return BedrockProvider
|
|
82
82
|
elif provider == 'groq':
|
|
83
83
|
from .groq import GroqProvider
|
|
84
84
|
|
|
85
|
-
return GroqProvider
|
|
85
|
+
return GroqProvider
|
|
86
86
|
elif provider == 'anthropic':
|
|
87
87
|
from .anthropic import AnthropicProvider
|
|
88
88
|
|
|
89
|
-
return AnthropicProvider
|
|
89
|
+
return AnthropicProvider
|
|
90
90
|
elif provider == 'mistral':
|
|
91
91
|
from .mistral import MistralProvider
|
|
92
92
|
|
|
93
|
-
return MistralProvider
|
|
93
|
+
return MistralProvider
|
|
94
94
|
elif provider == 'cohere':
|
|
95
95
|
from .cohere import CohereProvider
|
|
96
96
|
|
|
97
|
-
return CohereProvider
|
|
97
|
+
return CohereProvider
|
|
98
98
|
elif provider == 'grok':
|
|
99
99
|
from .grok import GrokProvider
|
|
100
100
|
|
|
101
|
-
return GrokProvider
|
|
101
|
+
return GrokProvider
|
|
102
102
|
elif provider == 'fireworks':
|
|
103
103
|
from .fireworks import FireworksProvider
|
|
104
104
|
|
|
105
|
-
return FireworksProvider
|
|
105
|
+
return FireworksProvider
|
|
106
106
|
elif provider == 'together':
|
|
107
107
|
from .together import TogetherProvider
|
|
108
108
|
|
|
109
|
-
return TogetherProvider
|
|
109
|
+
return TogetherProvider
|
|
110
110
|
elif provider == 'heroku':
|
|
111
111
|
from .heroku import HerokuProvider
|
|
112
112
|
|
|
113
|
-
return HerokuProvider
|
|
113
|
+
return HerokuProvider
|
|
114
114
|
else: # pragma: no cover
|
|
115
115
|
raise ValueError(f'Unknown provider: {provider}')
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def infer_provider(provider: str) -> Provider[Any]:
|
|
119
|
+
"""Infer the provider from the provider name."""
|
|
120
|
+
provider_class = infer_provider_class(provider)
|
|
121
|
+
return provider_class()
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
from __future__ import annotations as _annotations
|
|
2
2
|
|
|
3
|
-
import asyncio
|
|
4
3
|
import dataclasses
|
|
5
4
|
import json
|
|
6
5
|
from collections.abc import Awaitable, Sequence
|
|
@@ -337,7 +336,7 @@ class Tool(Generic[AgentDepsT]):
|
|
|
337
336
|
validator=SchemaValidator(schema=core_schema.any_schema()),
|
|
338
337
|
json_schema=json_schema,
|
|
339
338
|
takes_ctx=False,
|
|
340
|
-
is_async=
|
|
339
|
+
is_async=_utils.is_async_callable(function),
|
|
341
340
|
)
|
|
342
341
|
|
|
343
342
|
return cls(
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|