Skip to content

Commit d28e0b8

Browse files
committed
fix: several small fixes around content parts
1 parent 7d825af commit d28e0b8

File tree

1 file changed

+31
-8
lines changed
  • sentry_sdk/integrations/google_genai

1 file changed

+31
-8
lines changed

sentry_sdk/integrations/google_genai/utils.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,12 @@ def extract_contents_messages(contents: "ContentListUnion") -> "List[Dict[str, A
260260
# File object
261261
file_uri = getattr(contents, "uri", None)
262262
mime_type = getattr(contents, "mime_type", None)
263-
if file_uri and mime_type:
263+
# Process if we have file_uri, even if mime_type is missing
264+
if file_uri is not None:
265+
# Default to empty string if mime_type is None
266+
if mime_type is None:
267+
mime_type = ""
268+
264269
blob_part = {
265270
"type": "uri",
266271
"modality": get_modality_from_mime_type(mime_type),
@@ -325,7 +330,12 @@ def _extract_part_content(part: "Any") -> "Optional[dict[str, Any]]":
325330
file_data = part.file_data
326331
file_uri = getattr(file_data, "file_uri", None)
327332
mime_type = getattr(file_data, "mime_type", None)
328-
if file_uri and mime_type:
333+
# Process if we have file_uri, even if mime_type is missing (consistent with dict handling)
334+
if file_uri is not None:
335+
# Default to empty string if mime_type is None (consistent with transform_google_content_part)
336+
if mime_type is None:
337+
mime_type = ""
338+
329339
return {
330340
"type": "uri",
331341
"modality": get_modality_from_mime_type(mime_type),
@@ -338,13 +348,25 @@ def _extract_part_content(part: "Any") -> "Optional[dict[str, Any]]":
338348
inline_data = part.inline_data
339349
data = getattr(inline_data, "data", None)
340350
mime_type = getattr(inline_data, "mime_type", None)
341-
if data and mime_type:
351+
# Process if we have data, even if mime_type is missing/empty (consistent with dict handling)
352+
if data is not None:
353+
# Default to empty string if mime_type is None (consistent with transform_google_content_part)
354+
if mime_type is None:
355+
mime_type = ""
356+
357+
# Handle both bytes (binary data) and str (base64-encoded data)
342358
if isinstance(data, bytes):
343-
return {
344-
"type": "blob",
345-
"mime_type": mime_type,
346-
"content": BLOB_DATA_SUBSTITUTE,
347-
}
359+
content = BLOB_DATA_SUBSTITUTE
360+
else:
361+
# For non-bytes data (e.g., base64 strings), use as-is
362+
content = data
363+
364+
return {
365+
"type": "blob",
366+
"modality": get_modality_from_mime_type(mime_type),
367+
"mime_type": mime_type,
368+
"content": content,
369+
}
348370

349371
return None
350372

@@ -417,6 +439,7 @@ def _extract_pil_image(image: "Any") -> "Optional[dict[str, Any]]":
417439

418440
return {
419441
"type": "blob",
442+
"modality": get_modality_from_mime_type(mime_type),
420443
"mime_type": mime_type,
421444
"content": BLOB_DATA_SUBSTITUTE,
422445
}

0 commit comments

Comments
 (0)