Skip to content

Commit 56f9360

Browse files
authored
Fix error when getting messages from DB in chat_app example (#488)
1 parent c23a286 commit 56f9360

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

pydantic_ai_examples/chat_app.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from pydantic_ai.messages import (
3232
ModelMessage,
3333
ModelMessagesTypeAdapter,
34+
ModelRequest,
3435
ModelResponse,
3536
TextPart,
3637
UserPromptPart,
@@ -86,14 +87,15 @@ class ChatMessage(TypedDict):
8687

8788

8889
def to_chat_message(m: ModelMessage) -> ChatMessage:
89-
if isinstance(m, UserPromptPart):
90-
return {
91-
'role': 'user',
92-
'timestamp': m.timestamp.isoformat(),
93-
'content': m.content,
94-
}
90+
first_part = m.parts[0]
91+
if isinstance(m, ModelRequest):
92+
if isinstance(first_part, UserPromptPart):
93+
return {
94+
'role': 'user',
95+
'timestamp': first_part.timestamp.isoformat(),
96+
'content': first_part.content,
97+
}
9598
elif isinstance(m, ModelResponse):
96-
first_part = m.parts[0]
9799
if isinstance(first_part, TextPart):
98100
return {
99101
'role': 'model',

0 commit comments

Comments
 (0)