Skip to content

ValueError: "IMAGE_OTHER" is not a valid backing value for enum Gemini\Enums\FinishReason #146

Description

@boonstoppel

Package Version: ^2.0

PHP Version: 8.4.1

Description:
When making an image generation request (specifically, an image-to-image edit), the API is returning a finishReason of IMAGE_OTHER. This value is not present in the Gemini\Enums\FinishReason enum in the current version of the client, causing an unhandled ValueError when the client attempts to parse the API response.

This crashes the application instead of allowing for graceful error handling.

Steps to Reproduce:

Make an image generation call using gemini-pro-vision or a similar model that involves providing a base image.

Trigger a response from the API that returns finishReason: 'IMAGE_OTHER'. (This often happens when the input image is flagged by safety or content policies).

The client's Gemini\Data\Candidate::from() method will be called.

This method attempts to execute Gemini\Enums\FinishReason::from('IMAGE_OTHER').

The client throws a ValueError because IMAGE_OTHER is not a defined case in the enum.

Expected Behavior:
The Gemini\Enums\FinishReason enum should include a case for IMAGE_OTHER, allowing the client to parse the response successfully. This would ideally result in a specific exception (like a SafetyException or similar) that the user can try/catch, rather than a fatal ValueError.

Actual Behavior:
The application crashes with the following error:

ValueError: "IMAGE_OTHER" is not a valid backing value for enum Gemini\Enoms\FinishReason in /path/to/vendor/google-gemini-php/client/src/Data/Candidate.php:56

Stack Trace (Partial):

#0 /path/to/vendor/google-gemini-php/client/src/Data/Candidate.php(56): Gemini\Enums\FinishReason::from('IMAGE_OTHER')
#1 /path/to/vendor/google-gemini-php/client/src/Responses/GenerativeModel/GenerateContentResponse.php(106): Gemini\Data\Candidate::from(Array)
#2 [internal function]: Gemini\Responses\GenerativeModel\GenerateContentResponse::{closure:Gemini\Responses\GenerativeModel\GenerateContentResponse::from():106}(Array)
#3 /path/to/vendor/google-gemini-php/client/src/Responses/GenerativeModel/GenerateContentResponse.php(105): array_map(Object(Closure), Array)
#4 /path/to/vendor/google-gemini-php/client/src/Resources/GenerativeModel.php(129): Gemini\Responses\GenerativeModel\GenerateContentResponse::from(Array)
...

Suggested Fix:
Add the IMAGE_OTHER case to the Gemini\Enums\FinishReason enum.

// In Gemini\Enums\FinishReason.php

enum FinishReason: string
{
// ... existing cases ...
case SAFETY = 'SAFETY';
case RECITATION = 'RECITATION';
case OTHER = 'OTHER';
case IMAGE_OTHER = 'IMAGE_OTHER'; // <-- Add this case
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions