Let a named `InstructionPart` passed to a run's `instructions` opt into `on_change='append'`, keyed `run:<name>`
Follow-up to #8188. Instructions passed to agent.run(..., instructions=...) (and run_sync, run_stream, iter) had no source, so they could never use on_change='append'. Yet per-request context that an application passes to each run of a conversation, such as the page the user is on, is exactly what changes between runs. Opting in only produced a warning, and the warning blamed a missing capability or toolset id, which doesn't apply to run-level instructions.
[!WARNING]
Compatibility impact: InstructionSource (the type of InstructionId.source) gains a fourth member, RunInstructionSource. Code that matches on every member, and that a type checker verifies is exhaustive (for example with assert_never), will report the new case as unhandled. Separately, every named InstructionPart passed to a run method's instructions now has id=InstructionId(RunInstructionSource(), name=...) where it had id=None. That includes the default on_change='rewrite', so the rule matches every other source: a named part from a source with an identity is addressable. Only the instruction_parts metadata that capabilities, models and instrumentation see changes. The provider request is the same, because rewrite-mode parts are sent as the same text, and unnamed runtime instructions are untouched.
Why this can ship in a minor release: nothing is removed or renamed. Widening a union that the framework produces is an additive change under the version policy, and a part that had no id gaining one doesn't change any existing id.
Migration: add case RunInstructionSource(): (or an isinstance branch) wherever you match InstructionId.source exhaustively. Code that keys configuration on instruction ids can now address named run-level parts as 'run:<name>'.
Now a named InstructionPart passed to a run is keyed run:<name> under a new RunInstructionSource, so its changes across runs of one conversation are appended as deltas. Unnamed runtime instructions (strings, callables, unnamed parts) stay unidentified and keep rewriting, because nothing says a later run's unnamed text is the same block. An unnamed append part gets the accurate warning: "without a name ... Pass name=". The misleading "has no id" text now only shows for capabilities and toolsets without an id, where it's accurate.
RunInstructionSourceis exported frompydantic_aiandpydantic_ai.messagesnext to the other instruction sources, and'run:<name>'deserializes. History from an older version never contains it. An older version reading newer history treats the unknown key as unaddressable and starts a fresh baseline, as it does for any unknown namespace.- Tests: two runs of one conversation,
runandrun_stream, with history round-tripped through JSON. The second run changes the value and produces arun:pagedelta, and the instruction prefix the model is sent stays byte-identical. Unnamed append still warns and rewrites.test_a_run_level_part_keeps_its_unresolved_namepinned the old behaviour; it's updated to the new keying. - Docs: the id table in
docs/agent.md, the list of unaddressable parts, an example in "Appending instruction changes", and the agent skill.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.
🤖 Generated with Claude Code