What this thread does
Do first: Import from pydantic_ai import Agent, ModelMessagesTypeAdapter.
When you need it
The page describes storing message history on disk or in a database and sharing it across languages. It exports a message type adapter and shows continuing a new run with reloaded history.
Steps
1. Create the example agent and collect its messages:
from pydantic_ai import Agent, ModelMessagesTypeAdapter
agent = Agent('openai:gpt-6.1-sol', instructions='Be a helpful assistant.')
result1 = agent.run_sync('Tell me a joke.')
history_step_1 = result1.all_messages()
2. Use the page's direct JSON alternative:
from pydantic_core import to_json
as_json_objects = to_json(history_step_1)
same_history_as_step_1 = ModelMessagesTypeAdapter.validate_json(as_json_objects)
3. Supply the reloaded history to the next run:
result2 = agent.run_sync(
'Tell me a different joke.', message_history=same_history_as_step_1
)
Check it worked
The page identifies continuation with same_history_as_step_1 on a new run as the result. It supplies no separate confirmation command for the direct JSON example.
Limits
To preserve a conversation for continuation, the page advises storing Conversation rather than messages alone: messages omit usage, conversation ID and deferred tool requests. JSON normalizes non-native metadata values, such as tuples to lists and datetimes to strings. The page does not name a version.
Not for: UI-server history without duplicate turns; see pydantic-ai-263-ui-server-history-without-duplicate-turns.
Sources
- Message History, Pydantic AI documentation, read 2026-11-24: "Alternatively you can serialize to/from JSON directly:"
Checked
2026-11-24; read <https://pydantic.dev/docs/ai/core-concepts/message-history/index.md#storing-and-loading-messages-to-json>; no command was run.
Started from a post by @Pydantic, packaged by the editor. Original post