Skip to content

Item 'rs_...' of type 'reasoning' was provided without its required following item #7099

Closed

Description

@iborqe

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

Activity

  1. added
    bugSomething isn't working as documented
    provider/openaiIssues related to the @ai-sdk/openai provider
    and removed on Jul 7, 2025
  2. iborqe commented on Jul 7, 2025

    @iborqe
    Author

    @lgrammel is there any hot-fix for this issue?

  3. lgrammel commented on Jul 10, 2025

    @lgrammel
    Collaborator

    Hi can you check the latest ai sdk 5 beta and latest ai sdk 5 beta openai provider? we made fixes in that area

  4. garymjr commented on Jul 10, 2025

    @garymjr

    This is still happening in the latest beta releases

  5. iborqe commented on Jul 10, 2025

    @iborqe
    Author

    @lgrammel still there

      cause: undefined,
      url: 'https://api.openai.com/v1/responses',
      requestBodyValues: [Object],
      statusCode: 400,
      responseHeaders: [Object],
      responseBody: '{\n' +
        '  "error": {\n' +
        `    "message": "Item 'rs_68705394944881a0bad1de4b641bb7a80d2db2349dd4408e' of type 'reasoning' was provided without its required following item.",\n` +
        '    "type": "invalid_request_error",\n' +
        '    "param": "input",\n' +
        '    "code": null\n' +
        '  }\n' +
        '}',
      isRetryable: false,
      data: [Object]
    }```
    
  6. nneven commented on Jul 12, 2025

    @nneven

    I am also experiencing the same issue and am on the latest version of the v5 beta.

  7. iborqe commented on Jul 12, 2025

    @iborqe
    Author

    is there any update?

  8. thdxr commented on Jul 12, 2025

    @thdxr
    Contributor

    we're seeing this as well in opencode

  9. iborqe commented on Jul 13, 2025

    @iborqe
    Author

    ai: "^5.0.0-beta.16" still have the issue

  10. dineshgadge commented on Jul 14, 2025

    @dineshgadge

    @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

  11. lgrammel commented on Jul 14, 2025

    @lgrammel
    Collaborator

    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?

  12. 49 remaining items

  13. imaai commented on Oct 23, 2025

    @imaai

    Still expirencing the same issue.

  14. wowtah commented on Nov 1, 2025

    @wowtah

    Also having this issue.

  15. saymondamasio commented on Nov 5, 2025

    @saymondamasio

    store: false, // No data retention - makes interaction stateless
    include: ['reasoning.encrypted_content'], // Hence, we need to retrieve the model's encrypted reasoning to be able to pass it to follow-up requests
    } satisfies OpenAIResponsesProviderOptions

    that fixed it, thanks

  16. coji commented on Nov 9, 2025

    @coji

    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:

    • Using Vercel AI SDK v5 with OpenAI Responses API (gpt-5)
    • Client-side tool execution generating long SQL queries
    • 5-12 tool calls per user request, each producing hundreds of tool-input-delta events
    • Complex React application (React Query, Toast, etc.)

    What 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: 100 to useChat:

    const { messages, sendMessage, status, addToolResult } = useChat({
      experimental_throttle: 100, // Batch updates every 100ms
      // ...
    });

    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!

  17. chrispychiki commented on Nov 10, 2025

    @chrispychiki

    Additional Root Cause: Zod Validation Failures

    We encountered the same error intermittently:

    Item 'rs_xxx' of type 'reasoning' was provided without its required following item.
    

    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:

    1. Model generates reasoning items
    2. Model calls a tool with invalid arguments
    3. OpenAI's server rejects the tool call (validation fails)
    4. Request fails with 400 error - your tool never executes, model never sees the error
    5. Reasoning item left without a valid "following item" (the rejected tool call)

    This is intermittent because the model sometimes generates valid arguments, sometimes doesn't.

    Our Setup

    • GPT-5 via Vercel AI SDK streamText()
    • Tool calling with Zod input schemas
    • Using previousResponseId (though unclear if this is required to trigger the bug)
    • [email protected], @ai-sdk/[email protected]

    Reproduction

    Force validation to always fail:

    inputSchema: z.object({
      param: z.string().refine(() => false, 'Always fails')
    })

    Result: 100% reproduction of the reasoning error.

    Workaround

    Move validation from schema to execution so model receives the error and can retry:

    // Before (breaks reasoning chain)
    inputSchema: z.object({
      timestamp: z.string().regex(/^\d{2}:\d{2}:\d{2}\.\d{3}$/)
    })
    
    // After (model sees error, can retry in same turn)
    inputSchema: z.object({
      timestamp: z.string()
    }),
    execute: async ({ timestamp }) => {
      if (!/^\d{2}:\d{2}:\d{2}\.\d{3}$/.test(timestamp)) {
        throw new Error(`Invalid format: ${timestamp}`);
      }
    }

    Bottom line: Avoid regex, refine, transform in tool schemas when using GPT-5. Move all validation into the execute function.

  18. marcel-happyfloat commented on Nov 22, 2025

    @marcel-happyfloat

    No solution worked for me unfortunately, switched to xAI and it worked...

  19. lvndry commented on Nov 23, 2025

    @lvndry
    Contributor
    {
      openai: {
          reasoningEffort: "medium",
          store: false,
          include: ["reasoning.encrypted_content"],
      } satisfies OpenAIResponsesProviderOptions,
    }

    Worked but now I can't display reasoning tokens anymore

  20. LarHope commented on Dec 4, 2025

    @LarHope

    still no solid resolution here?

  21. added a commit that references this issue on Feb 4, 2026
    d2af273
  22. lgrammel commented on Jul 22, 2026

    @lgrammel
    Collaborator

    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

    • OpenAI Responses reasoning models return a 400 AI_APICallError after tool calls: a reasoning item is missing its required following item.
    • Maintainer reproduced the failure with o3 tool calling and later with gpt-5-mini plus webSearchPreview across subsequent generateText calls.
    • Maintainer identified missing function-call/item IDs as a likely cause and opened fix (provider/openai): multi-step reasoning with tool calls #7285 as a fix, while later reports showed remaining variants in multi-step interactions.
  23. added theissue type on Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai/providerrelated to a provider package. Must be assigned together with at least one `provider/*` labelbugSomething isn't working as documentedmerged-mainprovider/openaiIssues related to the @ai-sdk/openai providertask-identify-issue-type-done

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions