mastra-ai/mastra · error · HTTPException

Memory is not initialized

Error message

Memory is not initialized

What it means

GET /memory/threads exhausted every source of thread data: the agent's memory was absent, and the Mastra-level storage fallback also had no storage instance or no memory store. The endpoint cannot list threads without at least one of these, so it throws this final 400.

Source

Thrown at packages/server/src/server/handlers/memory.ts:973

          );
          if (!shouldFilterThreadsWithFGA(mastra, requestContext)) {
            return result;
          }

          const accessibleThreads = await filterAccessibleThreads({
            mastra,
            requestContext,
            threads: result.threads,
          });
          return paginateThreads({
            threads: accessibleThreads,
            page,
            perPage,
          });
        }
      }

      throw new HTTPException(400, { message: 'Memory is not initialized' });
    } catch (error) {
      return handleError(error, 'Error listing threads');
    }
  },
});

export const GET_THREAD_BY_ID_ROUTE = createRoute({
  method: 'GET',
  path: '/memory/threads/:threadId',
  responseType: 'json',
  pathParamSchema: threadIdPathParams,
  queryParamSchema: getThreadByIdQuerySchema,
  responseSchema: getThreadByIdResponseSchema,
  summary: 'Get thread by ID',
  description: 'Returns details for a specific conversation thread',
  tags: ['Memory'],
  requiresAuth: true,
  handler: async ({ mastra, agentId, threadId, resourceId, requestContext }) => {

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Configure storage on the Mastra instance: new Mastra({ ..., storage: new LibSQLStore({ url: ... }) }).
  2. Or attach a Memory (with storage) to the agent being queried.
  3. Verify env vars/DB availability in the target environment so storage initializes.
  4. Client-side: catch the 400 and show an empty threads list / config-needed state.

Example fix

// before
export const mastra = new Mastra({ agents });
// after
export const mastra = new Mastra({ agents, storage: new LibSQLStore({ url: 'file:./mastra.db' }) });
Defensive patterns

Strategy: validation

Validate before calling

// before listing threads, confirm storage exists on the Mastra instance
if (!mastra.getStorage?.()) {
  throw new Error('Mastra has no storage configured; /memory/threads will 400');
}

Type guard

function mastraHasStorage(m: unknown): boolean {
  return !!m && typeof (m as any).getStorage === 'function' && !!(m as any).getStorage();
}

Try / catch

try {
  const res = await fetch('/api/memory/threads?resourceId=' + resourceId);
  if (!res.ok) {
    const body = await res.json().catch(() => null);
    if (body?.message === 'Memory is not initialized') {
      return { threads: [], page: 0, total: 0, hasMore: false }; // degraded UI state
    }
  }
  return await res.json();
} catch (e) {
  console.error('listThreads failed', e);
  return { threads: [], page: 0, total: 0, hasMore: false };
}

Prevention

When it happens

Trigger: GET /api/memory/threads when the Mastra instance has no storage configured and the agent has no memory; getMemoryFromContext (with allowMissingAgent) returns undefined AND getStorageFromContext returns undefined or a store-less storage.

Common situations: Minimal Mastra setups (`new Mastra({ agents })`) with no storage key; agents configured without memory; DB not configured in the deployed environment; listing the threads panel before any storage is wired up.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/474fdc9a0f29a5ab. Report an issue: GitHub.