mastra-ai/mastra · error
Invalid MemoryRequestContext.resourceId: expected string, go
Error message
Invalid MemoryRequestContext.resourceId: expected string, got ${typeof ctx.resourceId} What it means
parseMemoryRequestContext validates the 'MastraMemory' RequestContext entry; this error is thrown when `resourceId` is present but is not a string. The resourceId identifies the user/owner for memory scoping, so the library enforces a string to keep resource-scoped memory lookups deterministic.
Source
Thrown at packages/core/src/memory/types.ts:169
throw new Error(`Invalid MemoryRequestContext: expected object, got ${typeof memoryContext}`);
}
const ctx = memoryContext as Record<string, unknown>;
// Validate thread if present
if (ctx.thread !== undefined) {
if (typeof ctx.thread !== 'object' || ctx.thread === null) {
throw new Error(`Invalid MemoryRequestContext.thread: expected object, got ${typeof ctx.thread}`);
}
const thread = ctx.thread as Record<string, unknown>;
if (typeof thread.id !== 'string') {
throw new Error(`Invalid MemoryRequestContext.thread.id: expected string, got ${typeof thread.id}`);
}
}
// Validate resourceId if present
if (ctx.resourceId !== undefined && typeof ctx.resourceId !== 'string') {
throw new Error(`Invalid MemoryRequestContext.resourceId: expected string, got ${typeof ctx.resourceId}`);
}
return memoryContext as MemoryRequestContext;
}
export type MessageResponse<T extends 'raw' | 'core_message'> = {
raw: MastraMessageV1[];
core_message: CoreMessage[];
}[T];
type BaseWorkingMemory = {
enabled: boolean;
/**
* Scope for working memory storage.
* - 'resource': Memory persists across all threads for the same resource/user (default)
* - 'thread': Memory is isolated per conversation thread
*
* @default 'resource'View on GitHub (pinned to 75dd419e61)
Solutions
- Set `resourceId` to a plain string in the 'MastraMemory' RequestContext entry: `resourceId: 'user-123'`.
- Coerce at construction time: `resourceId: String(user.id)`.
- If the resource is unknown, omit the key entirely instead of passing null/objects.
- Validate the context shape with parseMemoryRequestContext in your own request setup to catch the mistake at the boundary.
Example fix
// before
requestContext.set('MastraMemory', { thread: { id: 't1' }, resourceId: user.id }); // number
// after
requestContext.set('MastraMemory', { thread: { id: 't1' }, resourceId: String(user.id) }); Defensive patterns
Strategy: validation
Validate before calling
const ctx = requestContext?.get('MastraMemory');
if (ctx && ctx.resourceId !== undefined && typeof ctx.resourceId !== 'string') {
throw new TypeError(`resourceId must be a string, got ${typeof ctx.resourceId}`);
} Type guard
function hasStringResourceId(ctx: unknown): ctx is { resourceId: string } {
return typeof ctx === 'object' && ctx !== null &&
'resourceId' in ctx && typeof (ctx as any).resourceId === 'string';
} Try / catch
try {
const memCtx = parseMemoryRequestContext(requestContext);
// use memCtx.resourceId
} catch (err) {
if (err instanceof Error && err.message.includes('resourceId')) {
logger.error('Malformed memory context: resourceId is not a string', { err });
requestContext.set('MastraMemory', undefined); // or rebuild context correctly
return;
}
throw err;
} Prevention
- Coerce user ids to strings once where they enter your app (String(user.id)).
- Keep resourceId and threadId as plain strings in shared context-building utilities.
- Add a unit test asserting the shape of the 'MastraMemory' RequestContext entry you build.
- Omit resourceId rather than passing null/objects when unknown.
When it happens
Trigger: Any call routed through parseMemoryRequestContext (memoryContext, threadId, memoryRunState, getThreadId, resolveThreadId, memoryConfig) where the 'MastraMemory' context has `resourceId` defined as a non-string — e.g. `resourceId: 12345`, `resourceId: { id: 'u1' }`, or a deserialized object/number.
Common situations: Passing a numeric user id from your database directly as resourceId; passing a user object instead of its id; JSON round-tripping that converted the id; mixing up the resourceId/threadId fields in a hand-built context.
Related errors
- Invalid MemoryRequestContext: expected object, got ${typeof
- Invalid MemoryRequestContext.thread: expected object, got ${
- Invalid MemoryRequestContext.thread.id: expected string, got
- Resource ID is required to list threads
- MastraClient.deleteThread() requires exactly one of agentId
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/1ad4be7e4a3c4980.
Report an issue: GitHub.