mastra-ai/mastra · error
Invalid input: must be an array of message IDs or message ob
Error message
Invalid input: must be an array of message IDs or message objects
What it means
Memory message deletion accepts an array of message IDs (strings) or message objects with `id`. Before creating the delete span, the library validates that the input is an array; anything else (undefined, a single ID string, an object) throws this error immediately.
Source
Thrown at packages/memory/src/index.ts:2819
return memoryStore.updateMessages({ messages });
}
/**
* Deletes one or more messages
* @param input - Must be an array containing either:
* - Message ID strings
* - Message objects with 'id' properties
* @returns Promise that resolves when all messages are deleted
*/
public async deleteMessages(
input: MessageDeleteInput,
observabilityContext?: Partial<ObservabilityContext>,
): 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) {View on GitHub (pinned to 75dd419e61)
Solutions
- Wrap the input in an array: deleteMessages([messageId]) or deleteMessages([message])
- Check that the source of the argument actually resolves to an array before calling
- Validate the payload shape at the boundary (e.g. with zod) to fail fast with a clearer error
Example fix
// before await memory.deleteMessages(messageId); // after await memory.deleteMessages([messageId]);
Defensive patterns
Strategy: validation
Validate before calling
function assertMessageIdArray(input: unknown): asserts input is (string | { id: string })[] {
if (!Array.isArray(input)) throw new TypeError('deleteMessages expects an array of ids or message objects');
} Type guard
const isMessageIdArray = (v: unknown): v is (string | { id: string })[] =>
Array.isArray(v) && v.every(i => typeof i === 'string' || (typeof i === 'object' && i !== null && 'id' in i)); Try / catch
try {
await memory.deleteMessages(input as any);
} catch (err) {
if (err instanceof Error && err.message.startsWith('Invalid input')) {
console.error('deleteMessages input must be an array:', input);
}
throw err;
} Prevention
- Always pass arrays, even for a single ID: [id]
- Type the variable as (string | { id: string })[] so TypeScript catches single-value misuse
- Avoid `any`-typed intermediaries that let undefined flow into deleteMessages
When it happens
Trigger: Passing a single string message ID instead of an array; passing undefined/null (e.g. from a variable that failed to populate); passing a single message object instead of [message].
Common situations: Calling deleteMessages(id) with one ID from an older code sample; an upstream query returning undefined that is forwarded directly; TypeScript types bypassed via `any` or JS callers.
Related errors
- Invalid input: array items must be strings or objects with a
- All message IDs must be non-empty strings
- Received input message with wrong threadId. Input ${message.
- Received input message with wrong resourceId. Input ${messag
- Found unhandled message ${JSON.stringify(message)}
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/e6ea52a9c489df3f.
Report an issue: GitHub.