pydantic_ai.messages
The structure of ModelMessage
can be shown as a graph:
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
A system prompt, generally written by the application developer.
This gives the model context and guidance on how to respond.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
timestamp
class-attribute
instance-attribute
The timestamp of the prompt.
dynamic_ref
class-attribute
instance-attribute
dynamic_ref: str | None = None
The ref of the dynamic system prompt function that generated this part.
Only set if system prompt is dynamic, see system_prompt
for more information.
part_kind
class-attribute
instance-attribute
part_kind: Literal['system-prompt'] = 'system-prompt'
Part type identifier, this is available on all parts as a discriminator.
VideoUrl
dataclass
A URL to an video.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
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 |
|
kind
class-attribute
instance-attribute
kind: Literal['video-url'] = 'video-url'
Type identifier, this is available on all parts as a discriminator.
media_type
property
media_type: VideoMediaType
Return the media type of the video, based on the url.
format
property
format: VideoFormat
The file format of the video.
The choice of supported formats were based on the Bedrock Converse API. Other APIs don't require to use a format.
AudioUrl
dataclass
A URL to an audio file.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
kind
class-attribute
instance-attribute
kind: Literal['audio-url'] = 'audio-url'
Type identifier, this is available on all parts as a discriminator.
media_type
property
media_type: AudioMediaType
Return the media type of the audio file, based on the url.
ImageUrl
dataclass
A URL to an image.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
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 |
|
kind
class-attribute
instance-attribute
kind: Literal['image-url'] = 'image-url'
Type identifier, this is available on all parts as a discriminator.
media_type
property
media_type: ImageMediaType
Return the media type of the image, based on the url.
format
property
format: ImageFormat
The file format of the image.
The choice of supported formats were based on the Bedrock Converse API. Other APIs don't require to use a format.
DocumentUrl
dataclass
The URL of the document.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
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 |
|
kind
class-attribute
instance-attribute
kind: Literal['document-url'] = 'document-url'
Type identifier, this is available on all parts as a discriminator.
format
property
format: DocumentFormat
The file format of the document.
The choice of supported formats were based on the Bedrock Converse API. Other APIs don't require to use a format.
BinaryContent
dataclass
Binary content, e.g. an audio or image file.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
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 |
|
UserPromptPart
dataclass
A user prompt, generally written by the end user.
Content comes from the user_prompt
parameter of Agent.run
,
Agent.run_sync
, and Agent.run_stream
.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
|
timestamp
class-attribute
instance-attribute
The timestamp of the prompt.
part_kind
class-attribute
instance-attribute
part_kind: Literal['user-prompt'] = 'user-prompt'
Part type identifier, this is available on all parts as a discriminator.
ToolReturnPart
dataclass
A tool return message, this encodes the result of running a tool.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
341 342 343 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 |
|
tool_call_id
instance-attribute
tool_call_id: str
The tool call identifier, this is used by some models including OpenAI.
timestamp
class-attribute
instance-attribute
The timestamp, when the tool returned.
part_kind
class-attribute
instance-attribute
part_kind: Literal['tool-return'] = 'tool-return'
Part type identifier, this is available on all parts as a discriminator.
model_response_str
model_response_str() -> str
Return a string representation of the content for the model.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
360 361 362 363 364 365 |
|
model_response_object
Return a dictionary representation of the content, wrapping non-dict types appropriately.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
367 368 369 370 371 372 373 |
|
RetryPromptPart
dataclass
A message back to a model asking it to try again.
This can be sent for a number of reasons:
- Pydantic validation of tool arguments failed, here content is derived from a Pydantic
ValidationError
- a tool raised a
ModelRetry
exception - no tool was found for the tool name
- the model returned plain text when a structured response was expected
- Pydantic validation of a structured response failed, here content is derived from a Pydantic
ValidationError
- an output validator raised a
ModelRetry
exception
Source code in pydantic_ai_slim/pydantic_ai/messages.py
385 386 387 388 389 390 391 392 393 394 395 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 443 444 |
|
content
instance-attribute
content: list[ErrorDetails] | str
Details of why and how the model should retry.
If the retry was triggered by a ValidationError
, this will be a list of
error details.
tool_name
class-attribute
instance-attribute
tool_name: str | None = None
The name of the tool that was called, if any.
tool_call_id
class-attribute
instance-attribute
The tool call identifier, this is used by some models including OpenAI.
In case the tool call id is not provided by the model, PydanticAI will generate a random one.
timestamp
class-attribute
instance-attribute
The timestamp, when the retry was triggered.
part_kind
class-attribute
instance-attribute
part_kind: Literal['retry-prompt'] = 'retry-prompt'
Part type identifier, this is available on all parts as a discriminator.
model_response
model_response() -> str
Return a string message describing why the retry is requested.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
423 424 425 426 427 428 429 430 |
|
ModelRequestPart
module-attribute
ModelRequestPart = Annotated[
Union[
SystemPromptPart,
UserPromptPart,
ToolReturnPart,
RetryPromptPart,
],
Discriminator("part_kind"),
]
A message part sent by PydanticAI to a model.
ModelRequest
dataclass
A request generated by PydanticAI and sent to a model, e.g. a message from the PydanticAI app to the model.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
453 454 455 456 457 458 459 460 461 462 463 464 |
|
TextPart
dataclass
A plain text response from a model.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
467 468 469 470 471 472 473 474 475 476 477 478 479 |
|
part_kind
class-attribute
instance-attribute
part_kind: Literal['text'] = 'text'
Part type identifier, this is available on all parts as a discriminator.
has_content
has_content() -> bool
Return True
if the text content is non-empty.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
477 478 479 |
|
ToolCallPart
dataclass
A tool call from a model.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
|
args
instance-attribute
The arguments to pass to the tool.
This is stored either as a JSON string or a Python dictionary depending on how data was received.
tool_call_id
class-attribute
instance-attribute
The tool call identifier, this is used by some models including OpenAI.
In case the tool call id is not provided by the model, PydanticAI will generate a random one.
part_kind
class-attribute
instance-attribute
part_kind: Literal['tool-call'] = 'tool-call'
Part type identifier, this is available on all parts as a discriminator.
args_as_dict
Return the arguments as a Python dictionary.
This is just for convenience with models that require dicts as input.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
504 505 506 507 508 509 510 511 512 513 |
|
args_as_json_str
args_as_json_str() -> str
Return the arguments as a JSON string.
This is just for convenience with models that require JSON strings as input.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
515 516 517 518 519 520 521 522 |
|
has_content
has_content() -> bool
Return True
if the arguments contain any data.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
524 525 526 527 528 529 530 531 |
|
ModelResponsePart
module-attribute
ModelResponsePart = Annotated[
Union[TextPart, ToolCallPart],
Discriminator("part_kind"),
]
A message part returned by a model.
ModelResponse
dataclass
A response from a model, e.g. a message from the model to the PydanticAI app.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
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 |
|
model_name
class-attribute
instance-attribute
model_name: str | None = None
The name of the model that generated the response.
timestamp
class-attribute
instance-attribute
The timestamp of the response.
If the model provides a timestamp in the response (as OpenAI does) that will be used.
kind
class-attribute
instance-attribute
kind: Literal['response'] = 'response'
Message type identifier, this is available on all parts as a discriminator.
otel_events
otel_events() -> list[Event]
Return OpenTelemetry events for the response.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
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 |
|
ModelMessage
module-attribute
ModelMessage = Annotated[
Union[ModelRequest, ModelResponse],
Discriminator("kind"),
]
Any message sent to or returned by a model.
ModelMessagesTypeAdapter
module-attribute
ModelMessagesTypeAdapter = TypeAdapter(
list[ModelMessage],
config=ConfigDict(
defer_build=True,
ser_json_bytes="base64",
val_json_bytes="base64",
),
)
Pydantic TypeAdapter
for (de)serializing messages.
TextPartDelta
dataclass
A partial update (delta) for a TextPart
to append new text content.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
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 |
|
content_delta
instance-attribute
content_delta: str
The incremental text content to add to the existing TextPart
content.
part_delta_kind
class-attribute
instance-attribute
part_delta_kind: Literal['text'] = 'text'
Part delta type identifier, used as a discriminator.
apply
apply(part: ModelResponsePart) -> TextPart
Apply this text delta to an existing TextPart
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
part
|
ModelResponsePart
|
The existing model response part, which must be a |
required |
Returns:
Type | Description |
---|---|
TextPart
|
A new |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in pydantic_ai_slim/pydantic_ai/messages.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
|
ToolCallPartDelta
dataclass
A partial update (delta) for a ToolCallPart
to modify tool name, arguments, or tool call ID.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
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 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
|
tool_name_delta
class-attribute
instance-attribute
tool_name_delta: str | None = None
Incremental text to add to the existing tool name, if any.
args_delta
class-attribute
instance-attribute
Incremental data to add to the tool arguments.
If this is a string, it will be appended to existing JSON arguments. If this is a dict, it will be merged with existing dict arguments.
tool_call_id
class-attribute
instance-attribute
tool_call_id: str | None = None
Optional tool call identifier, this is used by some models including OpenAI.
Note this is never treated as a delta — it can replace None, but otherwise if a non-matching value is provided an error will be raised.
part_delta_kind
class-attribute
instance-attribute
part_delta_kind: Literal['tool_call'] = 'tool_call'
Part delta type identifier, used as a discriminator.
as_part
as_part() -> ToolCallPart | None
Convert this delta to a fully formed ToolCallPart
if possible, otherwise return None
.
Returns:
Type | Description |
---|---|
ToolCallPart | None
|
A |
Source code in pydantic_ai_slim/pydantic_ai/messages.py
647 648 649 650 651 652 653 654 655 656 |
|
apply
apply(part: ModelResponsePart) -> ToolCallPart
apply(
part: ModelResponsePart | ToolCallPartDelta,
) -> ToolCallPart | ToolCallPartDelta
apply(
part: ModelResponsePart | ToolCallPartDelta,
) -> ToolCallPart | ToolCallPartDelta
Apply this delta to a part or delta, returning a new part or delta with the changes applied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
part
|
ModelResponsePart | ToolCallPartDelta
|
The existing model response part or delta to update. |
required |
Returns:
Type | Description |
---|---|
ToolCallPart | ToolCallPartDelta
|
Either a new |
Raises:
Type | Description |
---|---|
ValueError
|
If |
UnexpectedModelBehavior
|
If applying JSON deltas to dict arguments or vice versa. |
Source code in pydantic_ai_slim/pydantic_ai/messages.py
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
|
ModelResponsePartDelta
module-attribute
ModelResponsePartDelta = Annotated[
Union[TextPartDelta, ToolCallPartDelta],
Discriminator("part_delta_kind"),
]
A partial update (delta) for any model response part.
PartStartEvent
dataclass
An event indicating that a new part has started.
If multiple PartStartEvent
s are received with the same index,
the new one should fully replace the old one.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 |
|
event_kind
class-attribute
instance-attribute
event_kind: Literal['part_start'] = 'part_start'
Event type identifier, used as a discriminator.
PartDeltaEvent
dataclass
An event indicating a delta update for an existing part.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
761 762 763 764 765 766 767 768 769 770 771 772 |
|
event_kind
class-attribute
instance-attribute
event_kind: Literal['part_delta'] = 'part_delta'
Event type identifier, used as a discriminator.
FinalResultEvent
dataclass
An event indicating the response to the current model request matches the output schema and will produce a result.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
775 776 777 778 779 780 781 782 783 784 |
|
tool_name
instance-attribute
tool_name: str | None
The name of the output tool that was called. None
if the result is from text content and not from a tool.
tool_call_id
instance-attribute
tool_call_id: str | None
The tool call ID, if any, that this result is associated with.
event_kind
class-attribute
instance-attribute
event_kind: Literal['final_result'] = 'final_result'
Event type identifier, used as a discriminator.
ModelResponseStreamEvent
module-attribute
ModelResponseStreamEvent = Annotated[
Union[PartStartEvent, PartDeltaEvent],
Discriminator("event_kind"),
]
An event in the model response stream, either starting a new part or applying a delta to an existing one.
AgentStreamEvent
module-attribute
AgentStreamEvent = Annotated[
Union[PartStartEvent, PartDeltaEvent, FinalResultEvent],
Discriminator("event_kind"),
]
An event in the agent stream.
FunctionToolCallEvent
dataclass
An event indicating the start to a call to a function tool.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
796 797 798 799 800 801 802 803 804 805 806 807 808 |
|
call_id
class-attribute
instance-attribute
An ID used for matching details about the call to its result. If present, defaults to the part's tool_call_id.
event_kind
class-attribute
instance-attribute
event_kind: Literal["function_tool_call"] = (
"function_tool_call"
)
Event type identifier, used as a discriminator.
FunctionToolResultEvent
dataclass
An event indicating the result of a function tool call.
Source code in pydantic_ai_slim/pydantic_ai/messages.py
811 812 813 814 815 816 817 818 819 820 |
|
result
instance-attribute
result: ToolReturnPart | RetryPromptPart
The result of the call to the function tool.
tool_call_id
instance-attribute
tool_call_id: str
An ID used to match the result to its original call.
event_kind
class-attribute
instance-attribute
event_kind: Literal["function_tool_result"] = (
"function_tool_result"
)
Event type identifier, used as a discriminator.