Skip to main content

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.
ThinkParser is intended for use with models which always include <think>...</think> tags but do NOT parse them automatically. This will cause parsing failures if the model does not include <think>...</think> tags, or if the chat template automatically removes <think>...</think> tags. In particular, you should NOT use this parser with Qwen3 or DeepSeek-R1 models.

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
Returns: The content after the last </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.
Returns: A reward function that checks if each assistant message follows the correct format:
  • Must start with <think>
  • Must contain exactly one <think> tag
  • Must contain exactly one </think> tag
  • Must have non-empty content after </think>
The reward function returns the average score across all assistant messages (1.0 for well-formatted, 0.0 for poorly-formatted).

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

Use ThinkParser 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
Do NOT use 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