-
-
Notifications
You must be signed in to change notification settings - Fork 20.1k
feat(responses): stateless multi-turn via encrypted_content state carrier (RFC #26934) #35740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
will-deines
wants to merge
6
commits into
vllm-project:main
from
will-deines:feat/stateless-responses-encrypted-content
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d9962cd
feat(responses): stateless multi-turn via encrypted_content (RFC #26934)
garrio-1 2ca70cf
fix(responses): cancel_responses stateless mode — 404 on unknown id, …
garrio-1 1d1be94
fix+test: address code review feedback (cancel error codes, missing c…
garrio-1 cac95d7
fix: incomplete carrier history, unconditional carrier guard, short k…
garrio-1 bdc8b36
fix: remove unused TYPE_CHECKING import to pass pre-commit ruff check
garrio-1 da69322
Merge branch 'main' into feat/stateless-responses-encrypted-content
will-deines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
417 changes: 417 additions & 0 deletions
417
tests/entrypoints/openai/responses/test_serving_stateless.py
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| """Unit tests for the stateless Responses API state carrier (state.py).""" | ||
|
|
||
| import importlib | ||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Helpers | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def _reset_signing_key(): | ||
| """Force state.py to re-derive the signing key on next call.""" | ||
| import vllm.entrypoints.openai.responses.state as state_mod | ||
|
|
||
| state_mod._SIGNING_KEY = None | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Fixtures | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def isolated_key(monkeypatch): | ||
| """Each test gets a fresh, deterministic signing key.""" | ||
| monkeypatch.setenv( | ||
| "VLLM_RESPONSES_STATE_SIGNING_KEY", "ab" * 32 # 64 hex chars = 32 bytes | ||
| ) | ||
| _reset_signing_key() | ||
| yield | ||
| _reset_signing_key() | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Import after env is patched (in case module was already imported) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def state(): | ||
| import vllm.entrypoints.openai.responses.state as m | ||
|
|
||
| return m | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Round-trip tests | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_roundtrip_plain_dicts(state): | ||
| messages = [ | ||
| {"role": "user", "content": "Hello"}, | ||
| {"role": "assistant", "content": "Hi there!"}, | ||
| ] | ||
| blob = state.serialize_state(messages) | ||
| recovered = state.deserialize_state(blob) | ||
| assert recovered == messages | ||
|
|
||
|
|
||
| def test_roundtrip_empty_list(state): | ||
| blob = state.serialize_state([]) | ||
| recovered = state.deserialize_state(blob) | ||
| assert recovered == [] | ||
|
|
||
|
|
||
| def test_roundtrip_nested_structure(state): | ||
| messages = [ | ||
| { | ||
| "role": "user", | ||
| "content": [{"type": "text", "text": "What is 2+2?"}], | ||
| }, | ||
| {"role": "assistant", "content": "4", "extra": {"key": [1, 2, 3]}}, | ||
| ] | ||
| blob = state.serialize_state(messages) | ||
| recovered = state.deserialize_state(blob) | ||
| assert recovered == messages | ||
|
|
||
|
|
||
| def test_roundtrip_pydantic_model(state): | ||
| """Objects with model_dump() should serialize transparently.""" | ||
|
|
||
| class FakeModel: | ||
| def model_dump(self): | ||
| return {"author": {"role": "user"}, "content": "hi"} | ||
|
|
||
| messages = [FakeModel()] | ||
| blob = state.serialize_state(messages) | ||
| recovered = state.deserialize_state(blob) | ||
| # After JSON round-trip, FakeModel becomes a plain dict | ||
| assert recovered == [{"author": {"role": "user"}, "content": "hi"}] | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # is_state_carrier | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_is_state_carrier_true(state): | ||
| blob = state.serialize_state([{"role": "user", "content": "hi"}]) | ||
|
|
||
| class FakeItem: | ||
| encrypted_content = blob | ||
|
|
||
| assert state.is_state_carrier(FakeItem()) | ||
|
|
||
|
|
||
| def test_is_state_carrier_false_external(state): | ||
| """Real encrypted_content from external models should not be detected.""" | ||
|
|
||
| class FakeItem: | ||
| encrypted_content = "some-opaque-blob-from-openai" | ||
|
|
||
| assert not state.is_state_carrier(FakeItem()) | ||
|
|
||
|
|
||
| def test_is_state_carrier_false_no_field(state): | ||
| class FakeItem: | ||
| pass | ||
|
|
||
| assert not state.is_state_carrier(FakeItem()) | ||
|
|
||
|
|
||
| def test_is_state_carrier_false_none(state): | ||
| class FakeItem: | ||
| encrypted_content = None | ||
|
|
||
| assert not state.is_state_carrier(FakeItem()) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # deserialize_state — non-carrier inputs | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_deserialize_returns_none_for_non_carrier(state): | ||
| assert state.deserialize_state("some-random-opaque-string") is None | ||
|
|
||
|
|
||
| def test_deserialize_returns_none_for_empty_string(state): | ||
| assert state.deserialize_state("") is None | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # HMAC tamper detection | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_tampered_payload_raises(state): | ||
| blob = state.serialize_state([{"role": "user", "content": "original"}]) | ||
| # Corrupt the payload part (index 2 when split on ':') | ||
| parts = blob.split(":", 3) | ||
| assert len(parts) == 4 | ||
| parts[2] = parts[2][:-4] + "XXXX" # corrupt the base64 payload | ||
| tampered = ":".join(parts) | ||
| with pytest.raises(ValueError, match="HMAC verification failed"): | ||
| state.deserialize_state(tampered) | ||
|
|
||
|
|
||
| def test_tampered_sig_raises(state): | ||
| blob = state.serialize_state([{"role": "user", "content": "hello"}]) | ||
| parts = blob.split(":", 3) | ||
| parts[3] = "0" * 64 # replace HMAC with zeros | ||
| tampered = ":".join(parts) | ||
| with pytest.raises(ValueError, match="HMAC verification failed"): | ||
| state.deserialize_state(tampered) | ||
|
|
||
|
|
||
| def test_malformed_carrier_raises(state): | ||
| malformed = "vllm:1:onlythreeparts" | ||
| with pytest.raises(ValueError, match="Malformed vLLM state carrier"): | ||
| state.deserialize_state(malformed) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Cross-key incompatibility | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_different_keys_are_incompatible(monkeypatch): | ||
| """A blob signed with key A must not validate with key B.""" | ||
| import vllm.entrypoints.openai.responses.state as state_mod | ||
|
|
||
| monkeypatch.setenv("VLLM_RESPONSES_STATE_SIGNING_KEY", "aa" * 32) | ||
| state_mod._SIGNING_KEY = None | ||
| blob = state_mod.serialize_state([{"role": "user", "content": "secret"}]) | ||
|
|
||
| # Switch to a different key | ||
| monkeypatch.setenv("VLLM_RESPONSES_STATE_SIGNING_KEY", "bb" * 32) | ||
| state_mod._SIGNING_KEY = None | ||
|
|
||
| with pytest.raises(ValueError, match="HMAC verification failed"): | ||
| state_mod.deserialize_state(blob) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Random-key warning (no env var) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_no_env_var_generates_random_key(monkeypatch): | ||
| """Without the env var, a random 32-byte key is generated. | ||
|
|
||
| The warning is emitted via vLLM's logger (visible in test output) but is | ||
| not capturable via capsys/caplog since vLLM writes to sys.__stdout__ directly. | ||
| """ | ||
| import vllm.entrypoints.openai.responses.state as state_mod | ||
|
|
||
| monkeypatch.delenv("VLLM_RESPONSES_STATE_SIGNING_KEY", raising=False) | ||
| state_mod._SIGNING_KEY = None | ||
|
|
||
| key = state_mod._get_signing_key() | ||
|
|
||
| assert key is not None | ||
| assert len(key) == 32 | ||
| # A second call returns the same cached key (warning only fires once) | ||
| key2 = state_mod._get_signing_key() | ||
| assert key == key2 | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Invalid hex key | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def test_invalid_hex_key_raises(monkeypatch): | ||
| import vllm.entrypoints.openai.responses.state as state_mod | ||
|
|
||
| monkeypatch.setenv("VLLM_RESPONSES_STATE_SIGNING_KEY", "not-valid-hex!!") | ||
| state_mod._SIGNING_KEY = None | ||
|
|
||
| with pytest.raises(ValueError, match="valid hex string"): | ||
| state_mod._get_signing_key() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this field one of the standard fields in the Open Response API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No,
previous_responseis a vLLM extension — the standard OpenAI API only hasprevious_response_id(a string ID that requires server-side storage).This extension is needed because
previous_response_iddepends on vLLM's in-process store (theresponse_store/msg_store/event_storedicts marked# HACK/# FIXME), which leaks memory (#34738), loses state on restart, and is incompatible with production multi-node deployments (RFC #26934, raised by @qandrew at Meta).The approach follows @grs's proposal in #26934: the client sends back the full response object; vLLM extracts the signed state from the existing
encrypted_contentfield on aReasoningItem— so the wire-format response itself uses zero new fields.This is consistent with vLLM's existing extension policy (RFC #32850) — the Responses API already ships non-standard request fields (
top_k,request_id,priority, etc.), and the Open Responses spec explicitly allows implementation extensions.Mutual exclusion with
previous_response_idis enforced via a Pydanticmodel_validator.