extractReasoningMiddleware
Middleware that extracts reasoning sections from generated text using configurable delimiters
extractReasoningMiddleware is a middleware function that extracts delimited reasoning sections from generated text and exposes them separately from the main text content. This is particularly useful when you want to separate an AI model's reasoning process from its final output.
For models that emit non-XML delimiters, pass literal opening and closing
strings. For example, Gemma 4's raw thought-channel output uses a newline in its
opening delimiter:
Custom delimiters must be non-empty. They are matched literally, including
characters such as |, and can span multiple streaming chunks. Empty reasoning
blocks are removed from the answer text as well. Use this middleware when the
server returns these markers in text; providers that already return reasoning
separately do not need it.
Import
import { extractReasoningMiddleware } from "ai"
API Signature
Parameters
tagName:
separator?:
startWithReasoning?:
Returns
Returns a middleware object that:
- Processes both streaming and non-streaming responses
- Extracts content between the specified delimiters as reasoning
- Removes the delimiters and reasoning from the main text
- Emits reasoning content parts separately from text content parts
- Maintains proper separation between text sections using the specified separator
Type Parameters
The middleware works with the LanguageModelV4StreamPart type for streaming responses.