Skip to content
Back to skills

Boxed Answer Extraction

ASecurity

Extract final numeric answers from LaTeX \boxed{} notation in LLM math reasoning output, scanning matches in reverse for robustness

  • 61 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 12, 2026
toolspython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill boxed-answer-extraction --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Boxed Answer Extraction?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Boxed Answer Extraction
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-boxed-answer-extraction/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-boxed-answer-extraction)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: llm-boxed-answer-extraction
description: Extract final numeric answers from LaTeX \boxed{} notation in LLM math reasoning output, scanning matches in reverse for robustness
---

# Boxed Answer Extraction

## Overview

Math-reasoning LLMs typically wrap their final answer in `\boxed{}`. However, chain-of-thought outputs may contain multiple `\boxed{}` occurrences (intermediate results, corrections). Scan all matches in reverse order and take the last non-empty one — this corresponds to the model's final answer after any self-corrections.

## Quick Start

```python
import re

def extract_boxed_answer(text):
    pattern = r'\\boxed\{([^{}]*(?:\{[^{}]*\}[^{}]*)*)\}'
    matches = re.findall(pattern, text)
    if not matches:
        return None
    for match in reversed(matches):
        match = match.strip()
        if match:
            nums = re.findall(r'-?\d+', match)
            if nums:
                return int(nums[-1])
    return None

answers = [extract_boxed_answer(resp) for resp in responses]
valid = [a for a in answers if a is not None]
```

## Workflow

1. Generate reasoning chain from math LLM (DeepSeek-R1, QwQ, etc.)
2. Find all `\boxed{...}` matches via regex
3. Iterate matches in reverse (last = final answer)
4. Parse numeric content, handling nested braces and formatting
5. Apply domain constraints (e.g., modulo 1000 for competition)

## Key Decisions

- **Reverse scan**: models self-correct — intermediate \boxed{} may be wrong, last is final
- **Nested braces**: use non-greedy matching with brace balancing for `\boxed{\frac{1}{2}}`
- **Fallback**: if no \boxed{} found, try extracting last number from the response
- **Normalization**: strip LaTeX commands (`\frac`, `\text`) before numeric parsing

## References

- [Deepseek-r1-distill-qwen-7b](https://www.kaggle.com/code/itahiro/deepseek-r1-distill-qwen-7b)
- [[LB 20] QWQ-32B-preview Optimized inference](https://www.kaggle.com/code/mbmmurad/lb-20-qwq-32b-preview-optimized-inference)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…