-
Notifications
You must be signed in to change notification settings - Fork 760
Description
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.