yikart/AiToEarn · error

No image generated

Error message

No image generated

What it means

generateImage inspects the Gemini API response for inline image parts and collects them into an images array. If the response contains zero images (text-only reply, blocked/filtered content, or an unexpected response shape) it logs 'No image generated from Gemini API' and throws a plain Error('No image generated'). This signals the model ran but produced no image data.

Source

Thrown at project/aitoearn-backend/apps/aitoearn-ai/src/core/ai/libs/gemini/gemini.service.ts:98

        },
      })

      const images: GeminiGeneratedImage[] = []

      if (response.candidates?.[0]?.content?.parts) {
        for (const part of response.candidates[0].content.parts) {
          if ('inlineData' in part && part.inlineData) {
            images.push({
              imageData: Buffer.from(part.inlineData.data!, 'base64'),
              mimeType: part.inlineData.mimeType || 'image/png',
            })
          }
        }
      }

      if (images.length === 0) {
        this.logger.error('No image generated from Gemini API')
        throw new Error('No image generated')
      }

      const usage: GeminiImageUsage | undefined = response.usageMetadata
        ? {
            promptTokenCount: response.usageMetadata.promptTokenCount || 0,
            candidatesTokenCount: response.usageMetadata.candidatesTokenCount || 0,
            totalTokenCount: response.usageMetadata.totalTokenCount || 0,
            inputTokenDetails: this.extractGeminiModalityTokenDetails(response.usageMetadata['promptTokensDetails'] || []),
            outputTokenDetails: this.extractGeminiModalityTokenDetails(response.usageMetadata['candidatesTokensDetails'] || []),
          }
        : undefined

      this.logger.debug({
        imageCount: images.length,
        totalSize: images.reduce((sum, img) => sum + img.imageData.length, 0),
        usage,
      }, 'Image generation completed')

View on GitHub (pinned to d3aa8bea5b)

Solutions

  1. Inspect the full Gemini response (candidates, finishReason, promptFeedback.blockReason) to see why no image came back
  2. Rephrase the prompt to avoid safety filter triggers
  3. Confirm the configured model is an image-generation-capable variant (e.g. gemini-*-image-*)
  4. Catch this error upstream and surface a user-facing 'content blocked / no image' message instead of retrying blindly
  5. Pin/verify the SDK and API version so inlineData parts parsing matches the actual response schema

Example fix

// before
const result = await geminiService.generateImage({ prompt: rawUserText })
// after
try {
  const result = await geminiService.generateImage({ prompt: sanitize(rawUserText) })
} catch (e) {
  if (e.message === 'No image generated') {
    throw new BadRequestException('Image generation was blocked or produced no image; try a different prompt')
  }
  throw e
}
Defensive patterns

Strategy: try-catch

Validate before calling

const BLOCKED_HINTS = [/(real )?person/i, /celebrit/i, /brand logo/i]
if (BLOCKED_HINTS.some(r => r.test(prompt))) console.warn('prompt may trigger Gemini safety filter and yield no image')

Type guard

function isNoImageError(e: unknown): e is Error {
  return e instanceof Error && e.message === 'No image generated'
}

Try / catch

try {
  const img = await geminiService.generateImage(req)
} catch (e) {
  if (isNoImageError(e)) {
    throw new BadRequestException('Gemini returned no image (likely safety-blocked or text-only). Adjust the prompt and retry.')
  }
  throw e
}

Prevention

When it happens

Trigger: Gemini returns only text parts (prompt didn't elicit an image), safety filters removed the candidate content, response finishReason indicates a block, or the response structure changed so the inlineData extraction loop matches nothing.

Common situations: Prompts triggering safety blocking (people, brands, violence); using a non-image-capable Gemini model variant; model degraded to text-only output; API version drift changing the parts shape.

Related errors


AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31). Data as JSON: /api/errors/57bb7e4d8434f9b3. Report an issue: GitHub.