{"record":{"id":"780b267cf589a0e0","repo":"nexu-io/open-design","slug":"openrouter-image-download-imgresp-status","errorCode":null,"errorMessage":"openrouter image download ${imgResp.status}","messagePattern":"openrouter image download (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/daemon/src/media/index.ts","lineNumber":1906,"sourceCode":"  }\n\n  const dataUrl: string | undefined = images[0]?.image_url?.url;\n  if (!dataUrl) {\n    throw new Error(\n      `openrouter image response missing image_url.url: ${truncate(text, 200)}`,\n    );\n  }\n\n  // Strip the data URL prefix (e.g. \"data:image/png;base64,\") and\n  // decode the remaining base64 payload.\n  const b64Match = dataUrl.match(/^data:image\\/[^;]+;base64,(.+)$/s);\n  let bytes: Buffer;\n  if (b64Match) {\n    bytes = Buffer.from(b64Match[1]!, 'base64');\n  } else if (dataUrl.startsWith('http')) {\n    // Some models may return a plain URL instead of inline base64.\n    const imgResp = await fetch(dataUrl, withMediaRequestInit(ctx));\n    if (!imgResp.ok) throw new Error(`openrouter image download ${imgResp.status}`);\n    bytes = Buffer.from(await imgResp.arrayBuffer());\n  } else {\n    // Assume raw base64 without prefix.\n    bytes = Buffer.from(dataUrl, 'base64');\n  }\n\n  return {\n    bytes,\n    providerNote: `openrouter/${wireModel} · ${aspectRatio} · ${bytes.length} bytes`,\n    suggestedExt: sniffImageExt(bytes),\n  };\n}\n\n// ---------------------------------------------------------------------------\n// OpenRouter's video API is a normalised, asynchronous interface sitting\n// in front of multiple upstream providers (ByteDance Seedance 2.0,\n// Google Veo 3.1, Alibaba Wan 2.7, etc.). The workflow mirrors the\n// Grok / Volcengine pattern used elsewhere in this file:","sourceCodeStart":1888,"sourceCodeEnd":1924,"githubUrl":"https://github.com/nexu-io/open-design/blob/5be4028344c2eb4c667c5a97bda8f750c5597ef7/apps/daemon/src/media/index.ts#L1888-L1924","documentation":"Thrown when OpenRouter returned an image as a hosted http(s) URL (rather than an inline base64 data URL) and the secondary fetch of that URL returned non-2xx. The code branches on a `data:image/...;base64,` regex first; only if the URL starts with 'http' does it fetch — so this fires only on the hosted-URL delivery path.","triggerScenarios":"The hosted image URL expired before download, the CDN returned 403/404 because no referer/auth was attached, the host had a transient 5xx, or the URL is behind a rate-limited gateway.","commonSituations":"Slow networks where the signed URL TTL lapses, OpenRouter image-CDN incidents, or corporate proxies stripping required headers off the secondary fetch.","solutions":["Retry the render — hosted-URL expiry is the most common cause.","If reproducible, attach the same authorization/attribution headers to the secondary fetch (currently withMediaRequestInit(ctx) carries ctx-level options).","Check OpenRouter status for CDN incidents when the status is 5xx.","Where possible prefer models/config that return inline base64 to avoid the secondary fetch entirely."],"exampleFix":"// before\nconst imgResp = await fetch(dataUrl, withMediaRequestInit(ctx));\nif (!imgResp.ok) throw new Error(`openrouter image download ${imgResp.status}`);\n\n// after — one retry on 5xx with clearer status\nlet imgResp = await fetch(dataUrl, withMediaRequestInit(ctx));\nif (!imgResp.ok && imgResp.status >= 500) {\n  await sleep(1000);\n  imgResp = await fetch(dataUrl, withMediaRequestInit(ctx));\n}\nif (!imgResp.ok) {\n  throw new Error(`openrouter image download ${imgResp.status} from ${truncate(dataUrl, 80)}`);\n}","handlingStrategy":"retry","validationCode":"// Validate the hosted URL shape and freshness before fetching\nfunction isOpenRouterImageUrl(u: unknown): u is string {\n  return typeof u === 'string' && /^https?:\\/\\//.test(u) && u.length < 8192;\n}","typeGuard":"function isRetryableDownloadStatus(status: number): boolean {\n  return status === 429 || status >= 500;\n}","tryCatchPattern":"let bytes: Buffer | undefined;\nfor (let attempt = 0; attempt < 2; attempt++) {\n  const r = await fetch(dataUrl, withMediaRequestInit(ctx));\n  if (r.ok) { bytes = Buffer.from(await r.arrayBuffer()); break; }\n  if (attempt === 0 && isRetryableDownloadStatus(r.status)) { await sleep(1000); continue; }\n  throw new Error(`openrouter image download ${r.status}`);\n}","preventionTips":["Prefer inline base64 data URLs over hosted URLs to eliminate the secondary fetch.","Fetch hosted URLs immediately — they often carry short TTLs.","Carry auth/attribution headers into the secondary fetch in case the host requires them.","Retry once on 5xx/429 before surfacing."],"tags":["openrouter","image-generation","network","secondary-fetch","provider-api"],"backgroundTag":null,"analyzedSha":"5be4028344c2eb4c667c5a97bda8f750c5597ef7","analyzedAt":"2026-08-12T12:03:58.812Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}