Skip to content

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

@Eelahne

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.

Metadata

Metadata

Assignees

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: 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