Skip to content

Indefinite hang accessing finish_reason when API returns IMAGE_SAFETY or NO_IMAGE #2024

Closed as not planned

Description

@Eelahne

Description:

I encountered a critical issue where the google-genai Python SDK hangs indefinitely when accessing the finish_reason property (and potentially other properties) of a response object. This occurs specifically when the API returns a status that triggers an internal enum validation warning, such as IMAGE_SAFETY or NO_IMAGE during image generation.
Instead of raising an error or returning a string, the main thread hangs in futex_wait_queue, seemingly blocked on an internal threading primitive within the SDK's property accessors or logging mechanisms.

Reproduction Steps:

Use google-genai SDK (Python).
Perform a models.generate_content call with response_modalities=["IMAGE"].
Trigger a scenario where the model refuses to generate an image (e.g., safety violation or generation failure).
Attempt to access response.candidates[0].finish_reason.

Expected Behavior:

The SDK should return the finish_reason (as a string or Enum) or raise an exception if the value is unknown.

Actual Behavior:

The process emits a UserWarning and then hangs indefinitely. The process state usually shows it waiting on futex_wait_queue.
Console Output:
/usr/local/lib/python3.10/site-packages/google/genai/common.py:218: UserWarning: IMAGE_SAFETY is not a valid FinishReason
warnings.warn(f"{value} is not a valid {cls.name}")
Process hangs here indefinitely
Process State (Linux):
$ ps -p -o etime=,state=,wchan=
05:27 S futex_wait_queue

Reproduction Script:

from google import genai
from google.genai import types
Initialize client
client = genai.Client(api_key="YOUR_API_KEY")
Trigger a request that is likely to be blocked or fail generation
(This prompt is just an example; any prompt triggering IMAGE_SAFETY causes the hang)
response = client.models.generate_content(
model='gemini-2.0-flash-exp',
contents="generate an image",
config=types.GenerateContentConfig(
response_modalities=["IMAGE"])
)
print("Request completed. Accessing finish_reason...")
This line causes the indefinite hang if result is IMAGE_SAFETY
reason = response.candidates[0].finish_reason
print(f"Finish reason: {reason}")

Environment:

OS: Linux (Debian/Ubuntu)
Python Version: 3.10
SDK Version: google-genai (Latest)

Analysis/Workaround:

I found that the hang happens strictly during property access on the response object when the enum value is unrecognized by the client mapping.
My current workaround is to wrap all SDK property access in a separate thread with a timeout to prevent the main application from locking up.

Activity

  1. added
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    priority: p2Moderately-important priority. Fix may not be included in next release.
    on Feb 4, 2026
  2. matthew29tang commented on Feb 11, 2026

    @matthew29tang
    Contributor

    Can you provide the exact model and prompt that triggers IMAGE_SAFETY?

    From your code snippet, gemini-2.0-flash-exp is an old model and returns a 404 since it was already removed

  3. removed their assignment
    on Feb 11, 2026
  4. github-actions commented on Jun 17, 2026

    @github-actions

    This issue has been marked as stale because it has been open for 7 days with no activity. It will be closed in 2 days if no further activity occurs.

  5. github-actions commented on Jun 19, 2026

    @github-actions

    This issue was closed because it has been inactive for 9 days. Please post a new issue if you need further assistance. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.status:awaiting user responsestatus:staletype: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions