fix: normalize unsupported images before sending to vision providers - #9780
Open
unknowbug wants to merge 2 commits into
Open
fix: normalize unsupported images before sending to vision providers#9780unknowbug wants to merge 2 commits into
unknowbug wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/utils/media_utils.py" line_range="164" />
<code_context>
+
+ supported = supported_mimes or IMAGE_PROVIDER_SUPPORTED_MIME_TYPES
+ mime = (getattr(image_data, "mime_type", "") or "").lower()
+ if mime in supported:
+ return image_data
+
</code_context>
<issue_to_address>
**issue (bug_risk):** Images whose declared MIME type is already in the provider-supported set are returned without decoding or validating their bytes. A corrupt payload or an unsupported image mislabeled as `image/png`, `image/jpeg`, `image/webp`, or `image/gif` is therefore still sent to the provider and triggers the same unsupported/invalid-image request failure this normalization is intended to prevent.
**Triggers:** When cached tool data or an image reference carries a provider-safe MIME label but contains invalid or differently formatted bytes.
**Suggested fix:** Validate the payload even for supported MIME types, and return the original data only when the detected format matches a supported provider format; otherwise convert it or return `None`.
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: astrbot/core/utils/media_utils.py:164
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9771
Summary
OpenAI-compatible vision providers such as DeepSeek reject image formats outside
webp/png/jpeg/gifwith a400 unsupported imageerror. AstrBot currently:image_urlblock when local image materialization fails (return resolved_part or part);image/svg+xml,image/bmp, etc.) without any provider-safe conversion;"unsupported image"in_handle_api_error, so a transient bad image leaves the whole agent inERRORinstead of falling back to text-only.This PR normalizes unsupported image formats before they are sent to the provider, guards the tool cached-image review path, and adds the missing error fallback.
Changes
normalize_image_for_provider():webp/png/jpeg/gifas-is;BMP/TIFF/AVIFetc. toJPEG(orPNGwhen transparency is present);Nonefor images that cannot be parsed (e.g. SVG) so callers can omit them._resolve_image_part()now normalizes image data before constructingimage_url;_transform_content_part()replaces an unusable image with a text placeholder instead of keeping the badimage_url;_is_invalid_attachment_error()now matches"unsupported image", enabling the existing text-only retry fallback.[Image from tool ...]marker remains).test_media_utils.py:normalize_image_for_providerBMP conversion, unsupported SVG handling, supported MIME passthrough.test_openai_source.py: unsupported-image API error fallback, BMP to JPEG resolution, SVG rejection, unresolvable context image placeholder replacement.Notes
tool_image_cacheis intentionally left unchanged: cached images may still need to be delivered to users in their original format (e.g. SVG). The provider-review boundary is now guarded bynormalize_image_for_provider().Summary by Sourcery
Ensure vision-provider requests contain only valid, supported image formats while degrading gracefully when images cannot be used.
Bug Fixes:
Enhancements:
Tests: