pydantic_ai.models.function
A model controlled by a local function.
FunctionModel
is similar to TestModel
,
but allows greater control over the model's behavior.
Its primary use case is for more advanced unit testing than is possible with TestModel
.
FunctionModel
dataclass
Bases: Model
A model controlled by a local function.
Apart from __init__
, all methods are private or match those of the base class.
Source code in pydantic_ai_slim/pydantic_ai/models/function.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
__init__
__init__(function: FunctionDef) -> None
__init__(*, stream_function: StreamFunctionDef) -> None
__init__(
function: FunctionDef,
*,
stream_function: StreamFunctionDef
) -> None
__init__(
function: FunctionDef | None = None,
*,
stream_function: StreamFunctionDef | None = None
)
Initialize a FunctionModel
.
Either function
or stream_function
must be provided, providing both is allowed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
FunctionDef | None
|
The function to call for non-streamed requests. |
None
|
stream_function
|
StreamFunctionDef | None
|
The function to call for streamed requests. |
None
|
Source code in pydantic_ai_slim/pydantic_ai/models/function.py
46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
AgentInfo
dataclass
Information about an agent.
This is passed as the second to functions used within FunctionModel
.
Source code in pydantic_ai_slim/pydantic_ai/models/function.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
function_tools
instance-attribute
function_tools: Mapping[str, AbstractToolDefinition]
The function tools available on this agent.
These are the tools registered via the tool
and
tool_plain
decorators.
allow_text_result
instance-attribute
allow_text_result: bool
Whether a plain text result is allowed.
result_tools
instance-attribute
result_tools: list[AbstractToolDefinition] | None
The tools that can called as the final result of the run.
DeltaToolCall
dataclass
Incremental change to a tool call.
Used to describe a chunk when streaming structured responses.
Source code in pydantic_ai_slim/pydantic_ai/models/function.py
99 100 101 102 103 104 105 106 107 108 109 |
|
DeltaToolCalls
module-attribute
DeltaToolCalls: TypeAlias = dict[int, DeltaToolCall]
A mapping of tool call IDs to incremental changes.
FunctionDef
module-attribute
FunctionDef: TypeAlias = Callable[
[list[Message], AgentInfo],
Union[ModelAnyResponse, Awaitable[ModelAnyResponse]],
]
A function used to generate a non-streamed response.
StreamFunctionDef
module-attribute
StreamFunctionDef: TypeAlias = Callable[
[list[Message], AgentInfo],
AsyncIterator[Union[str, DeltaToolCalls]],
]
A function used to generate a streamed response.
While this is defined as having return type of AsyncIterator[Union[str, DeltaToolCalls]]
, it should
really be considered as Union[AsyncIterator[str], AsyncIterator[DeltaToolCalls]
,
E.g. you need to yield all text or all DeltaToolCalls
, not mix them.
FunctionAgentModel
dataclass
Bases: AgentModel
Implementation of AgentModel
for FunctionModel.
Source code in pydantic_ai_slim/pydantic_ai/models/function.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 |
|
FunctionStreamTextResponse
dataclass
Bases: StreamTextResponse
Implementation of StreamTextResponse
for FunctionModel.
Source code in pydantic_ai_slim/pydantic_ai/models/function.py
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 |
|
FunctionStreamStructuredResponse
dataclass
Bases: StreamStructuredResponse
Implementation of StreamStructuredResponse
for FunctionModel.
Source code in pydantic_ai_slim/pydantic_ai/models/function.py
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 |
|