Skip to content

fix: accept Gemini candidates without generated content - #57

Merged
jeffpaul merged 1 commit into
WordPress:trunkfrom
JDeepD:fix/23-empty-candidate-content
Nov 30, 2026
Merged

jeffpaul merged 1 commit into
WordPress:trunkfrom
JDeepD:fix/23-empty-candidate-content

Conversation

@JDeepD

@JDeepD JDeepD commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Gemini can return a candidate with no content, an empty content object, or content without parts when nothing was generated, for example when thinking uses up the token limit or an image model returns NO_IMAGE. The parser rejected these with "Missing the candidates[0].content key", which broke image generation in the AI plugin.

Such candidates are now parsed into an empty model message and the finish reason explains the outcome. Newer finish reasons are mapped (CONTINUATION to length, IMAGE_PROHIBITED_CONTENT, IMAGE_RECITATION and ESCALATION to content_filter), and any other value maps to error instead of throwing.

Fixes #23

@jeffpaul jeffpaul added this to the 1.4.0 milestone Nov 16, 2026
@jeffpaul

Copy link
Copy Markdown
Member

@JDeepD is this PR still in progress or can it come out of draft status for review?

Gemini can return a candidate with no content, an empty content object,
or content without parts when nothing was generated, for example when
thinking uses up the token limit or an image model returns NO_IMAGE.
The parser rejected these with "Missing the candidates[0].content key",
which broke image generation in the AI plugin.

Such candidates are now parsed into an empty model message and the
finish reason explains the outcome. Newer finish reasons are mapped
(CONTINUATION to length, IMAGE_PROHIBITED_CONTENT, IMAGE_RECITATION and
ESCALATION to content_filter), and any other value maps to error instead
of throwing.

Fixes WordPress#23
@JDeepD
JDeepD force-pushed the fix/23-empty-candidate-content branch from 109265e to 545a7ab Compare November 26, 2026 06:14
@JDeepD
JDeepD marked this pull request as ready for review November 26, 2026 06:31
@JDeepD
JDeepD requested a review from a team November 26, 2026 06:31
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: JDeepD <jdeep@git.wordpress.org>
Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org>
Co-authored-by: akshatkedia <akshatkedia@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@JDeepD

JDeepD commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@jeffpaul I've tested this with a free Gemini API key, which only has access to the text generation models. It fixes the Missing the candidates[0].content key error from #23 for text models, but I couldn't test image models because they need API credits.

I've marked this ready for review. Since #23 is about image generation, it would be great if @akshatkedia could confirm the fix with image models.

Steps to deterministically reproduce the issue:

  1. Add this as an MU plugin, e.g. wp-content/mu-plugins/gemini-max-tokens.php. It caps the image request at 16 output tokens, which isn't enough for an image. When Gemini runs out of output tokens before generating anything, it sends back a candidate with no content, so this should trigger the error on every request.
<?php
add_filter( 'wpai_image_generation_prompt_builder', static fn( $builder ) => $builder->using_max_tokens( 16 ) );
  1. On Trunk, Go to Media > Generate Image and generate any image. It should fail with Unexpected Google API response: Missing the "candidates[0].content" key.

  2. Switch the plugin to this PR's branch and generate again. The parse error should be gone and it should display "No image data was generated." instead.

  3. Remove the MU plugin snippet and generate a few images with your usual prompts. They should generate as expected.

@JDeepD

JDeepD commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

I've set up two WordPress Playground sites to make this easier to test. Both have the AI plugin with Image Generation and Title Generation turned on, plus two MU snippets that cap requests at 16 output tokens. Title Generation also uses gemini-3-flash-preview, which reliably returns an empty candidate at that limit.

  1. Released 1.3.0
  2. This PR branch

Steps to reproduce:

  1. In each one, add a temporary Google API key (revoke after testing) under Settings > Connectors.
  2. Text Generation: Create a post with at least 250 characters of content and click Generate above the title.
  1. Image Generation: Go to Media > Generate Image and generate anything. 1.3.0 should show the same parse error, and this PR should show "No image data was generated." instead.

@superdav42 superdav42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested and verified this is working. Thanks!

@jeffpaul
jeffpaul merged commit 48ab99e into WordPress:trunk Nov 30, 2026
15 checks passed
@saarnilauri saarnilauri mentioned this pull request Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Image Generation fails with "Unexpected Google API response: Missing the candidates[0].content key"

3 participants