can1357/oh-my-pi · error · Error

Missing modelRegistry for xAI image generation

Error message

Missing modelRegistry for xAI image generation

What it means

The image-gen tool's xAI branch requires access to the session's model registry to resolve xAI credentials (OAuth or API key). ctx.modelRegistry is optional on the tool context; when absent the tool cannot call resolveXAIHttpCredentials and throws immediately. This is an internal wiring invariant violation, not a user input problem.

Source

Thrown at packages/coding-agent/src/tools/image-gen.ts:1474

						const imagePaths = await saveImagesToTemp(parsed.images);

						return {
							content: [{ type: "text", text: buildResponseSummary(provider, model, imagePaths, responseText) }],
							details: {
								provider,
								model,
								imageCount: parsed.images.length,
								imagePaths,
								images: parsed.images,
								responseText,
								usage: parsed.usage,
							},
						};
					}

					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}.`,
							);
						}

View on GitHub (pinned to 9690622007)

Solutions

  1. Pass a modelRegistry when constructing the tool context that runs image-gen (the same registry the session uses for model resolution).
  2. If embedding the SDK, switch to the standard session/tool-context factory so modelRegistry is populated automatically.
  3. As a workaround, use a non-xAI provider (openai, gemini, openrouter) whose branch does not need the registry, though fixing the context is the real fix.

Example fix

// before
await ctx.tools.run('image-gen', { prompt }, partialCtx);
// after
await ctx.tools.run('image-gen', { prompt }, { ...partialCtx, modelRegistry });
Defensive patterns

Strategy: validation

Validate before calling

if (!ctx.modelRegistry) throw new Error('image-gen requires a modelRegistry in the tool context');
await runImageGen(ctx, params);

Type guard

function hasModelRegistry(ctx: ToolContext): ctx is ToolContext & { modelRegistry: NonNullable<ToolContext['modelRegistry']> } {
  return ctx.modelRegistry != null;
}

Try / catch

try {
  await imageGen(params);
} catch (err) {
  if (err instanceof Error && err.message.includes('Missing modelRegistry')) {
    // rebuild context with modelRegistry and retry once
  } else throw err;
}

Prevention

When it happens

Trigger: Invoking the image generation tool with provider=xai while the ToolContext was constructed without a modelRegistry (e.g. SDK embedding, tests, or a stripped-down harness that omits it), reaching packages/coding-agent/src/tools/image-gen.ts:1474.

Common situations: Embedding the coding agent SDK and building a custom tool context; running image generation in a test fixture; a regression where the TUI/RPC path stopped passing modelRegistry into tool context.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/aac621c24781e15e. Report an issue: GitHub.