When using OpenAI models (gpt-5.2-codex, gpt-4.1) via AI Gateway, the model passes empty string "" for optional tool parameters instead of omitting them or passing undefined.
This bug is specific to OpenAI models. Other providers like Anthropic and xAI handle optional parameters correctly.
| Model | Result |
|---|---|
openai/gpt-5.2-codex | ❌ BUG - passes "" |
openai/gpt-4.1 | ❌ BUG - passes "" |
anthropic/claude-sonnet-4.5 | ✅ OK - passes undefined |
xai/grok-3 | ✅ OK - passes undefined |
import { generateText, tool } from 'ai';
import { gateway } from '@ai-sdk/gateway';
import { z } from 'zod/v4';
const greetTool = tool({
description: 'Greet a user. If nickname is not provided, use their full name.',
inputSchema: z.object({
fullName: z.string().describe('The full name of the user'),
nickname: z.string().optional().describe('Optional nickname to use instead'),
}),
execute: async ({ fullName, nickname }) => {
console.log('nickname:', JSON.stringify(nickname));
console.log('nickname === "":', nickname === ''); // OpenAI: true, Others: false
console.log('nickname === undefined:', nickname === undefined); // OpenAI: false, Others: true
return { greeting: `Hello, ${nickname || fullName}!` };
},
});
await generateText({
model: gateway('openai/gpt-4.1'), // BUG: passes nickname: ""
// model: gateway('anthropic/claude-sonnet-4.5'), // OK: passes nickname: undefined
prompt: 'Greet the user "John Smith". Do not use any nickname.',
tools: { greet: greetTool },
});
When the user explicitly says "do not use any nickname", the nickname parameter should be:
undefined, OROpenAI models consistently pass nickname: "" (empty string):
# OpenAI models (gpt-5.2-codex, gpt-4.1)
nickname: ""
nickname === "": true
nickname === undefined: false
# Other models (claude-sonnet-4.5, grok-3)
nickname: undefined
nickname === "": false
nickname === undefined: true
This behavior is 100% reproducible across multiple runs with OpenAI models.
This bug breaks code that checks for undefined to determine if an optional parameter was provided:
// This logic breaks with OpenAI models because nickname is "" not undefined
if (nickname !== undefined) {
// Developer expects this only runs when nickname was explicitly provided
useNickname(nickname);
}
Developers must add extra checks for empty strings:
// Workaround for OpenAI models
if (nickname !== undefined && nickname !== '') {
useNickname(nickname);
}
This appears to be a model-level behavior issue specific to OpenAI models when handling optional parameters in tool schemas. The models seem to default to empty string instead of omitting optional fields. Other providers (Anthropic, xAI) correctly handle this by passing undefined.