Skip to content

fix: treat a single image value as one image instead of iterating it - #737

Open
betacatsling wants to merge 1 commit into
ollama:mainfrom
betacatsling:fix/single-image-sequence
Open

betacatsling wants to merge 1 commit into
ollama:mainfrom
betacatsling:fix/single-image-sequence

Conversation

@betacatsling

Copy link
Copy Markdown

Summary

Fixes #734.

_copy_images iterates whatever it is given. When a caller passes a single image value instead of a list — e.g. client.chat(..., messages=[{'role': 'user', 'images': 'aW1hZ2U='}]) or client.generate(..., images='aW1hZ2U=') — the str/bytes value is a Sequence, so it is iterated one character/byte at a time and each element is wrapped in its own Image. A single base64 string becomes dozens of one-character images and serialization fails with a cryptic PydanticSerializationError: ValueError: Invalid image data, expected base64 string or path to image file; a file path string similarly explodes into per-character paths.

This wraps a bare str, bytes, os.PathLike, or Image in a single-element list before iterating, so one image value produces one image in the request. List/tuple input is unchanged.

Validation

  • New regression tests in tests/test_client.py (pytest-httpserver asserts the exact request body sent):
    • test_client_chat_single_image_not_in_list (parametrized over base64 string and raw bytes)
    • test_client_generate_single_image_not_in_list
  • Before this change: all three fail — each character/byte becomes a separate Image and request serialization raises PydanticSerializationError.
  • After: pytest tests/test_client.py -k 'single_image or chat_images' — 8 passed; full suite pytest tests/ — 100 passed.
  • ruff check and ruff format --check pass on changed files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A single image passed as a bare string/bytes is iterated character-by-character

1 participant