DEV Community

Alex Spinov
Alex Spinov

Posted on

Outlines Has a Free API — Guaranteed Structured Output From LLMs

Outlines: LLM Output That Always Matches Your Schema

Outlines uses constrained generation to guarantee LLM outputs match your schema. Not retries, not hope — actual token-level enforcement using finite state machines.

How It Differs From Instructor

Instructor retries on validation failure. Outlines prevents invalid tokens from being generated in the first place. The LLM literally cannot produce invalid output.

The Free API

import outlines

model = outlines.models.transformers("mistralai/Mistral-7B-v0.3")

# JSON output matching schema
from pydantic import BaseModel

class Person(BaseModel):
    name: str
    age: int
    city: str

generator = outlines.generate.json(model, Person)
person = generator("Extract: Alice is 30 from NYC")
print(person)  # Person(name=Alice, age=30, city=NYC)
Enter fullscreen mode Exit fullscreen mode

Choice (Classification)

sentiment = outlines.generate.choice(model, ["positive", "negative", "neutral"])
result = sentiment("This product is amazing!")
print(result)  # "positive" — guaranteed to be one of the choices
Enter fullscreen mode Exit fullscreen mode

Regex Constrained

# Phone number
phone_gen = outlines.generate.regex(model, r"\+1-\d{3}-\d{3}-\d{4}")
phone = phone_gen("Generate a US phone number")
# Always matches the regex pattern

# Date
date_gen = outlines.generate.regex(model, r"\d{4}-\d{2}-\d{2}")
date = date_gen("When was Python released?")
# Always YYYY-MM-DD format
Enter fullscreen mode Exit fullscreen mode

Grammar Constrained

# Generate valid SQL
sql_grammar = outlines.grammars.read("sql")
sql_gen = outlines.generate.cfg(model, sql_grammar)
query = sql_gen("Write a query to get all users over 25")
# Always valid SQL syntax
Enter fullscreen mode Exit fullscreen mode

Outlines vs Instructor

Feature Outlines Instructor
Method Constrained generation Retry on failure
Reliability 100% schema match ~99% with retries
Speed Single pass May need retries
Models Local models Any API
Best for Local deployment API-based apps

Real-World Use Case

A data extraction pipeline processed 100K documents. With prompt-based JSON, 5% failed validation even with retries. Outlines: 100% valid output, zero retries, 30% faster throughput.

Quick Start

pip install outlines
Enter fullscreen mode Exit fullscreen mode

Resources


Need structured AI data? Check out my tools on Apify or email spinov001@gmail.com for custom AI pipelines.

Top comments (0)