linshenkx/prompt-optimizer · error · ImageError

GENERATION_FAILED

GENERATION_FAILED

Error message

DashScope API error: ${data.code}

What it means

Thrown by the DashScope adapter's generateWithQwenImage when the Qwen-Image generation API returns a JSON body containing a top-level code field, which DashScope uses to signal API-level errors (auth failure, quota, invalid parameters, content policy). The message prefers the API's own message and falls back to 'DashScope API error: <code>'.

Source

Thrown at packages/core/src/services/image/adapters/dashscope.ts:315

    if (!response.ok) {
      let errorMessage = `DashScope API error: ${response.status} ${response.statusText}`
      try {
        const errorData = await response.json()
        if (errorData.message) {
          errorMessage = errorData.message
        }
      } catch {
        // 忽略 JSON 解析错误
      }
      throw new ImageError(IMAGE_ERROR_CODES.GENERATION_FAILED, errorMessage)
    }

    const data = await response.json()

    // 检查是否有错误
    if (data.code) {
      throw new ImageError(IMAGE_ERROR_CODES.GENERATION_FAILED, data.message || `DashScope API error: ${data.code}`)
    }

    // 解析 Qwen-Image 响应
    const choices = data.output?.choices || []
    if (choices.length === 0) {
      throw new ImageError(IMAGE_ERROR_CODES.INVALID_RESPONSE_FORMAT)
    }

    // 只取第一张图像
    const choice = choices[0]
    const content = choice.message?.content || []
    const imageContent = content.find((c: any) => c.image)
    if (!imageContent) {
      throw new ImageError(IMAGE_ERROR_CODES.INVALID_RESPONSE_FORMAT)
    }

    return {
      images: [{

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Inspect the error message / data.code to identify the exact DashScope error (auth vs quota vs parameter).
  2. Verify the API key is set, valid, and has image-generation entitlements; rotate if expired.
  3. Check quota/billing on the DashScope console and the exact model id spelling.
  4. If content-policy flagged, rephrase the prompt and retry.

Example fix

// before
await adapter.doGenerate({ model: 'qwen-image', prompt: '...' }) // bad key -> throws

// after
process.env.DASHSCOPE_API_KEY = await vault.get('dashscope')
await adapter.doGenerate({ model: 'qwen-image', prompt: '...' })
Defensive patterns

Strategy: retry

Type guard

const isDashScopeImageError = (e: unknown): e is ImageError =>
  e instanceof ImageError && e.code === IMAGE_ERROR_CODES.GENERATION_FAILED

Try / catch

try {
  await adapter.doGenerate(req)
} catch (e) {
  if (e instanceof ImageError && e.code === IMAGE_ERROR_CODES.GENERATION_FAILED) {
    if (/quota|Throttling/i.test(e.message)) await sleep(backoff).then(retry) // retryable
    else throw e // auth/param errors need user action
  }
}

Prevention

When it happens

Trigger: Calling doGenerate on the DashScope image adapter with an invalid/expired API key (code like InvalidApiKey), exhausted quota, unknown model id, or a policy-flagged prompt; DashScope returning 200 with an error body.

Common situations: Wrong DASHSCOPE_API_KEY or expired key; free-tier quota exhausted; model name typo (e.g. misspelled qwen-image variant); prompts triggering content moderation; DashScope regional endpoint mismatch.

Related errors


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