Send stored response, item, conversation and container IDs back only to the endpoint that created them, via `Model.accepts_server_state`

Merged
DO
1mo ago
+526-22

One provider_name covers many endpoints: OpenAIProvider takes any base_url, and AnthropicProvider any client. Server-side state (a stored response, its item IDs, a conversation, a code execution container) can only be looked up at the endpoint that created it, but it was sent back to any provider with the same name. This is the stateful counterpart to #11504: stateless signed or encrypted data is judged by family, and server-side IDs are now judged by endpoint.

[!WARNING]
Compatibility impact: A history that continues at another endpoint under the same provider name (an OpenAIProvider or AnthropicProvider with a different base URL) no longer sends the other endpoint's server-side IDs: no previous_response_id, Responses item IDs, response-scoped tool call IDs, openai_conversation_id='auto' conversation, prompt cache comparison response, Anthropic code execution container or cache diagnostics baseline. Its text, function calls and encrypted reasoning are sent as before. Histories that stay at one endpoint, and responses without a provider_url, are unchanged.

Why this can ship in a minor release: It fixes requests that referred to state the receiving endpoint couldn't look up; no public API changes shape.

Migration: None needed. When two URLs reach the same stored state, like a proxy alias, override Provider.shares_server_state_with() on a provider subclass.

New public surface

  • Provider.shares_server_state_with(provider_url) -> bool: compares the stored provider_url with the provider's base_url, ignoring the case of the scheme and host, a default port and a trailing /. Overridable for aliases; nothing is derived from credentials.
  • Model.accepts_server_state(response) -> bool, next to Model.accepts_stateless_artifacts, passed through by WrapperModel, FallbackModel (first model) and TemporalModel (active model). A response without a provider_url is judged by provider_name alone.

No new ModelResponse field: the predicate reads the provider_url every response already stores, so existing histories that crossed endpoints are fixed too, and snapshots don't change.

Where it's used

  • OpenAIResponsesModel: previous_response_id, item IDs (should_send_item_id), response-scoped tool call IDs and the carried response_id in _map_messages, openai_conversation_id='auto', and the prompt cache comparison response ID. A part from this provider in a response from another provider has no endpoint of its own, so it's still judged by its name.
  • AnthropicModel: the code execution container reused from history and the diagnostics.previous_message_id baseline.
  • Not covered: UploadedFiles, including code interpreter and container upload file IDs. The user picks the endpoint for files they upload; docs/input.md and docs/message-history.md say so.

Verification

Each site has a test with a response from https://gateway-a.example replayed at another endpoint under the same name; all of them failed before the fix, and the same-endpoint controls pass on both sides:

  • tests/models/test_openai_responses.py: test_openai_responses_server_state_scoped_by_endpoint (a real request() at endpoint A, then previous_response_id, web search, message and function call item IDs at B), test_response_scoped_tool_call_id_from_other_endpoint, test_openai_conversation_id_auto_skips_other_endpoint, test_openai_prompt_cache_comparison_skips_other_endpoint
  • tests/models/test_anthropic.py::test_anthropic_container_from_history_scoped_by_endpoint, tests/models/anthropic/test_cache_diagnostics.py::test_cache_diagnostics_skip_other_endpoint
  • tests/test_server_state.py: the predicate matrix (URL spelling, host, path, port, query, legacy, other provider), an alias override, and wrapper and fallback delegation

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