Repository navigation
Closed
Labels
ai/providerrelated to a provider package. Must be assigned together with at least one `provider/*` labelrelated to a provider package. Must be assigned together with at least one `provider/*` labelbugSomething isn't working as documentedSomething isn't working as documentedmerged-mainprovider/openaiIssues related to the @ai-sdk/openai providerIssues related to the @ai-sdk/openai providertask-identify-issue-type-done
Milestone
Description
Description
When using the openai.responses() API with a reasoning model, a tool call results in an error.
However, using openai.chat() avoids the error — but tool calling and reasoning streaming are not available in that mode.
Code
import { openai } from "@ai-sdk/openai";
import { streamText, Output, stepCountIs, convertToModelMessages } from "ai";
import { devTools } from "../tools";
import { z } from "zod";
// Allow streaming responses up to 60 seconds for more complex generations
export const maxDuration = 60;
export function errorHandler(error: unknown) {
if (error == null) {
return "unknown error";
}
if (typeof error === "string") {
return error;
}
if (error instanceof Error) {
return error.message;
}
return JSON.stringify(error);
}
// Structured output schema for website code
const WebsiteSchema = z.object({
html: z.string().describe("Whole HTML code"),
});
export async function POST(req: Request) {
try {
const { messages } = await req.json();
// Validate messages
if (!Array.isArray(messages)) {
return new Response("Invalid messages format", { status: 400 });
}
const result = streamText({
model: openai.responses(process.env.OPENAI_MODEL || "gpt-4o"),
system: "...",
messages: convertToModelMessages(messages),
stopWhen: stepCountIs(100),
tools: devTools,
abortSignal: req.signal, // Add abort signal for proper cancellation
experimental_output: Output.object({
schema: WebsiteSchema,
}),
});
return result.toUIMessageStreamResponse({
onError: errorHandler,
});
} catch (error) {
console.error("Dev API error:", error);
return new Response(errorHandler(error), { status: 500 });
}
}Problem
When using reasoning models with openai.responses(), calling a tool throws the following error:
[Error [AI_APICallError]: Item 'rs_...' of type 'reasoning' was provided without its required following item.] {
cause: undefined,
url: 'https://api.openai.com/v1/responses',
requestBodyValues: [Object],
statusCode: 400,
responseHeaders: [Object],
responseBody: '{
"error": {
"message": "Item 'rs_...' of type 'reasoning' was provided without its required following item.",
"type": "invalid_request_error",
"param": "input",
"code": null
}
}',
isRetryable: false,
data: [Object]
}
Workaround
Using openai.chat() avoids the error entirely, but does not support streaming reasoning or tool calls, which limits its utility in certain applications.
AI SDK Versions
ai:^5.0.0-beta.7@ai-sdk/openai:^2.0.0-beta.5@ai-sdk/react:^2.0.0-beta.7
Reacted by Daan, Nicholas Haley, Lirian Su and Scott Street
Metadata
Metadata
Assignees
Labels
ai/providerrelated to a provider package. Must be assigned together with at least one `provider/*` labelrelated to a provider package. Must be assigned together with at least one `provider/*` labelbugSomething isn't working as documentedSomething isn't working as documentedmerged-mainprovider/openaiIssues related to the @ai-sdk/openai providerIssues related to the @ai-sdk/openai providertask-identify-issue-type-done
Activity
iborqe commentedon Jul 7, 2025
@lgrammel is there any hot-fix for this issue?
lgrammel commentedon Jul 10, 2025
Hi can you check the latest ai sdk 5 beta and latest ai sdk 5 beta openai provider? we made fixes in that area
garymjr commentedon Jul 10, 2025
This is still happening in the latest beta releases
iborqe commentedon Jul 10, 2025
@lgrammel still there
nneven commentedon Jul 12, 2025
I am also experiencing the same issue and am on the latest version of the v5 beta.
iborqe commentedon Jul 12, 2025
is there any update?
thdxr commentedon Jul 12, 2025
we're seeing this as well in opencode
iborqe commentedon Jul 13, 2025
ai: "^5.0.0-beta.16" still have the issue
dineshgadge commentedon Jul 14, 2025
@thdxr I am facing this while using opencode too. It seems the issue is at openai's end and the workaround is to set reasoning-effort=none Related: openai/codex#1498
lgrammel commentedon Jul 14, 2025
Starting to look into this, suspect it is related to tool calling and openai item ids.
Can someone share a minimal reproduction that leaves nothing out?
49 remaining items
imaai commentedon Oct 23, 2025
Still expirencing the same issue.
wowtah commentedon Nov 1, 2025
Also having this issue.
saymondamasio commentedon Nov 5, 2025
that fixed it, thanks
coji commentedon Nov 9, 2025
Different Root Cause: Backpressure Leading to Same Error
I encountered the same error message ("Item 'rs_...' of type 'reasoning' was provided without its required following item") but with a different root cause in my case.
My scenario:
gpt-5)tool-input-deltaeventsWhat happened:
The stream would stop mid-execution with no errors (HTTP 200, no console/server logs). The next message send would trigger the "reasoning" 400 error.
Root cause:
Backpressure from React re-rendering. The massive number of streaming events (1000+ for multiple tool calls with long SQL generation) overwhelmed React's rendering capacity. The ReadableStream buffer filled up, TCP flow control kicked in, and the server eventually timed out and closed the connection normally (HTTP 200).
This left incomplete reasoning parts in message history, causing the 400 error on the next request.
Solution:
Adding
experimental_throttle: 100touseChat:This reduced re-renders from 1000+ to ~60, allowing the stream to complete successfully.
Why it's tricky:
Hope this helps others debugging similar symptoms!
chrispychiki commentedon Nov 10, 2025
Additional Root Cause: Zod Validation Failures
We encountered the same error intermittently:
Root cause in our case: Zod schema validation failures in tool definitions.
The Core Issue
OpenAI validates tool arguments server-side against your Zod schemas before your tool executes. When validation fails with GPT-5:
This is intermittent because the model sometimes generates valid arguments, sometimes doesn't.
Our Setup
streamText()previousResponseId(though unclear if this is required to trigger the bug)Reproduction
Force validation to always fail:
Result: 100% reproduction of the reasoning error.
Workaround
Move validation from schema to execution so model receives the error and can retry:
Bottom line: Avoid regex, refine, transform in tool schemas when using GPT-5. Move all validation into the
executefunction.marcel-happyfloat commentedon Nov 22, 2025
No solution worked for me unfortunately, switched to xAI and it worked...
lvndry commentedon Nov 23, 2025
Worked but now I can't display reasoning tokens anymore
LarHope commentedon Dec 4, 2025
still no solid resolution here?
fix(birmel): use Chat Completions API instead of Responses API
fix: Update AI SDK to fix reasoning/tool-call pairing bug
fix(birmel): use Chat Completions API instead of Responses API
lgrammel commentedon Jul 22, 2026
Identify issue type
Identified as Bug
Confidence: High
Rationale
This is a concrete, repeatedly reproduced failure in existing AI SDK handling of reasoning items and tool calls, including confirmation and attempted fixes from a maintainer.
Evidence