-
-
Notifications
You must be signed in to change notification settings - Fork 3k
fix: normalize WebP images for multimodal providers #10145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -146,6 +146,79 @@ def to_data_url(self) -> str: | |||||
| return f"data:{self.mime_type};base64,{self.base64_data}" | ||||||
|
|
||||||
|
|
||||||
| IMAGE_PROVIDER_SUPPORTED_MIME_TYPES = frozenset( | ||||||
| { | ||||||
| "image/gif", | ||||||
| "image/jpeg", | ||||||
| "image/png", | ||||||
| } | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def normalize_image_for_provider( | ||||||
| image_data: ResolvedMediaData | None, | ||||||
| supported_mimes: set[str] | frozenset[str] | None = None, | ||||||
| ) -> ResolvedMediaData | None: | ||||||
| """Normalize image bytes to a MIME type accepted by a vision provider. | ||||||
|
|
||||||
| Args: | ||||||
| image_data: Resolved image bytes and metadata. | ||||||
| supported_mimes: MIME types accepted by the provider. Defaults to JPEG, | ||||||
| PNG, and GIF. | ||||||
|
|
||||||
| Returns: | ||||||
| Validated image data with corrected MIME metadata, or converted image data | ||||||
| when the source format is not supported. | ||||||
| """ | ||||||
| if image_data is None: | ||||||
| return None | ||||||
|
|
||||||
| supported = supported_mimes or IMAGE_PROVIDER_SUPPORTED_MIME_TYPES | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick (bug_risk): An explicitly supplied empty Triggers: When a caller passes Suggested fix: Use
Suggested change
|
||||||
| if image_data.mime_type in supported: | ||||||
| return image_data | ||||||
|
|
||||||
| raw = image_data.to_bytes() | ||||||
| with PILImage.open(io.BytesIO(raw)) as image: | ||||||
| actual_mime = { | ||||||
| "GIF": "image/gif", | ||||||
| "JPEG": "image/jpeg", | ||||||
| "PNG": "image/png", | ||||||
| "WEBP": "image/webp", | ||||||
| }.get(str(image.format or "").upper()) | ||||||
| if actual_mime in supported: | ||||||
| return ResolvedMediaData( | ||||||
| base64_data=image_data.base64_data, | ||||||
| mime_type=actual_mime, | ||||||
| format=image_data.format, | ||||||
| ) | ||||||
|
|
||||||
| has_alpha = image.mode in {"RGBA", "LA", "PA"} or "transparency" in image.info | ||||||
| if has_alpha and "image/png" in supported: | ||||||
| output_format = "PNG" | ||||||
| output_mime = "image/png" | ||||||
| converted = image.convert("RGBA") | ||||||
| elif "image/jpeg" in supported: | ||||||
| output_format = "JPEG" | ||||||
| output_mime = "image/jpeg" | ||||||
| converted = image.convert("RGB") | ||||||
| elif "image/png" in supported: | ||||||
| output_format = "PNG" | ||||||
| output_mime = "image/png" | ||||||
| converted = image.convert("RGB") | ||||||
| else: | ||||||
| return None | ||||||
|
|
||||||
| try: | ||||||
| output = io.BytesIO() | ||||||
| converted.save(output, format=output_format) | ||||||
| return ResolvedMediaData( | ||||||
| base64_data=base64.b64encode(output.getvalue()).decode("utf-8"), | ||||||
| mime_type=output_mime, | ||||||
| ) | ||||||
| finally: | ||||||
| converted.close() | ||||||
|
|
||||||
|
|
||||||
| @dataclass(slots=True) | ||||||
| class _LocalMediaFile: | ||||||
| path: Path | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): An unsupported or incorrectly labelled image whose resolved bytes are not a valid Pillow image causes
PILImage.opento raise, and_image_ref_to_data_urldoes not catch that exception. This breaks the safe image path by propagating the decoding error instead of returningNoneand letting_resolve_image_partignore the invalid attachment.Triggers: When a safe-mode image reference resolves successfully but contains malformed bytes or a format unsupported by Pillow.
Suggested fix: Catch Pillow decoding/conversion errors in the safe path, or make
normalize_image_for_providerreturnNonefor invalid image bytes.