{"record":{"id":"09208a68b9e17102","repo":"mastra-ai/mastra","slug":"invalid-memoryrequestcontext-thread-expected-obje","errorCode":null,"errorMessage":"Invalid MemoryRequestContext.thread: expected object, got ${typeof ctx.thread}","messagePattern":"Invalid MemoryRequestContext\\.thread: expected object, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/memory/types.ts","lineNumber":159,"sourceCode":"    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}`);\n  }\n\n  return memoryContext as MemoryRequestContext;\n}\n\nexport type MessageResponse<T extends 'raw' | 'core_message'> = {\n  raw: MastraMessageV1[];\n  core_message: CoreMessage[];","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/memory/types.ts#L141-L177","documentation":"parseMemoryRequestContext validates that ctx.thread, when present, is an object with a string id. If ctx.thread exists but is not an object (e.g. a string or number), this error is thrown. It is a distinct check from error 1443: the top-level context is a valid object, but its 'thread' field has the wrong type.","triggerScenarios":"Passing { thread: 'thread-123' } or { thread: 42 } instead of { thread: { id: 'thread-123' } } in memory request context; also a null thread field reaches this branch since it is neither undefined nor an object.","commonSituations":"Migrating from APIs that accepted a bare thread ID string in the thread field; hand-built request payloads in tests; backend proxies reshaping payloads incorrectly; confusion between thread (object) and threadId (string) field names across Mastra versions.","solutions":["Wrap the thread ID in an object: { thread: { id: 'thread-123' } }","If you only have a thread ID, use the threadId field of the request context instead of nesting a string under thread","Fix payload construction upstream (server proxy, SDK version mismatch) so thread arrives as an object","Validate with the type guard below before assigning to memoryContext"],"exampleFix":"// before\nmemoryContext: { thread: 'thread-123' }\n// after\nmemoryContext: { thread: { id: 'thread-123' } }","handlingStrategy":"type-guard","validationCode":"function isValidThreadField(t: unknown): boolean {\n  return t === undefined || (typeof t === 'object' && t !== null && typeof (t as { id?: unknown }).id === 'string');\n}\n// before assigning:\nif (!isValidThreadField(rawContext?.thread)) throw new TypeError('thread must be an object with string id');","typeGuard":"function isThreadRef(v: unknown): v is { id: string } {\n  return typeof v === 'object' && v !== null && typeof (v as { id?: unknown }).id === 'string';\n}","tryCatchPattern":"try {\n  const ctx = parseMemoryRequestContext(memoryContext);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('.thread:')) {\n    // normalize legacy bare-string thread IDs\n    if (typeof (memoryContext as { thread?: unknown })?.thread === 'string') {\n      memoryContext = { ...memoryContext, thread: { id: (memoryContext as { thread: string }).thread } };\n      return parseMemoryRequestContext(memoryContext);\n    }\n    throw new TypeError('memoryContext.thread must be { id: string }');\n  }\n  throw err;\n}","preventionTips":["Always nest thread IDs as { thread: { id } }, never as a raw string","Distinguish the thread object field from the flat threadId field and use each consistently","Type request builders with the MemoryRequestContext type to catch wrong nesting at compile time","Normalize legacy payloads (bare thread ID strings) at your API edge before passing to Mastra","Add a unit test that round-trips your context serialization to ensure the thread field shape survives"],"tags":["memory","type-error","validation","request-context","nested-field"],"backgroundTag":"schema-validation-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}