File: extract-reasoning-middleware.md | Updated: 11/15/2025
Menu
v5 (Latest)
AI SDK 5.x
Model Context Protocol (MCP) Tools
Experimental_StdioMCPTransport
Copy markdown
========================================================================================================================================
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',});
import { extractReasoningMiddleware } from "ai"
string
The name of the XML tag to extract reasoning from (without angle brackets)
string
The separator to use between reasoning and text sections. Defaults to "\n"
boolean
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 LanguageModelV2StreamPart type for streaming responses.
On this page
Deploy and Scale AI Apps with Vercel.
Vercel delivers the infrastructure and developer experience you need to ship reliable AI-powered applications at scale.
Trusted by industry leaders: