From 3bce83cad41b293e6672c623640589a35a8915f1 Mon Sep 17 00:00:00 2001 From: shobhitagnihotri69 Date: Thu, 10 Sep 2026 11:09:51 +0530 Subject: [PATCH] fix(cookbooks): normalize LaTeX and markdown formatting in Fireworks arithmetic grader LLMs frequently format final integer answers with LaTeX boxing (\boxed{...}, $\boxed{...}$) or markdown bold/code tags on the final line. Previously, re.fullmatch rejected this formatting, causing correct model rollouts to receive reward 0.0 and corrupting RL training signals. This unwraps \boxed{...} and strips markdown/LaTeX delimiters from the final answer line, updates the test expectations, adds tests for formatted answers, and syncs the cookbook documentation. --- cookbooks/fireworks-rl-training/env.py | 3 +++ cookbooks/fireworks-rl-training/test_train.py | 5 ++++- docs/v6/cookbooks/fireworks-rl-training.mdx | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cookbooks/fireworks-rl-training/env.py b/cookbooks/fireworks-rl-training/env.py index f3eb3b9ae..a866e321f 100644 --- a/cookbooks/fireworks-rl-training/env.py +++ b/cookbooks/fireworks-rl-training/env.py @@ -14,6 +14,9 @@ def grade_final_integer(answer: object, expected: int) -> EvaluationResult: """Grade an integer on the last nonempty line, allowing thousands separators.""" text = (answer if isinstance(answer, str) else str(answer)).strip() final = text.splitlines()[-1].strip() if text else "" + final = ( + re.sub(r"\\boxed\{\s*([^{}]+?)\s*\}", r"\1", final.rstrip(".")).strip("$*` \t").rstrip(".") + ) got = ( int(final.replace(",", "")) if re.fullmatch(r"[+-]?(?:\d+|\d{1,3}(?:,\d{3})+)", final) diff --git a/cookbooks/fireworks-rl-training/test_train.py b/cookbooks/fireworks-rl-training/test_train.py index d466ff0c0..45c09478c 100644 --- a/cookbooks/fireworks-rl-training/test_train.py +++ b/cookbooks/fireworks-rl-training/test_train.py @@ -336,7 +336,10 @@ def test_default_renderer_matches_qwen38_chat_template() -> None: (4861, 3217, "The answer is 15637837, as calculated above.", 0.0), (4861, 3217, "The answer is 15,637,837, as calculated above.", 0.0), (2, 3, "6,", 0.0), - (4861, 3217, r"\boxed{15,637,837}", 0.0), + (4861, 3217, r"\boxed{15,637,837}", 1.0), + (4861, 3217, r"$\boxed{15,637,837}$", 1.0), + (4861, 3217, "**15,637,837**", 1.0), + (4861, 3217, "`15,637,837`", 1.0), (4861, 3217, "Working...\n\n 15,637,837 \n \t\n", 1.0), (4861, 3217, "+15,637,837", 1.0), (4861, 3217, "15637837\n0", 0.0), diff --git a/docs/v6/cookbooks/fireworks-rl-training.mdx b/docs/v6/cookbooks/fireworks-rl-training.mdx index 79d910d69..e2e646a51 100644 --- a/docs/v6/cookbooks/fireworks-rl-training.mdx +++ b/docs/v6/cookbooks/fireworks-rl-training.mdx @@ -128,6 +128,9 @@ env = Environment(name="fireworks-arithmetic") def grade_final_integer(answer: object, expected: int) -> EvaluationResult: text = (answer if isinstance(answer, str) else str(answer)).strip() final = text.splitlines()[-1].strip() if text else "" + final = ( + re.sub(r"\\boxed\{\s*([^{}]+?)\s*\}", r"\1", final.rstrip(".")).strip("$*` \t").rstrip(".") + ) got = ( int(final.replace(",", "")) if re.fullmatch(r"[+-]?(?:\d+|\d{1,3}(?:,\d{3})+)", final)