mastra-ai/mastra · error
Invalid MemoryRequestContext: expected object, got ${typeof
Error message
Invalid MemoryRequestContext: expected object, got ${typeof memoryContext} What it means
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.
Source
Thrown at packages/core/src/memory/types.ts:151
/**
* Parse and validate memory runtime context from RequestContext
* @param requestContext - The RequestContext to extract memory context from
* @returns The validated MemoryRequestContext or null if not available
* @throws Error if the context exists but is malformed
*/
export function parseMemoryRequestContext(requestContext?: RequestContext): MemoryRequestContext | null {
if (!requestContext) {
return null;
}
const memoryContext = requestContext.get('MastraMemory');
if (!memoryContext) {
return null;
}
// Validate the structure
if (typeof memoryContext !== 'object' || memoryContext === null) {
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}`);View on GitHub (pinned to 75dd419e61)
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
Example fix
// before
memoryOptions = { memoryContext: 'thread-123' };
// after
memoryOptions = { memoryContext: { thread: { id: 'thread-123' } } }; Defensive patterns
Strategy: type-guard
Validate before calling
function isValidMemoryContext(v: unknown): boolean {
return v == null || (typeof v === 'object' && !Array.isArray(v));
}
// before assigning:
if (!isValidMemoryContext(rawContext)) throw new TypeError('memoryContext must be an object'); Type guard
function isMemoryRequestContext(v: unknown): v is Record<string, unknown> {
return typeof v === 'object' && v !== null && !Array.isArray(v);
} Try / catch
try {
const ctx = parseMemoryRequestContext(memoryContext);
} catch (err) {
if (err instanceof Error && err.message.startsWith('Invalid MemoryRequestContext:')) {
console.error('memoryContext payload has wrong type:', typeof memoryContext, memoryContext);
return null; // fall back to no-memory-context for this request
}
throw err;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- Invalid MemoryRequestContext.thread: expected object, got ${
- Invalid MemoryRequestContext.thread.id: expected string, got
- Invalid MemoryRequestContext.resourceId: expected string, go
- CursorSDKAgent resumeData.agentId must be a string when prov
- MastraClient.deleteThread() requires exactly one of agentId
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/64c70115635f6118.
Report an issue: GitHub.