Outlines guarantees structured LLM output by constraining token sampling with Finite State Machines. These exercises cover the FSM approach, Pydantic schema integration, regex-constrained generation, multi-backend support, and categorical choice generation for classification tasks.
0 / 5 completed
1 / 5
What technique does the Outlines library use to guarantee structured output from language models?
Outlines uses a Finite State Machine (FSM) compiled from the target structure (regex or JSON schema). At each generation step, it masks the logits to assign zero probability to tokens that would violate the FSM, guaranteeing every generated token advances the FSM toward a valid complete structure.
2 / 5
A developer uses outlines.generate.json(model, MyPydanticModel). What does Outlines do with the Pydantic model?
Outlines converts the Pydantic model's JSON schema into an internal FSM/regex representation. This FSM constrains token sampling so the generated text is guaranteed to be valid JSON conforming to the schema. The result is automatically parsed into the Pydantic model instance.
3 / 5
Which Outlines function generates text constrained to match a specific regex pattern?
outlines.generate.regex(model, pattern) creates a generator that constrains output to match the provided regex. For example, outlines.generate.regex(model, r'\d{4}-\d{2}-\d{2}') guarantees the output is a date in YYYY-MM-DD format, since only tokens that keep the FSM on a valid path are allowed.
4 / 5
Outlines supports multiple model backends. Which backends does it natively support for local inference?
Outlines supports multiple backends including Hugging Face Transformers, llama.cpp (via llama-cpp-python), mlx-lm for Apple Silicon, and vLLM for production serving. The constrained generation logic is the same; only the model loading and token sampling interface differs per backend.
5 / 5
A developer uses outlines.generate.choice(model, ['positive', 'negative', 'neutral']). What does this guarantee?
outlines.generate.choice() constrains generation so the model outputs exactly one of the provided strings. This is implemented as a regex alternation pattern. It's ideal for classification tasks where you need guaranteed categorical output without post-processing or parsing.