Skip to content

SDK should auto-resize oversized images before sending to LLM providers #2467

@xingyaoww

Description

@xingyaoww

Bug Description

When a user sends an oversized image in a conversation, the LLM API call fails with:

litellm.BadRequestError: Error code: 400 - AnthropicException -
{"type":"error","error":{"type":"invalid_request_error",
"message":"messages.4.content.1.image.source.base64.data:
At least one of the image dimensions exceed max allowed size
for many-image requests: 2000 pixels"}}

The error crashes the conversation with no recovery, even though the runtime is still alive.

Root Cause

The ImageContent class in openhands-sdk/openhands/sdk/llm/message.py passes image URLs/base64 data directly to the LLM provider without any dimension validation or resizing. Anthropic enforces a 2000px max dimension for many-image requests, and other providers have similar limits.

class ImageContent(BaseContent):
    type: Literal["image"] = "image"
    image_urls: list[str]

    def to_llm_dict(self) -> list[dict[str, str | dict[str, str]]]:
        images: list[dict[str, str | dict[str, str]]] = []
        for url in self.image_urls:
            images.append({"type": "image_url", "image_url": {"url": url}})
        # No dimension check or resize happens here
        return images

Expected Behavior

The SDK should automatically downscale images that exceed provider dimension limits before including them in LLM API calls. This should happen transparently so users can attach any image without hitting cryptic API errors.

Proposed Solution

  1. Add image resizing logic — When converting ImageContent to LLM format (in to_llm_dict() or at the message serialization layer), detect base64-encoded images, decode them, check dimensions, and resize if any dimension exceeds the max (e.g., 2000px for Anthropic, or a conservative default).
  2. Make the limit configurable — The max dimension could be tied to the LLM provider config or exposed as a setting, since different providers have different limits.
  3. Handle both base64 and URL images — For base64 images, resize in-memory. For URL-based images, consider fetching and resizing, or at minimum validate and warn.
  4. Preserve aspect ratio — When downscaling, maintain the original aspect ratio.

Reproduction Steps

  1. Start a conversation using an Anthropic model (e.g., claude-opus-4-6)
  2. Send a message with an attached image where at least one dimension exceeds 2000px
  3. Observe the litellm.BadRequestError crash

Environment

  • Model: claude-opus-4-6 (via LiteLLM)
  • Error source: Anthropic API invalid_request_error

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions