pydantic_ai.output
OutputDataT 模块属性
OutputDataT = TypeVar(
"OutputDataT", default=str, covariant=True
)
用于运行的输出数据类型的协变类型变量。
ToolOutput 数据类
基类:Generic[OutputDataT]
标记类,用于将工具用作输出,并可选择性地自定义该工具。
示例
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-4o',
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')
源代码位于 pydantic_ai_slim/pydantic_ai/output.py
68 69 70 71 72 73 74 75 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 |
|
name instance-attribute
name: str | None = name
将传递给模型的工具名称。如果未指定且只提供了一个输出,将使用 final_result
。如果提供了多个输出,输出类型或函数的名称将被添加到工具名称中。
NativeOutput 数据类
基类:Generic[OutputDataT]
标记类,用于使用模型的原生结构化输出功能进行输出,并可选择性地自定义名称和描述。
示例
from pydantic_ai import Agent, NativeOutput
from tool_output import Fruit, Vehicle
agent = Agent(
'openai:gpt-4o',
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)
源代码位于 pydantic_ai_slim/pydantic_ai/output.py
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 |
|
outputs 实例属性
outputs: (
OutputTypeOrFunction[OutputDataT]
| Sequence[OutputTypeOrFunction[OutputDataT]]
) = outputs
输出类型或函数。
description 实例属性
description: str | None = description
将传递给模型的结构化输出的描述。如果未指定且只提供了一个输出,将使用输出类型或函数的文档字符串。
PromptedOutput 数据类
基类:Generic[OutputDataT]
标记类,用于通过提示词告知模型输出什么,并可选择性地自定义提示词。
示例
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-4o',
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-4o',
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)
源代码位于 pydantic_ai_slim/pydantic_ai/output.py
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 |
|
outputs 实例属性
outputs: (
OutputTypeOrFunction[OutputDataT]
| Sequence[OutputTypeOrFunction[OutputDataT]]
) = outputs
输出类型或函数。
template 实例属性
template: str | None = template
传递给模型的提示词模板。'{schema}' 占位符将被替换为输出的 JSON 模式。如果未指定,将使用模型配置中指定的默认模板。
TextOutput 数据类
基类:Generic[OutputDataT]
标记类,用于为一个接受字符串参数的输出函数使用文本输出。
示例
from pydantic_ai import Agent, TextOutput
def split_into_words(text: str) -> list[str]:
return text.split()
agent = Agent(
'openai:gpt-4o',
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.']
源代码位于 pydantic_ai_slim/pydantic_ai/output.py
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 |
|
StructuredDict
StructuredDict(
json_schema: JsonSchemaValue,
name: str | None = None,
description: str | None = None,
) -> type[JsonSchemaValue]
返回一个附加了 JSON 模式的 dict[str, Any]
子类,该模式将用于结构化输出。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
json_schema
|
JsonSchemaValue
|
一个类型为 |
必需 |
name
|
str | None
|
可选的结构化输出名称。如果未提供,并且 JSON 模式中存在 |
None
|
description
|
str | None
|
可选的结构化输出描述。如果未提供,并且 JSON 模式中存在 |
None
|
示例
from pydantic_ai import Agent, StructuredDict
schema = {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'age': {'type': 'integer'}
},
'required': ['name', 'age']
}
agent = Agent('openai:gpt-4o', output_type=StructuredDict(schema))
result = agent.run_sync('Create a person')
print(result.output)
#> {'name': 'John Doe', 'age': 30}
源代码位于 pydantic_ai_slim/pydantic_ai/output.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
DeferredToolRequests 数据类
需要批准或外部执行的工具调用。
这可以用作代理(agent)的 output_type
,并且如果模型调用了任何延迟工具,它将作为代理运行的输出。
可以使用一个具有相同工具调用 ID 的 DeferredToolResults
对象将结果传递给下一次代理运行。
更多信息请参见延迟工具文档。
源代码位于 pydantic_ai_slim/pydantic_ai/tools.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|