8000 Toolsets by DouweM · Pull Request #2024 · pydantic/pydantic-ai · GitHub
[go: up one dir, main page]

Skip to content

Toolsets #2024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 162 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
162 commits
Select commit Hold shift + click to select a range
e290951
WIP: Output modes
DouweM Jun 3, 2025
2056539
WIP: More output modes
DouweM Jun 3, 2025
bceba19
Merge remote-tracking branch 'origin/main' into output-modes
DouweM Jun 3, 2025
0cb25c4
Fix tests
DouweM Jun 3, 2025
933b74e
Remove syntax invalid before Python 3.12
DouweM Jun 3, 2025
7974df0
Fix tests
DouweM Jun 3, 2025
9cc19e2
Add TextOutput marker
DouweM Jun 9, 2025
bc6bb65
Merge remote-tracking branch 'origin/main' into output-modes
DouweM Jun 9, 2025
0e356a3
Add VCR recording of new test
DouweM Jun 9, 2025
81312dc
Implement additional output modes in GeminiModel and GoogleModel
DouweM Jun 10, 2025
52ef4d5
Fix prompted_json on OpenAIResponses
DouweM Jun 10, 2025
fe05956
Test output modes on Gemini and Anthropic
DouweM Jun 10, 2025
94421f3
Add VCR recordings of Gemini output mode tests
DouweM Jun 10, 2025
1902d00
Remove some old TODO comments
DouweM Jun 10, 2025
1f53c9b
Add missing VCR recording of Gemini output mode test
DouweM Jun 10, 2025
a4c2877
Add more missing VCR recordings
DouweM Jun 10, 2025
56e58f9
Fix OpenAI tools
DouweM Jun 10, 2025
a5234e1
Improve test coverage
DouweM Jun 10, 2025
40def08
Update unsupported output mode error message
DouweM Jun 10, 2025
837d305
Improve test coverage
DouweM Jun 10, 2025
3598bef
Merge branch 'main' into output-modes
DouweM Jun 10, 2025
5f71ba8
Test streaming with structured text output
DouweM Jun 10, 2025
cfc2749
Make TextOutputFunction Python 3.9 compatible
DouweM Jun 10, 2025
a137641
Properly merge JSON schemas accounting for defs
DouweM Jun 11, 2025
f495d46
Refactor output schemas and modes: more 'isinstance(output_schema, ..…
DouweM Jun 12, 2025
449ed0d
Merge branch 'main' into output-modes
DouweM Jun 12, 2025
e70d249
Clean up some variable names
DouweM Jun 12, 2025
4592b0b
Improve test coverage
DouweM Jun 12, 2025
db1c628
Merge branch 'main' into output-modes
DouweM Jun 13, 2025
f57d078
Combine JsonSchemaOutput and PromptedJsonOutput into StructuredTextOu…
DouweM Jun 13, 2025
5112455
Add missing cassettes
DouweM Jun 13, 2025
416cc7d
Can't use dataclass kw_only on 3.9
DouweM Jun 13, 2025
4b0e5cf
Improve test coverage
DouweM Jun 13, 2025
094920f
Improve test coverage
DouweM Jun 13, 2025
9f61706
Improve test coverage
DouweM Jun 13, 2025
9f51387
Remove unnecessary coverage ignores
DouweM Jun 13, 2025
9a1e628
Remove unnecessary coverage ignore
DouweM Jun 13, 2025
2b5fa81
Add docs
DouweM Jun 13, 2025
6c4662b
Fix docs refs
DouweM Jun 13, 2025
3ed3431
Fix nested list in docs
DouweM Jun 13, 2025
3d77818
Merge branch 'main' into output-modes
DouweM Jun 17, 2025
a86d7d4
Split StructuredTextOutput into ModelStructuredOutput and PromptedStr…
DouweM Jun 17, 2025
ce985a0
Merge branch 'main' into output-modes
DouweM Jun 17, 2025
71d1655
Fix WrapperModel.profile
DouweM Jun 17, 2025
8c04144
Update output modes docs
DouweM Jun 17, 2025
d78b5f7
Add examples to output mode marker docstrings
DouweM Jun 17, 2025
70d1197
Fix mypy type inference
DouweM Jun 17, 2025
2eb7fd1
Improve test coverage
DouweM Jun 17, 2025
25ccb54
Merge branch 'main' into output-modes
DouweM Jun 17, 2025
9e00c32
Import cast and RunContext in _function_schema
DouweM Jun 17, 2025
7de3c0d
Move RunContext and AgentDepsT into their own module to solve circula…
DouweM Jun 17, 2025
4029fac
Make _run_context module private, RunContext can be accessed through …
DouweM Jun 17, 2025
98bccf2
Merge branch 'main' into output-modes
DouweM Jun 19, 2025
8041cf3
Fix thinking part related tests
DouweM Jun 19, 2025
9bfed04
Implement Toolset
DouweM Jun 20, 2025
0f8da74
Make MCPServer a Toolset
DouweM Jun 20, 2025
8a29836
--no-edit
DouweM Jun 21, 2025
3d2012c
Add MappedToolset
DouweM Jun 21, 2025
901267d
Import Never from typing_extensions instead of typing
DouweM Jun 21, 2025
b9258d7
from __future__ import annotations
DouweM Jun 21, 2025
27ccbd1
Update client.md
DouweM Jun 21, 2025
3031e55
Pass only RunToolset to agent graph
DouweM Jun 21, 2025
ebd0b57
Make WrapperToolset abstract
DouweM Jun 21, 2025
867bf68
Introduce ToolDefinition.kind == 'pending'
DouweM Jun 21, 2025
c1115ae
Rename pending tools to deferred tools
DouweM Jun 21, 2025
6abd603
Merge branch 'main' into toolsets
DouweM Jun 24, 2025
a2f69df
Fix retries
DouweM Jun 24, 2025
0e0bf35
Remove duplicate cassettes
DouweM Jun 24, 2025
735df29
Merge branch 'main' into toolsets
DouweM Jun 26, 2025
8745a7a
Pass just one toolset into the run
DouweM Jun 26, 2025
05aa972
WIP
DouweM Jun 26, 2025
ad6e826
Fix streaming tool calls
DouweM Jun 27, 2025
84cd954
Stop double counting retries and reset on success
DouweM Jun 27, 2025
74a56ae
Fix retry error wrapping
DouweM Jun 27, 2025
0360e77
Make DeferredToolCalls work with streaming
DouweM Jun 30, 2025
6607b00
Merge branch 'main' into toolsets
DouweM Jun 30, 2025
8a3febb
Let toolsets be overridden in run/iter/run_stream/run_sync
DouweM Jun 30, 2025
2e200ac
Add DeferredToolset
DouweM Jun 30, 2025
1cb7f32
Add LangChainToolset
DouweM Jun 30, 2025
a6eba43
Add Agent.prepare_output_tools
DouweM Jun 30, 2025
0c96126
Require WrapperToolset subclasses to implement their own prepare_for_run
DouweM Jul 1, 2025
2348f45
Require DeferredToolCalls to be used with other output type
DouweM Jul 1, 2025
9dc684e
Merge branch 'main' into toolsets
DouweM Jul 1, 2025
f3124c0
Lots of cleanup
DouweM Jul 1, 2025
f660cc1
Some more tweaks
DouweM Jul 2, 2025
64dacbb
Merge branch 'main' into toolsets
DouweM Jul 2, 2025
5ca305e
Fix docs example
DouweM Jul 2, 2025
c5ef5f6
Address some feedback
DouweM Jul 2, 2025
badbe23
Merge branch 'main' into toolsets
DouweM Jul 2, 2025
acddb8d
Add sampling_model to Agent __init__, iter, run (etc), and override, …
DouweM Jul 2, 2025
89fc266
Turn RunContext.retries from a defaultdict into a dict again as the 0…
DouweM Jul 2, 2025
7e3331b
Remove unnecessary if TYPE_CHECKING
DouweM Jul 2, 2025
ebf6f40
Remove Agent sampling_model field (and method argument) in favor of A…
DouweM Jul 3, 2025
f7db040
Allow OutputSpec to be nested
DouweM Jul 3, 2025
fe07149
Document Agent.__aenter__
DouweM Jul 3, 2025
a0f4678
Import Self from typing_extensions instead of typing
DouweM Jul 3, 2025
db82d00
Actually use Agent.prepare_output_tools
DouweM Jul 4, 2025
dea8050
Update test to account for fact that text output with early end_strat…
DouweM Jul 4, 2025
131a325
Improve test coverage
DouweM Jul 4, 2025
8203732
Merge branch 'main' into toolsets
DouweM Jul 4, 2025
778962c
Make Agent MCP-related tests only run when mcp can be imported
DouweM Jul 4, 2025
e6575a9
Add tests
DouweM Jul 4, 2025
9f9ee55
AbstractToolset.call_tool now takes a ToolCallPart
DouweM Jul 4, 2025
a3c9a59
Fix MCP process_tool_call example
DouweM Jul 4, 2025
6eae653
Fix test coverage
DouweM Jul 4, 2025
2b3a9e5
Merge branch 'main' into toolsets
DouweM Jul 4, 2025
b2aa894
Improve coverage
DouweM Jul 4, 2025
ecf6f75
Merge branch 'main' into toolsets
DouweM Jul 8, 2025
1c2d221
Address feedback
DouweM Jul 8, 2025
ca4915b
Make test_docs_examples an async test so Python 3.9 lets us instantia…
DouweM Jul 8, 2025
972e4a7
Merge branch 'main' into toolsets
DouweM Jul 8, 2025
93bb682
Fix test snapshots
DouweM Jul 8, 2025
8a986be
Revert "Make test_docs_examples an async test so Python 3.9 lets us i…
DouweM Jul 8, 2025
9c399c7
Make asyncio.Lock work in Python 3.9 when there is no event loop yet
DouweM Jul 8, 2025
a4f8c48
Address feedback, fix docs test
DouweM Jul 8, 2025
3e1847f
Give the A2A task some more time to complete
DouweM Jul 8, 2025
4daa152
Branch is OK to not be covered
DouweM Jul 8, 2025
c5c6f00
agent.iter(toolsets=...) is now additional, while new agent.override(…
DouweM Jul 9, 2025
f9ba559
Respect overridden toolsets in Agent.__aenter__ and Agent.set_mcp_sam…
DouweM Jul 9, 2025
b165503
Fix tool conflict error message
DouweM Jul 9, 2025
4baa710
Rename FunctionToolset.register_{tool,function} to add_{tool,function}
DouweM Jul 9, 2025
39e0353
Branch is OK to not be covered
DouweM Jul 9, 2025
18fcdf7
Add test to ensure tools can be added during a run
DouweM Jul 9, 2025
af6ce7d
Make CallableToolset public as we're going to want to let people defi…
DouweM Jul 9, 2025
93e6691
Make it easier to override tool call behavior by subclassing WrapperT…
DouweM Jul 10, 2025
3a4c4c8
Start writing docs
DouweM Jul 10, 2025
87aaa6c
Make WrapperToolset easier to subclass with new _rewrap_for_run method
DouweM Jul 10, 2025
8b81e65
Add classes I forget to add and push
DouweM Jul 10, 2025
e72548e
Make all public toolsets importable from pydantic_ai.toolsets
DouweM Jul 10, 2025
b8c93f1
Add ACIToolset
DouweM Jul 10, 2025
f87319c
Document LangChainToolset and ACIToolset
DouweM Jul 10, 2025
8136441
Merge branch 'main' into toolsets
DouweM Jul 10, 2025
239fc3d
Toolset._call_tool is always async
DouweM Jul 10, 2025
febbd08
A WrapperToolset subclass with no additional fields does not need to …
DouweM Jul 10, 2025
cfa9ccc
Add some more docs
DouweM Jul 10, 2025
50a72a0
Merge branch 'main' into toolsets
DouweM Jul 10, 2025
0151e20
Mostly finish docs
DouweM Jul 11, 2025
d27b4ec
Fix FunctionToolset.max_retries
DouweM Jul 11, 2025
692898e
Fix docs example output
DouweM Jul 11, 2025
06838e2
Make AbstractToolset overridable methods public
DouweM Jul 15, 2025
215eaae
Merge branch 'main' into toolsets
DouweM Jul 15, 2025
8018600
Merge remote-tracking branch 'origin/toolsets' into toolsets 8000
DouweM Jul 15, 2025
b2637f8
WIP
DouweM Jul 15, 2025
a25df7f
WIP
DouweM Jul 15, 2025
13d9c03
Remove AbstractToolset.for_run_step
DouweM Jul 15, 2025
ac5f77d
Rename AbstractToolset.accept to apply
DouweM Jul 15, 2025
0c8b25a
Fix toolsets docs
DouweM Jul 15, 2025
6046a1c
Fix example for 3.9
DouweM Jul 15, 2025
70d24da
Update docs
DouweM Jul 15, 2025
a4dedb3
Improve coverage
DouweM Jul 15, 2025
f48bd73
Improve docstrings
DouweM Jul 15, 2025
a4e0c04
Improve docstrings
DouweM Jul 16, 2025
fef897d
Fix docs link
DouweM Jul 16, 2025
9d3d240
Fix docs links
DouweM Jul 16, 2025
f3d1ae0
Add filtered, prefixed, prepared, renamed and wrap convenience method…
DouweM Jul 16, 2025
f756132
Merge branch 'main' into toolsets
DouweM Jul 16, 2025
57b0720
Move tool call tracing to ToolManager
DouweM Jul 16, 2025
e2e0f58
Merge branch 'main' into toolsets
DouweM Jul 16, 2025
7830c73
Fix huggingface_hub.AsyncInferenceClient link in docs
DouweM Jul 16, 2025
c9c8873
Add huggingface doc to nav
DouweM Jul 16, 2025
7e4629e
Fix coverage
DouweM Jul 16, 2025
28c753d
Fix coverage
DouweM Jul 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move RunContext and AgentDepsT into their own module to solve circula…
…r import
  • Loading branch information
DouweM committed Jun 17, 2025
commit 7de3c0d2ea036f9e6cc0a8ff544c362fbee1ecc2
5 changes: 2 additions & 3 deletions pydantic_ai_slim/pydantic_ai/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@

from typing_inspection.introspection import get_literal_values

from pydantic_ai.output import OutputDataT
from pydantic_ai.tools import AgentDepsT

from . import __version__
from .agent import Agent
from .exceptions import UserError
from .messages import ModelMessage
from .models import KnownModelName, infer_model
from .output import OutputDataT
from .run_context import AgentDepsT

try:
import argcomplete
Expand Down
2 changes: 1 addition & 1 deletion pydantic_ai_slim/pydantic_ai/_function_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from ._griffe import doc_descriptions
from ._utils import check_object_json_schema, is_async_callable, is_model_like, run_in_executor
from .tools import RunContext
from .run_context import RunContext

if TYPE_CHECKING:
from .tools import DocstringFormat, ObjectJsonSchema
Expand Down
3 changes: 2 additions & 1 deletion pydantic_ai_slim/pydantic_ai/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
ToolRetryError,
_flatten_output_spec, # pyright: ignore[reportPrivateUsage]
)
from .tools import AgentDepsT, GenerateToolJsonSchema, ObjectJsonSchema, RunContext, ToolDefinition
from .run_context import AgentDepsT, RunContext
from .tools import GenerateToolJsonSchema, ObjectJsonSchema, ToolDefinition

