Keep the stored id on the user message in `AGUIAdapter.dump_messages` when an activity message follows it

Merged
RS
10d ago
+160-13

Researched and implemented by Claude Code (claude-fable-5-1) on behalf of Richard (@rscholz98).

Why we make these changes. AGUIAdapter.dump_messages put a message's kept UI message id on the last AG-UI message dumped from it. When the run records a ModelSettingsDeltaPart behind the user prompt, which happens on the first request of a conversation that sets a thinking effort and on every effort change, that part dumps as an activity message after the user message and took the id, while the user message got a fresh UUID. @ag-ui/client never sends activity messages back, so a client that reloaded server-held history (manage_message_history='server' or a conversation_store) resent its user message under an id the server did not know, and a regenerate or edit appended the turn a second time instead of replacing it. See #12691.

New public surface. none

User-visible behavior. Dumping the stored first turn of a conversation with a thinking effort, then resending the reloaded user message:

-user 0b1a4b96-da4e-452c-821d-645f11214c43
-activity u1
+user u1
+activity 0b1a4b96-da4e-452c-821d-645f11214c43
 assistant ffd818be-bd37-48c7-a71d-7d7cd582020a
-user prompts in the history after regenerating: 2
+user prompts in the history after regenerating: 1

The kept id now goes on the last chat message dumped from a ModelRequest or ModelResponse; an activity message takes it only when the message dumps to nothing else. The same statement covers a response whose compaction activity trailed its assistant message, which reproduced the same way on main. load_messages follows the same rule: an activity message merged after a chat message no longer replaces the id the chat message kept, so the id also survives load_messages(dump_messages(...)), and a client that sends the activity messages back loads under the user message's id.

Verification.

  • test_load_dump_message_id_skips_activity_messages and test_server_message_history_regenerate_after_reload_with_settings_delta in tests/test_ag_ui.py fail on the unfixed adapter and pass with the fix. The first also asserts that load_messages(dump_messages(...)) keeps every id; that assertion fails without the load_messages change.
  • test_conversation_store_regenerate_after_reload in tests/ui_roundtrip/test_ui_roundtrip.py drives the real @ag-ui/client and AI SDK clients through a retry from a client hydrated with dump_messages(). The AG-UI case fails on the unfixed adapter (the user message is resent under a random UUID); the Vercel AI case passes either way, since its settings delta rides inside the user message. The driver's regenerate now takes messages to hydrate the client first, as submit already did.
  • tests/test_ag_ui.py, the regenerate, reload, edit and activity scenarios in tests/ui_roundtrip/test_ui_roundtrip.py, tests/test_cache_prefix_stability.py and the AG-UI round trips in tests/test_tool_search.py pass locally; pyright reports no errors on the changed files.

What changes for existing users. A dumped AG-UI history keeps the client's id on the chat message instead of on a trailing activity message, and a loaded one whose activity message follows a chat message keeps the chat message's id instead of the activity's; nothing else changes.

PR decisions

2026-11-11 · kept id goes on the last non-activity message · iter -

  • Decision: dump_messages puts the kept UI message id on the last chat message dumped from a ModelMessage and only on an activity when nothing else was dumped; load_messages is unchanged
  • Why: activities carry run-added records the AG-UI JS client never sends back, the regenerate matcher needs the id on the user message, and the same statement covers the confirmed response plus compaction case; mirrors the Vercel AI metadata placement
  • Source: https://github.com/pydantic/pydantic-ai/issues/12691
  • Session: b7e5fcf9-597c-45b1-b8b7-fc5a794a49fc

2026-11-11 · pre-push review 1 of 3 at 959c83fd711e1abfb1339e611097553e7ee8889c · iter -

2026-11-11 · pre-push review 1 of 3 at 959c83fd711e1abfb1339e611097553e7ee8889c · iter -

2026-11-11 · pre-push review 2 of 3 at 872d6028648e4a8793e76f395f58da8e862f6209 · iter -

2026-11-11 · pre-push review 2 of 3 at 872d6028648e4a8793e76f395f58da8e862f6209 · iter -

2026-11-11 · exempt delta at 5170599881cd568523f1c79c89a7b6c8fd8833f8 · iter -

2026-11-24 · adopted PR #13161 at 2026-11-24 · iter -

  • Decision: Branch-context bootstrapped from existing PR + issue(s). Decisions prior to this entry are backfilled from resolved threads (none resolved at adoption).
  • Why: PR predates the branch-context setup
  • Source: https://github.com/pydantic/pydantic-ai/pull/13161
  • Session: 15dcd34a-1429-466b-b809-a4a72d7ddd37

2026-11-24 · pre-push review budget carried over: 2 of 3 used · iter -

  • Decision: the author's session dispatched pre-push reviews 1 and 2 (at 959c83fd7 and 872d60286) and pushed 517059988 as an exempt delta; the next dispatch is call 3 of 3
  • Why: budget is per PR; those entries live only in the PR body's published decisions, not in this worktree's log
  • Source: https://github.com/pydantic/pydantic-ai/pull/13161
  • Session: 15dcd34a-1429-466b-b809-a4a72d7ddd37

2026-11-24 · load_messages: an activity never replaces a chat message's kept id · iter -

  • Decision: load_messages tracks the message a chat (non-activity) AG-UI message last set the id on, and skips set_ui_message_id for an ActivityMessage landing in that same message; dump-side placement is unchanged
  • Why: makes load follow the rule dump follows, so load(dump(x)) keeps the id; pydanty's suggested dump-side fix instead writes the same id on two AG-UI messages, which AG-UI defines as unique; #13375's load_messages change is a post-loop part reorder and merges cleanly
  • Source: https://github.com/pydantic/pydantic-ai/pull/13161#issuecomment-7139213632
  • Session: 15dcd34a-1429-466b-b809-a4a72d7ddd37
  • Supersedes: 2026-11-11 · kept id goes on the last non-activity message (its "load_messages is unchanged" clause only)

2026-11-24 · pre-push review 3 of 3 at d739a89aa3901ba782b9a5b6a0137170ff418c7a · iter -

2026-11-24 · pre-push review 3 of 3 at d739a89aa3901ba782b9a5b6a0137170ff418c7a · iter -

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it. (None.)
  • PR title is fit for the release changelog.

Summary by cubic

Fixes AGUIAdapter.dump_messages and load_messages so the stored UI message id stays on the chat message instead of a trailing activity message, preventing duplicate turns on regenerate or edit after a reload.

  • When a ModelSettingsDeltaPart (e.g., thinking effort change) is recorded behind a user prompt, it dumps as an activity message that previously took the kept id; the user message got a fresh UUID, so a client resending server-held history could not replace the stored turn.
  • The id now goes on the last chat message dumped from a ModelMessage; activity messages only take it when the message dumps to nothing else.
  • load_messages now also skips the id for an activity message merged after a chat message, so load_messages(dump_messages(...)) keeps the id on the user message.
  • Adds regression tests in tests/test_ag_ui.py and a real-client round-trip test in tests/ui_roundtrip that hydrates a client from dump_messages() before retrying.