← Journal
Reliable JSON from an LLM: Running Outlines

Reliable JSON from an LLM: Running Outlines

A record of running Outlines, a library that forces LLM output to follow a specified schema, after disabling network access. A small 0.5B model returned valid JSON with the correct values.

Repository covered in this article

dottxt-ai/outlines

Structured Outputs

14.5k Python Apache License 2.0

Tell an LLM to “answer in JSON,” and it may add an introductory sentence or omit a comma, leaving output that a parser cannot accept. Outlines constrains the token candidates during generation. The model can choose tokens that keep the output within your schema. I installed it and tested structured generation after disabling network access.

At each generation step, Outlines removes tokens that cannot match the specified JSON Schema or regular expression before the model can choose them. This makes parse failure during post-processing impossible by construction. The project releases its source under the Apache 2.0 License (*1).

1. Install Outlines and Download a Small Model

To test structured generation, I chose the small Qwen2.5 0.5B Instruct model.

FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'torch==2.13.0' --index-url https://download.pytorch.org/whl/cpu
RUN pip install --no-cache-dir --quiet 'transformers==4.57.1' 'outlines==1.3.1'
COPY warmup_model.py /tmp/warmup_model.py
RUN python /tmp/warmup_model.py
WORKDIR /work
podman build -t outlines-sandbox .

2. Pass a Schema and Generate Structured Output

I converted an incident report into JSON with a fixed schema containing severity, the affected service name, and downtime. I ran the conversion with the network disabled.

from pydantic import BaseModel
from transformers import AutoModelForCausalLM, AutoTokenizer
import outlines

class ContainerIncident(BaseModel):
    severity: str
    affected_service: str
    downtime_minutes: int

tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
hf_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
model = outlines.from_transformers(hf_model, tokenizer)

prompt = (
    "Summarize this incident as JSON: a rootless container crashed and the "
    "checkout service was down for 12 minutes. This was a high severity issue."
)
result = model(prompt, ContainerIncident, max_new_tokens=100)
parsed = ContainerIncident.model_validate_json(result)
podman run --rm --network none --memory 3g --cpus 2 --pids-limit 512 \
  -v "$(pwd)":/work -w /work outlines-sandbox \
  python structured_demo.py

The -v option makes the specified directory available for reads and writes from the container. Run the command from an empty directory. Use that directory for the experiment. While the command ran, torch.compile searched for a C++ compiler for optimization. It could not find one and printed a large number of warnings. The run used eager mode without compilation, and the warnings did not affect the generated result.

The command printed:

raw output: {"severity": "High", "affected_service": "checkout service", "downtime_minutes": 12}
parsed severity: High
parsed affected_service: checkout service
parsed downtime_minutes: 12

Pydantic parsed the output into a model, and the values matched the input text: severity High, checkout service, and 12 minutes. The 0.5B model avoided malformed output because Outlines kept schema-violating tokens out of the candidate set from the start.

Guarantee the Output Format at Generation Time

When a program consumes an LLM's output, a parse failure may require retry logic. Outlines can enforce the schema during generation, so you do not need post-processing retries for schema parse failures. You can use the small-model result when choosing a lower-cost configuration.

Sources

*1 Outlines (GitHub), https://github.com/dottxt-ai/outlines, Apache 2.0 License