fix(api-nodes-gemini): raise exception when no candidates due to safety block

This commit is contained in:
bigcat88 2026-01-13 15:46:15 +02:00
parent 5ac1372533
commit bde1daf314

View File

@ -130,7 +130,7 @@ def get_parts_by_type(response: GeminiGenerateContentResponse, part_type: Litera
Returns: Returns:
List of response parts matching the requested type. List of response parts matching the requested type.
""" """
if response.candidates is None: if not response.candidates:
if response.promptFeedback and response.promptFeedback.blockReason: if response.promptFeedback and response.promptFeedback.blockReason:
feedback = response.promptFeedback feedback = response.promptFeedback
raise ValueError( raise ValueError(
@ -141,14 +141,24 @@ def get_parts_by_type(response: GeminiGenerateContentResponse, part_type: Litera
"try changing it to `IMAGE+TEXT` to view the model's reasoning and understand why image generation failed." "try changing it to `IMAGE+TEXT` to view the model's reasoning and understand why image generation failed."
) )
parts = [] parts = []
for part in response.candidates[0].content.parts: blocked_reasons = []
if part_type == "text" and part.text: for candidate in response.candidates:
parts.append(part) if candidate.finishReason and candidate.finishReason.upper() == "IMAGE_PROHIBITED_CONTENT":
elif part.inlineData and part.inlineData.mimeType == part_type: blocked_reasons.append(candidate.finishReason)
parts.append(part) continue
elif part.fileData and part.fileData.mimeType == part_type: if candidate.content is None or candidate.content.parts is None:
parts.append(part) continue
# Skip parts that don't match the requested type for part in candidate.content.parts:
if part_type == "text" and part.text:
parts.append(part)
elif part.inlineData and part.inlineData.mimeType == part_type:
parts.append(part)
elif part.fileData and part.fileData.mimeType == part_type:
parts.append(part)
if not parts and blocked_reasons:
raise ValueError(f"Gemini API blocked the request. Reasons: {blocked_reasons}")
return parts return parts