How to beginner · 3 min read

Image too large for LLM API fix

Quick answer
When an image is too large for a multimodal LLM API, fix it by resizing or compressing the image to meet the API's size limits before sending. Use libraries like Pillow to resize or convert images to lower resolution or formats like JPEG to reduce file size.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key or relevant LLM API key
  • pip install pillow openai>=1.0

Setup

Install the necessary Python packages to handle image resizing and API calls.

bash
pip install pillow openai

Step by step

Resize or compress the image before sending it to the LLM API to avoid size limit errors. The example below uses Pillow to resize an image and then sends it as base64 or file bytes to the API.

python
import os
from PIL import Image
from io import BytesIO
from openai import OpenAI

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Load and resize image
image_path = "large_image.png"
max_size = (1024, 1024)  # max width and height

with Image.open(image_path) as img:
    img = img.convert('RGB')  # convert if image has transparency
    img.thumbnail(max_size)  # resize in-place maintaining aspect ratio
    buffer = BytesIO()
    img.save(buffer, format="JPEG", quality=85)  # compress to JPEG
    buffer.seek(0)
    image_bytes = buffer.read()

# Example: send image bytes to a multimodal LLM API (pseudo code)
# Replace with actual API call and model supporting images
response = client.chat.completions.create(
    model="gpt-4o-mini",  # or a multimodal model
    messages=[
        {"role": "user", "content": "Describe this image."},
        {"role": "user", "content": image_bytes}  # depends on API spec
    ]
)

print(response.choices[0].message.content)
output
A descriptive text about the resized image.

Common variations

  • Use img.convert('RGB') before saving if the image has transparency.
  • Adjust quality parameter in img.save() to balance size and quality.
  • For async APIs, use async client calls accordingly.
  • Some APIs accept base64-encoded images; encode with base64.b64encode() if needed.

Troubleshooting

  • If you get 413 Payload Too Large, reduce image dimensions or compression quality further.
  • Check the API documentation for exact image size and format limits.
  • Use image format conversion (e.g., PNG to JPEG) to reduce file size.
  • Verify the image is correctly encoded or sent as per the API's multimodal input specification.

Key Takeaways

  • Always resize or compress images to fit within the LLM API's size limits before sending.
  • Use Pillow to efficiently resize and convert images to smaller formats like JPEG.
  • Check API docs for exact image size and format requirements to avoid errors.
  • Encoding images properly (base64 or bytes) is essential for successful multimodal API calls.
Verified 2026-04 · gpt-4o-mini
Verify ↗