pydantic_ai.tools
RunContext
dataclass
Information about the current call.
Source code in pydantic_ai_slim/pydantic_ai/tools.py
40 41 42 43 44 45 46 47 48 49 |
|
SystemPromptFunc
module-attribute
SystemPromptFunc = Union[
Callable[[RunContext[AgentDeps]], str],
Callable[[RunContext[AgentDeps]], Awaitable[str]],
Callable[[], str],
Callable[[], Awaitable[str]],
]
A function that may or maybe not take RunContext
as an argument, and may or may not be async.
Usage SystemPromptFunc[AgentDeps]
.
ResultValidatorFunc
module-attribute
ResultValidatorFunc = Union[
Callable[
[RunContext[AgentDeps], ResultData], ResultData
],
Callable[
[RunContext[AgentDeps], ResultData],
Awaitable[ResultData],
],
Callable[[ResultData], ResultData],
Callable[[ResultData], Awaitable[ResultData]],
]
A function that always takes ResultData
and returns ResultData
,
but may or maybe not take CallInfo
as a first argument, and may or may not be async.
Usage ResultValidator[AgentDeps, ResultData]
.
ToolFuncContext
module-attribute
ToolFuncContext = Callable[
Concatenate[RunContext[AgentDeps], ToolParams], Any
]
A tool function that takes RunContext
as the first argument.
Usage ToolContextFunc[AgentDeps, ToolParams]
.
ToolFuncPlain
module-attribute
ToolFuncPlain = Callable[ToolParams, Any]
A tool function that does not take RunContext
as the first argument.
Usage ToolPlainFunc[ToolParams]
.
ToolFuncEither
module-attribute
ToolFuncEither = Union[
ToolFuncContext[AgentDeps, ToolParams],
ToolFuncPlain[ToolParams],
]
Either kind of tool function.
This is just a union of ToolFuncContext
and
ToolFuncPlain
.
Usage ToolFuncEither[AgentDeps, ToolParams]
.
ToolPrepareFunc
module-attribute
ToolPrepareFunc: TypeAlias = (
"Callable[[RunContext[AgentDeps], ToolDefinition], Awaitable[ToolDefinition | None]]"
)
Definition of a function that can prepare a tool definition at call time.
See tool docs for more information.
Example — here only_if_42
is valid as a ToolPrepareFunc
:
from typing import Union
from pydantic_ai import RunContext, Tool
from pydantic_ai.tools import ToolDefinition
async def only_if_42(
ctx: RunContext[int], tool_def: ToolDefinition
) -> Union[ToolDefinition, None]:
if ctx.deps == 42:
return tool_def
def hitchhiker(ctx: RunContext[int], answer: str) -> str:
return f'{ctx.deps} {answer}'
hitchhiker = Tool(hitchhiker, prepare=only_if_42)
Usage ToolPrepareFunc[AgentDeps]
.
Tool
dataclass
A tool function for an agent.
Source code in pydantic_ai_slim/pydantic_ai/tools.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
|
__init__
__init__(
function: ToolFuncEither[AgentDeps, ...],
*,
takes_ctx: bool | None = None,
max_retries: int | None = None,
name: str | None = None,
description: str | None = None,
prepare: ToolPrepareFunc[AgentDeps] | None = None
)
Create a new tool instance.
Example usage:
from pydantic_ai import Agent, RunContext, Tool
async def my_tool(ctx: RunContext[int], x: int, y: int) -> str:
return f'{ctx.deps} {x} {y}'
agent = Agent('test', tools=[Tool(my_tool)])
or with a custom prepare method:
from typing import Union
from pydantic_ai import Agent, RunContext, Tool
from pydantic_ai.tools import ToolDefinition
async def my_tool(ctx: RunContext[int], x: int, y: int) -> str:
return f'{ctx.deps} {x} {y}'
async def prep_my_tool(
ctx: RunContext[int], tool_def: ToolDefinition
) -> Union[ToolDefinition, None]:
# only register the tool if `deps == 42`
if ctx.deps == 42:
return tool_def
agent = Agent('test', tools=[Tool(my_tool, prepare=prep_my_tool)])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
ToolFuncEither[AgentDeps, ...]
|
The Python function to call as the tool. |
required |
takes_ctx
|
bool | None
|
Whether the function takes a |
None
|
max_retries
|
int | None
|
Maximum number of retries allowed for this tool, set to the agent default if |
None
|
name
|
str | None
|
Name of the tool, inferred from the function if |
None
|
description
|
str | None
|
Description of the tool, inferred from the function if |
None
|
prepare
|
ToolPrepareFunc[AgentDeps] | None
|
custom method to prepare the tool definition for each step, return |
None
|
Source code in pydantic_ai_slim/pydantic_ai/tools.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
prepare_tool_def
async
prepare_tool_def(
ctx: RunContext[AgentDeps],
) -> ToolDefinition | None
Get the tool definition.
By default, this method creates a tool definition, then either returns it, or calls self.prepare
if it's set.
Returns:
Type | Description |
---|---|
ToolDefinition | None
|
return a |
Source code in pydantic_ai_slim/pydantic_ai/tools.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
|
run
async
Run the tool function asynchronously.
Source code in pydantic_ai_slim/pydantic_ai/tools.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
ObjectJsonSchema
module-attribute
Type representing JSON schema of an object, e.g. where "type": "object"
.
This type is used to define tools parameters (aka arguments) in ToolDefinition.
With PEP-728 this should be a TypedDict with type: Literal['object']
, and extra_items=Any
ToolDefinition
dataclass
Definition of a tool passed to a model.
This is used for both function tools result tools.
Source code in pydantic_ai_slim/pydantic_ai/tools.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
|
parameters_json_schema
instance-attribute
parameters_json_schema: ObjectJsonSchema
The JSON schema for the tool's parameters.
outer_typed_dict_key
class-attribute
instance-attribute
outer_typed_dict_key: str | None = None
The key in the outer [TypedDict] that wraps a result tool.
This will only be set for result tools which don't have an object
JSON schema.