Overview
ThinkParser extracts content that appears after </think> tags in model responses. It is designed for models that generate reasoning within <think>...</think> tags but do not automatically remove them from the output.
Class Signature
Parameters
Callable[[str], str]
default:"lambda x: x"
Optional extraction function to further process the parsed text after removing think tags. For example, you can use this to extract boxed answers from math problems.
Methods
parse
Extracts content after the last</think> tag.
str
required
The text to parse, potentially containing
<think>...</think> tags</think> tag, or an empty string if no </think> tag is found. The result is then passed through extract_fn if provided.
Behavior:
- If
</think>is found: Returns everything after the last</think>tag - If
</think>is NOT found: Returns an empty string (strict enforcement) - Whitespace is automatically stripped
get_format_reward_func
Returns a reward function that validates the think tag format in completions.- Must start with
<think> - Must contain exactly one
<think>tag - Must contain exactly one
</think>tag - Must have non-empty content after
</think>
Usage Examples
Basic Usage
With Custom Extraction Function
Using Format Reward Function
In a Rubric
Multiple Think Blocks
When multiple think blocks are present, only content after the last</think> tag is returned:
When to Use ThinkParser
UseThinkParser when:
- Your model always generates
<think>...</think>tags for reasoning - The model does NOT automatically strip these tags from responses
- You want strict enforcement (fail if tags are missing)
- You need to validate that responses follow the think tag format
ThinkParser with:
- Qwen3 models (automatically parse think tags)
- DeepSeek-R1 models (automatically parse think tags)
- Models that may or may not include think tags (use MaybeThinkParser instead)
- Non-reasoning models that never use think tags
See Also
- MaybeThinkParser - For models that optionally include think tags
- Parser - Base parser class
- XMLParser - For XML-formatted responses