Significant-Gravitas/AutoGPT · error · Error

Failed to fetch session (status: ${response.status})

Error message

Failed to fetch session (status: ${response.status})

What it means

Thrown while paginating a copilot chat session's messages for markdown export (exportChatAsMarkdown.ts). The code loops up to EXPORT_MAX_PAGES calling fetchSession(id, {limit, before_sequence}) and throws if any page returns a status other than 200. Because the generated fetchSession client resolves (rather than throws) on non-2xx, this explicit status check is the only thing that surfaces backend failures during export.

Source

Thrown at autogpt_platform/frontend/src/app/(platform)/copilot/helpers/exportChatAsMarkdown.ts:101

export async function fetchAndExportChat(
  id: string,
  title: string | null | undefined,
  fetchSession: typeof import("@/app/api/__generated__/endpoints/chat/chat").getV2GetSession,
): Promise<void> {
  const allMessages: SessionChatMessage[] = [];
  let beforeSequence: number | undefined = undefined;
  let truncated = false;

  for (let page = 0; page < EXPORT_MAX_PAGES; page++) {
    const opts: { limit: number; before_sequence?: number } = {
      limit: EXPORT_PAGE_SIZE,
    };
    if (beforeSequence !== undefined) opts.before_sequence = beforeSequence;

    const response = await fetchSession(id, opts);
    if (response.status !== 200) {
      throw new Error(`Failed to fetch session (status: ${response.status})`);
    }

    const pageMessages = (response.data.messages ??
      []) as unknown as SessionChatMessage[];
    allMessages.unshift(...pageMessages);

    const hasMore = !!response.data.has_more_messages;
    const oldestSeq = response.data.oldest_sequence;
    if (!hasMore || oldestSeq == null) break;
    if (page === EXPORT_MAX_PAGES - 1) {
      truncated = true;
      break;
    }
    beforeSequence = oldestSeq;
  }

  if (truncated) {
    throw new Error(

View on GitHub (pinned to 9c8bb5550f)

Solutions

  1. Re-authenticate (sign out/in) and retry the export if the status was 401/403.
  2. Check the network response body for the failing GET session request — FastAPI's detail field says whether it's 404 gone or 422 bad cursor.
  3. Retry after the backend is healthy if it was a transient 5xx; the export is read-only and idempotent.
  4. If it recurs on one specific chat, that session's message history may be inconsistent — fetch it in the copilot UI to confirm, and report the session ID.
  5. For code hardening, include response.status in the thrown message (already done) and surface response.data?.detail too.

Example fix

// before
if (response.status !== 200) {
  throw new Error(`Failed to fetch session (status: ${response.status})`);
}

// after (retry once on 5xx, surface backend detail)
if (response.status >= 500 && page === 0) { /* single retry */ }
if (response.status !== 200) {
  const detail = (response.data as any)?.detail;
  throw new Error(
    `Failed to fetch session (status: ${response.status}${detail ? `: ${detail}` : ""})`,
  );
}
Defensive patterns

Strategy: retry

Validate before calling

function canExport(sessionId: string): boolean {
  return typeof sessionId === "string" && sessionId.length > 0;
}

Type guard

function isSessionFetchFailure(err: unknown): err is Error {
  return err instanceof Error && err.message.startsWith("Failed to fetch session");
}

Try / catch

try {
  await exportSession(id);
} catch (error) {
  if (isSessionFetchFailure(error)) {
    // parse embedded status; 401 -> re-auth, 5xx -> retry once, else report
    toast({ title: "Export failed", description: error.message, variant: "destructive" });
  }
}

Prevention

When it happens

Trigger: GET /copilot/sessions/{id} returning 404 (session deleted or owned by another user), 401/403 (expired token — note fetchSession here is called without getCopilotAuthHeaders context if auth lapsed), 422 (bad before_sequence after messages were pruned mid-export), or 5xx from the backend while the user clicks Export.

Common situations: Exporting a chat that was just deleted in another tab; session expired between opening the chat and clicking export; backend restart/upgrade mid-pagination so sequence numbers shift; pagination cursor (oldest_sequence) pointing at trimmed messages in very long chats.

Related errors


AI-assisted analysis of Significant-Gravitas/AutoGPT@9c8bb5550f (2026-08-14). Data as JSON: /api/errors/cbec60e0e0d27ba2. Report an issue: GitHub.