Get structured outputs
Use structured_outputs in POST /v1/completions to force the generated text into a shape. The model then writes one of your texts, or text that matches your regular expression. It can also write JSON that your JSON Schema allows, or text that follows your grammar. This is how you call tools with a Base model. Each example reads ACS_API_BASE and ACS_API_KEY, which you export in the Quickstart.
Check that the model takes it
Inference takes structured_outputs on each model where capabilities.structured_outputs is true in GET /v1/models (Keys of capabilities). A model gets it after a check of that model on Inference. llama-8b, llama-405b and trinity-truebase take it. On another model, the API refuses the request with 400 before it holds any tokens. The sections below show request bodies for llama-8b.
curl -s "$ACS_API_BASE/models" \
-H "Authorization: Bearer $ACS_API_KEY" \
| jq -c '.data[] | {id, structured_outputs: .capabilities.structured_outputs}'Choose a shape
Give one of these keys in structured_outputs, and no other key. structured_outputs is a field of vLLM, so the openai SDK sends it in extra_body.
| Key | The generated text |
|---|---|
choice | Is exactly one of a list of texts. A text can be empty. |
regex | Matches the whole regular expression, in the syntax of Rust's regex crate. |
json | Is JSON that a JSON Schema allows. Send the schema as an object or as its JSON text. {} allows any JSON. |
grammar | Follows a grammar in GBNF or Lark syntax. |
json_object | Is one JSON object. The value is true. |
Size caps lists how large each one can be.
Pick one of your texts
Send the texts in choice. This request body labels a review:
{
"model": "llama-8b",
"prompt": "Review: I loved every page.\nSentiment:",
"max_tokens": 4,
"structured_outputs": { "choice": [" positive", " negative"] }
}The shape lets the model write only one of the two labels. Each label starts with a space, because the prompt ends without one.
Write a tool call
- Write your tools into the prompt.
- End the prompt where the call starts.
- Send the call's JSON Schema in
json. - Check
finish_reasonbefore you read the call (Check the finish reason).
{
"model": "llama-8b",
"prompt": "Tools: search(query), calculate(expression)\nUser: How tall is the Eiffel Tower?\nCall: ",
"max_tokens": 64,
"structured_outputs": {
"json": {
"type": "object",
"properties": {
"name": { "enum": ["search", "calculate"] },
"arguments": {
"type": "object",
"properties": { "query": { "type": "string" } },
"required": ["query"]
}
},
"required": ["name", "arguments"],
"additionalProperties": false
}
}
}Inference writes the JSON without spaces or line breaks between its parts. The Completion ends when the JSON ends.
Check the finish reason
A choice with finish_reason "length" reached max_tokens before the shape was complete. Its text was cut off, so its JSON may not parse, and its text may not match your expression or grammar. Check finish_reason before you read the text, and raise max_tokens when a choice ends with "length".
Follow a grammar
Send a grammar in GBNF, the grammar syntax of llama.cpp, or in Lark, the syntax of the llguidance library. This GBNF grammar allows two queries only:
{
"model": "llama-8b",
"prompt": "Show the names of all users in SQL:\n",
"max_tokens": 16,
"structured_outputs": {
"grammar": "root ::= \"SELECT \" column \" FROM users\"\ncolumn ::= \"id\" | \"name\""
}
}In Lark, %json puts a JSON Schema inside a grammar. This grammar lets the model write one line of text, then forces a tool call in tags:
{
"model": "llama-8b",
"prompt": "User: How tall is the Eiffel Tower?\nAssistant:",
"max_tokens": 64,
"structured_outputs": {
"grammar": "start: TEXT \"<call>\" %json {\"type\": \"object\", \"properties\": {\"query\": {\"type\": \"string\"}}, \"required\": [\"query\"]} \"</call>\"\nTEXT: /[^<\\n]*/"
}
}The API checks a %json schema as it checks json, and Inference writes it without spaces or line breaks between its parts, as it writes json. A grammar has no special tokens, such as <|eot_id|> or <[128009]>: a model that does not have one answers with no text. Write the text of a tag in quotes, such as "<call>".
Know what Inference holds
Inference holds every rule of your schema that it takes. It refuses the rest before it writes a token, so a shape never leaves out a rule without an error.
- The API refuses a schema with
uniqueItems,contains,minContains,maxContains,unevaluatedItems,unevaluatedProperties,dependentRequired,dependentSchemas,dependencies,propertyNames,not,if,then,else,$dynamicRef,$dynamicAnchor,$recursiveRef,$recursiveAnchor,multipleOf, orx-guidance. Check these rules in your own code after the Completion. - Inference refuses a
oneOfwhose branches can both match a value, and aformatthat it does not know. It takesdate,date-time,email,uuid,ipv4, anduri, among others. - The API refuses
multipleOfbecause, for some numbers and for some rules beside it, Inference's library stops the text early with no error. - Without
additionalProperties, an object can have other keys, as JSON Schema says. Set"additionalProperties": falseto allow only the keys that you name. - A
$refrefers to a part of the same schema, such as"#/$defs/name". The API refuses$idbelow the top of a schema, because it moves where each$refinside it points.
Combine it with other fields
structured_outputs works with n, a list of prompts, stream, stop, and the sampling fields. Each choice follows the shape on its own. A stop string ends the text where it appears, also inside the shape: then finish_reason is "stop", stop_reason is the string, and the text may not fit the shape. A Job takes it as POST /v1/completions does (Run a Job).
logprobs shows the probabilities of the generated tokens after Inference removes the tokens that the shape does not allow. The shape can allow fewer tokens than logprobs asks for. Inference then fills the other top entries with tokens that the shape does not allow, each at -9999.0. The shape does not change prompt_logprobs, which show the model's own probabilities of the prompt.
The API refuses structured_outputs with output_residual_stream, apply_steering_vectors, batch_invariant, or blue_noise, and in a Branch of a Loom. It does not take vLLM's structural_tag either, because Inference's library lets the text after a tag's trigger break the tag's schema. Put the tag at the end of your prompt and send json, or write a Lark grammar as in Follow a grammar.
Next steps
Errors shows each refusal of structured_outputs. Sample continuations explains the sampling fields that work with a shape.