Boxed Answer Extraction for Math Benchmarks

Medium
LLM

Implement a function that extracts the final answer from a model's response in math benchmarks like MATH and GSM8K. In these benchmarks, models are expected to output their final answer enclosed in a LaTeX-style \boxed{} command, for example: \boxed{42}.

Your function should:

  1. Find and extract the content within \boxed{...} from the response string
  2. Handle nested braces correctly (e.g., \boxed{\frac{1}{2}} should extract \frac{1}{2})
  3. If multiple \boxed{} expressions exist, return the content from the last one
  4. If no \boxed{} is found, return an empty string

Note: The input string will contain the literal characters backslash-boxed-openbrace, not a LaTeX rendering.

Examples

Example 1:
Input: response = 'Let me solve step by step. First 2+2=4. Therefore \\boxed{4}'
Output: 4
Explanation: The function scans the input string for the pattern \boxed{. It finds \boxed{ starting at position 52. It then tracks brace depth: starting at depth 1 after the opening brace, it increments for each { and decrements for each }. When depth returns to 0 (at the closing }), extraction ends. The content between the opening and closing braces is '4', which is returned.
Example 2:
Input: Hidden test case or specific edge case
Output: Correct evaluated result
Explanation: An additional example to demonstrate the robustness of the implementation.

Starter Code

def extract_boxed_answer(response: str) -> str:
    """
    Extract the answer from within \boxed{...} in a model response.
    
    Args:
        response: The model's text response containing a boxed answer
    
    Returns:
        The content inside the last \boxed{}, or empty string if not found
    """
    # Your code here
    pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews