{"record":{"id":"22004e929bd64d3d","repo":"nexu-io/open-design","slug":"grok-image-resp-status-truncate-text-240","errorCode":null,"errorMessage":"grok image ${resp.status}: ${truncate(text, 240)}","messagePattern":"grok image (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/daemon/src/media/index.ts","lineNumber":1638,"sourceCode":"  const aspectRatio = grokAspectFor(ctx.aspect);\n  const body = {\n    model: ctx.wireModel,\n    prompt: ctx.prompt || 'A high-quality reference image.',\n    n: 1,\n    aspect_ratio: aspectRatio,\n    response_format: 'b64_json',\n  };\n  const resp = await fetch(`${baseUrl}/images/generations`, withMediaRequestInit(ctx, {\n    method: 'POST',\n    headers: {\n      'authorization': `Bearer ${credentials.apiKey}`,\n      'content-type': 'application/json',\n    },\n    body: JSON.stringify(body),\n  }));\n  const text = await resp.text();\n  if (!resp.ok) {\n    throw new Error(`grok image ${resp.status}: ${truncate(text, 240)}`);\n  }\n  let data: any;\n  try {\n    data = JSON.parse(text);\n  } catch {\n    throw new Error(`grok image non-JSON: ${truncate(text, 200)}`);\n  }\n  const entry = data && Array.isArray(data.data) ? data.data[0] : null;\n  if (!entry) throw new Error('grok image response had no data[0]');\n  let bytes;\n  if (entry.b64_json) {\n    bytes = Buffer.from(entry.b64_json, 'base64');\n  } else if (entry.url) {\n    const imgResp = await fetch(entry.url, withMediaRequestInit(ctx));\n    if (!imgResp.ok) throw new Error(`grok image fetch ${imgResp.status}`);\n    bytes = Buffer.from(await imgResp.arrayBuffer());\n  } else {\n    throw new Error('grok image response missing b64_json/url');","sourceCodeStart":1620,"sourceCodeEnd":1656,"githubUrl":"https://github.com/nexu-io/open-design/blob/5be4028344c2eb4c667c5a97bda8f750c5597ef7/apps/daemon/src/media/index.ts#L1620-L1656","documentation":"Thrown when `POST /v1/images/generations` to xAI returns a non-2xx status. The message carries the HTTP status plus up to 240 chars of body, surfacing the xAI error (auth, model access, moderation, invalid `aspect_ratio`).","triggerScenarios":"`resp.ok === false` on the Grok image call. Concretely: 401 from an invalid/expired key or OAuth token, 403 because the Imagine model isn't enabled for the account/tier, 400 from an unsupported `aspect_ratio` (via `grokAspectFor`), or 429 rate limit.","commonSituations":"(1) `XAI_API_KEY` revoked or OAuth token expired; (2) Imagine model not available on the user's xAI tier; (3) aspect ratio not in xAI's supported set; (4) prompt tripped xAI moderation; (5) rate limit on a shared key.","solutions":["Inspect the embedded status+body: 401 → refresh the key/OAuth token; 403 → confirm the Imagine model is enabled for the account tier; 400 → adjust aspect; 429 → back off.","Re-run `hermes auth add xai-oauth` if using OAuth and the token expired.","Verify the `baseUrl` (default `https://api.x.ai/v1`) and that the model name matches a valid xAI image model."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  return await renderGrokImage(ctx, credentials);\n} catch (err) {\n  const msg = err instanceof Error ? err.message : String(err);\n  if (/image 40[13]/.test(msg)) { throw new Error('xAI auth failed — refresh XAI_API_KEY or re-run OAuth'); }\n  if (/permission|forbidden/i.test(msg)) { throw new Error('Grok Imagine model not enabled for this account tier'); }\n  if (/image 429/.test(msg)) { /* back off */ }\n  throw err;\n}","preventionTips":["Refresh the key/OAuth token on 401; xAI OAuth tokens expire.","Confirm the Imagine model is available on the user's xAI tier.","Map aspect ratios only to values xAI accepts via `grokAspectFor`."],"tags":["xai","grok","image","provider-error"],"backgroundTag":null,"analyzedSha":"5be4028344c2eb4c667c5a97bda8f750c5597ef7","analyzedAt":"2026-08-12T12:03:58.812Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}