From f97e82bdb61377eaa10b992296ccd45f9e58286f Mon Sep 17 00:00:00 2001 From: mortysean Date: Tue, 20 Oct 2026 21:29:23 +0800 Subject: [PATCH] fix: reject conflicting golden tokens and structured output constraints --- deepinfer/sampling_params.py | 3 ++ .../engine/test_sampling_params_golden_so.py | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/engine/test_sampling_params_golden_so.py diff --git a/deepinfer/sampling_params.py b/deepinfer/sampling_params.py index 0d03690..74bec2c 100644 --- a/deepinfer/sampling_params.py +++ b/deepinfer/sampling_params.py @@ -62,6 +62,9 @@ class SamplingParams: ) -> None: del logprobs + if so_args is not None and golden_token_ids is not None: + raise ValueError("golden_token_ids cannot be combined with structured output constraints (so_args)") + self.temperature = temperature self.top_p = top_p self.top_k = top_k diff --git a/tests/engine/test_sampling_params_golden_so.py b/tests/engine/test_sampling_params_golden_so.py new file mode 100644 index 0000000..942ef87 --- /dev/null +++ b/tests/engine/test_sampling_params_golden_so.py @@ -0,0 +1,28 @@ +import pytest + +from deepinfer.sampling_params import SamplingParams + + +def test_golden_tokens_conflict_with_structured_output() -> None: + with pytest.raises(ValueError, match="cannot be combined"): + SamplingParams( + golden_token_ids=[2], + so_args={ + "schema_type": "regex", + "schema": "[0-9]+", + }, + ) + + +def test_golden_tokens_without_structured_output() -> None: + params = SamplingParams(golden_token_ids=[2]) + assert params.golden_token_ids == [2] + + +def test_structured_output_without_golden_tokens() -> None: + so_args = { + "schema_type": "regex", + "schema": "[0-9]+", + } + params = SamplingParams(so_args=so_args) + assert params.so_args == so_args