Display reasoning separately
Separate a model's internal-style <think> section from its user-facing answer.
AI Tools · Function
Extracts delimited reasoning from generated model output and exposes it separately from the main text. It supports XML-style tags and custom literal delimiters.
The middleware examines generated output in both streaming and non-streaming modes. It recognizes configured delimiters, removes the delimited reasoning from the main text, and emits reasoning separately; separator and startWithReasoning control how sections are interpreted and joined.
extractReasoningMiddleware({ tagName, separator?, startWithReasoning? })| Name | Type | Description |
|---|---|---|
tagName | string | { opening: string; closing: string } | An XML tag name without angle brackets, or an object containing literal opening and closing delimiters. |
separator | string | Separator used between reasoning and text sections; defaults to "\n". |
startWithReasoning | boolean | Whether the response starts with reasoning tokens; defaults to false. |
A middleware object that processes streaming and non-streaming responses, extracts delimited reasoning, removes it from main text, and emits reasoning separately.
import { generateText, wrapLanguageModel, extractReasoningMiddleware } from 'ai';
import { yourModel } from './model';
const model = wrapLanguageModel({
model: yourModel,
middleware: extractReasoningMiddleware({ tagName: 'think' }),
});
const { reasoning, text } = await generateText({
model,
prompt: 'Explain quantum entanglement.',
});
console.log({ reasoning, text });Wraps a language model so content inside <think> tags is returned separately as reasoning while the remaining response is available as text.
Separate a model's internal-style <think> section from its user-facing answer.
Process tagged reasoning while consuming generateText or streamText results.
Adapt the middleware to providers that use non-XML markers.
wrapLanguageModelgenerateTextstreamText