From 9d6964926e78e3a35da49234b9b87153239d33c4 Mon Sep 17 00:00:00 2001 From: Nan Qin Date: Tue, 14 Oct 2025 16:23:22 -0500 Subject: [PATCH] fix: response_format for completion (#23212) Signed-off-by: Nan2018 --- vllm/entrypoints/openai/protocol.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/vllm/entrypoints/openai/protocol.py b/vllm/entrypoints/openai/protocol.py index 1f2c40e703834..f41fa196acd81 100644 --- a/vllm/entrypoints/openai/protocol.py +++ b/vllm/entrypoints/openai/protocol.py @@ -1197,6 +1197,10 @@ class CompletionRequest(OpenAIBaseModel): "Please pass `grammar` to `structured_outputs` instead." ), ) + structural_tag: str | None = Field( + default=None, + description=("If specified, the output will follow the structural tag schema."), + ) guided_decoding_backend: str | None = Field( default=None, description=( @@ -1357,10 +1361,27 @@ class CompletionRequest(OpenAIBaseModel): echo_without_generation = self.echo and self.max_tokens == 0 + guided_json_object = None + if self.response_format is not None: + if self.response_format.type == "json_object": + guided_json_object = True + elif self.response_format.type == "json_schema": + json_schema = self.response_format.json_schema + assert json_schema is not None + self.guided_json = json_schema.json_schema + elif self.response_format.type == "structural_tag": + structural_tag = self.response_format + assert structural_tag is not None and isinstance( + structural_tag, StructuralTagResponseFormat + ) + s_tag_obj = structural_tag.model_dump(by_alias=True) + self.structural_tag = json.dumps(s_tag_obj) + # Forward deprecated guided_* parameters to structured_outputs if self.structured_outputs is None: kwargs = dict[str, Any]( json=self.guided_json, + json_object=guided_json_object, regex=self.guided_regex, choice=self.guided_choice, grammar=self.guided_grammar, @@ -1370,13 +1391,6 @@ class CompletionRequest(OpenAIBaseModel): if len(kwargs) > 0: self.structured_outputs = StructuredOutputsParams(**kwargs) - if ( - self.structured_outputs is not None - and self.response_format is not None - and self.response_format.type == "json_object" - ): - self.structured_outputs.json_object = True - extra_args: dict[str, Any] = self.vllm_xargs if self.vllm_xargs else {} if self.kv_transfer_params: # Pass in kv_transfer_params via extra_args