|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from types import SimpleNamespace |
| 15 | + |
| 16 | +from starlette.datastructures import Headers |
| 17 | + |
| 18 | +from tensorrt_llm.serve.openai_disagg_server import OpenAIDisaggServer |
| 19 | +from tensorrt_llm.serve.openai_protocol import CompletionRequest, DisaggregatedParams |
| 20 | + |
| 21 | + |
| 22 | +def _raw_request(headers: dict[str, str]): |
| 23 | + return SimpleNamespace(headers=Headers(headers=headers)) |
| 24 | + |
| 25 | + |
| 26 | +def test_extract_conversation_id_from_headers(): |
| 27 | + cases = [ |
| 28 | + ({"X-Session-ID": "session-id"}, "session-id"), |
| 29 | + ({"X-Correlation-ID": "correlation-id"}, "correlation-id"), |
| 30 | + ({"x-session-affinity": "session-affinity"}, "session-affinity"), |
| 31 | + ({"x-multi-turn-session-id": "multi-turn-session-id"}, "multi-turn-session-id"), |
| 32 | + ( |
| 33 | + { |
| 34 | + "X-Correlation-ID": "correlation-id", |
| 35 | + "X-Session-ID": "session-id", |
| 36 | + "x-session-affinity": "session-affinity", |
| 37 | + "x-multi-turn-session-id": "multi-turn-session-id", |
| 38 | + }, |
| 39 | + "session-id", |
| 40 | + ), |
| 41 | + ( |
| 42 | + { |
| 43 | + "x-session-affinity": "session-affinity", |
| 44 | + "x-multi-turn-session-id": "multi-turn-session-id", |
| 45 | + }, |
| 46 | + "session-affinity", |
| 47 | + ), |
| 48 | + ] |
| 49 | + |
| 50 | + for headers, expected_conversation_id in cases: |
| 51 | + request = CompletionRequest(model="test-model", prompt="hello") |
| 52 | + |
| 53 | + OpenAIDisaggServer._extract_conversation_id(request, _raw_request(headers)) |
| 54 | + |
| 55 | + assert request.disaggregated_params is not None |
| 56 | + assert request.disaggregated_params.conversation_id == expected_conversation_id |
| 57 | + |
| 58 | + |
| 59 | +def test_extract_conversation_id_preserves_body_conversation_id(): |
| 60 | + request = CompletionRequest( |
| 61 | + model="test-model", |
| 62 | + prompt="hello", |
| 63 | + disaggregated_params=DisaggregatedParams( |
| 64 | + request_type="context_only", |
| 65 | + conversation_id="body-id", |
| 66 | + ), |
| 67 | + ) |
| 68 | + |
| 69 | + OpenAIDisaggServer._extract_conversation_id( |
| 70 | + request, |
| 71 | + _raw_request({"X-Session-ID": "header-id"}), |
| 72 | + ) |
| 73 | + |
| 74 | + assert request.disaggregated_params.conversation_id == "body-id" |
| 75 | + |
| 76 | + |
| 77 | +def test_extract_conversation_id_populates_existing_disaggregated_params(): |
| 78 | + request = CompletionRequest( |
| 79 | + model="test-model", |
| 80 | + prompt="hello", |
| 81 | + disaggregated_params=DisaggregatedParams(request_type="context_only"), |
| 82 | + ) |
| 83 | + |
| 84 | + OpenAIDisaggServer._extract_conversation_id( |
| 85 | + request, |
| 86 | + _raw_request({"x-multi-turn-session-id": "multi-turn-session-id"}), |
| 87 | + ) |
| 88 | + |
| 89 | + assert request.disaggregated_params.conversation_id == "multi-turn-session-id" |
0 commit comments