Stop continuing an OpenAI response with `openai_previous_response_id` once the history before it has changed

Merged
DO
1mo ago
+465-34

With openai_previous_response_id, Pydantic AI continues from the most recent OpenAI response in the history and sends only the messages after it, trusting that OpenAI holds everything before it. Until now, nothing checked that the messages before that response were still the ones it was created from. So any change to them was silently dropped: the server continued from its own copy.

That covers every way history gets rewritten:

  • a ProcessHistory that redacts or trims earlier turns;
  • Harness compaction, unless it goes through OpenAI's own /compact endpoint, which already breaks the chain;
  • a UI or application that edits stored history.

In a reproduction, a processor redacted a user prompt from an earlier turn, and the next request still sent previous_response_id='resp_1' plus only the new prompt. The model kept seeing the original text.

Fix

  • When openai_previous_response_id is set, each response records a digest of the history it was created from in a new typed field, ModelResponse.history_digest. The history is the messages as sent, after prepare_messages, so an answered request-scoped prompt also counts as a change. merge_responses keeps the turn's first digest across a background resume.
  • By default the digest is a plain SHA-256. Someone holding stored history could use it to test guesses at content redacted afterwards; the field docs say so, and OpenAIResponsesModel(..., history_digest_key=...) makes it an HMAC. The key is a constructor argument, not a setting, so it never lands in Temporal's workflow history.
  • Before continuing from a response, the hash of the messages now before it is compared with the recorded one. On a mismatch the request sends the full history with no previous_response_id, the same fallback compaction responses already use. A concrete seed is not used past a changed response either, since the seed only applies before the first response.
  • A response with no recorded history is continued from as before. That covers histories saved before this change and responses created without the setting. The docs example that enables 'auto' on the second run keeps chaining.

The comparison serializes the history with ModelMessagesTypeAdapter, so an unchanged history still matches after a JSON round trip (tested). Two things are left out of the digest:

  • Application-only metadata, which is never sent to the model and may not serialize to JSON. The digest clears it by type, on messages, on tool return parts and on user prompt TextContent. A tool's return value is hashed whole, since it's sent as is, metadata keys included.
  • Fields that are None, so a release that adds a message field doesn't make every stored chain resend once after the upgrade, or during a rolling deploy. Defaults aren't dropped in general, because that would drop the part kinds too.

If anything else makes the hash differ, the request resends the full history rather than skipping a change.

Behavior change: users who rewrite history while openai_previous_response_id is set now send the full history on the requests after a rewrite. That costs more input tokens, but it's what they asked the model to see. Users who don't rewrite history see no difference. Responses gain the history_digest field, which serializes as null unless the setting is on.

openai_conversation_id has the same trimming, but there's no clean fallback: an OpenAI conversation is a durable object that every request appends to, so resending the history into it would duplicate items. Its docstring and the OpenAI docs now say that edits to earlier messages don't reach the model there.

This is the strict-extension check discussed in https://github.com/pydantic/pydantic-ai/issues/9945#issuecomment-6225240959. A Responses WebSocket mode would need the same check.

Tests

  • A redacting ProcessHistory makes the next request resend the full, redacted history.
  • An unchanged history that went through a JSON round trip still chains.
  • A concrete seed is not used past a stale response.
  • A response answered with a request-scoped prompt isn't continued from.
  • A keyed digest is an HMAC, and a model with a different key resends instead of continuing.
  • A streamed response records the hash, and a background response resumed by the run keeps it, since the run merges the resumed response into the suspended one.
  • With the check disabled, the redaction and seed tests fail.
  • metadata that isn't JSON-serializable, on a tool return, a request or user prompt TextContent, neither raises nor breaks the chain.
  • A tool result with a metadata key that a processor redacts makes the next request resend the full history.
  • The existing chain-through-retries test now carries history_digest in its snapshot.

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