mastra-ai/mastra · error · HTTPException

Stored response ${body.previous_response_id} was not found

Error message

Stored response ${body.previous_response_id} was not found

What it means

When previous_response_id is supplied, the server looks up the stored response turn record. If no record exists with that ID, prepareCreateResponseRequest throws this 404 — chaining requires a real, previously stored response.

Source

Thrown at packages/server/src/server/handlers/responses.ts:669

      if (!previousResponseTurnRecord) {
        const owningResponseTurnRecord = await findResponseTurnRecordAcrossAgents({
          mastra,
          responseId: body.previous_response_id,
          requestContext,
        });

        if (owningResponseTurnRecord) {
          if (owningResponseTurnRecord.metadata.agentId === body.agent_id) {
            previousResponseTurnRecord = owningResponseTurnRecord;
          } else {
            throw new HTTPException(400, {
              message: `Stored response ${body.previous_response_id} belongs to agent ${owningResponseTurnRecord.metadata.agentId}, not ${body.agent_id}`,
            });
          }
        }

        if (!previousResponseTurnRecord) {
          throw new HTTPException(404, { message: `Stored response ${body.previous_response_id} was not found` });
        }
      }
    } else {
      if (!mastra) {
        throw new HTTPException(500, { message: 'Mastra instance is required for agent-backed responses' });
      }

      previousResponseTurnRecord = await findResponseTurnRecordAcrossAgents({
        mastra,
        responseId: body.previous_response_id,
        requestContext,
      });

      if (!previousResponseTurnRecord) {
        throw new HTTPException(404, { message: `Stored response ${body.previous_response_id} was not found` });
      }
    }
  }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Verify the previous_response_id came from an actual prior response in the same environment/storage
  2. Drop previous_response_id to start a new conversation
  3. Check the server's storage backend contains the response turn record
  4. Fix client caching so ids are taken from the latest stored response, not stale state

Example fix

// before
const r = await client.responses.create({ agent_id: 'a', previous_response_id: 'resp_hardcoded_123', input: 'more' });
// after
const first = await client.responses.create({ agent_id: 'a', input: 'hi' });
const r = await client.responses.create({ agent_id: 'a', previous_response_id: first.id, input: 'more' });
Defensive patterns

Strategy: try-catch

Validate before calling

if (previousResponseId && !(await responseExists(previousResponseId))) throw new Error(`Stored response ${previousResponseId} missing`);

Type guard

null

Try / catch

try {
  return await client.responses.create({ agent_id, previous_response_id, input });
} catch (e) {
  if (e?.status === 404 && /was not found/.test(e.message)) {
    return client.responses.create({ agent_id, input }); // start fresh conversation
  }
  throw e;
}

Prevention

When it happens

Trigger: POST /api/responses with a previous_response_id that was never created, was created with store semantics off, was deleted, or exists in a different database/environment.

Common situations: Replaying old session ids against a fresh DB; typo'd or fabricated response ids; retention/cleanup removing old turns; pointing the client at another environment than where the response was stored.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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