Skip to content

OpenAI o3 model throws "Invalid schema for function" error when optional parameters are not included in 'required' array #7082

@khanhduyvt0101

Description

@khanhduyvt0101

Description

Description

When using the OpenAI o3 model with function calling through the AI SDK, the model throws a validation error for function schemas that have optional parameters not listed in the required array. This behavior differs from other OpenAI models (like GPT-4o, o1, o3-mini, etc.) which handle optional parameters correctly.

Error Message

Invalid schema for function 'semantic_scholar_search': In context=(), 'required' is required to be supplied and to be an array including every key in properties. Missing 'limit'.

Expected Behavior

The o3 model should handle function schemas with optional parameters the same way as other OpenAI models. Optional parameters (those marked with .optional() in Zod schema) should be treated as optional, not required.

Current Behavior

The o3 model appears to require ALL properties defined in the function schema to be included in the required array, even if they are meant to be optional parameters.

Reproduction Steps

  1. Define a function tool with optional parameters using Zod:
  semantic_scholar_search: {
    description: "Search academic papers using Semantic Scholar API.",
    parameters: z.object({
      query: z
        .string()
        .describe(
          "The text to search for. Always convert the question to a make sense search query before passing it in.",
        ),
      limit: z
        .number()
        .int()
        .optional()
        .describe("The maximum number of results to return. Min 5, max 100."),
      offset: z
        .number()
        .int()
        .nonnegative()
        .optional()
        .describe(
          "Used for pagination. When returning a list of results, start with the element at this position in the list.",
        ),
      year: z
        .number()
        .int()
        .optional() 
        .describe(
          "Restricts results to the given publication year or range of years, formatted as YYYY (Specific year).",
        ),
    }),
  }
  1. Use the function with OpenAI o3 model:
  import { generateText } from "ai";
  import { openai } from "@ai-sdk/openai";

  const result = await generateText({
    model: openai("o3"),
    tools: {
      semantic_scholar_search: {
        description: "Search academic papers using Semantic Scholar API.",
        parameters: /* Zod schema from above */
      }
    },
    prompt: "Search for papers about machine learning",
  });
  1. The error occurs when the SDK converts the Zod schema to OpenAI's function schema format.

Environment

  • AI SDK version: ^4.3.16
  • @ai-sdk/openai version: ^1.3.22
  • Node.js version: 22.15
  • OpenAI model: o3
  • Using Zod for schema validation

Additional Context

  • This issue only occurs with the o3 model
  • Other OpenAI models (gpt-4o, gpt-4o-mini, o1, o3-mini, o4-mini) work correctly with the same function schemas
  • The issue seems to be related to how the o3 model validates function schemas differently from other models
  • The error suggests o3 expects the required field to contain ALL properties from the schema, not just the non-optional ones

AI SDK Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy