[GH-ISSUE #7871] pydantic issue with converted PNG images #5036

Closed
opened 2026-04-12 16:07:32 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @ibagur on GitHub (Nov 28, 2024).
Original GitHub issue: https://github.com/ollama/ollama/issues/7871

What is the issue?

Directly feeding a PNG image does not work (failed to decode image: image: unknown format), so until recently, I was using the code bellow in order to encode the image file and it used to work fine:

import base64
import io
from PIL import Image
import ollama

def encode_image_to_base64(image_path: str, format: str = "PNG") -> str:
    """Encodes an image file to a base64 string."""
    with Image.open(image_path) as img:
        buffered = io.BytesIO()
        img.save(buffered, format=format)
        return base64.b64encode(buffered.getvalue()).decode('utf-8')

response = ollama.chat(
    model='llama3.2-vision:latest',
    messages=[{
        'role': 'user',
        'content': 'What is in this image?',
        'images': [encode_image_to_base64('image1.png')]
    }]
)

# Print the response
print(response['message']['content'])

But now I get a pydantic serialization error:

PydanticSerializationError: Error calling function `serialize_model`: OSError: [Errno 63] File name too long: 'iVBORw0KGgoAAAANSUhEUgAAAOYAAAGQCAIAAAA1BIuEAAEAAElEQ...

The only workaround I have found to make it work is to directly convert the PNG image into JPG and then feed the JPG image directly to ollama llama3.2-vision:latest model:

def convert_to_jpg(image_path: str) -> bytes:
    """
    Convert image to JPG in memory and return the bytes
    """
    with Image.open(image_path) as img:
        # Convert to RGB if needed
        if img.mode != 'RGB':
            img = img.convert('RGB')
        
        # Save as JPG to memory buffer
        buffered = io.BytesIO()
        img.save(buffered, format='JPEG', quality=85)
        return buffered.getvalue()

response = ollama.chat(
    model='llama3.2-vision:latest',
    messages=[{
        'role': 'user',
        'content': 'What is in this image?',
        'images': [convert_to_jpg('image1.png')]
    }]
)

# Print the response
print(response['message']['content'])

What could be the reason? Any recent update on either ollama, pydantic or pillow libraries? These are the versions I am using in my venv:

ollama            0.4.1
pillow            11.0.0
pydantic          2.10.2
pydantic_core     2.27.1

Thanks for your suggestions!

OS

macOS

GPU

Apple

CPU

Apple

Ollama version

0.4.1

UPDATE:

The particular image I was using was wrong format. Apparently it was a 'webp' image wrongly misnamed as PNG. Now feeding directly a proper PNG image works fine, no need for conversion

Originally created by @ibagur on GitHub (Nov 28, 2024). Original GitHub issue: https://github.com/ollama/ollama/issues/7871 ### What is the issue? Directly feeding a PNG image does not work (`failed to decode image: image: unknown format`), so until recently, I was using the code bellow in order to encode the image file and it used to work fine: ``` import base64 import io from PIL import Image import ollama def encode_image_to_base64(image_path: str, format: str = "PNG") -> str: """Encodes an image file to a base64 string.""" with Image.open(image_path) as img: buffered = io.BytesIO() img.save(buffered, format=format) return base64.b64encode(buffered.getvalue()).decode('utf-8') response = ollama.chat( model='llama3.2-vision:latest', messages=[{ 'role': 'user', 'content': 'What is in this image?', 'images': [encode_image_to_base64('image1.png')] }] ) # Print the response print(response['message']['content']) ``` But now I get a pydantic serialization error: ``` PydanticSerializationError: Error calling function `serialize_model`: OSError: [Errno 63] File name too long: 'iVBORw0KGgoAAAANSUhEUgAAAOYAAAGQCAIAAAA1BIuEAAEAAElEQ... ``` The only workaround I have found to make it work is to directly convert the PNG image into JPG and then feed the JPG image directly to ollama `llama3.2-vision:latest` model: ``` def convert_to_jpg(image_path: str) -> bytes: """ Convert image to JPG in memory and return the bytes """ with Image.open(image_path) as img: # Convert to RGB if needed if img.mode != 'RGB': img = img.convert('RGB') # Save as JPG to memory buffer buffered = io.BytesIO() img.save(buffered, format='JPEG', quality=85) return buffered.getvalue() response = ollama.chat( model='llama3.2-vision:latest', messages=[{ 'role': 'user', 'content': 'What is in this image?', 'images': [convert_to_jpg('image1.png')] }] ) # Print the response print(response['message']['content']) ``` What could be the reason? Any recent update on either `ollama`, `pydantic` or `pillow` libraries? These are the versions I am using in my `venv`: ``` ollama 0.4.1 pillow 11.0.0 pydantic 2.10.2 pydantic_core 2.27.1 ``` Thanks for your suggestions! ### OS macOS ### GPU Apple ### CPU Apple ### Ollama version 0.4.1 UPDATE: The particular image I was using was wrong format. Apparently it was a 'webp' image wrongly misnamed as PNG. Now feeding directly a proper PNG image works fine, no need for conversion
GiteaMirror added the bug label 2026-04-12 16:07:32 -05:00
Author
Owner

@ibagur commented on GitHub (Nov 28, 2024):

UPDATE:

The particular image I was using was wrong format. Apparently it was a 'webp' image wrongly misnamed as PNG. Now feeding directly a proper PNG image works fine, no need for conversion

<!-- gh-comment-id:2505977090 --> @ibagur commented on GitHub (Nov 28, 2024): UPDATE: The particular image I was using was wrong format. Apparently it was a 'webp' image wrongly misnamed as PNG. Now feeding directly a proper PNG image works fine, no need for conversion
Author
Owner

@rick-github commented on GitHub (Nov 28, 2024):

Although it works for you, it does highlight a problem. The 'images' field can take either a filename or an encoded image, so in your example you could just use 'images': ['image1.png'] and it would work just fine. What you error revealed is that in order to find out what type of input the chat function has received, it does a stat call on the value of the images field to determine whether it's a file or an encoded image. The stat failed because the string of encoded bytes is too long to be a filename, which results in an exception from serialize_model.

<!-- gh-comment-id:2505987895 --> @rick-github commented on GitHub (Nov 28, 2024): Although it works for you, it does highlight a problem. The 'images' field can take either a filename or an encoded image, so in your example you could just use `'images': ['image1.png']` and it would work just fine. What you error revealed is that in order to find out what type of input the `chat` function has received, it does a `stat` call on the value of the `images` field to determine whether it's a file or an encoded image. The `stat` failed because the string of encoded bytes is too long to be a filename, which results in an exception from `serialize_model`.
Author
Owner

@ibagur commented on GitHub (Nov 28, 2024):

Although it works for you, it does highlight a problem. The 'images' field can take either a filename or an encoded image,

Yes, in some previous notebooks I had it was taking a base64 encoded image with no issues. That approach is giving error now. I am changing my code to directly feed the filename as you say. Thanks!

<!-- gh-comment-id:2506479669 --> @ibagur commented on GitHub (Nov 28, 2024): > Although it works for you, it does highlight a problem. The 'images' field can take either a filename or an encoded image, Yes, in some previous notebooks I had it was taking a base64 encoded image with no issues. That approach is giving error now. I am changing my code to directly feed the filename as you say. Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#5036