pydantic_ai.output
OutputDataT
module-attribute
OutputDataT = TypeVar(
"OutputDataT", default=str, covariant=True
)
Covariant type variable for the output data type of a run.
ToolOutput
dataclass
Bases: Generic[OutputDataT]
Marker class to use a tool for output and optionally customize the tool.
Example:
from pydantic import BaseModel
from pydantic_ai import Agent, ToolOutput
class Fruit(BaseModel):
name: str
color: str
class Vehicle(BaseModel):
name: str
wheels: int
agent = Agent(
'openai:gpt-5.2',
output_type=[
ToolOutput(Fruit, name='return_fruit'),
ToolOutput(Vehicle, name='return_vehicle'),
],
)
result = agent.run_sync('What is a banana?')
print(repr(result.output))
#> Fruit(name='banana', color='yellow')
Source code in pydantic_ai_slim/pydantic_ai/output.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
output
instance-attribute
output: OutputTypeOrFunction[OutputDataT] = type_
An output type or function.
name
instance-attribute
name: str | None = name
The name of the tool that will be passed to the model. If not specified and only one output is provided, final_result will be used. If multiple outputs are provided, the name of the output type or function will be added to the tool name.
description
instance-attribute
description: str | None = description
The description of the tool that will be passed to the model. If not specified, the docstring of the output type or function will be used.
max_retries
instance-attribute
max_retries: int | None = max_retries
Per-tool retry limit for this output tool.
Overrides the output side of the agent's retry budget, which itself acts as the per-tool default for output tools that do not specify their own limit. If not set, the agent-level value is used.
sequential
instance-attribute
sequential: bool = sequential
Whether this output tool must run as a barrier, not overlapping with other tool calls.
Only meaningful under end_strategy='exhaustive', where tools otherwise run in parallel: a
sequential=True output tool runs alone, so function tools the model emitted before it complete
first. Under 'early'/'graceful' output tools already run sequentially, so this has no effect.
NativeOutput
dataclass
Bases: Generic[OutputDataT]
Marker class to use the model's native structured outputs functionality for outputs and optionally customize the name and description.
Example:
from pydantic_ai import Agent, NativeOutput
from tool_output import Fruit, Vehicle
agent = Agent(
'openai:gpt-5.2',
output_type=NativeOutput(
[Fruit, Vehicle],
name='Fruit or vehicle',
description='Return a fruit or vehicle.'
),
)
result = agent.run_sync('What is a Ford Explorer?')
print(repr(result.output))
#> Vehicle(name='Ford Explorer', wheels=4)
Source code in pydantic_ai_slim/pydantic_ai/output.py
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 | |
outputs
instance-attribute
outputs: (
OutputTypeOrFunction[OutputDataT]
| Sequence[OutputTypeOrFunction[OutputDataT]]
) = outputs
The output types or functions.
name
instance-attribute
name: str | None = name
The name of the structured output that will be passed to the model. If not specified and only one output is provided, the name of the output type or function will be used.
description
instance-attribute
description: str | None = description
The description of the structured output that will be passed to the model. If not specified and only one output is provided, the docstring of the output type or function will be used.
strict
instance-attribute
strict: bool | None = strict
Whether to use strict mode for the output, if the model supports it.
template
instance-attribute
Template for the prompt passed to the model.
The '{schema}' placeholder will be replaced with the output JSON schema.
If no template is specified but the model's profile indicates that it requires the schema to be sent as a prompt, the default template specified on the profile will be used.
Set to False to disable the schema prompt entirely.
PromptedOutput
dataclass
Bases: Generic[OutputDataT]
Marker class to use a prompt to tell the model what to output and optionally customize the prompt.
Example:
from pydantic import BaseModel
from pydantic_ai import Agent, PromptedOutput
from tool_output import Vehicle
class Device(BaseModel):
name: str
kind: str
agent = Agent(
'openai:gpt-5.2',
output_type=PromptedOutput(
[Vehicle, Device],
name='Vehicle or device',
description='Return a vehicle or device.'
),
)
result = agent.run_sync('What is a MacBook?')
print(repr(result.output))
#> Device(name='MacBook', kind='laptop')
agent = Agent(
'openai:gpt-5.2',
output_type=PromptedOutput(
[Vehicle, Device],
template='Gimme some JSON: {schema}'
),
)
result = agent.run_sync('What is a Ford Explorer?')
print(repr(result.output))
#> Vehicle(name='Ford Explorer', wheels=4)
Source code in pydantic_ai_slim/pydantic_ai/output.py
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 | |
outputs
instance-attribute
outputs: (
OutputTypeOrFunction[OutputDataT]
| Sequence[OutputTypeOrFunction[OutputDataT]]
) = outputs
The output types or functions.
name
instance-attribute
name: str | None = name
The name of the structured output that will be passed to the model. If not specified and only one output is provided, the name of the output type or function will be used.
description
instance-attribute
description: str | None = description
The description that will be passed to the model. If not specified and only one output is provided, the docstring of the output type or function will be used.
template
instance-attribute
Template for the prompt passed to the model.
The '{schema}' placeholder will be replaced with the output JSON schema.
If not specified, the default template specified on the model's profile will be used.
Set to False to disable the schema prompt entirely.
TextOutput
dataclass
Bases: Generic[OutputDataT]
Marker class to use text output for an output function taking a string argument.
Example:
from pydantic_ai import Agent, TextOutput
def split_into_words(text: str) -> list[str]:
return text.split()
agent = Agent(
'openai:gpt-5.2',
output_type=TextOutput(split_into_words),
)
result = agent.run_sync('Who was Albert Einstein?')
print(result.output)
#> ['Albert', 'Einstein', 'was', 'a', 'German-born', 'theoretical', 'physicist.']
Source code in pydantic_ai_slim/pydantic_ai/output.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
output_function
instance-attribute
output_function: TextOutputFunc[OutputDataT]
The function that will be called to process the model's plain text output. The function must take a single string argument.
StructuredDict
StructuredDict(
json_schema: JsonSchemaValue,
name: str | None = None,
description: str | None = None,
) -> type[JsonSchemaValue]
Returns a dict[str, Any] subclass with a JSON schema attached that will be used for structured output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_schema
|
JsonSchemaValue
|
A JSON schema of type |
required |
name
|
str | None
|
Optional name of the structured output. If not provided, the |
None
|
description
|
str | None
|
Optional description of the structured output. If not provided, the |
None
|
Example:
from pydantic_ai import Agent, StructuredDict
schema = {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'age': {'type': 'integer'}
},
'required': ['name', 'age']
}
agent = Agent('openai:gpt-5.2', output_type=StructuredDict(schema))
result = agent.run_sync('Create a person')
print(result.output)
#> {'name': 'John Doe', 'age': 30}
Source code in pydantic_ai_slim/pydantic_ai/output.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | |