pydantic_ai.messages
ModelMessage
的结构可以显示为图表
graph RL
SystemPromptPart(SystemPromptPart) --- ModelRequestPart
UserPromptPart(UserPromptPart) --- ModelRequestPart
ToolReturnPart(ToolReturnPart) --- ModelRequestPart
RetryPromptPart(RetryPromptPart) --- ModelRequestPart
TextPart(TextPart) --- ModelResponsePart
ToolCallPart(ToolCallPart) --- ModelResponsePart
ModelRequestPart("ModelRequestPart<br>(Union)") --- ModelRequest
ModelRequest("ModelRequest(parts=list[...])") --- ModelMessage
ModelResponsePart("ModelResponsePart<br>(Union)") --- ModelResponse
ModelResponse("ModelResponse(parts=list[...])") --- ModelMessage("ModelMessage<br>(Union)")
SystemPromptPart dataclass
系统提示,通常由应用程序开发人员编写。
这为模型提供了上下文和关于如何响应的指导。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
timestamp class-attribute
instance-attribute
提示的时间戳。
dynamic_ref class-attribute
instance-attribute
dynamic_ref: str | None = None
生成此部分的动态系统提示函数的引用。
仅当系统提示是动态的时设置,请参阅 system_prompt
了解更多信息。
part_kind class-attribute
instance-attribute
part_kind: Literal['system-prompt'] = 'system-prompt'
部分类型标识符,这在所有部分上都可用作鉴别器。
AudioUrl dataclass
音频文件的 URL。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
ImageUrl dataclass
图像的 URL。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
66 67 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 |
|
kind class-attribute
instance-attribute
kind: Literal['image-url'] = 'image-url'
类型标识符,这在所有部分上都可用作鉴别器。
media_type property
media_type: ImageMediaType
根据 URL 返回图像的媒体类型。
format property
format: ImageFormat
图像的文件格式。
支持格式的选择基于 Bedrock Converse API。其他 API 不需要使用格式。
DocumentUrl dataclass
文档的 URL。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
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 |
|
BinaryContent dataclass
二进制内容,例如音频或图像文件。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
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 |
|
media_type instance-attribute
media_type: (
AudioMediaType
| ImageMediaType
| DocumentMediaType
| str
)
二进制数据的媒体类型。
UserPromptPart dataclass
用户提示,通常由最终用户编写。
内容来自 Agent.run
、Agent.run_sync
和 Agent.run_stream
的 user_prompt
参数。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
ToolReturnPart dataclass
工具返回消息,这编码了运行工具的结果。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
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 294 295 296 297 298 299 |
|
tool_call_id class-attribute
instance-attribute
tool_call_id: str | None = None
可选的工具调用标识符,一些模型(包括 OpenAI)使用它。
timestamp class-attribute
instance-attribute
工具返回时的时间戳。
part_kind class-attribute
instance-attribute
part_kind: Literal['tool-return'] = 'tool-return'
部分类型标识符,这在所有部分上都可用作鉴别器。
model_response_str
model_response_str() -> str
返回模型内容的字符串表示形式。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
280 281 282 283 284 285 |
|
model_response_object
返回内容的字典表示形式,适当地包装非字典类型。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
287 288 289 290 291 292 293 |
|
RetryPromptPart dataclass
发回给模型的消息,要求它重试。
发送此消息的原因有很多
- 工具参数的 Pydantic 验证失败,此处内容来自 Pydantic
ValidationError
- 工具引发了
ModelRetry
异常 - 找不到工具名称对应的工具
- 当需要结构化响应时,模型返回了纯文本
- 结构化响应的 Pydantic 验证失败,此处内容来自 Pydantic
ValidationError
- 结果验证器引发了
ModelRetry
异常
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
305 306 307 308 309 310 311 312 313 314 315 316 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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
content instance-attribute
content: list[ErrorDetails] | str
模型应该重试的原因和方式的详细信息。
如果重试是由 ValidationError
触发的,这将是错误详细信息列表。
tool_call_id class-attribute
instance-attribute
tool_call_id: str | None = None
可选的工具调用标识符,一些模型(包括 OpenAI)使用它。
timestamp class-attribute
instance-attribute
触发重试时的时间戳。
part_kind class-attribute
instance-attribute
part_kind: Literal['retry-prompt'] = 'retry-prompt'
部分类型标识符,这在所有部分上都可用作鉴别器。
model_response
model_response() -> str
返回描述请求重试原因的字符串消息。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
340 341 342 343 344 345 346 347 |
|
ModelRequestPart module-attribute
ModelRequestPart = Annotated[
Union[
SystemPromptPart,
UserPromptPart,
ToolReturnPart,
RetryPromptPart,
],
Discriminator("part_kind"),
]
PydanticAI 发送到模型的消息部分。
ModelRequest dataclass
由 PydanticAI 生成并发送到模型的请求,例如从 PydanticAI 应用发送到模型的消息。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
370 371 372 373 374 375 376 377 378 |
|
kind class-attribute
instance-attribute
kind: Literal['request'] = 'request'
消息类型标识符,这在所有部分上都可用作鉴别器。
TextPart dataclass
来自模型的纯文本响应。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
381 382 383 384 385 386 387 388 389 390 391 392 393 |
|
part_kind class-attribute
instance-attribute
part_kind: Literal['text'] = 'text'
部分类型标识符,这在所有部分上都可用作鉴别器。
has_content
has_content() -> bool
如果文本内容非空,则返回 True
。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
391 392 393 |
|
ToolCallPart dataclass
来自模型的工具调用。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
|
args instance-attribute
传递给工具的参数。
这以 JSON 字符串或 Python 字典的形式存储,具体取决于接收数据的方式。
tool_call_id class-attribute
instance-attribute
tool_call_id: str | None = None
可选的工具调用标识符,一些模型(包括 OpenAI)使用它。
part_kind class-attribute
instance-attribute
part_kind: Literal['tool-call'] = 'tool-call'
部分类型标识符,这在所有部分上都可用作鉴别器。
args_as_dict
以 Python 字典形式返回参数。
这只是为了方便需要字典作为输入的模型。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
415 416 417 418 419 420 421 422 423 424 |
|
args_as_json_str
args_as_json_str() -> str
以 JSON 字符串形式返回参数。
这只是为了方便需要 JSON 字符串作为输入的模型。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
426 427 428 429 430 431 432 433 |
|
has_content
has_content() -> bool
如果参数包含任何数据,则返回 True
。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
435 436 437 438 439 440 441 442 |
|
ModelResponsePart module-attribute
ModelResponsePart = Annotated[
Union[TextPart, ToolCallPart],
Discriminator("part_kind"),
]
模型返回的消息部分。
ModelResponse dataclass
来自模型的响应,例如从模型发送到 PydanticAI 应用的消息。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
|
timestamp class-attribute
instance-attribute
响应的时间戳。
如果模型在响应中提供时间戳(如 OpenAI 所做的那样),将使用该时间戳。
kind class-attribute
instance-attribute
kind: Literal['response'] = 'response'
消息类型标识符,这在所有部分上都可用作鉴别器。
otel_events
otel_events() -> list[Event]
返回响应的 OpenTelemetry 事件。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
|
ModelMessage module-attribute
ModelMessage = Annotated[
Union[ModelRequest, ModelResponse],
Discriminator("kind"),
]
发送到模型或由模型返回的任何消息。
ModelMessagesTypeAdapter module-attribute
ModelMessagesTypeAdapter = TypeAdapter(
list[ModelMessage],
config=ConfigDict(
defer_build=True, ser_json_bytes="base64"
),
)
用于(反)序列化消息的 Pydantic TypeAdapter
。
TextPartDelta dataclass
用于 TextPart
的部分更新(增量),以附加新的文本内容。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
|
part_delta_kind class-attribute
instance-attribute
part_delta_kind: Literal['text'] = 'text'
部分增量类型标识符,用作鉴别器。
apply
apply(part: ModelResponsePart) -> TextPart
将此文本增量应用于现有的 TextPart
。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
part
|
ModelResponsePart
|
现有的模型响应部分,必须是 |
必需 |
返回值
类型 | 描述 |
---|---|
TextPart
|
具有更新文本内容的新 |
引发
类型 | 描述 |
---|---|
ValueError
|
如果 |
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
|
ToolCallPartDelta dataclass
用于 ToolCallPart
的部分更新(增量),以修改工具名称、参数或工具调用 ID。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
|
tool_name_delta class-attribute
instance-attribute
tool_name_delta: str | None = None
要添加到现有工具名称的增量文本(如果有)。
args_delta class-attribute
instance-attribute
要添加到工具参数的增量数据。
如果这是一个字符串,它将附加到现有的 JSON 参数。如果这是一个字典,它将与现有的字典参数合并。
tool_call_id class-attribute
instance-attribute
tool_call_id: str | None = None
可选的工具调用标识符,一些模型(包括 OpenAI)使用它。
请注意,这永远不会被视为增量——它可以替换 None,但否则如果提供了不匹配的值,则会引发错误。
part_delta_kind class-attribute
instance-attribute
part_delta_kind: Literal['tool_call'] = 'tool_call'
部分增量类型标识符,用作鉴别器。
as_part
as_part() -> ToolCallPart | None
如果可能,将此增量转换为完全形成的 ToolCallPart
,否则返回 None
。
返回值
类型 | 描述 |
---|---|
ToolCallPart | None
|
如果 |
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
|
apply
apply(part: ModelResponsePart) -> ToolCallPart
apply(
part: ModelResponsePart | ToolCallPartDelta,
) -> ToolCallPart | ToolCallPartDelta
apply(
part: ModelResponsePart | ToolCallPartDelta,
) -> ToolCallPart | ToolCallPartDelta
将此增量应用于部分或增量,返回应用更改的新部分或增量。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
part
|
ModelResponsePart | ToolCallPartDelta
|
要更新的现有模型响应部分或增量。 |
必需 |
返回值
类型 | 描述 |
---|---|
ToolCallPart | ToolCallPartDelta
|
新的 |
引发
类型 | 描述 |
---|---|
ValueError
|
如果 |
UnexpectedModelBehavior
|
如果将 JSON 增量应用于字典参数,反之亦然。 |
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
|
ModelResponsePartDelta module-attribute
ModelResponsePartDelta = Annotated[
Union[TextPartDelta, ToolCallPartDelta],
Discriminator("part_delta_kind"),
]
任何模型响应部分的部分更新(增量)。
PartStartEvent dataclass
指示新部分已开始的事件。
如果收到多个具有相同索引的 PartStartEvent
,则新的事件应完全替换旧的事件。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
|
event_kind class-attribute
instance-attribute
event_kind: Literal['part_start'] = 'part_start'
事件类型标识符,用作鉴别器。
PartDeltaEvent dataclass
指示现有部分增量更新的事件。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
690 691 692 693 694 695 696 697 698 699 700 701 |
|
event_kind class-attribute
instance-attribute
event_kind: Literal['part_delta'] = 'part_delta'
事件类型标识符,用作鉴别器。
FinalResultEvent dataclass
指示对当前模型请求的响应与结果模式匹配的事件。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
704 705 706 707 708 709 710 711 712 713 |
|
ModelResponseStreamEvent module-attribute
ModelResponseStreamEvent = Annotated[
Union[PartStartEvent, PartDeltaEvent],
Discriminator("event_kind"),
]
模型响应流中的事件,可以是开始新部分或将增量应用于现有部分。
AgentStreamEvent module-attribute
AgentStreamEvent = Annotated[
Union[PartStartEvent, PartDeltaEvent, FinalResultEvent],
Discriminator("event_kind"),
]
代理流中的事件。
FunctionToolCallEvent dataclass
指示开始调用函数工具的事件。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
725 726 727 728 729 730 731 732 733 734 735 736 737 |
|
call_id class-attribute
instance-attribute
用于将有关调用的详细信息与其结果匹配的 ID。如果存在,则默认为部分的 tool_call_id。
event_kind class-attribute
instance-attribute
event_kind: Literal["function_tool_call"] = (
"function_tool_call"
)
事件类型标识符,用作鉴别器。
FunctionToolResultEvent dataclass
指示函数工具调用结果的事件。
源代码位于 pydantic_ai_slim/pydantic_ai/messages.py
740 741 742 743 744 745 746 747 748 749 |
|
event_kind class-attribute
instance-attribute
event_kind: Literal["function_tool_result"] = (
"function_tool_result"
)
事件类型标识符,用作鉴别器。