An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
fix: strip reasoning parts for OpenAI models to fix multi-turn errors
Yoshiki Miurad6fa6f7160d2
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.tsindex 29dd4360..a3e08997 100644--- a/lib/streaming/create-chat-stream-response.ts+++ b/lib/streaming/create-chat-stream-response.ts@@ -27,6 +27,7 @@ import { perfLog, perfTime } from '../utils/perf-logging'import { persistStreamResults } from './helpers/persist-stream-results'import { prepareMessages } from './helpers/prepare-messages'import { streamRelatedQuestions } from './helpers/stream-related-questions'+import { stripReasoningParts } from './helpers/strip-reasoning-parts'import type { StreamContext } from './helpers/types'import { BaseStreamConfig } from './types'@@ -135,18 +136,23 @@ export async function createChatStreamResponse(modelType})+ // For OpenAI models, strip reasoning parts from UIMessages before conversion+ // OpenAI's Responses API requires reasoning items and their following items to be kept together+ // See: https://github.com/vercel/ai/issues/11036+ const isOpenAI = context.modelId.startsWith('openai:')+ const messagesToConvert = isOpenAI+ ? stripReasoningParts(messagesToModel)+ : messagesToModel+// Convert to model messages and apply context window management- let modelMessages = await convertToModelMessages(messagesToModel)+ let modelMessages = await convertToModelMessages(messagesToConvert)- // Prune messages to remove unnecessary tool calls and empty messages- // This reduces token usage while keeping recent context- // Note: reasoning is kept ('none') for OpenAI reasoning model compatibility- // (removing reasoning without its paired tool-call causes API errors)+ // Prune messages to reduce token usage while keeping recent contextmodelMessages = pruneMessages({messages: modelMessages,- reasoning: 'none', // Keep all reasoning for OpenAI compatibility- toolCalls: 'before-last-2-messages', // Keep recent tool calls (last 2 messages)- emptyMessages: 'remove' // Remove messages that become empty after pruning+ reasoning: 'before-last-message',+ toolCalls: 'before-last-2-messages',+ emptyMessages: 'remove'})if (shouldTruncateMessages(modelMessages, model)) {diff --git a/lib/streaming/helpers/strip-reasoning-parts.ts b/lib/streaming/helpers/strip-reasoning-parts.tsnew file mode 100644index 00000000..4856cb5a--- /dev/null+++ b/lib/streaming/helpers/strip-reasoning-parts.ts@@ -0,0 +1,30 @@+import { UIMessage } from 'ai'++/**+ * Strips reasoning parts from UIMessages for OpenAI models.+ *+ * OpenAI's Responses API requires reasoning items and their following items+ * (tool-calls or text) to be kept together. The AI SDK's convertToModelMessages+ * doesn't properly handle these requirements, causing errors like:+ * "Item 'rs_...' of type 'reasoning' was provided without its required following item"+ *+ * By stripping reasoning parts before conversion, we avoid this compatibility issue.+ *+ * @see https://github.com/vercel/ai/issues/11036+ */+export function stripReasoningParts(messages: UIMessage[]): UIMessage[] {+ return messages.map(msg => {+ if (msg.role !== 'assistant' || !msg.parts) {+ return msg+ }++ const filteredParts = msg.parts.filter(part => part.type !== 'reasoning')++ // If all parts were reasoning, keep the original message+ if (filteredParts.length === 0) {+ return msg+ }++ return { ...msg, parts: filteredParts }+ })+}