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 10000
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
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
Refactor output schemas and modes: more 'isinstance(output_schema, ..…
….)', less 'output_schema.mode == ...'
  • Loading branch information
DouweM committed Jun 12, 2025
commit f495d4693c40a8bb84b49a651ef3473594423917
45 changes: 24 additions & 21 deletions pydantic_ai_slim/pydantic_ai/_agent_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,12 @@ async def add_mcp_server_tools(server: MCPServer) -> None:
function_tool_defs = await ctx.deps.prepare_tools(run_context, function_tool_defs) or []

output_schema = ctx.deps.output_schema
assert output_schema.mode is not None # Should have been set in agent._prepare_output_schema

return models.ModelRequestParameters(
function_tools=function_tool_defs,
output_mode=output_schema.mode,
output_object=output_schema.text_output_schema.object_def if output_schema.text_output_schema else None,
output_tools=output_schema.tool_defs(),
allow_text_output=output_schema.allow_text_output == 'plain',
output_object=output_schema.object_def if isinstance(output_schema, _output.JsonTextOutputSchema) else None,
output_tools=output_schema.tool_defs() if isinstance(output_schema, _output.ToolOutputSchema) else [],
allow_text_output=isinstance(output_schema, _output.TextOutputSchema),
)


Expand Down Expand Up @@ -452,7 +450,7 @@ async def _run_stream() -> AsyncIterator[_messages.HandleResponseEvent]:
# when the model has already returned text along side tool calls
# in this scenario, if text responses are allowed, we return text from the most recent model
# response, if any
if ctx.deps.output_schema.allow_text_output:
if isinstance(ctx.deps.output_schema, _output.TextOutputSchema):
for message in reversed(ctx.state.message_history):
if isinstance(message, _messages.ModelResponse):
last_texts = [p.content for p in message.parts if isinstance(p, _messages.TextPart)]
Expand All @@ -475,21 +473,23 @@ async def _handle_tool_calls(
output_schema = ctx.deps.output_schema
run_context = build_run_context(ctx)

# first, look for the output tool call
final_result: result.FinalResult[NodeRunEndT] | None = None
parts: list[_messages.ModelRequestPart] = []
for call, output_tool in output_schema.find_tool(tool_calls):
try:
result_data = await output_tool.process(call, run_context)
result_data = await _validate_output(result_data, ctx, call)
except _output.ToolRetryError as e:
# TODO: Should only increment retry stuff once per node execution, not for each tool call
# Also, should increment the tool-specific retry count rather than the run retry count
ctx.state.increment_retries(ctx.deps.max_result_retries, e)
parts.append(e.tool_retry)
else:
final_result = result.FinalResult(result_data, call.tool_name, call.tool_call_id)
break

# first, look for the output tool call
if isinstance(output_schema, _output.ToolOutputSchema):
for call, output_tool in output_schema.find_tool(tool_calls):
try:
result_data = await output_tool.process(call, run_context)
result_data = await _validate_output(result_data, ctx, call)
except _output.ToolRetryError as e:
# TODO: Should only increment retry stuff once per node execution, not for each tool call
# Also, should increment the tool-specific retry count rather than the run retry count
ctx.state.increment_retries(ctx.deps.max_result_retries, e)
parts.append(e.tool_retry)
else:
final_result = result.FinalResult(result_data, call.tool_name, call.tool_call_id)
break

# Then build the other request parts based on end strategy
tool_responses: list[_messages.ModelRequestPart] = self._tool_responses
Expand Down Expand Up @@ -535,7 +535,7 @@ async def _handle_text_response(

text = '\n\n'.join(texts)
try:
if output_schema.allow_text_output:
if isinstance(output_schema, _output.TextOutputSchema):
run_context = build_run_context(ctx)
result_data = await output_schema.process(text, run_context)
else:
Expand Down Expand Up @@ -765,7 +765,10 @@ def _unknown_tool(
) -> _messages.RetryPromptPart:
ctx.state.increment_retries(ctx.deps.max_result_retries)
tool_names = list(ctx.deps.function_tools.keys())
tool_names.extend(ctx.deps.output_schema.tool_names())

output_schema = ctx.deps.output_schema
if isinstance(output_schema, _output.ToolOutputSchema):
tool_names.extend(output_schema.tool_names())

if tool_names:
msg = f'Available tools: {", ".join(tool_names)}'
Expand Down
Loading
0