-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Description
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
- 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).",
),
}),
}
- 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",
});
- 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