An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Fix OpenAI API error with GPT-5 reasoning parts in chat history
Yoshiki Miura50c08bd33dbd
diff --git a/bun.lock b/bun.lockindex 2043913a..e15b995e 100644--- a/bun.lock+++ b/bun.lock@@ -5,10 +5,10 @@"name": "morphic","dependencies": {"@ai-sdk/anthropic": "^2.0.1",- "@ai-sdk/gateway": "^1.0.2",- "@ai-sdk/google": "^2.0.2",- "@ai-sdk/openai": "^2.0.3",- "@ai-sdk/react": "^2.0.4",+ "@ai-sdk/gateway": "^1.0.4",+ "@ai-sdk/google": "^2.0.3",+ "@ai-sdk/openai": "^2.0.7",+ "@ai-sdk/react": "^2.0.8","@aws-sdk/client-s3": "^3.850.0","@paralleldrive/cuid2": "^2.2.2","@radix-ui/react-alert-dialog": "^1.0.5",@@ -34,7 +34,7 @@"@tailwindcss/typography": "^0.5.12","@upstash/redis": "^1.34.0","@vercel/analytics": "^1.5.0",- "ai": "^5.0.4",+ "ai": "^5.0.8","class-variance-authority": "^0.7.1","clsx": "^2.1.0","cmdk": "1.0.0",@@ -94,17 +94,17 @@"@ai-sdk/anthropic": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "2.0.0", "@ai-sdk/provider-utils": "3.0.1" }, "peerDependencies": { "zod": "^3.25.76 || ^4" } }, "sha512-HtNbpNV9qXQosHu00+CBMEcdTerwZY+kpVMNak0xP/P5TF6XkPf7IyizhLuc7y5zcXMjZCMA7jDGkcEdZCEdkw=="],- "@ai-sdk/gateway": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "2.0.0", "@ai-sdk/provider-utils": "3.0.1" }, "peerDependencies": { "zod": "^3.25.76 || ^4" } }, "sha512-RCE6v6/7JMzSHmjB0R7oWT54y/VYeEv+6NYLtkQKdxN871UawFFHpL7TW7IRnn+/SHdRlr7XoWxxGdHEaGRKiA=="],+ "@ai-sdk/gateway": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "2.0.0", "@ai-sdk/provider-utils": "3.0.1" }, "peerDependencies": { "zod": "^3.25.76 || ^4" } }, "sha512-1roLdgMbFU3Nr4MC97/te7w6OqxsWBkDUkpbCcvxF3jz/ku91WVaJldn/PKU8feMKNyI5W9wnqhbjb1BqbExOQ=="],- "@ai-sdk/google": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "2.0.0", "@ai-sdk/provider-utils": "3.0.1" }, "peerDependencies": { "zod": "^3.25.76 || ^4" } }, "sha512-ZTETUnuXPBErzRiXSFvPjRUJQ6kAZLueFi3qCtpxPe7xgmQAqY+0z4pR+v4zOBgrCtgpuB6nayXXXG/8zUaCjA=="],+ "@ai-sdk/google": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "2.0.0", "@ai-sdk/provider-utils": "3.0.1" }, "peerDependencies": { "zod": "^3.25.76 || ^4" } }, "sha512-rQkY4nQGZnQw6pfftqsGsXXAqkGnl1gYjSbvD8+zyrVg2KAYeJU0NxIaacLWCORNcW+yQ/cqTpK/Uku/SIdEqA=="],- "@ai-sdk/openai": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "2.0.0", "@ai-sdk/provider-utils": "3.0.1" }, "peerDependencies": { "zod": "^3.25.76 || ^4" } }, "sha512-W8n024wj8fZ3zZR92xueNP+zCHhpBh86+Z/Gkk4d0lLakVeOhEK7TbqZd08iJI5ba+dBWc3GCe5NP/4iLJ01YQ=="],+ "@ai-sdk/openai": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "2.0.0", "@ai-sdk/provider-utils": "3.0.1" }, "peerDependencies": { "zod": "^3.25.76 || ^4" } }, "sha512-gIomHziQDBx1hs4VzDSDEFlfojf4/YQWY9F+r8ZiviGNUs5JKNbgHh1JoEHuC2XmMQ0x9LSyavLcqRn6aDxcyg=="],"@ai-sdk/provider": ["@ai-sdk/[email protected]", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-6o7Y2SeO9vFKB8lArHXehNuusnpddKPk7xqL7T2/b+OvXMRIXUO1rR4wcv1hAFUAT9avGZshty3Wlua/XA7TvA=="],"@ai-sdk/provider-utils": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "2.0.0", "@standard-schema/spec": "^1.0.0", "eventsource-parser": "^3.0.3", "zod-to-json-schema": "^3.24.1" }, "peerDependencies": { "zod": "^3.25.76 || ^4" } }, "sha512-/iP1sKc6UdJgGH98OCly7sWJKv+J9G47PnTjIj40IJMUQKwDrUMyf7zOOfRtPwSuNifYhSoJQ4s1WltI65gJ/g=="],- "@ai-sdk/react": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider-utils": "3.0.1", "ai": "5.0.4", "swr": "^2.2.5", "throttleit": "2.1.0" }, "peerDependencies": { "react": "^18 || ^19 || ^19.0.0-rc", "zod": "^3.25.76 || ^4" }, "optionalPeers": ["zod"] }, "sha512-l8UkmogLx3vwIs4n7KbPLaaAq2DynSqCX78yVKbrQESYxv/0mdw0+qe+EVMPPxvG2fBjFLlImUxc33l0Dhb19w=="],+ "@ai-sdk/react": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider-utils": "3.0.1", "ai": "5.0.8", "swr": "^2.2.5", "throttleit": "2.1.0" }, "peerDependencies": { "react": "^18 || ^19 || ^19.0.0-rc", "zod": "^3.25.76 || ^4" }, "optionalPeers": ["zod"] }, "sha512-I9CI53lncv0nUy3TPW0lrnoHCZx4NUaAuFCgm6U0bId0PQKeYi61Z9XFrgo3mDByH2SSC3OVsPq6d5Db8kF1FQ=="],"@alloc/quick-lru": ["@alloc/[email protected]", "", {}, "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw=="],@@ -866,7 +866,7 @@"agent-base": ["[email protected]", "", {}, "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ=="],- "ai": ["[email protected]", "", { "dependencies": { "@ai-sdk/gateway": "1.0.2", "@ai-sdk/provider": "2.0.0", "@ai-sdk/provider-utils": "3.0.1", "@opentelemetry/api": "1.9.0" }, "peerDependencies": { "zod": "^3.25.76 || ^4" } }, "sha512-nizyqRxFlJShCAh/xPqor9EYpuaiGqCcYqau9A2GaSL+di+Qf6pA+Hos5x1orlodmT+EciZfoC2Sq+I12OmIUg=="],+ "ai": ["[email protected]", "", { "dependencies": { "@ai-sdk/gateway": "1.0.4", "@ai-sdk/provider": "2.0.0", "@ai-sdk/provider-utils": "3.0.1", "@opentelemetry/api": "1.9.0" }, "peerDependencies": { "zod": "^3.25.76 || ^4" } }, "sha512-qbnhj046UvG30V1S5WhjBn+RBGEAmi8PSZWqMhRsE3EPxvO5BcePXTZFA23e9MYyWS9zr4Vm8Mv3wQXwLmtIBw=="],"ajv": ["[email protected]", "", { "dependencies": { "fast-deep-equal": "^3.1.1", "fast-json-stable-stringify": "^2.0.0", "json-schema-traverse": "^0.4.1", "uri-js": "^4.2.2" } }, "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g=="],diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.tsindex 2547e0bf..0c5b099d 100644--- a/lib/streaming/create-chat-stream-response.ts+++ b/lib/streaming/create-chat-stream-response.ts@@ -18,6 +18,7 @@ import {} from '../utils/context-window'import { getTextFromParts } from '../utils/message-utils'+import { filterReasoningParts } from './helpers/filter-reasoning-parts'import { handleStreamFinish } from './helpers/handle-stream-finish'import { prepareMessages } from './helpers/prepare-messages'import type { StreamContext } from './helpers/types'@@ -78,8 +79,12 @@ export async function createChatStreamResponse(writer})+ // Filter out reasoning parts from messages before converting to model messages+ // OpenAI API requires reasoning messages to be followed by assistant messages+ const filteredMessages = filterReasoningParts(messagesToModel)+// Convert to model messages and apply context window management- let modelMessages = convertToModelMessages(messagesToModel)+ let modelMessages = convertToModelMessages(filteredMessages)if (shouldTruncateMessages(modelMessages, model)) {const maxTokens = getMaxAllowedTokens(model)diff --git a/lib/streaming/helpers/filter-reasoning-parts.ts b/lib/streaming/helpers/filter-reasoning-parts.tsnew file mode 100644index 00000000..bc73492d--- /dev/null+++ b/lib/streaming/helpers/filter-reasoning-parts.ts@@ -0,0 +1,29 @@+import { UIMessage } from 'ai'++/**+ * Filter out reasoning parts from messages before sending to the model API.+ *+ * OpenAI API requires that reasoning messages are followed by assistant messages,+ * and empty reasoning parts can cause API errors. This helper removes all reasoning+ * parts from the message history to prevent these issues.+ *+ * @param messages - Array of UI messages from the chat history+ * @returns Filtered messages without reasoning parts+ */+export function filterReasoningParts(messages: UIMessage[]): UIMessage[] {+ return messages.map(msg => {+ if (msg.parts && Array.isArray(msg.parts)) {+ const filteredParts = msg.parts.filter((part: any) => {+ // Remove all reasoning parts (including empty ones)+ // These are displayed in the UI but should not be sent to the API+ return part.type !== 'reasoning'+ })++ return {+ ...msg,+ parts: filteredParts+ }+ }+ return msg+ })+}diff --git a/package.json b/package.jsonindex dc27bbad..e434843f 100644--- a/package.json+++ b/package.json@@ -18,10 +18,10 @@},"dependencies": {"@ai-sdk/anthropic": "^2.0.1",- "@ai-sdk/gateway": "^1.0.2",- "@ai-sdk/google": "^2.0.2",- "@ai-sdk/openai": "^2.0.3",- "@ai-sdk/react": "^2.0.4",+ "@ai-sdk/gateway": "^1.0.4",+ "@ai-sdk/google": "^2.0.3",+ "@ai-sdk/openai": "^2.0.7",+ "@ai-sdk/react": "^2.0.8","@aws-sdk/client-s3": "^3.850.0","@paralleldrive/cuid2": "^2.2.2","@radix-ui/react-alert-dialog": "^1.0.5",@@ -47,7 +47,7 @@"@tailwindcss/typography": "^0.5.12","@upstash/redis": "^1.34.0","@vercel/analytics": "^1.5.0",- "ai": "^5.0.4",+ "ai": "^5.0.8","class-variance-authority": "^0.7.1","clsx": "^2.1.0","cmdk": "1.0.0",