Lesson 31.3 · 3 of 5 · 7 min read · Reviewed 24 Nov 2026
GRPO and verifiable rewards
By the end, you can
- Explain how GRPO turns a group of sampled answers into a training signal.
- Design a verifiable reward and spot ways it can be gamed.
DPODPO: Direct preference optimization: training directly on pairs of preferred and rejected responses, without a separate reward model. (31.2) learns from fixed pairs that someone collected in advance. Some tasks have a better source of truth: you can simply check the answer. Code either passes its tests or not. A maths answer either matches or not. GRPO is a reinforcement learning method that makes good use of that kind of check.
The loop
GRPO stands for group relative policy optimisation. For each prompt:
- Sample a group. The model writes several answers to the same prompt, say 4 to 16.
- Score each answer with a reward function.
- Compare within the group. Each answer’s advantage is its reward minus the group’s average, divided by the group’s spread (standard deviation).
- Update. Raise the probability of answers with a positive advantage, lower it for negative ones. A penalty keeps the model close to a reference copy, as in DPO.
Older RLHF-style methods (5.3) train a separate value model to estimate how good an answer “should” be. GRPO uses the group average instead, which saves a whole model’s worth of memory and tuning.
A worked example
A prompt asks for a function, checked by 5 unit tests. The reward is 1 if all tests pass, else 0. The model writes 4 answers:
| Answer | Tests passed | Reward | Advantage |
|---|---|---|---|
| A | 5 of 5 | 1 | +1 |
| B | 3 of 5 | 0 | -1 |
| C | 5 of 5 | 1 | +1 |
| D | 0 of 5 | 0 | -1 |
The mean is 0.5 and the standard deviation is 0.5, so passing answers get +1 and failing ones -1. If all four had passed, every advantage would be 0, and this prompt would teach nothing. That is why GRPO datasets favour prompts the model solves sometimes, not always or never.
Verifiable rewards
A verifiable reward comes from a program, not a judge. Common checks:
| Task | Check |
|---|---|
| Maths | Final answer equals the known answer |
| Code | Unit tests pass in a sandbox |
| Structured output | JSON parses and matches the schema |
| Format | Reasoning and final answer appear in the required sections |
Checks are cheap, consistent and hard to argue with, which is why reasoning models are often trained this way. Partial credit (3 of 5 tests) can help on hard tasks, but it gives the model more ways to game the score.
Reward hacking
The model optimises the checker, not your intention. If the tests only check that a function returns a list, it will learn to return any list. If the format rule rewards a “final answer” line, it may add one to every reply. Defences:
- Write checks that are hard to pass without doing the task: hidden tests, varied inputs.
- Read samples during training, not only the reward curve.
- Keep a separate evalEval: A repeatable test that measures how well an AI system performs on a set of cases with defined success criteria. set, scored differently, to catch gains that do not transfer.
When to use it
GRPO with verifiable rewards suits tasks with a reliable automatic check and a model that already succeeds some of the time. For taste, tone and helpfulness, where no program can judge, preference pairs and DPO are the simpler tool.
Key points
- GRPO samples several answers to the same prompt, scores each one, and compares every score with the group's average.
- Answers above the average are reinforced and answers below it are discouraged; no separate value model is needed.
- Verifiable rewards come from a checker, such as unit tests, an exact answer match or a format rule, not from a judge's opinion.
- If every answer in a group gets the same score, that group teaches nothing, so prompts must be neither too easy nor too hard.
Score a group with verifiable rewards
Turn unit tests into rewards and rewards into GRPO advantages.
You will have: A reward() checker, an advantages() function and a printout for a group of five sampled answers.
Steps
- Fill in reward(): 1 only if the answer passes every test.
- Fill in advantages(): reward minus the group mean, divided by the spread.
- Run it and see which answer is reinforced.
- Check the all-equal group: every advantage should be zero.
Done when
- Only answer A passes, with advantage +2.00.
- The failing answers each get -0.50.
- The all-equal group prints all zeros.
Open in the Playground No API key needed.
Starter code (Python)
# Score a group of sampled answers with a verifiable reward, then compute GRPO advantages.
import statistics
def solution_a(xs): return sorted(xs)
def solution_b(xs): return xs
def solution_c(xs): return sorted(xs, reverse=True)
def solution_d(xs): return sorted(set(xs))
def solution_e(xs): return []
group = {"A": solution_a, "B": solution_b, "C": solution_c, "D": solution_d, "E": solution_e}
TESTS = [([3, 1, 2], [1, 2, 3]), ([5, 5, 1], [1, 5, 5]), ([], []), ([2, 1], [1, 2])]
def reward(fn):
# TODO: return 1 only if fn passes every test in TESTS, else 0
return 0
def advantages(rewards):
# TODO: (reward - group mean) / group standard deviation (statistics.pstdev); all zeros if the spread is 0
return [0.0 for _ in rewards]
rewards = [reward(fn) for fn in group.values()]
for name, r, a in zip(group, rewards, advantages(rewards)):
print(f"answer {name}: reward {r}, advantage {a:+.2f}")
print("all-equal group advantages:", advantages([1, 1, 1, 1]))
Show a solution
# Score a group of sampled answers with a verifiable reward, then compute GRPO advantages.
import statistics
def solution_a(xs): return sorted(xs)
def solution_b(xs): return xs
def solution_c(xs): return sorted(xs, reverse=True)
def solution_d(xs): return sorted(set(xs))
def solution_e(xs): return []
group = {"A": solution_a, "B": solution_b, "C": solution_c, "D": solution_d, "E": solution_e}
TESTS = [([3, 1, 2], [1, 2, 3]), ([5, 5, 1], [1, 5, 5]), ([], []), ([2, 1], [1, 2])]
def reward(fn):
# Verifiable: 1 only if every test passes.
return 1 if all(fn(list(inp)) == out for inp, out in TESTS) else 0
def advantages(rewards):
mean = statistics.mean(rewards)
sd = statistics.pstdev(rewards)
if sd == 0:
return [0.0 for _ in rewards]
return [(r - mean) / sd for r in rewards]
rewards = [reward(fn) for fn in group.values()]
for name, r, a in zip(group, rewards, advantages(rewards)):
print(f"answer {name}: reward {r}, advantage {a:+.2f}")
print("all-equal group advantages:", advantages([1, 1, 1, 1]))