{"record":{"id":"64c70115635f6118","repo":"mastra-ai/mastra","slug":"invalid-memoryrequestcontext-expected-object-got","errorCode":null,"errorMessage":"Invalid MemoryRequestContext: expected object, got ${typeof memoryContext}","messagePattern":"Invalid MemoryRequestContext: expected object, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/memory/types.ts","lineNumber":151,"sourceCode":"/**\n * Parse and validate memory runtime context from RequestContext\n * @param requestContext - The RequestContext to extract memory context from\n * @returns The validated MemoryRequestContext or null if not available\n * @throws Error if the context exists but is malformed\n */\nexport function parseMemoryRequestContext(requestContext?: RequestContext): MemoryRequestContext | null {\n  if (!requestContext) {\n    return null;\n  }\n\n  const memoryContext = requestContext.get('MastraMemory');\n  if (!memoryContext) {\n    return null;\n  }\n\n  // Validate the structure\n  if (typeof memoryContext !== 'object' || memoryContext === null) {\n    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}`);","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/memory/types.ts#L133-L169","documentation":"parseMemoryRequestContext validates that a MemoryRequestContext is a plain object before parsing its fields. If a value was passed (truthy) but is not an object — e.g. a string, number, or boolean — this error is thrown. It protects downstream property access (ctx.thread, ctx.resourceId, etc.) from crashing on primitives.","triggerScenarios":"Passing a non-object to the memory context slot of a request: e.g. memoryContext: 'thread-123' instead of { thread: { id: 'thread-123' } }, or memoryContext: 42 / true.","commonSituations":"Serializing/deserializing context across a network boundary and losing object shape (JSON string parsed as string); accidentally passing a thread ID string directly; template-driven code where a variable holding the context object is undefined-replaced with a string; older versions of client SDKs sending different payloads.","solutions":["Pass a proper MemoryRequestContext object: { thread: { id } } or { resourceId }","If you have a serialized string, JSON.parse it before assigning to memoryContext","Check the calling layer (memoryContext getter or request builder) for accidental string coercion","Wrap the call in a try/catch to surface a clearer application-level message while you fix the payload"],"exampleFix":"// before\nmemoryOptions = { memoryContext: 'thread-123' };\n// after\nmemoryOptions = { memoryContext: { thread: { id: 'thread-123' } } };","handlingStrategy":"type-guard","validationCode":"function isValidMemoryContext(v: unknown): boolean {\n  return v == null || (typeof v === 'object' && !Array.isArray(v));\n}\n// before assigning:\nif (!isValidMemoryContext(rawContext)) throw new TypeError('memoryContext must be an object');","typeGuard":"function isMemoryRequestContext(v: unknown): v is Record<string, unknown> {\n  return typeof v === 'object' && v !== null && !Array.isArray(v);\n}","tryCatchPattern":"try {\n  const ctx = parseMemoryRequestContext(memoryContext);\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('Invalid MemoryRequestContext:')) {\n    console.error('memoryContext payload has wrong type:', typeof memoryContext, memoryContext);\n    return null; // fall back to no-memory-context for this request\n  }\n  throw err;\n}","preventionTips":["Never pass a bare string/number as memoryContext; always build { thread: { id } } or { resourceId }","JSON.parse serialized payloads before assigning and confirm typeof === 'object'","Use the MemoryRequestContext TypeScript type at call sites so the compiler catches wrong shapes","Add runtime validation at the boundary where context crosses process/network boundaries","Log typeof the offending value on failure to spot coercion bugs quickly"],"tags":["memory","type-error","validation","request-context"],"backgroundTag":"schema-validation-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}