We are seeing this error when an agent try to do a tool call
[CONVEX A(agentActions:generateResponse)] Uncaught AI_APICallError: Invalid schema for function 'getOrganization': schema must be a JSON Schema of 'type: "object"', got 'type: "None"'.
at <anonymous> (../../node_modules/@ai-sdk/provider-utils/src/response-handler.ts:56:10)
at async postToApi (../../node_modules/@ai-sdk/provider-utils/src/post-to-api.ts:112:10)
at async doGenerate [as doGenerate] (../../node_modules/@ai-sdk/openai/src/openai-chat-language-model.ts:316:6)
at async fn (../../node_modules/ai/src/generate-text/generate-text.ts:369:21)
at async <anonymous> (../../node_modules/ai/src/telemetry/record-span.ts:19:10)
at async _retryWithExponentialBackoff (../../node_modules/ai/src/util/retry-with-exponential-backoff.ts:96:11)
at async fn (../../node_modules/ai/src/generate-text/generate-text.ts:324:45)
at async <anonymous> (../../node_modules/ai/src/telemetry/record-span.ts:19:10)
at async generateText (../../node_modules/ai/src/generate-text/generate-text.ts:262:12)
at async generateText [as generateText] (../../node_modules/@convex-dev/agent/src/client/index.ts:457:41)
I've tried with different ai and @ai-sdk/openai versions. right now on:
"@ai-sdk/openai": "^2.0.3",
"@convex-dev/agent": "^0.1.17",
"ai": "^5.0.4",
The tool we are using is farely simple just fetching an organization data from a query
const organizationSchema = z.object({
organizationId: z.string().describe("The organization ID")
})
export const getOrganization = createTool({
description: "Get the current organization details including settings like default currency",
args: organizationSchema,
handler: async (ctx, { organizationId }): Promise<Doc<"organizations">> => {
const org = await ctx.runQuery(api.organizations.getOrganization, {
organizationId: organizationId as Id<"organizations">
});
return org;
},
})
The agent setup
export const agent = new Agent(components.agent, {
name: "Assistant",
chat: openai.chat("gpt-4o-mini"),
instructions: You are Assistant, an AI helper. Use tools if needed...,
tools: {
getOrganization: getOrganization,
}
})
And the sending message action
export const sendMessage = mutation({
args: { prompt: v.string(), threadId: v.string() },
handler: async (ctx, { prompt, threadId }) => {
const { messageId } = await saveMessage(ctx, components.agent, {
threadId,
prompt,
});
await ctx.scheduler.runAfter(0, internal.agentActions.generateResponse, {
threadId,
promptMessageId: messageId,
});
},
});
export const generateResponse = internalAction({
args: { promptMessageId: v.string(), threadId: v.string() },
handler: async (ctx, { promptMessageId, threadId }) => {
await paymentAgent.generateText(ctx, { threadId}, { promptMessageId });
},
});