-
Notifications
You must be signed in to change notification settings - Fork 312
fix(sdk): resize Anthropic many-image inputs #2552
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
19c998b
fix(sdk): resize Anthropic many-image inputs
Zheng-Lu b3f67af
Merge branch 'main' into fix/2467-image-downscale
openhands-agent 7dbe9e0
Merge branch 'OpenHands:main' into fix/2467-image-downscale
Zheng-Lu 079183f
Merge branch 'OpenHands:main' into fix/2467-image-downscale
Zheng-Lu 4d047ed
fix(sdk): handle Anthropic single-image limits
Zheng-Lu 487da93
Merge branch 'OpenHands:main' into fix/2467-image-downscale
Zheng-Lu 82447d2
Merge branch 'main' into fix/2467-image-downscale
xingyaoww aef2b73
Merge branch 'main' into fix/2467-image-downscale
xingyaoww e2ed9aa
fix(sdk): simplify image resize utilities
Zheng-Lu 7fce88f
Merge branch 'OpenHands:main' into fix/2467-image-downscale
Zheng-Lu 64f0104
fix(sdk): require pillow for image resizing
Zheng-Lu 5394fef
Merge branch 'main' into fix/2467-image-downscale
Zheng-Lu c400168
Merge branch 'main' into fix/2467-image-downscale
xingyaoww 4303819
feat(example): add many-image send_message to exercise Anthropic resize
openhands-agent 813919d
Merge remote-tracking branch 'origin/main' into fix/2467-image-downscale
openhands-agent 5095dd0
fix(ci): use head SHA for wait-on-check-action in fork PRs
openhands-agent f339ec1
fix(ci): handle missing Build & Push check for fork PRs
openhands-agent 76aec9c
fix(ci): make PR comment posting non-fatal for fork PRs
openhands-agent fc2bd09
Merge branch 'main' into fix/2467-image-downscale
xingyaoww 2c4287e
Merge branch 'main' into fix/2467-image-downscale
Zheng-Lu 09e84fa
Merge main into fix/2467-image-downscale, resolve pyproject.toml conf…
openhands-agent 75390e7
fix(sdk): resize proxy Anthropic images
xingyaoww 1e420e2
chore: remove run examples workflow changes
xingyaoww 574aa42
Merge branch 'main' into fix/2467-image-downscale
xingyaoww 172e0ee
Merge branch 'main' into fix/2467-image-downscale
xingyaoww File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import base64 | ||
| import io | ||
| from unittest.mock import patch | ||
|
|
||
| from PIL import Image | ||
| from pydantic import SecretStr | ||
|
|
||
| from openhands.sdk.llm import LLM, ImageContent, Message, TextContent | ||
|
|
||
|
|
||
| def _make_png_data_url(width: int, height: int) -> str: | ||
| image = Image.new("RGB", (width, height), color="red") | ||
| buffer = io.BytesIO() | ||
| image.save(buffer, format="PNG") | ||
| encoded = base64.b64encode(buffer.getvalue()).decode("ascii") | ||
| return f"data:image/png;base64,{encoded}" | ||
|
|
||
|
|
||
| def _data_url_dimensions(url: str) -> tuple[int, int]: | ||
| _header, _sep, encoded = url.partition(";base64,") | ||
| image_bytes = base64.b64decode(encoded) | ||
| with Image.open(io.BytesIO(image_bytes)) as image: | ||
| return image.size | ||
|
|
||
|
|
||
| def _image_urls_from_chat_message(chat_message: dict) -> list[str]: | ||
| return [ | ||
| item["image_url"]["url"] | ||
| for item in chat_message["content"] | ||
| if item.get("type") == "image_url" | ||
| ] | ||
|
|
||
|
|
||
| def _format_for_provider( | ||
| llm: LLM, messages: list[Message], *, provider: str | ||
| ) -> list[dict]: | ||
| with ( | ||
| patch.object(LLM, "vision_is_active", return_value=True), | ||
| patch.object(LLM, "_infer_litellm_provider", return_value=provider), | ||
| ): | ||
| return llm.format_messages_for_llm(messages) | ||
|
Zheng-Lu marked this conversation as resolved.
|
||
|
|
||
|
Zheng-Lu marked this conversation as resolved.
|
||
|
|
||
| def test_anthropic_many_image_requests_resize_base64_images(): | ||
| original_url = _make_png_data_url(2400, 1200) | ||
| message = Message( | ||
| role="user", | ||
| content=[ | ||
| TextContent(text="Describe these images."), | ||
| ImageContent(image_urls=[original_url] * 21), | ||
| ], | ||
| ) | ||
| llm = LLM( | ||
| model="anthropic/claude-opus-4-6", | ||
| api_key=SecretStr("test-key"), | ||
| usage_id="test-anthropic-many-image", | ||
| ) | ||
|
|
||
| formatted = _format_for_provider(llm, [message], provider="anthropic") | ||
|
|
||
| image_urls = _image_urls_from_chat_message(formatted[0]) | ||
| assert len(image_urls) == 21 | ||
| assert _data_url_dimensions(image_urls[0]) == (2000, 1000) | ||
| original_content = message.content[1] | ||
| assert isinstance(original_content, ImageContent) | ||
| assert original_content.image_urls[0] == original_url | ||
|
|
||
|
|
||
| def test_anthropic_exactly_twenty_images_do_not_resize(): | ||
| original_url = _make_png_data_url(2400, 1200) | ||
| message = Message( | ||
| role="user", | ||
| content=[ | ||
| TextContent(text="Describe these images."), | ||
| ImageContent(image_urls=[original_url] * 20), | ||
| ], | ||
| ) | ||
| llm = LLM( | ||
| model="anthropic/claude-opus-4-6", | ||
| api_key=SecretStr("test-key"), | ||
| usage_id="test-anthropic-twenty-images", | ||
| ) | ||
|
|
||
| formatted = _format_for_provider(llm, [message], provider="anthropic") | ||
|
|
||
| image_urls = _image_urls_from_chat_message(formatted[0]) | ||
| assert len(image_urls) == 20 | ||
| assert _data_url_dimensions(image_urls[0]) == (2400, 1200) | ||
|
|
||
|
|
||
| def test_anthropic_single_image_requests_do_not_resize(): | ||
| original_url = _make_png_data_url(2400, 2400) | ||
| message = Message( | ||
| role="user", | ||
| content=[ | ||
| TextContent(text="Describe this image."), | ||
| ImageContent(image_urls=[original_url]), | ||
| ], | ||
| ) | ||
| llm = LLM( | ||
| model="anthropic/claude-opus-4-6", | ||
| api_key=SecretStr("test-key"), | ||
| usage_id="test-anthropic-single-image", | ||
| ) | ||
|
|
||
| formatted = _format_for_provider(llm, [message], provider="anthropic") | ||
|
|
||
| image_urls = _image_urls_from_chat_message(formatted[0]) | ||
| assert image_urls == [original_url] | ||
| assert _data_url_dimensions(image_urls[0]) == (2400, 2400) | ||
|
|
||
|
|
||
| def test_anthropic_many_image_requests_leave_url_images_unchanged(): | ||
| image_url = "https://example.com/image.png" | ||
| message = Message( | ||
| role="user", | ||
| content=[ | ||
| TextContent(text="Describe these images."), | ||
| ImageContent(image_urls=[image_url] * 21), | ||
| ], | ||
| ) | ||
| llm = LLM( | ||
| model="anthropic/claude-opus-4-6", | ||
| api_key=SecretStr("test-key"), | ||
| usage_id="test-anthropic-url-images", | ||
| ) | ||
|
|
||
| formatted = _format_for_provider(llm, [message], provider="anthropic") | ||
|
|
||
| assert _image_urls_from_chat_message(formatted[0]) == [image_url] * 21 | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.