Significant-Gravitas/AutoGPT · error · Error
Unexpected n8n API response format
Error message
Unexpected n8n API response format
What it means
Thrown by fetchN8nWorkflow after JSON.parse succeeds but the unwrapped object has no `nodes` array. The code tries several envelope shapes (data.workflow, data itself, template.workflow, template) because the n8n templates API has changed its response shape over time; if none of them yield a workflow with a `nodes` array, the response is considered unparseable. A JSON.parse failure would throw a SyntaxError instead — this error specifically means valid JSON, unexpected shape.
Source
Thrown at autogpt_platform/frontend/src/app/(platform)/library/components/LibraryImportDialog/components/ExternalWorkflowTab/fetchWorkflowFromUrl.ts:82
const safeId = parseInt(templateId, 10);
if (!Number.isFinite(safeId) || safeId <= 0) {
throw new Error("Invalid template ID");
}
const res = await fetch(`${N8N_TEMPLATES_API}/${safeId.toString()}`);
if (!res.ok) throw new Error(`n8n template not found (${res.status})`);
const contentLength = res.headers.get("content-length");
if (contentLength && parseInt(contentLength, 10) > MAX_RESPONSE_BYTES) {
throw new Error("Response too large.");
}
const text = await res.text();
if (text.length > MAX_RESPONSE_BYTES) throw new Error("Response too large.");
const data = JSON.parse(text);
const template = data?.workflow ?? data;
const workflow = template?.workflow ?? template;
if (!workflow?.nodes) throw new Error("Unexpected n8n API response format");
if (!workflow.name) workflow.name = template?.name ?? data?.name ?? "";
return JSON.stringify(workflow);
}
View on GitHub (pinned to 9c8bb5550f)
Solutions
- Log/inspect the actual JSON returned by the N8N_TEMPLATES_API URL in DevTools to see the new envelope shape.
- If the shape changed upstream, extend the unwrap chain in fetchN8nWorkflow (add the new path) — this is exactly why the chain exists.
- Verify the pasted ID is a workflow template, not a collection/credential ID.
- Retry later / bypass proxies if the payload was rewritten in transit.
Example fix
// before const template = data?.workflow ?? data; const workflow = template?.workflow ?? template; // after (extend envelope chain after confirming new shape) const template = data?.workflow ?? data?.data ?? data; const workflow = template?.workflow ?? template;
Defensive patterns
Strategy: try-catch
Validate before calling
function hasNodes(workflow: unknown): workflow is { nodes: unknown[]; name?: string } {
return (
typeof workflow === "object" && workflow !== null &&
Array.isArray((workflow as any).nodes)
);
} Type guard
function isN8nWorkflow(data: unknown): data is { nodes: unknown[]; name?: string } {
return hasNodes(data) || hasNodes((data as any)?.workflow);
} Try / catch
try {
const wf = await fetchN8nWorkflow(id);
} catch (error) {
if (error instanceof Error && error.message === "Unexpected n8n API response format") {
// upstream shape changed — inspect the raw response and extend the unwrap chain
}
} Prevention
- Type-guard the unwrapped object (nodes is an array) before trusting it, as shown.
- Keep the envelope-unwrap chain in one place so an upstream shape change is a one-line fix.
- Separate JSON-parse failures (SyntaxError) from shape failures in error handling — they mean different things.
When it happens
Trigger: n8n API returns a redirect/HTML-wrapped payload (proxy interference), an error envelope like {code:..., message:...} with 200 status, or a new API version that nests the workflow differently than the tried paths.
Common situations: n8n ships an API change moving/renaming the workflow field; an intermediary (corporate proxy, CDN) rewriting the response; requesting a non-workflow resource ID that returns metadata-only JSON with 200.
Related errors
- n8n template not found (${res.status})
- Response too large.
- Authentication failed — please sign in again.
- Failed to fetch session (status: ${response.status})
- Invalid form
AI-assisted analysis of Significant-Gravitas/AutoGPT@9c8bb5550f (2026-08-14).
Data as JSON: /api/errors/5cc1726848472124.
Report an issue: GitHub.