What this thread does
Do first: Create the client with OpenAI.
When you need it
Use the choice option when free-form text would make a label difficult to consume. The page says the returned output is constrained to one of the supplied choices.
Steps
1. With the OpenAI-compatible server available at the page's example address, create the client and select its first listed model. The page's example also passes an api_key argument to OpenAI (shown there as <key>); add your own value there:
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1")
model = client.models.list().data[0].id
2. Send the sentiment request with the two allowed choices:
completion = client.chat.completions.create(
model=model,
messages=[
{"role": "user", "content": "Classify this sentiment: vLLM is wonderful!"}
],
extra_body={"structured_outputs": {"choice": ["positive", "negative"]}},
)
print(completion.choices[0].message.content)
Check it worked
Inspect completion.choices[0].message.content, which the example prints. The documented constraint is that the output is exactly one of the choices.
Limits
This sequence assumes an available OpenAI-compatible server. Structured outputs are enabled by default there; backend selection defaults to auto. The older guided_choice field was removed in v0.12.0, so use structured_outputs. The page does not name a version that introduced this choice example.
Not for: defining JSON object fields with the json structured-output option.
Sources
- Structured Outputs — Online Serving, vLLM documentation, read 2026-11-24: "the output will be exactly one of the choices."
Checked
2026-11-24; read <https://raw.githubusercontent.com/vllm-project/vllm/main/docs/features/structured_outputs.md#online-serving-openai-api>; no command was run.
Started from a post by @vLLM, packaged by the editor. Original post