Skip to content

fix(ai-sdk): use z.coerce.number() for limit/offset params to handle string inputs from LLMs #1191

Description

@DeepanshuMishraa

Description

When using the AI SDK tools from @supermemory/tools, calls to searchMemories and documentList fail with a validation error when the LLM generates limit or offset as a string instead of a number:

tool call validation failed: parameters for tool searchMemories did not match schema: errors: [`/limit`: expected number, but got string]

Root Cause

The zod schemas for searchMemories and documentList define limit and offset as z.number(). However, LLMs occasionally emit numeric parameters as JSON strings (e.g., "10" instead of 10). z.number() rejects strings, while z.coerce.number() would silently coerce them.

Affected Fields

Tool Field Current Schema
searchMemories limit z.number().optional().default(10)
documentList limit z.number().optional().default(10)
documentList offset z.number().optional()

Fix

Change z.number()z.coerce.number() in packages/tools/src/ai-sdk.ts for all three fields.

z.coerce.number() is a standard Zod v4 pattern that converts string inputs to numbers during validation. If the value is already a number, it passes through unchanged. If it is undefined (for optional fields), it remains undefined. Only string-form numbers get coerced.

Relevant Code

https://github.com/supermemoryai/supermemory/blob/main/packages/tools/src/ai-sdk.ts

Environment

  • @supermemory/tools: 2.0.0
  • zod: ^4.1.5
  • Used via @supermemory/tools/ai-sdk export

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions