{"record":{"id":"1ad4be7e4a3c4980","repo":"mastra-ai/mastra","slug":"invalid-memoryrequestcontext-resourceid-expected","errorCode":null,"errorMessage":"Invalid MemoryRequestContext.resourceId: expected string, got ${typeof ctx.resourceId}","messagePattern":"Invalid MemoryRequestContext\\.resourceId: expected string, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/memory/types.ts","lineNumber":169,"sourceCode":"    throw new Error(`Invalid MemoryRequestContext: expected object, got ${typeof memoryContext}`);\n  }\n\n  const ctx = memoryContext as Record<string, unknown>;\n\n  // Validate thread if present\n  if (ctx.thread !== undefined) {\n    if (typeof ctx.thread !== 'object' || ctx.thread === null) {\n      throw new Error(`Invalid MemoryRequestContext.thread: expected object, got ${typeof ctx.thread}`);\n    }\n    const thread = ctx.thread as Record<string, unknown>;\n    if (typeof thread.id !== 'string') {\n      throw new Error(`Invalid MemoryRequestContext.thread.id: expected string, got ${typeof thread.id}`);\n    }\n  }\n\n  // Validate resourceId if present\n  if (ctx.resourceId !== undefined && typeof ctx.resourceId !== 'string') {\n    throw new Error(`Invalid MemoryRequestContext.resourceId: expected string, got ${typeof ctx.resourceId}`);\n  }\n\n  return memoryContext as MemoryRequestContext;\n}\n\nexport type MessageResponse<T extends 'raw' | 'core_message'> = {\n  raw: MastraMessageV1[];\n  core_message: CoreMessage[];\n}[T];\n\ntype BaseWorkingMemory = {\n  enabled: boolean;\n  /**\n   * Scope for working memory storage.\n   * - 'resource': Memory persists across all threads for the same resource/user (default)\n   * - 'thread': Memory is isolated per conversation thread\n   *\n   * @default 'resource'","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/memory/types.ts#L151-L187","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nrequestContext.set('MastraMemory', { thread: { id: 't1' }, resourceId: user.id }); // number\n// after\nrequestContext.set('MastraMemory', { thread: { id: 't1' }, resourceId: String(user.id) });","handlingStrategy":"validation","validationCode":"const ctx = requestContext?.get('MastraMemory');\nif (ctx && ctx.resourceId !== undefined && typeof ctx.resourceId !== 'string') {\n  throw new TypeError(`resourceId must be a string, got ${typeof ctx.resourceId}`);\n}","typeGuard":"function hasStringResourceId(ctx: unknown): ctx is { resourceId: string } {\n  return typeof ctx === 'object' && ctx !== null &&\n    'resourceId' in ctx && typeof (ctx as any).resourceId === 'string';\n}","tryCatchPattern":"try {\n  const memCtx = parseMemoryRequestContext(requestContext);\n  // use memCtx.resourceId\n} catch (err) {\n  if (err instanceof Error && err.message.includes('resourceId')) {\n    logger.error('Malformed memory context: resourceId is not a string', { err });\n    requestContext.set('MastraMemory', undefined); // or rebuild context correctly\n    return;\n  }\n  throw err;\n}","preventionTips":["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."],"tags":["memory","validation","request-context","resource-id"],"backgroundTag":"invalid-memory-request-context","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}