|
17 | 17 | from vllm.engine.arg_utils import AsyncEngineArgs |
18 | 18 | from vllm.engine.async_llm_engine import AsyncLLMEngine |
19 | 19 | from vllm.sampling_params import SamplingParams |
| 20 | + |
| 21 | +# vLLM renamed GuidedDecodingParams to StructuredOutputsParams in newer versions. |
| 22 | +# The corresponding SamplingParams field also changed from guided_decoding to structured_outputs. |
20 | 23 | try: |
21 | | - from vllm.sampling_params import GuidedDecodingParams |
| 24 | + from vllm.sampling_params import StructuredOutputsParams |
| 25 | + _structured_output_cls = StructuredOutputsParams |
| 26 | + _structured_output_field = "structured_outputs" |
22 | 27 | except ImportError: |
23 | | - GuidedDecodingParams = None |
| 28 | + try: |
| 29 | + from vllm.sampling_params import GuidedDecodingParams |
| 30 | + _structured_output_cls = GuidedDecodingParams |
| 31 | + _structured_output_field = "guided_decoding" |
| 32 | + except ImportError: |
| 33 | + _structured_output_cls = None |
| 34 | + _structured_output_field = None |
24 | 35 | from vllm.utils import random_uuid |
25 | 36 | from vllm.transformers_utils.tokenizer import get_tokenizer |
26 | 37 | from vllm.multimodal.utils import fetch_image |
@@ -234,16 +245,18 @@ async def _predict(self, request, context, streaming=False): |
234 | 245 | if value not in (None, 0, [], False): |
235 | 246 | setattr(sampling_params, param_field, value) |
236 | 247 |
|
237 | | - # Handle structured output via guided decoding |
238 | | - if GuidedDecodingParams is not None: |
239 | | - guided_decoding = None |
| 248 | + # Handle structured output via guided decoding / structured outputs |
| 249 | + if _structured_output_cls is not None: |
| 250 | + constraint = None |
240 | 251 | if hasattr(request, 'JSONSchema') and request.JSONSchema: |
241 | | - guided_decoding = GuidedDecodingParams(json_schema=request.JSONSchema) |
| 252 | + constraint = _structured_output_cls(json=request.JSONSchema) |
242 | 253 | elif hasattr(request, 'ResponseFormat') and request.ResponseFormat == "json_object": |
243 | | - guided_decoding = GuidedDecodingParams(json_object=True) |
| 254 | + constraint = _structured_output_cls(json_object=True) |
| 255 | + elif hasattr(request, 'Grammar') and request.Grammar: |
| 256 | + constraint = _structured_output_cls(grammar=request.Grammar) |
244 | 257 |
|
245 | | - if guided_decoding is not None: |
246 | | - sampling_params.guided_decoding = guided_decoding |
| 258 | + if constraint is not None: |
| 259 | + setattr(sampling_params, _structured_output_field, constraint) |
247 | 260 |
|
248 | 261 | # Extract image paths and process images |
249 | 262 | prompt = request.Prompt |
|
0 commit comments