content/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx
extractReasoningMiddleware()extractReasoningMiddleware is a middleware function that extracts XML-tagged 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.
import { extractReasoningMiddleware } from 'ai';
const middleware = extractReasoningMiddleware({
tagName: 'reasoning',
separator: '\n',
});
<Snippet
text={import { extractReasoningMiddleware } from "ai"}
prompt={false}
/>
<PropertiesTable content={[ { name: 'tagName', type: 'string', isOptional: false, description: 'The name of the XML tag to extract reasoning from (without angle brackets)', }, { name: 'separator', type: 'string', isOptional: true, description: 'The separator to use between reasoning and text sections. Defaults to "\n"', }, { name: 'startWithReasoning', type: 'boolean', isOptional: true, description: 'Starts with reasoning tokens. Set to true when the response always starts with reasoning and the initial tag is omitted. Defaults to false.', }, ]} />
Returns a middleware object that:
reasoning property to the result containing the extracted contentThe middleware works with the LanguageModelV4StreamPart type for streaming responses.