Skip to content

Commit a11171c

Browse files
committed
fix: use glob matching for Gemini image MIME types
gemini-3-pro-image-preview nondeterministically returns image/jpeg instead of image/png. get_image_from_response() hardcoded get_parts_by_type(response, "image/png"), silently dropping JPEG responses and falling back to torch.zeros (all-black output). Add _mime_matches() helper using fnmatch for glob-style MIME matching. Change get_image_from_response() to request "image/*" so any image format returned by the API is correctly captured.
1 parent 19236ed commit a11171c

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

comfy_api_nodes/nodes_gemini.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import base64
77
import os
88
from enum import Enum
9+
from fnmatch import fnmatch
910
from io import BytesIO
1011
from typing import Literal
1112

@@ -119,6 +120,13 @@ async def create_image_parts(
119120
return image_parts
120121

121122

123+
def _mime_matches(mime: GeminiMimeType | None, pattern: str) -> bool:
124+
"""Check if a MIME type matches a pattern. Supports fnmatch globs (e.g. 'image/*')."""
125+
if mime is None:
126+
return False
127+
return fnmatch(mime.value, pattern)
128+
129+
122130
def get_parts_by_type(response: GeminiGenerateContentResponse, part_type: Literal["text"] | str) -> list[GeminiPart]:
123131
"""
124132
Filter response parts by their type.
@@ -151,9 +159,9 @@ def get_parts_by_type(response: GeminiGenerateContentResponse, part_type: Litera
151159
for part in candidate.content.parts:
152160
if part_type == "text" and part.text:
153161
parts.append(part)
154-
elif part.inlineData and part.inlineData.mimeType == part_type:
162+
elif part.inlineData and _mime_matches(part.inlineData.mimeType, part_type):
155163
parts.append(part)
156-
elif part.fileData and part.fileData.mimeType == part_type:
164+
elif part.fileData and _mime_matches(part.fileData.mimeType, part_type):
157165
parts.append(part)
158166

159167
if not parts and blocked_reasons:
@@ -178,7 +186,7 @@ def get_text_from_response(response: GeminiGenerateContentResponse) -> str:
178186

179187
async def get_image_from_response(response: GeminiGenerateContentResponse) -> Input.Image:
180188
image_tensors: list[Input.Image] = []
181-
parts = get_parts_by_type(response, "image/png")
189+
parts = get_parts_by_type(response, "image/*")
182190
for part in parts:
183191
if part.inlineData:
184192
image_data = base64.b64decode(part.inlineData.data)

0 commit comments

Comments
 (0)