Skip to the content.

Context

An LLM is asked for output in a fixed shape (JSON, a delimited record), but format compliance is only probabilistic, and the parsed result feeds code on the critical path.

Example

Multi-word-expression detection asks the LLM for a JSON array (groups of word positions in a sentence) but refuses to blindly trust that it will get one.

The same layered try / extract / validate / fall-back appears in the translation validator, the simplification service, and the example generators.

Problem

How can a probabilistic formatting slip be kept from turning into a failed request?

Forces

Solution

Do not trust the raw output’s shape.

Parse in layers: strip known wrappers, attempt a lenient parse, extract the expected structure from the surrounding text if that fails, validate the parsed shape (types, ranges, required fields), and then degrade gracefully (a default, a skip, a retry, or the next provider in a fallback chain of LLM vendors) instead of raising.

Keep the strictness in code, where it is deterministic and testable, rather than in ever-longer prompt instructions.

Consequences

Known Uses

Notes


💬 Open an issue about this pattern