r"""Extract the answer from the model response using math-verify. Tries to parse the last \boxed{} expression. Falls back to extracting raw \boxed{} content if math-verify parsing fails.
(response: str)
| 15 | |
| 16 | |
| 17 | def parse_answer(response: str) -> Optional[str]: |
| 18 | r"""Extract the answer from the model response using math-verify. |
| 19 | |
| 20 | Tries to parse the last \boxed{} expression. Falls back to extracting |
| 21 | raw \boxed{} content if math-verify parsing fails. |
| 22 | """ |
| 23 | # First try math-verify's robust parser |
| 24 | try: |
| 25 | parsed = parse(response) |
| 26 | if parsed: |
| 27 | # Return string representation for logging; actual comparison uses verify() |
| 28 | return str(parsed[-1]).strip() |
| 29 | except Exception: |
| 30 | pass |
| 31 | |
| 32 | # Fallback: manually extract last \boxed{} |
| 33 | results = _extract_boxed(response) |
| 34 | if results: |
| 35 | return results[-1].strip() |
| 36 | |
| 37 | return None |
| 38 | |
| 39 | |
| 40 | def _extract_boxed(text: str) -> list[str]: |
no test coverage detected