-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix(provider): 修复 base64:// 图片引用的 MIME 类型声明不准确问题 #8177
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 |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import base64 | ||
| import builtins | ||
| from io import BytesIO | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
@@ -1031,6 +1033,27 @@ async def test_resolve_image_part_supports_base64_scheme(): | |
| await provider.terminate() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_resolve_image_part_preserves_base64_png_mime_type(): | ||
| provider = _make_provider() | ||
| try: | ||
| image_buffer = BytesIO() | ||
| PILImage.new("RGBA", (1, 1), (255, 0, 0, 255)).save( | ||
| image_buffer, | ||
| format="PNG", | ||
| ) | ||
| image_base64 = base64.b64encode(image_buffer.getvalue()).decode("ascii") | ||
|
|
||
| image_part = await provider._resolve_image_part(f"base64://{image_base64}") | ||
|
Comment on lines
+1045
to
+1047
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. suggestion (testing): Add a test for invalid or malformed base64 inputs to ensure legacy JPEG behavior is preserved Since |
||
|
|
||
| assert image_part == { | ||
| "type": "image_url", | ||
| "image_url": {"url": f"data:image/png;base64,{image_base64}"}, | ||
| } | ||
| finally: | ||
| await provider.terminate() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_prepare_chat_payload_materializes_context_localhost_file_uri_image_urls( | ||
| tmp_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.
Decoding the entire base64 string into memory just to detect the image format can be inefficient for very large images. Since most image headers are within the first few dozen bytes, you could potentially optimize this by decoding only the beginning of the string. However, given that the full base64 is required for the final data URL and Pillow's
verify()is relatively lightweight, this is a minor performance consideration. Additionally, as this introduces new functionality for handling attachments, please ensure it is accompanied by corresponding unit tests.References