Force Valid JSON Output With Retry on Parse Failure enforced JSON syntax specifically. Plenty of structured-output needs are simpler than that: a classifier that must output exactly one of a small, fixed set of labels, a router that must output exactly one of a known set of destinations. The failure mode here isn't "malformed syntax" — it's "the model said something that isn't one of the allowed values at all," and the fix is the same retry-loop shape with a different validity check.
You're given max_retries, the model's raw output text for each attempts, and allowed_values — the closed set of exactly-valid output strings. Implement enforce_format_with_retry(max_retries, attempts, allowed_values). Try attempt 1, then 2, ... up to at most max_retries + 1 total attempts or len(attempts) attempts, whichever is fewer, stopping at the first attempt whose stripped text is exactly one of allowed_values.
Return "SUCCESS i" (1-indexed) for the first valid attempt, or "FAILURE t" where t is the number of attempts actually tried.
max_retries; 1 to 20 attempts logged; matching is case-sensitive, exact (after stripping leading/trailing whitespace).This is the exact same retry-loop shape as Force Valid JSON Output With Retry on Parse Failure and Retry a Malformed Tool Call With Model-Guided Correction — compute the attempt bound once, scan up to it, stop on the first success. Only the validity check itself changes.
attempts[i].strip() in allowed_values is the entire validity check — a model's raw text output often has incidental leading/trailing whitespace or a trailing newline that shouldn't count against it.
No results yet
Run your code to check it against the examples, or submit to run every test.