mastra-ai/mastra · error

Invalid input: array items must be strings or objects with a

Error message

Invalid input: array items must be strings or objects with an id property

What it means

After confirming the input is an array, deleteMessages normalizes each item: it must be a string (message ID) or an object containing an `id` property. An item that is null, a number, an object without `id`, etc. throws this error inside the map.

Source

Thrown at packages/memory/src/index.ts:2832

  ): Promise<void> {
    // Normalize input to messageIds before creating span to avoid leaking full message objects into traces
    let messageIds: string[];

    if (!Array.isArray(input)) {
      throw new Error('Invalid input: must be an array of message IDs or message objects');
    }

    if (input.length === 0) {
      return;
    }

    messageIds = input.map(item => {
      if (typeof item === 'string') {
        return item;
      } else if (item && typeof item === 'object' && 'id' in item) {
        return item.id;
      } else {
        throw new Error('Invalid input: array items must be strings or objects with an id property');
      }
    });

    const invalidIds = messageIds.filter(id => !id || typeof id !== 'string');
    if (invalidIds.length > 0) {
      throw new Error('All message IDs must be non-empty strings');
    }

    const span = this.createMemorySpan('delete', observabilityContext, undefined, {
      messageCount: messageIds.length,
    });

    try {
      const memoryStore = await this.getMemoryStore();

      await memoryStore.deleteMessages(messageIds);
      if (this.vector) {
        this.trackVectorCleanup(this.deleteMessageVectors(messageIds));

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Ensure every array item is a string ID or an object with an `id` property (e.g. map rows: rows.map(r => r.id))
  2. Filter out null/undefined entries before calling
  3. Rename or normalize keys so the identifier is exposed as `id`

Example fix

// before
await memory.deleteMessages(rows); // rows: { messageId: string }[]

// after
await memory.deleteMessages(rows.map(r => ({ id: r.messageId })));
Defensive patterns

Strategy: type-guard

Validate before calling

const validItems = (items: unknown[]): (string | { id: string })[] =>
  items.filter((i): i is string | { id: string } =>
    typeof i === 'string' || (typeof i === 'object' && i !== null && typeof (i as any).id !== 'undefined')
  );

Type guard

const isDeletableItem = (i: unknown): i is string | { id: string } =>
  typeof i === 'string' || (typeof i === 'object' && i !== null && 'id' in i);

Try / catch

try {
  await memory.deleteMessages(items);
} catch (err) {
  if (err instanceof Error && err.message.includes('must be strings or objects with an id')) {
    console.error('Bad item in deleteMessages input:', items.find(i => !isDeletableItem(i)));
  }
  throw err;
}

Prevention

When it happens

Trigger: Passing an array containing numbers, null/undefined entries, or objects that carry the ID under a different key (e.g. { messageId }) instead of { id }.

Common situations: Mapping DB rows with differently-named ID fields directly into deleteMessages; mixing partial objects from API responses that omit `id`; untyped JS callers constructing mixed arrays.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/62eec15c69a2ef08. Report an issue: GitHub.