Skip to content

Commit a049835

Browse files
fix(processing): Mime types for Image Summarization (#5471)
1 parent d186d8e commit a049835

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

backend/onyx/file_processing/image_summarization.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
from onyx.configs.app_configs import IMAGE_SUMMARIZATION_USER_PROMPT
1111
from onyx.llm.interfaces import LLM
1212
from onyx.llm.utils import message_to_string
13+
from onyx.utils.b64 import get_image_type_from_bytes
1314
from onyx.utils.logger import setup_logger
1415

1516
logger = setup_logger()
1617

1718

19+
class UnsupportedImageFormatError(ValueError):
20+
"""Raised when an image uses a MIME type unsupported by the summarization flow."""
21+
22+
1823
def prepare_image_bytes(image_data: bytes) -> str:
1924
"""Prepare image bytes for summarization.
2025
Resizes image if it's larger than 20MB. Encodes image as a base64 string."""
@@ -74,7 +79,14 @@ def summarize_image_with_error_handling(
7479
user_prompt = (
7580
f"The image has the file name '{context_name}'.\n{user_prompt_template}"
7681
)
77-
return summarize_image_pipeline(llm, image_data, user_prompt, system_prompt)
82+
try:
83+
return summarize_image_pipeline(llm, image_data, user_prompt, system_prompt)
84+
except UnsupportedImageFormatError:
85+
logger.info(
86+
"Skipping image summarization due to unsupported MIME type for %s",
87+
context_name,
88+
)
89+
return None
7890

7991

8092
def _summarize_image(
@@ -109,10 +121,17 @@ def _summarize_image(
109121

110122

111123
def _encode_image_for_llm_prompt(image_data: bytes) -> str:
112-
"""Getting the base64 string."""
124+
"""Prepare a data URL with the correct MIME type for the LLM message."""
125+
try:
126+
mime_type = get_image_type_from_bytes(image_data)
127+
except ValueError as exc:
128+
raise UnsupportedImageFormatError(
129+
"Unsupported image format for summarization"
130+
) from exc
131+
113132
base64_encoded_data = base64.b64encode(image_data).decode("utf-8")
114133

115-
return f"data:image/jpeg;base64,{base64_encoded_data}"
134+
return f"data:{mime_type};base64,{base64_encoded_data}"
116135

117136

118137
def _resize_image_if_needed(image_data: bytes, max_size_mb: int = 20) -> bytes:

0 commit comments

Comments
 (0)