Leave out a request-scoped `SystemPromptPart` that no user message clears, as after a `no_output` retry on Anthropic

Merged
DO
1mo ago
+166-2

On a model that clears request-scoped SystemPromptParts itself (Anthropic's clear_at: "next_user_message", #9711), Pydantic AI sends every request-scoped prompt unchanged with every request and relies on the API to stop showing it once a user message follows. A no_output retry adds only system-voice feedback (#8094), so no user message follows the previous step's prompt before the retry request is sent, and the model saw it next to the retried step's own.

prepare_messages now leaves a superseded request-scoped prompt out when the request right after it reaches the model in the system voice alone (system prompts, instruction records, tool reveals, harness-voice retry feedback). Whether it goes is decided by that following request, which never changes, so it's left out of every later request too and the history stays append-only: dropping it only from the retry request would put it back, and move the prefix, in the next one. A prompt followed by a user message, such as a tool result or a fenced model_retry/validation_error retry, is still sent for the API to clear, so nothing changes outside this case.

I considered sending no_output feedback in a form that counts as a user message on these models instead, but that moves the framework's own fixed text out of the system voice everywhere the profile allows it, while this keeps the fix to the one prompt the API can't clear.

test_native_support_leaves_out_a_reminder_no_user_message_clears (tests/models/test_request_scoped_system_prompts.py) pins the sequence (no-output retry, then a tool call): the retry request carries only its own reminder, and the next one keeps the retry's reminder and leaves out the first. test_anthropic_no_output_retry_leaves_out_the_reminder_nothing_clears pins the same sequence on the Anthropic wire body with a mocked client: the first reminder is absent from the retry request and the one after it, and the retry's reminder, followed by a tool result, still goes out with clear_at. It's the same case the append-only property test in #10936 tracks as retry:no-output-retry+request-scoped, whose KNOWN entry can be removed once this lands.

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.