Share one `instructions` string across a conversation's requests instead of keeping a copy per request
No linked issue: follow-up to #13221 and #13307, from an audit of clai2's memory use.
Why we make these changes
Every ModelRequest records the instructions it was sent with in ModelRequest.instructions, and each request joins them into a new string. So a conversation held one copy of its instructions per request. On a real clai2 session, 386 requests each held their own copy of the same instructions, 39 MB in all.
Requests with equal instructions now share one string:
- when the agent sets a new request's instructions, it reuses the latest earlier request's string if they are equal;
- when
ModelMessagesTypeAdapterloads a history, it makes equalinstructionsstrings one object.
[!WARNING]
Compatibility impact: ModelMessagesTypeAdapter is now built with an AfterValidator, so the public-API check reports its definition as changed. Its type, JSON schema, serialization and validated values are unchanged. The one difference: in a loaded history, requests with equal instructions now hold the same string object, so code that compares them with is sees them as identical.
Why this can ship in a minor release: Strings are immutable and every value is unchanged; only object identity differs.
Migration: None needed. Compare instructions with ==, not is.
New public surface
None. ModelMessagesTypeAdapter keeps its type, TypeAdapter[list[ModelMessage]], and its JSON schema.
User-visible behavior
Values are unchanged; only how many copies are kept changes. Measured on a real clai2 resume:
instructions string objects | RSS | |
|---|---|---|
main | 386 | 382 MB |
| this PR | 1 | 351 MB |
Verification
test_requests_with_equal_instructions_share_one_stringchecks that a run's requests share one string, and that a history loaded from JSON and then continued still does. It fails onmain.- The public-API check's
ModelMessagesTypeAdapterfinding is waived against v2.69.0 in.github/api-compatibility-waivers/377773cf….json. - Not changed: the
on_change='append'window path in_instructions.pyalso joins a string, but_apply_instruction_partsreplaces it with the shared one afterwards, and I found no per-request copies there.
What changes for existing users
Long conversations use less memory. Nothing else changes.
PR decisions
2026-11-12 · pre-push review 1 of 3 at 251394b20a2141239ebc02e91385fc594c095512 · iter -
- Decision: dispatched
- Why: full branch scope
- Source: https://github.com/pydantic/pydantic-ai/tree/core-shared-instructions
- Session: 3c952642-1824-455b-9e24-2786bcd67c8d
2026-11-12 · pre-push review 1 of 3 at 251394b20a2141239ebc02e91385fc594c095512 · iter -
- Decision: remediated: early return for requests without instructions; docstring names the history argument. Not extended to the append-window path (_instructions.py:769): _apply_instruction_parts replaces that string afterwards, and no copies were reproduced there
- Why: reviewed tree 35eb02e47c; scope b8e111d1fa..251394b20a
- Source: https://github.com/pydantic/pydantic-ai/tree/core-shared-instructions
- Session: 3c952642-1824-455b-9e24-2786bcd67c8d
2026-11-12 · pre-push review 2 of 3 at 322b6a9995ad2760627892339d57f2abee5729e4 · iter -
- Decision: dispatched
- Why: follow-up scope 251394b20a..HEAD
- Source: https://github.com/pydantic/pydantic-ai/tree/core-shared-instructions
- Session: 3c952642-1824-455b-9e24-2786bcd67c8d
2026-11-12 · pre-push review 2 of 3 at 322b6a9995ad2760627892339d57f2abee5729e4 · iter -
- Decision: no blocking findings; test comment made precise after probing pydantic-core's JSON string cache (shares strings under 64 chars)
- Why: reviewed tree cfa02ddb3e; scope 251394b20a..322b6a9995; later delta is a test comment only (exempt, chains from 322b6a9995)
- Source: https://github.com/pydantic/pydantic-ai/tree/core-shared-instructions
- Session: 3c952642-1824-455b-9e24-2786bcd67c8d
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.
- PR title is fit for the release changelog.
🤖 Generated with Claude Code
Summary by cubic
Conversations no longer keep a copy of the instructions string per request, cutting memory in long sessions. On a real clai2 resume, 386 requests each held their own copy of the same instructions (39 MB); a new request now reuses an earlier request's string when equal, and ModelMessagesTypeAdapter deduplicates equal strings when loading a history. RSS on that resume dropped from 382 MB to 351 MB with no change to values or the public surface. Adds a regression test covering sharing, including after a history is loaded and resumed. An API compatibility waiver (moved to the v2.74.0 baseline) covers the added AfterValidator, whose type, schema, and values are unchanged.