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:
- Find and extract the content within \boxed{...} from the response string
- Handle nested braces correctly (e.g., \boxed{\frac{1}{2}} should extract \frac{1}{2})
- If multiple \boxed{} expressions exist, return the content from the last one
- 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:
4Explanation: 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 caseOutput:
Correct evaluated resultExplanation: 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
passPython3
ReadyLines: 1Characters: 0
Ready