本页还没有译文
本页 13 段还没译成日本語,下面显示的是英文原文。extractReasoningMiddleware
还没翻译译自英文(ai-sdk.dev)本页 13 段都还没译
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.
import { extractReasoningMiddleware } from 'ai';
const middleware = extractReasoningMiddleware({
tagName: 'reasoning',
separator: '\n',
});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:
const middleware = extractReasoningMiddleware({
tagName: { opening: '<|channel>thought\n', closing: '<channel|>' },
});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(string | { opening: string; closing: string }): An XML tag name without angle brackets (e.g. "think" for <think> / </think>), or an object containing non-empty literal opening and closing delimiters.separator?(string): The separator to use between reasoning and text sections. Defaults to "\n"startWithReasoning?(boolean): Starts with reasoning tokens. Set to true when the response always starts with reasoning and the opening delimiter is omitted. Defaults to false.
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.