if TYPE_CHECKING:
from .profiles import ModelProfile
Expand Down
3 changes: 2 additions & 1 deletion pydantic_ai_slim/pydantic_ai/_system_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from typing import Any, Callable, Generic, cast

from . import _utils
from .tools import AgentDepsT, RunContext, SystemPromptFunc
from .run_context import AgentDepsT, RunContext
from .tools import SystemPromptFunc


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion pydantic_ai_slim/pydantic_ai/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
OutputDataT,
ToolOutput,
)
from .tools import AgentDepsT, RunContext
from .run_context import AgentDepsT, RunContext
from .usage import Usage, UsageLimits

__all__ = (
Expand Down
56 changes: 56 additions & 0 deletions pydantic_ai_slim/pydantic_ai/run_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from __future__ import annotations as _annotations

import dataclasses
from collections.abc import Sequence
from dataclasses import field
from typing import TYPE_CHECKING, Generic

from typing_extensions import TypeVar

from . import _utils, messages as _messages

if TYPE_CHECKING:
from .models import Model
from .result import Usage

AgentDepsT = TypeVar('AgentDepsT', default=None, contravariant=True)
"""Type variable for agent dependencies."""


@dataclasses.dataclass(repr=False)
class RunContext(Generic[AgentDepsT]):
"""Information about the current call."""

deps: AgentDepsT
"""Dependencies for the agent."""
model: Model
"""The model used in this run."""
usage: Usage
"""LLM usage associated with the run."""
prompt: str | Sequence[_messages.UserContent] | None
"""The original user prompt passed to the run."""
messages: list[_messages.ModelMessage] = field(default_factory=list)
"""Messages exchanged in the conversation so far."""
tool_call_id: str | None = None
"""The ID of the tool call."""
tool_name: str | None = None
"""Name of the tool being called."""
retry: int = 0
"""Number of retries so far."""
run_step: int = 0
"""The current step in the run."""

def replace_with(
self,
retry: int | None = None,
tool_name: str | None | _utils.Unset = _utils.UNSET,
) -> RunContext[AgentDepsT]:
# Create a new `RunContext` a new `retry` value and `tool_name`.
kwargs = {}
if retry is not None:
kwargs['retry'] = retry
if tool_name is not _utils.UNSET: # pragma: no branch
kwargs['tool_name'] = tool_name
return dataclasses.replace(self, **kwargs)

__repr__ = _utils.dataclasses_no_defaults_repr
49 changes: 2 additions & 47 deletions pydantic_ai_slim/pydantic_ai/tools.py
B5F7
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
from collections.abc import Awaitable, Sequence
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Callable, Generic, Literal, Union
from typing import Any, Callable, Generic, Literal, Union

from opentelemetry.trace import Tracer
from pydantic import ValidationError
Expand All @@ -14,10 +14,7 @@

from . import _function_schema, _utils, messages as _messages
from .exceptions import ModelRetry, UnexpectedModelBehavior

if TYPE_CHECKING:
from .models import Model
from .result import Usage
from .run_context import AgentDepsT, RunContext

__all__ = (
'AgentDepsT',
Expand All @@ -35,48 +32,6 @@
'ToolDefinition',
)

AgentDepsT = TypeVar('AgentDepsT', default=None, contravariant=True)
"""Type variable for agent dependencies."""


@dataclasses.dataclass(repr=False)
class RunContext(Generic[AgentDepsT]):
"""Information about the current call."""

deps: AgentDepsT
"""Dependencies for the agent."""
model: Model
"""The model used in this run."""
usage: Usage
"""LLM usage associated with the run."""
prompt: str | Sequence[_messages.UserContent] | None
"""The original user prompt passed to the run."""
messages: list[_messages.ModelMessage] = field(default_factory=list)
"""Messages exchanged in the conversation so far."""
tool_call_id: str | None = None
"""The ID of the tool call."""
tool_name: str | None = None
"""Name of the tool being called."""
retry: int = 0
"""Number of retries so far."""
run_step: int = 0
"""The current step in the run."""

def replace_with(
self,
retry: int | None = None,
tool_name: str | None | _utils.Unset = _utils.UNSET,
) -> RunContext[AgentDepsT]:
# Create a new `RunContext` a new `retry` value and `tool_name`.
kwargs = {}
if retry is not None:
kwargs['retry'] = retry
if tool_name is not _utils.UNSET: # pragma: no branch
kwargs['tool_name'] = tool_name
return dataclasses.replace(self, **kwargs)

__repr__ = _utils.dataclasses_no_defaults_repr


ToolParams = ParamSpec('ToolParams', default=...)
"""Retrieval function param spec."""
Expand Down
0