Skip to content

Commit 219e788

Browse files
authored
Reformat messages so messages become simple list[ModelRequest | ModelResponse] (#259)
1 parent 8f74221 commit 219e788

34 files changed

+1462
-1161
lines changed

docs/agents.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,15 @@ except UnexpectedModelBehavior as e:
348348
"""
349349
messages:
350350
[
351-
UserPrompt(
352-
content='Please get me the volume of a box with size 6.',
353-
timestamp=datetime.datetime(...),
354-
role='user',
355-
message_kind='user-prompt',
351+
ModelRequest(
352+
parts=[
353+
UserPromptPart(
354+
content='Please get me the volume of a box with size 6.',
355+
timestamp=datetime.datetime(...),
356+
part_kind='user-prompt',
357+
)
358+
],
359+
kind='request',
356360
),
357361
ModelResponse(
358362
parts=[
@@ -364,16 +368,19 @@ except UnexpectedModelBehavior as e:
364368
)
365369
],
366370
timestamp=datetime.datetime(...),
367-
role='model',
368-
message_kind='model-response',
371+
kind='response',
369372
),
370-
RetryPrompt(
371-
content='Please try again.',
372-
tool_name='calc_volume',
373-
tool_call_id=None,
374-
timestamp=datetime.datetime(...),
375-
role='user',
376-
message_kind='retry-prompt',
373+
ModelRequest(
374+
parts=[
375+
RetryPromptPart(
376+
content='Please try again.',
377+
tool_name='calc_volume',
378+
tool_call_id=None,
379+
timestamp=datetime.datetime(...),
380+
part_kind='retry-prompt',
381+
)
382+
],
383+
kind='request',
377384
),
378385
ModelResponse(
379386
parts=[
@@ -385,8 +392,7 @@ except UnexpectedModelBehavior as e:
385392
)
386393
],
387394
timestamp=datetime.datetime(...),
388-
role='model',
389-
message_kind='model-response',
395+
kind='response',
390396
),
391397
]
392398
"""

docs/api/messages.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
# `pydantic_ai.messages`
22

3+
The structure of [`ModelMessage`][pydantic_ai.messages.ModelMessage] can be shown as a graph:
4+
5+
```mermaid
6+
graph RL
7+
SystemPromptPart(SystemPromptPart) --- ModelRequestPart
8+
UserPromptPart(UserPromptPart) --- ModelRequestPart
9+
ToolReturnPart(ToolReturnPart) --- ModelRequestPart
10+
RetryPromptPart(RetryPromptPart) --- ModelRequestPart
11+
TextPart(TextPart) --- ModelResponsePart
12+
ToolCallPart(ToolCallPart) --- ModelResponsePart
13+
ModelRequestPart("ModelRequestPart<br>(Union)") --- ModelRequest
14+
ModelRequest("ModelRequest(parts=list[...])") --- ModelMessage
15+
ModelResponsePart("ModelResponsePart<br>(Union)") --- ModelResponse
16+
ModelResponse("ModelResponse(parts=list[...])") --- ModelMessage("ModelMessage<br>(Union)")
17+
```
18+
319
::: pydantic_ai.messages
4-
options:
5-
members:
6-
- Message
7-
- SystemPrompt
8-
- UserPrompt
9-
- ToolReturn
10-
- RetryPrompt
11-
- ModelResponse
12-
- ToolCall
13-
- ArgsJson
14-
- ArgsObject
15-
- MessagesTypeAdapter

docs/api/models/function.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,27 @@ Here's a minimal example:
1111

1212
```py {title="function_model_usage.py" call_name="test_my_agent" lint="not-imports"}
1313
from pydantic_ai import Agent
14-
from pydantic_ai.messages import Message, ModelResponse
14+
from pydantic_ai.messages import ModelMessage, ModelResponse
1515
from pydantic_ai.models.function import FunctionModel, AgentInfo
1616

1717
my_agent = Agent('openai:gpt-4o')
1818

1919

20-
async def model_function(messages: list[Message], info: AgentInfo) -> ModelResponse:
20+
async def model_function(
21+
messages: list[ModelMessage], info: AgentInfo
22+
) -> ModelResponse:
2123
print(messages)
2224
"""
2325
[
24-
UserPrompt(
25-
content='Testing my agent...',
26-
timestamp=datetime.datetime(...),
27-
role='user',
28-
message_kind='user-prompt',
26+
ModelRequest(
27+
parts=[
28+
UserPromptPart(
29+
content='Testing my agent...',
30+
timestamp=datetime.datetime(...),
31+
part_kind='user-prompt',
32+
)
33+
],
34+
kind='request',
2935
)
3036
]
3137
"""

0 commit comments

Comments
 (0)