Skip to content
mentoor

Topic 5 of 5 · 4 min read

Where constrained decoding hits its limits

Goal: Simplify a schema the provider rejects or that hurts answer quality, and decide when to let the model reason before it answers in JSON.

Constrained decoding supports only part of JSON Schema, may add delay the first time a schema is used, and can hurt quality when the model must answer without room to think. Know these limits before you design a large schema.

Check yourself · 1 of 3

Your schema uses minLength and pattern, and the provider does not enforce them. What is the safe approach?

Choose one answer

Why it matters

You write a rich schema with minLength, pattern, deep nesting and 80 fields. The provider returns a 400 error, or it accepts the schema and the answers get worse. Each provider documents its own supported subset, and it changes, so read the current page for your model.

Limits you will meet

  1. Unsupported keywords. Providers often ignore or reject some constraints, for example numeric ranges, string length or complex regular expressions. Check the docs. Where a keyword is unsupported, move the rule into your validator.
  2. Size and nesting. Very deep nesting, long enums and many properties can be refused or slow the request. Keep schemas small: split one big extraction into two calls.
  3. First-request latency. Some providers compile the schema into a grammar, which can add time to the first call with a new schema. Reuse the same schema between calls rather than generating a new one for every request, and measure it.
  4. Quality when no reasoning is allowed. If the schema's first field is answer, the model must commit before it has reasoned. For a hard question, put a reasoning string field before the answer field, or let the model think in free text first and then ask for the JSON in a second step. Providers with a built-in thinking mode can reason before the structured reply.
  5. Field order matters. The model writes fields in schema order, so each field can use the ones above it.

Before and after

{
  "type": "object",
  "properties": {
    "verdict": { "type": "string", "enum": ["approve", "reject"] },
    "reasoning": { "type": "string" }
  },
  "required": ["verdict", "reasoning"]
}

The verdict is generated before the reasoning, so the reasoning only explains a decision that was already made. Swap the order:

{
  "type": "object",
  "properties": {
    "reasoning": { "type": "string", "description": "Check each rule in order, then conclude" },
    "verdict": { "type": "string", "enum": ["approve", "reject"] }
  },
  "required": ["reasoning", "verdict"]
}

What goes wrong

  • Writing for the full JSON Schema spec. The provider accepts only a subset.
  • One giant schema. A 60-field schema is slow and error-prone. Split it by task.
  • Constraints only in the schema. If a keyword is ignored, nothing enforces your rule. Duplicate it in code.
  • Decision before thought. Fields in the wrong order turn reasoning into decoration.

Examples

Splitting a large schema

One request asks for 60 fields about a contract. Replace it with two calls: the first returns parties and dates (8 fields), the second returns obligations and penalties (12 fields). Each schema is small enough to be accepted quickly and each answer is easier to validate.

Moving a rule out of the schema

class Invoice(BaseModel):
    total_eur: float
    iban: str

    @field_validator("iban")
    def iban_shape(cls, v):
        if not re.fullmatch(r"[A-Z]{2}\d{2}[A-Z0-9]{11,30}", v):
            raise ValueError("iban format")
        return v

The provider schema stays simple (iban is a string) and the rule lives in your code.

Key points

  • Providers support only a subset of JSON Schema, so read the docs for your model and move ignored rules into your validator.
  • Keep schemas small and shallow; split a large extraction into several calls.
  • Reuse one schema across requests, because a new schema can add first-call latency.
  • Put a reasoning field before the answer field, or reason first and format second.
  • The model writes fields in schema order, so order is part of the design.

Common mistakes

  • Using schema keywords the provider does not support and assuming they are enforced.
  • Building one very large schema for the whole task.
  • Putting the final answer field before any reasoning field.
  • Generating a new schema on every request.

Check yourself · 2 of 3

A schema has answer as its first field and reasoning as its second. Why can this hurt quality on a hard question?

Choose one answer

Check yourself · 3 of 3

The first call with a new schema is slower than later calls. What should you do in your design?

Choose one answer

Practice items, not counted towards your mastery.

Start here