{"record":{"id":"cfe0b4ec319703e3","repo":"mastra-ai/mastra","slug":"memory-storage-was-not-available-while-storing-the","errorCode":null,"errorMessage":"Memory storage was not available while storing the response","messagePattern":"Memory storage was not available while storing the response","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"packages/server/src/server/handlers/responses.storage.ts","lineNumber":791,"sourceCode":" * The response id becomes that assistant message id, and the response-specific\n * metadata is written onto the assistant message so later retrieval can rebuild\n * the Responses object from thread-backed storage.\n */\nexport async function persistResponseTurnRecord({\n  memoryStore,\n  responseId,\n  metadata,\n  threadContext,\n  messages,\n}: {\n  memoryStore: MemoryStorage | null;\n  responseId: string;\n  metadata: ResponseTurnRecordMetadata;\n  threadContext: ThreadExecutionContext;\n  messages: MastraDBMessage[];\n}): Promise<void> {\n  if (!memoryStore) {\n    throw new HTTPException(500, { message: 'Memory storage was not available while storing the response' });\n  }\n\n  const normalizedMessages: MastraDBMessage[] = messages.map(message => ({\n    ...message,\n    threadId: message.threadId ?? threadContext.threadId,\n    resourceId: message.resourceId ?? threadContext.resourceId,\n  }));\n\n  const lastAssistantIndex = [...normalizedMessages].map(message => message.role).lastIndexOf('assistant');\n  const responseAnchorIndex =\n    [...normalizedMessages]\n      .map((message, index) => ({ index, message }))\n      .reverse()\n      .find(({ message }) => message.role === 'assistant' && hasTextPart(message))?.index ?? lastAssistantIndex;\n  const lastAssistantMessage =\n    responseAnchorIndex >= 0\n      ? {\n          ...normalizedMessages[responseAnchorIndex]!,","sourceCodeStart":773,"sourceCodeEnd":809,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/responses.storage.ts#L773-L809","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Configure memory on the agent (new Memory({ storage }) passed to the Agent) so a memory store exists before calling the Responses endpoint","Verify the storage adapter (e.g., MastraStorage implementation) is correctly constructed and reachable in the server environment","Check server logs for earlier storage initialization failures; fix the underlying storage connection error","If the request should not persist, avoid triggering the storing path (store:false semantics) rather than relying on a missing store"],"exampleFix":"// before\nexport const agent = new Agent({ name: 'a', instructions: '...', model: 'openai/gpt-4o' });\n// after\nexport const agent = new Agent({\n  name: 'a',\n  instructions: '...',\n  model: 'openai/gpt-4o',\n  memory: new Memory({ storage: new LibSQLStore({ url: process.env.DATABASE_URL }) }),\n});","handlingStrategy":"try-catch","validationCode":"const memory = await agent.getMemory({ requestContext });\nif (!memory) throw new Error('Agent must have memory configured before calling responses with store enabled');","typeGuard":"function hasMemory(agent) { return typeof agent.getMemory === 'function'; }","tryCatchPattern":"try {\n  await storeCompletedResponse(...);\n} catch (e) {\n  if (e instanceof HTTPException && e.status === 500 && /Memory storage was not available/.test(e.message)) {\n    // surface config error: agent lacks memory storage; do not retry\n  }\n  throw e;\n}","preventionTips":["Always configure new Memory({ storage }) on agents used with the Responses API","Health-check the storage adapter at server startup","Monitor for prior storage connection errors in logs"],"tags":["server","storage","memory","http-500"],"backgroundTag":"memory-storage-unavailable","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}