Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Tools · Function

extractReasoningMiddleware

Extracts delimited reasoning from generated model output and exposes it separately from the main text. It supports XML-style tags and custom literal delimiters.

How it works

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.

Syntax

extractReasoningMiddleware({ tagName, separator?, startWithReasoning? })

Parameters

NameTypeDescription
tagNamestring | { opening: string; closing: string }An XML tag name without angle brackets, or an object containing literal opening and closing delimiters.
separatorstringSeparator used between reasoning and text sections; defaults to "\n".
startWithReasoningbooleanWhether the response starts with reasoning tokens; defaults to false.

Returns

A middleware object that processes streaming and non-streaming responses, extracts delimited reasoning, removes it from main text, and emits reasoning separately.

Key points

  • It separates delimited reasoning sections from the model's generated text.
  • Extracted reasoning is made available independently from the main response text.
  • The middleware supports both streamed and complete model responses.
  • Reasoning and its delimiters are excluded from the main text result.
  • Streaming output can contain distinct reasoning and text content parts.
  • The optional separator controls how remaining text sections are joined.

Common mistakes

  • Use an object with literal opening and closing delimiters when the model does not emit XML-style tags.
  • Enable startWithReasoning when reasoning begins immediately without its opening delimiter.

Example: Extract tagged reasoning

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.

When to use it

Display reasoning separately

Separate a model's internal-style <think> section from its user-facing answer.

Handle complete and streaming output

Process tagged reasoning while consuming generateText or streamText results.

Support custom delimiters

Adapt the middleware to providers that use non-XML markers.

  • wrapLanguageModel
  • generateText
  • streamText

Source: extract-reasoning-middleware. Checked 2026-10-15.