#TypeError with Agent tool calls

5 messages · Page 1 of 1 (latest)

desert fern
#

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 });
},
});

hasty sapphireBOT
#

Hi @desert fern. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error you're seeing:

Invalid schema for function 'getOrganization': schema must be a JSON Schema of 'type: "object"', got 'type: "None"'.

indicates that the tool's argument schema is not being correctly passed or recognized as a valid JSON schema object. This is a common issue when the schema is missing, undefined, or not properly constructed.

Based on the knowledge sources, here are some things to check and try:

  1. Schema Must Be a Zod Object
    The args property in your createTool call should be a Zod object schema. Your code appears correct in this regard:

    const organizationSchema = z.object({
        organizationId: z.string().describe("The organization ID")
    })
    

    However, if organizationSchema is undefined or not a Zod object at runtime (for example, due to an import cycle or incorrect export/import), you could see this error. There are known issues with import cycles causing validators to be undefined in Convex, especially when validators are imported across files and there are circular dependencies. Try moving your schema definition and tool definition into the same file, or ensure there are no circular imports involving your schema or tool definitions. This has been a root cause for similar issues in Convex projects [Discord: schema error discussion].

#
  1. Check for Import Cycles
    If you are importing organizationSchema or getOrganization across files, make sure there are no circular dependencies. Import cycles can cause the imported value to be undefined at runtime, leading to the "type: None" error. This is a well-documented issue in Convex and can be resolved by restructuring your imports or consolidating related validators/tools into a single file [Discord: schema error discussion].

  2. Zod Schema Compatibility
    Make sure you are using a compatible version of Zod and that your schema is not being transformed or altered in a way that would make it incompatible with the expected JSON schema format. The Convex agent tools expect a Zod object schema for arguments [Docs: Agents Tools].

  3. Tool Definition and Usage
    Your tool definition and agent setup look correct, and this pattern is supported by the Convex agent component [Docs: Agents Tools]. If you recently updated any packages, ensure all related packages (@convex-dev/agent, @ai-sdk/openai, ai, and zod) are compatible and up to date.

  4. TypeScript/Type Issues
    If you are seeing TypeScript errors about schema types being undefined, this can sometimes be worked around with a // @ts-ignore comment, but it's better to resolve the underlying import or type issue [Discord: generateObject schema type error].

#

Summary of steps to try:

  • Move your schema and tool definition into the same file to rule out import cycles.
  • Double-check that organizationSchema is defined and is a Zod object at runtime.
  • Ensure all package versions are compatible.
  • If the issue persists, try restarting your dev server and clearing any build caches.

If after these steps the error persists, please provide more details about your file structure and imports, as the root cause is likely related to how the schema is being imported or constructed at runtime.


References:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: