New Tool Radar

AI Concepts

How to Get a Forced-Choice Answer From vLLM With Structured Outputs

Ask vLLM for a sentiment label from a fixed list by passing a structured_outputs choice through extra_body.

Schematic diagram with four steps: send the sentiment prompt, add a structured_outputs choice of positive and negative, read the reply, then check it is exactly one label.

Use vLLM’s structured_outputs.choice when an application expects one exact label. The allowed values for this sentiment example are positive and negative.

Pass the allowed labels with extra_body

Start a vLLM OpenAI-compatible server and use the loaded model’s ID. The example assumes a loopback server with no configured API key. If authentication is enabled, read your existing API key from your environment.

from openai import OpenAI

client = OpenAI(base_url="http://127.0.0.1:8000/v1", api_key="EMPTY")
labels = ["positive", "negative"]
result = client.chat.completions.create(
    model="REPLACE_WITH_YOUR_MODEL_ID",
    messages=[{"role": "user", "content":
        "Classify this review as positive or negative: The export lost half the rows."}],
    extra_body={"structured_outputs": {"choice": labels}},
)
output = result.choices[0].message.content
assert output in labels, output
assert output == "negative", output

Check the label and the judgment

The example checks the output against the allowed values and the known label for a review. Add a clearly positive example and a borderline one. Decide how the application handles ambiguity before using forced choice in production.

vLLM removed guided_choice in 0.12.0. Migrate that field to structured_outputs.choice. For a JSON object with several fields, use response_format with a JSON schema. See the invoice example for schema-shaped output for a small exercise.

Try it: four checks

Check What you should see If you do not see it
Call succeeds The chat completion returns a reply Check the model name and the server address
Reply is a label The reply is exactly positive or negative Try a larger instruct model
No extra text The reply has no explanation around the label Tighten the prompt to ask for the label only
Repeatable The same prompt returns a label again A forced choice should be stable

Expected result. We checked the vLLM OpenAI-Compatible Server docs, which give the structured_outputs choice example with positive and negative for a sentiment prompt. We did not run vLLM on this machine.

Limits

This guide covers the choice form of structured outputs only. JSON object and JSON schema modes use response_format instead, and support varies by model. We did not test any call ourselves.

Sources

See all