Sub topic 5
Getting parseable verdicts with short reasoning
This chapter assumes you can prompt an LLM, read Python, and have a rubric for one criterion. The goal is a judge reply your code can read, validate, and store without guessing.
Why free text fails
A judge that answers "I'd say this is mostly faithful, maybe a 4" cannot feed a metric. You would have to extract the number with a regex, and the regex will eventually match the wrong digit. Ask for a fixed shape instead, and reject anything that does not fit it.
The verdict shape
Use JSON with two fields: a short reasoning string and an integer score. Three choices matter:
- Reasoning comes first. The model writes tokens in order. If the score comes first, the reasoning only justifies a number already chosen. With reasoning first, the score can depend on it.
- Keep reasoning to one or two sentences. Long explanations add cost and give the judge room to drift toward style over substance. A short note is enough to audit a verdict later.
- Define the score range in the rubric. The prompt should say what 1, 3 and 5 mean, so an out-of-range value is clearly an error.
Prompt and parser
The prompt repeats the rubric and ends with the exact reply format. The parser is strict: it fails loudly on bad input.
import json
RUBRIC = """Score the answer from 1 to 5 for faithfulness to the context.
1 = contradicts the context; 3 = partly supported; 5 = fully supported.
Judge faithfulness only. Ignore length and writing style."""
def build_prompt(question, context, answer):
return (
f"{RUBRIC}\n\nQuestion: {question}\nContext: {context}\nAnswer: {answer}\n\n"
'Reply with JSON only: {"reasoning": "<one or two sentences>", "score": <integer 1-5>}'
)
def parse_verdict(text):
data = json.loads(text)
score = data["score"]
if not isinstance(score, int) or not 1 <= score <= 5:
raise ValueError(f"invalid score: {score!r}")
return dataparse_verdict raises on three kinds of failure: text that is not JSON, a missing score key, and a score that is not an integer from 1 to 5. Note that isinstance(True, int) is True in Python, so a boolean would slip through. If your judge might return booleans, add isinstance(score, bool) to the check.
What to do when parsing fails
Treat a parse failure as a missing result, not as a low score. Silently scoring it 1, or dropping it without a count, biases the metric.
- Retry the call once or twice. Model output varies, so a retry often succeeds.
- If the retry also fails, record the item as
unparsedand keep the raw reply. - Report the unparsed rate next to the metric. A rate above a few percent means the prompt or format needs work.
Many providers offer a JSON mode or schema-constrained output that guarantees valid JSON. Use it when available, but keep the validation. A schema can ensure the score is an integer. It cannot ensure the score is a sensible one.
Common errors
- Wrapping the JSON in a Markdown fence or adding a sentence before it. Either breaks
json.loads. The "JSON only" instruction reduces this, and structured output modes remove it. - Asking for a score on a vague scale such as 1 to 10. Judges cluster around a few favorite values, so a narrow scale with defined anchors is easier to validate.
- Treating the reasoning as proof. It is a model-written account and may not reflect why the score was chosen. Use it to spot-check, not to certify.
Exercise
A judge reply is {"score": 6, "reasoning": "Fully supported."} for a rubric that runs from 1 to 5. What should your pipeline do with it, and why is putting the reasoning before the score in the prompt's format a good idea?
Show answer
The parser should reject it with an error, because 6 is outside the 1 to 5 range. Retry, and if it still fails, record the item as unparsed instead of clipping it to 5. Reasoning goes first because the model generates text in order, so the score can follow from the reasoning instead of the reasoning being written to defend a number already chosen.