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
Useresponse_format with json_schema type for reliable JSON generation:
Advanced Features
Whitespace Pattern Override
Customize whitespace handling in JSON decoding by combiningresponse_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.Additional Resources
- vLLM Structured Outputs Guide - Official documentation
- BoundaryML - Reliable structured outputs from any model
- Pydantic Documentation - Python schema validation
- Zod Documentation - TypeScript schema validation

