Skip to main content
The trl.rewards module provides ready-to-use reward functions primarily intended for GRPOTrainer and RLOOTrainer. All reward functions share the same calling convention: they receive a batch of completions and return a list of float rewards (or None for examples that should be skipped). Install the optional dependency required by the accuracy rewards:
Import from the trl.rewards sub-package:

accuracy_reward

Checks whether each model completion matches its ground-truth solution using symbolic math verification from the math_verify library.
  • If both the gold solution and the prediction are parseable LaTeX expressions, math_verify.verify is used for comparison.
  • If the gold solution cannot be parsed, None is returned for that example so the trainer can skip it.
Requires the math_verify package (pip install math_verify). The function detects non-main threads and disables signal-based timeouts automatically to avoid ValueError.

Signature

Parameters

list[list[dict[str, str]]]
Batch of completions. Each completion is a single-element list containing a message dict with a "content" key (the assistant’s output text).
list[str]
Batch of raw-text ground-truth solutions corresponding 1-to-1 with completions.
Additional keyword arguments accepted for compatibility with trainer interfaces (e.g., GRPOTrainer).

Returns

list[float | None]1.0 if the answer matches, 0.0 if not, or None if the gold solution could not be parsed.

Example


reasoning_accuracy_reward

Variant of accuracy_reward designed for reasoning models that emit a thinking block before their final answer (e.g., models using <think>...</think> tags). The function strips the reasoning section and evaluates only the text that follows the last reasoning delimiter.
  • Completions where no reasoning delimiter is found receive a reward of 0.0 (penalizing incomplete reasoning chains).
  • Completions where the gold solution is unparseable receive None (skip).

Signature

Parameters

list[list[dict[str, str]]]
Batch of completions. Each completion is a single-element list containing a message dict with a "content" key.
list[str]
Batch of ground-truth solution strings.
list[str]
List of delimiter strings marking the end of the reasoning block. Defaults to ["</think>"]. The final answer is taken as the text after the last occurrence of any delimiter.
Additional keyword arguments for trainer compatibility.

Returns

list[float | None]1.0 on correct answer, 0.0 if wrong or reasoning is incomplete, None if gold is unparseable.

Example


think_format_reward

A lightweight format-checking reward that returns 1.0 when the completion correctly wraps its reasoning inside a single <think>...</think> block, and 0.0 otherwise. The regex pattern enforced is:
This means the completion must:
  • Start with <think>.
  • Contain exactly one <think> opening tag.
  • Close with </think> before any additional content.

Signature

Parameters

list[list[dict[str, str]]]
Batch of completions. Each element is a single-element list with a message dict containing a "content" key.
Additional keyword arguments for trainer compatibility.

Returns

list[float]1.0 if format is correct, 0.0 otherwise.

Example


get_soft_overlong_punishment

A factory function that returns a reward function penalizing completions that exceed a target length. Based on Equation 13 from the DAPO paper. The returned reward function applies the following piecewise penalty: R(y)={0yLmaxLcache(LmaxLcache)yLcacheLmaxLcache<yLmax1y>LmaxR(y) = \begin{cases} 0 & |y| \le L_{\max} - L_{\text{cache}} \\ \dfrac{(L_{\max} - L_{\text{cache}}) - |y|}{L_{\text{cache}}} & L_{\max} - L_{\text{cache}} < |y| \le L_{\max} \\ -1 & |y| > L_{\max} \end{cases}

Signature

Parameters

int
Maximum allowed completion length LmaxL_{\max} in tokens.
int
Soft penalty window LcacheL_{\text{cache}}. Completions in the range (LmaxLcache,Lmax](L_{\max} - L_{\text{cache}},\, L_{\max}] receive a linearly interpolated penalty. Set to 0 to apply no minimum-length tolerance.

Returns

A callable with signature (completion_ids: list[list[int]], **kwargs) -> list[float] suitable for direct use as a reward function in GRPOTrainer.

Example

Using with GRPOTrainer