Keep the stored id on the user message in `AGUIAdapter.dump_messages` when an activity message follows it
Researched and implemented by Claude Code (claude-fable-5-1) on behalf of Richard (@rscholz98).
- Closes #12691
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_messagesandtest_server_message_history_regenerate_after_reload_with_settings_deltaintests/test_ag_ui.pyfail on the unfixed adapter and pass with the fix. The first also asserts thatload_messages(dump_messages(...))keeps every id; that assertion fails without theload_messageschange.test_conversation_store_regenerate_after_reloadintests/ui_roundtrip/test_ui_roundtrip.pydrives the real@ag-ui/clientand AI SDK clients through a retry from a client hydrated withdump_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'sregeneratenow takesmessagesto hydrate the client first, assubmitalready did.tests/test_ag_ui.py, the regenerate, reload, edit and activity scenarios intests/ui_roundtrip/test_ui_roundtrip.py,tests/test_cache_prefix_stability.pyand the AG-UI round trips intests/test_tool_search.pypass 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 -
- Decision: reserved; outcome appended below
- Why: tree afe9f3e3ed7501a76fe05e44fc82926f2de52636; scope merge-base 451c51eb77c449b03387ed2d659cad61bc332ebc to candidate 959c83fd711e1abfb1339e611097553e7ee8889c, full branch; first review
- Source: https://github.com/rscholz98/pydantic-ai/tree/agui-dump-messages-id-12691
- Session: b7e5fcf9-597c-45b1-b8b7-fc5a794a49fc
2026-11-11 · pre-push review 1 of 3 at 959c83fd711e1abfb1339e611097553e7ee8889c · iter -
- Decision: remediated: docs wording made consistent with the activities-only fallback, docstring states the fallback, real-client regenerate-after-reload test added to tests/ui_roundtrip (driver regenerate now takes messages to hydrate)
- Why: reviewed tree afe9f3e3ed7501a76fe05e44fc82926f2de52636; scope 451c51eb7..959c83fd7 full branch; three findings, all valid
- Source: https://github.com/rscholz98/pydantic-ai/tree/agui-dump-messages-id-12691
- Session: b7e5fcf9-597c-45b1-b8b7-fc5a794a49fc
2026-11-11 · pre-push review 2 of 3 at 872d6028648e4a8793e76f395f58da8e862f6209 · iter -
- Decision: reserved; outcome appended below
- Why: tree 9d2de42ab0f21c74c16f0d982bf54997788c364b; follow-up scope 959c83fd7..872d6028648e4a8793e76f395f58da8e862f6209 plus prior findings; full branch diff as context
- Source: https://github.com/rscholz98/pydantic-ai/tree/agui-dump-messages-id-12691
- Session: b7e5fcf9-597c-45b1-b8b7-fc5a794a49fc
2026-11-11 · pre-push review 2 of 3 at 872d6028648e4a8793e76f395f58da8e862f6209 · iter -
- Decision: remediated: two low priority wording findings (round-trip module docstring lists the new test; dump_messages docstring says beside one instead of after it) applied in 517059988
- Why: reviewed tree 9d2de42ab0f21c74c16f0d982bf54997788c364b; prior three findings verified fixed; delta 959c83fd7..872d60286 plus full branch context
- Source: https://github.com/rscholz98/pydantic-ai/tree/agui-dump-messages-id-12691
- Session: b7e5fcf9-597c-45b1-b8b7-fc5a794a49fc
2026-11-11 · exempt delta at 5170599881cd568523f1c79c89a7b6c8fd8833f8 · iter -
- Decision: pushed without a third review: docstring and test module docstring lines only, chained from reviewed 872d6028648e4a8793e76f395f58da8e862f6209
- Why: git diff 872d60286..HEAD touches only a docstring in _adapter.py and the module docstring of tests/ui_roundtrip/test_ui_roundtrip.py; no docs/** page; table row comments or docstrings only
- Source: https://github.com/rscholz98/pydantic-ai/tree/agui-dump-messages-id-12691
- Session: b7e5fcf9-597c-45b1-b8b7-fc5a794a49fc
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 -
- Decision: reserved; outcome appended below
- Why: tree 3192a15a5c7447a60fca2d1848749a473a4f35f6; follow-up scope 45ec5fdbf..d739a89aa (45ec5fdbf chains from reviewed 872d60286 via an exempt docstring delta and a clean main merge) plus pydanty and douwebot findings to verify; full branch diff from 432437826 as context; policy base fa40310f8
- Source: https://github.com/pydantic/pydantic-ai/pull/13161
- Session: 15dcd34a-1429-466b-b809-a4a72d7ddd37
2026-11-24 · pre-push review 3 of 3 at d739a89aa3901ba782b9a5b6a0137170ff418c7a · iter -
- Decision: no findings: current at d739a89aa3901ba782b9a5b6a0137170ff418c7a; all four prior findings verified addressed
- Why: reviewed tree 3192a15a5c7447a60fca2d1848749a473a4f35f6; scope 45ec5fdbf..d739a89aa plus full branch context; budget now exhausted
- Source: https://github.com/pydantic/pydantic-ai/pull/13161
- Session: 15dcd34a-1429-466b-b809-a4a72d7ddd37
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_messagesnow also skips the id for an activity message merged after a chat message, soload_messages(dump_messages(...))keeps the id on the user message.- Adds regression tests in
tests/test_ag_ui.pyand a real-client round-trip test intests/ui_roundtripthat hydrates a client fromdump_messages()before retrying.