can1357/oh-my-pi · error · Error
xAI image edits accept up to ${XAI_MAX_EDIT_IMAGES} referenc
Error message
xAI image edits accept up to ${XAI_MAX_EDIT_IMAGES} reference images; got ${resolvedImages.length}. What it means
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.
Source
Thrown at packages/coding-agent/src/tools/image-gen.ts:1489
if (provider === "xai") {
if (!ctx.modelRegistry) {
throw new Error("Missing modelRegistry for xAI image generation");
}
const xaiCreds = await resolveXAIHttpCredentials(ctx.modelRegistry, resolvedModel);
if (!xaiCreds) {
throw new Error(
"No xAI credentials. Run /login → xAI Grok OAuth (SuperGrok or X Premium+) or set XAI_API_KEY.",
);
}
const prompt = assemblePrompt(params);
const aspectRatio = params.aspect_ratio ?? "1:1";
const xaiResolution = resolveXAIResolution(params.image_size);
const isEdit = resolvedImages.length > 0;
if (isEdit && resolvedImages.length > XAI_MAX_EDIT_IMAGES) {
throw new Error(
`xAI image edits accept up to ${XAI_MAX_EDIT_IMAGES} reference images; got ${resolvedImages.length}.`,
);
}
const xaiBaseBody: XAIImageRequestBase = {
model: resolvedModel,
prompt,
aspect_ratio: aspectRatio,
resolution: xaiResolution,
n: 1,
response_format: "b64_json",
};
const xaiBody: XAIImageRequestBody = isEdit
? buildXAIEditPayload(xaiBaseBody, resolvedImages)
: xaiBaseBody;
const xaiEndpoint = isEdit ? "/images/edits" : "/images/generations";
const xaiKey: ApiKey = ctx.modelRegistry.resolver(xaiCreds.provider, {View on GitHub (pinned to 9690622007)
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.
Example fix
// before images: [img1, img2, img3, img4, img5, img6] // after images: [img1, img2] // within XAI_MAX_EDIT_IMAGES
Defensive patterns
Strategy: validation
Validate before calling
const XAI_MAX_EDIT_IMAGES = 10; // match provider limit
if (isEdit && images.length > XAI_MAX_EDIT_IMAGES) {
images = images.slice(0, XAI_MAX_EDIT_IMAGES); // or split into batches
} Type guard
const withinXaiEditLimit = (n: number, max: number): boolean => n > 0 && n <= max;
Try / catch
try {
await imageGen({ images, ...params });
} catch (err) {
if (err instanceof Error && /reference images/.test(err.message)) {
// retry with first N images or split into multiple calls
} else throw err;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Summary exceeds ${config.summaryHardLimit} bytes
- ${name} exceeds the ${SHARPSHOOTER_MAX_FILE_LINES}-line limi
- ${editUnsupportedProvider} image generation is text-to-image
- Archive ${field} exceeds ${maxPathBytes} bytes
- Archive is too large to read safely
AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31).
Data as JSON: /api/errors/e73e0dd0afc83989.
Report an issue: GitHub.