linshenkx/prompt-optimizer · error · ImageError

GENERATION_FAILED

GENERATION_FAILED

Error message

${errorMessage}

What it means

The Grok adapter's apiCall throws GENERATION_FAILED with an extracted errorMessage when the HTTP response is not ok. It first attempts to parse the error body for a message before falling back to the status text.

Source

Thrown at packages/core/src/services/image/adapters/grok.ts:345

    if (!response.ok) {
      let errorMessage = `Grok API error: ${response.status} ${response.statusText}`
      try {
        const errorData = await response.json()
        if (errorData.error?.message) {
          errorMessage = errorData.error.message
        }
      } catch {
        try {
          const errorText = await response.text()
          if (errorText) {
            errorMessage = `${errorMessage}: ${errorText}`
          }
        } catch {
          // ignore
        }
      }

      throw new ImageError(IMAGE_ERROR_CODES.GENERATION_FAILED, errorMessage)
    }

    return await response.json()
  }
}

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Read the embedded message — it is the API's own error text (e.g. 'Invalid API key')
  2. Verify API key and model ID in ImageModelConfig
  3. Retry with backoff on 429/5xx responses
  4. Check xAI status/limits if errors persist
Defensive patterns

Strategy: try-catch

Validate before calling

if (!config.apiKey) throw new Error('xAI API key required')

Type guard

function isGrokHttpFailure(e: unknown): e is ImageError {
  return e instanceof ImageError && e.code === IMAGE_ERROR_CODES.GENERATION_FAILED
}

Try / catch

try { ... } catch (e) {
  if (isGrokHttpFailure(e)) {
    if (/rate limit|429/i.test(e.message)) return retryWithBackoff(fn)
    throw e
  }
}

Prevention

When it happens

Trigger: Any non-2xx response from xAI's image API during generateImage/generateImageEdit — 401 invalid API key, 404 wrong model or endpoint, 429 rate limit, 5xx server errors. The parsed error body message is embedded in the thrown ImageError.

Common situations: Expired or wrong API key, using a model name not available on xAI, free-tier rate limits, or transient 5xx from xAI infrastructure.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/895060ba206f774e. Report an issue: GitHub.