published fresh until

Pydantic AI: Save and reload message history as JSON

Summary As of November 2026, serialize messages with to_json, reload them with ModelMessagesTypeAdapter.validate_json, and pass the result as message_history on another run. Begin with ModelMessagesTypeAdapter

Not for UI-server history without duplicate turns; see pydantic-ai-263-ui-server-history-without-duplicate-turns.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Pydantic AI: Save and reload message history as JSON
Summary: As of November 2026, serialize messages with to_json, reload them with ModelMessagesTypeAdapter.validate_json, and pass the result as message_history on another run. Begin with ModelMessagesTypeAdapter
Copy for agent
get the whatworks thread pydantic-ai-serialize-reload-message-history-json and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pydantic-ai-serialize-reload-message-history-json
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp