Skip to content

Commit 2a82544

Browse files
committed
BUG: vllm structured output issue (#4142)
1 parent 24d3089 commit 2a82544

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

xinference/model/llm/vllm/core.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,18 +1085,43 @@ async def async_generate(
10851085
logger.warning(f"Failed to create GuidedDecodingParams: {e}")
10861086
guided_options = None
10871087

1088-
# Use structured_outputs for vLLM >= 0.11.0, guided_decoding for older versions
1089-
if (
1090-
VLLM_VERSION >= version.parse("0.11.0")
1091-
or VLLM_VERSION.base_version >= "0.11.0"
1092-
):
1093-
sampling_params = SamplingParams(
1094-
structured_outputs=guided_options, **sanitized_generate_config
1095-
)
1096-
else:
1097-
sampling_params = SamplingParams(
1098-
guided_decoding=guided_options, **sanitized_generate_config
1088+
try:
1089+
import inspect
1090+
1091+
sp_sig = inspect.signature(SamplingParams)
1092+
# For v0.9.2 and similar versions, prioritize guided_decoding over structured_outputs
1093+
# structured_outputs was introduced later (around v0.11.0) and may not accept
1094+
# GuidedDecodingParams in earlier versions even if the parameter exists
1095+
if "guided_decoding" in sp_sig.parameters:
1096+
sampling_params = SamplingParams(
1097+
guided_decoding=guided_options, **sanitized_generate_config
1098+
)
1099+
elif "structured_outputs" in sp_sig.parameters:
1100+
try:
1101+
sampling_params = SamplingParams(
1102+
structured_outputs=guided_options,
1103+
**sanitized_generate_config,
1104+
)
1105+
except TypeError as e:
1106+
if "structured_outputs" in str(e):
1107+
# structured_outputs parameter exists but doesn't accept GuidedDecodingParams
1108+
# Fall back to no guided decoding
1109+
logger.warning(
1110+
f"structured_outputs parameter failed: {e}. "
1111+
"Falling back to no guided decoding for vLLM version compatibility."
1112+
)
1113+
sampling_params = SamplingParams(
1114+
**sanitized_generate_config
1115+
)
1116+
else:
1117+
raise
1118+
else:
1119+
sampling_params = SamplingParams(**sanitized_generate_config)
1120+
except Exception as e:
1121+
logger.warning(
1122+
f"Failed to create SamplingParams with guided decoding: {e}"
10991123
)
1124+
sampling_params = SamplingParams(**sanitized_generate_config)
11001125
else:
11011126
# ignore generate configs for older versions
11021127
sanitized_generate_config.pop("guided_json", None)

0 commit comments

Comments
 (0)