Skip to content

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

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

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