Skip to content

Commit 1764e57

Browse files
committed
fix(integrations): Use correct modality for Google-style content formats and use common function for data URI parsing
1 parent e76dddd commit 1764e57

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

sentry_sdk/integrations/langchain.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
GEN_AI_ALLOWED_MESSAGE_ROLES,
1313
get_start_span_function,
1414
normalize_message_roles,
15+
parse_data_uri,
1516
set_data_normalized,
1617
truncate_and_annotate_messages,
1718
)
@@ -199,6 +200,26 @@ def _transform_langchain_content_block(
199200
"mime_type": media_type,
200201
"uri": source.get("url", ""),
201202
}
203+
# Handle Google-style inline_data format with standard type
204+
elif "inline_data" in content_block:
205+
inline_data = content_block.get("inline_data", {})
206+
if isinstance(inline_data, dict):
207+
return {
208+
"type": "blob",
209+
"modality": modality,
210+
"mime_type": inline_data.get("mime_type", "") or mime_type,
211+
"content": inline_data.get("data", ""),
212+
}
213+
# Handle Google-style file_data format with standard type
214+
elif "file_data" in content_block:
215+
file_data = content_block.get("file_data", {})
216+
if isinstance(file_data, dict):
217+
return {
218+
"type": "uri",
219+
"modality": modality,
220+
"mime_type": file_data.get("mime_type", "") or mime_type,
221+
"uri": file_data.get("file_uri", ""),
222+
}
202223

203224
# Handle legacy image_url format (OpenAI style)
204225
elif block_type == "image_url":
@@ -210,18 +231,15 @@ def _transform_langchain_content_block(
210231

211232
# Check if it's a data URI (base64 encoded)
212233
if url and url.startswith("data:"):
213-
# Parse data URI: data:mime_type;base64,content
214234
try:
215-
# Format: data:image/jpeg;base64,/9j/4AAQ...
216-
header, content = url.split(",", 1)
217-
mime_type = header.split(":")[1].split(";")[0] if ":" in header else ""
235+
mime_type, content = parse_data_uri(url)
218236
return {
219237
"type": "blob",
220238
"modality": "image",
221239
"mime_type": mime_type,
222240
"content": content,
223241
}
224-
except (ValueError, IndexError):
242+
except ValueError:
225243
# If parsing fails, return as URI
226244
return {
227245
"type": "uri",

0 commit comments

Comments
 (0)