{"record":{"id":"e73e0dd0afc83989","repo":"can1357/oh-my-pi","slug":"xai-image-edits-accept-up-to-xai-max-edit-images","errorCode":null,"errorMessage":"xAI image edits accept up to ${XAI_MAX_EDIT_IMAGES} reference images; got ${resolvedImages.length}.","messagePattern":"xAI image edits accept up to (.+?) reference images; got (.+?)\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/tools/image-gen.ts","lineNumber":1489,"sourceCode":"\n\t\t\t\t\tif (provider === \"xai\") {\n\t\t\t\t\t\tif (!ctx.modelRegistry) {\n\t\t\t\t\t\t\tthrow new Error(\"Missing modelRegistry for xAI image generation\");\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst xaiCreds = await resolveXAIHttpCredentials(ctx.modelRegistry, resolvedModel);\n\t\t\t\t\t\tif (!xaiCreds) {\n\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t\"No xAI credentials. Run /login → xAI Grok OAuth (SuperGrok or X Premium+) or set XAI_API_KEY.\",\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst prompt = assemblePrompt(params);\n\t\t\t\t\t\tconst aspectRatio = params.aspect_ratio ?? \"1:1\";\n\t\t\t\t\t\tconst xaiResolution = resolveXAIResolution(params.image_size);\n\n\t\t\t\t\t\tconst isEdit = resolvedImages.length > 0;\n\t\t\t\t\t\tif (isEdit && resolvedImages.length > XAI_MAX_EDIT_IMAGES) {\n\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t`xAI image edits accept up to ${XAI_MAX_EDIT_IMAGES} reference images; got ${resolvedImages.length}.`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst xaiBaseBody: XAIImageRequestBase = {\n\t\t\t\t\t\t\tmodel: resolvedModel,\n\t\t\t\t\t\t\tprompt,\n\t\t\t\t\t\t\taspect_ratio: aspectRatio,\n\t\t\t\t\t\t\tresolution: xaiResolution,\n\t\t\t\t\t\t\tn: 1,\n\t\t\t\t\t\t\tresponse_format: \"b64_json\",\n\t\t\t\t\t\t};\n\t\t\t\t\t\tconst xaiBody: XAIImageRequestBody = isEdit\n\t\t\t\t\t\t\t? buildXAIEditPayload(xaiBaseBody, resolvedImages)\n\t\t\t\t\t\t\t: xaiBaseBody;\n\t\t\t\t\t\tconst xaiEndpoint = isEdit ? \"/images/edits\" : \"/images/generations\";\n\n\t\t\t\t\t\tconst xaiKey: ApiKey = ctx.modelRegistry.resolver(xaiCreds.provider, {","sourceCodeStart":1471,"sourceCodeEnd":1507,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/tools/image-gen.ts#L1471-L1507","documentation":"xAI's image edit endpoint accepts at most XAI_MAX_EDIT_IMAGES reference images. When the edit request (input images present) exceeds that constant, the tool rejects the call before making any network request. It is a client-side pre-validation of the provider's documented limit.","triggerScenarios":"Calling image-gen with edit mode (one or more input images) where resolvedImages.length > XAI_MAX_EDIT_IMAGES, e.g. attaching too many reference images in a single request, at image-gen.ts:1489.","commonSituations":"Passing a whole folder of reference images to compose into one edit; piping multiple prior tool outputs as inputs; confusing per-request limits with per-conversation attachment limits.","solutions":["Reduce the number of input images to XAI_MAX_EDIT_IMAGES or fewer before retrying.","Split the work into multiple sequential edit calls, each within the limit.","Use a provider with a higher reference-image cap (e.g. gemini/openrouter) for this request.","Keep only the most relevant reference images; providers weight the first images more anyway."],"exampleFix":"// before\nimages: [img1, img2, img3, img4, img5, img6]\n// after\nimages: [img1, img2] // within XAI_MAX_EDIT_IMAGES","handlingStrategy":"validation","validationCode":"const XAI_MAX_EDIT_IMAGES = 10; // match provider limit\nif (isEdit && images.length > XAI_MAX_EDIT_IMAGES) {\n  images = images.slice(0, XAI_MAX_EDIT_IMAGES); // or split into batches\n}","typeGuard":"const withinXaiEditLimit = (n: number, max: number): boolean => n > 0 && n <= max;","tryCatchPattern":"try {\n  await imageGen({ images, ...params });\n} catch (err) {\n  if (err instanceof Error && /reference images/.test(err.message)) {\n    // retry with first N images or split into multiple calls\n  } else throw err;\n}","preventionTips":["Cap reference images client-side before every xAI edit call","Batch large reference sets into sequential edit requests","Check provider docs when changing image-edit workflows"],"tags":["validation","limits","xai","image-edit"],"backgroundTag":"too-many-items","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}