Skip to main content

Structured Outputs

Structured outputs ensure that model responses match specific formats like JSON schemas, regex patterns, or predefined choices. Tinfoil uses vLLM’s guided decoding to constrain outputs by filtering next-token predictions, guaranteeing valid formats without post-processing.
Tinfoil supports structured outputs through vLLM. Use response_format with json_schema type for JSON outputs, or structured_outputs for choice and regex constraints. In Python, pass it via extra_body. In JavaScript, pass it directly on the request body (the OpenAI Node SDK does not support extra_body).

Benefits

  • Format Enforcement: Token-level filtering ensures outputs match your exact format
  • Type Safety: Works with Pydantic (Python), Zod (TypeScript), and native types in Go
  • No Post-Processing: Outputs are guaranteed valid
  • Deterministic: Next-token prediction is constrained to produce only valid tokens
  • Multiple Backends: Supports xgrammar and guidance backends
For complete documentation, see the vLLM Structured Outputs Guide and the vLLM blog post on structured decoding.

Quick Start

Here are basic examples for each structured output type:

Choice

Restrict output to a predefined list:

Regex

Enforce regex patterns for formatted outputs:

JSON

Use response_format with json_schema type for reliable JSON generation:
Prompt explicitly for JSON. While structured_outputs enforces valid JSON structure, the model produces more reliable results when your prompt explicitly requests JSON output and describes the expected fields. For example, use “Output a JSON object with…” rather than just “Generate…”

Advanced Features

Whitespace Pattern Override

Customize whitespace handling in JSON decoding by combining response_format with extra_body:

Complex Nested Schemas

Build complex nested structures with Pydantic:
Python

Best Practices

Markdown-Wrapped Responses: Some models may wrap JSON responses in markdown code blocks (```json ... ```). Strip the formatting before parsing the JSON.
Use Low Temperature for Deterministic Outputs
Validate Responses
Enable Streaming for Large Responses

Additional Resources