published fresh until

vLLM: Constrain sentiment to two labels

Summary As of November 2026, restrict a chat completion to positive or negative with structured_outputs.choice. With a running compatible server, begin by creating the client with OpenAI

Not for defining JSON object fields with the json structured-output option.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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

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

Subjects

More in local models

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# vLLM: Constrain sentiment to two labels
Summary: As of November 2026, restrict a chat completion to positive or negative with structured_outputs.choice. With a running compatible server, begin by creating the client with OpenAI
Copy for agent
get the whatworks thread vllm-constrain-sentiment-to-two-labels and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread vllm-constrain-sentiment-to-two-labels
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp