mastra-ai/mastra · error · HTTPException

Memory storage was not available while storing the response

Error message

Memory storage was not available while storing the response

What it means

This 500 is thrown by persistResponseTurnRecord when it is asked to persist a completed response turn's messages but no memory storage backend was resolved for the target agent. The Responses API stores every turn's messages in the agent's memory store; if the store handle is missing at persistence time, the server cannot save the turn and aborts with this error.

Source

Thrown at packages/server/src/server/handlers/responses.storage.ts:791

 * The response id becomes that assistant message id, and the response-specific
 * metadata is written onto the assistant message so later retrieval can rebuild
 * the Responses object from thread-backed storage.
 */
export async function persistResponseTurnRecord({
  memoryStore,
  responseId,
  metadata,
  threadContext,
  messages,
}: {
  memoryStore: MemoryStorage | null;
  responseId: string;
  metadata: ResponseTurnRecordMetadata;
  threadContext: ThreadExecutionContext;
  messages: MastraDBMessage[];
}): Promise<void> {
  if (!memoryStore) {
    throw new HTTPException(500, { message: 'Memory storage was not available while storing the response' });
  }

  const normalizedMessages: MastraDBMessage[] = messages.map(message => ({
    ...message,
    threadId: message.threadId ?? threadContext.threadId,
    resourceId: message.resourceId ?? threadContext.resourceId,
  }));

  const lastAssistantIndex = [...normalizedMessages].map(message => message.role).lastIndexOf('assistant');
  const responseAnchorIndex =
    [...normalizedMessages]
      .map((message, index) => ({ index, message }))
      .reverse()
      .find(({ message }) => message.role === 'assistant' && hasTextPart(message))?.index ?? lastAssistantIndex;
  const lastAssistantMessage =
    responseAnchorIndex >= 0
      ? {
          ...normalizedMessages[responseAnchorIndex]!,

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Configure memory on the agent (new Memory({ storage }) passed to the Agent) so a memory store exists before calling the Responses endpoint
  2. Verify the storage adapter (e.g., MastraStorage implementation) is correctly constructed and reachable in the server environment
  3. Check server logs for earlier storage initialization failures; fix the underlying storage connection error
  4. If the request should not persist, avoid triggering the storing path (store:false semantics) rather than relying on a missing store

Example fix

// before
export const agent = new Agent({ name: 'a', instructions: '...', model: 'openai/gpt-4o' });
// after
export const agent = new Agent({
  name: 'a',
  instructions: '...',
  model: 'openai/gpt-4o',
  memory: new Memory({ storage: new LibSQLStore({ url: process.env.DATABASE_URL }) }),
});
Defensive patterns

Strategy: try-catch

Validate before calling

const memory = await agent.getMemory({ requestContext });
if (!memory) throw new Error('Agent must have memory configured before calling responses with store enabled');

Type guard

function hasMemory(agent) { return typeof agent.getMemory === 'function'; }

Try / catch

try {
  await storeCompletedResponse(...);
} catch (e) {
  if (e instanceof HTTPException && e.status === 500 && /Memory storage was not available/.test(e.message)) {
    // surface config error: agent lacks memory storage; do not retry
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling POST /api/responses (via storeCompletedResponse) on an agent whose memory storage is unavailable/failed to initialize, or an internal path where resolveAgentMemoryStore passed but the memoryStore argument became undefined/null by the time persistResponseTurnRecord ran.

Common situations: Agent configured without memory but with store:true semantics; storage adapter misconfigured or failing to connect (e.g., bad DB URL); deployment where the memory package/adapter is missing; race between agent resolution and storage teardown.

Related errors


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