Validation error when providing a base64 string to Image(value=...) in Message(images=...) #185

Closed
opened 2026-02-15 16:28:33 -05:00 by yindo · 4 comments
Owner

Originally created by @daniel-iliesh on GitHub (Dec 12, 2024).

Originally assigned to: @ParthSareen on GitHub.

I am encountering a validation error while trying to create a Message object with an attached image using the ollama._types.Message and ollama._types.Image classes.
According to the documentation, the Message constructor’s images parameter should accept a Sequence[Image], and the Image constructor should accept a str, bytes, or Path.
I am passing a single base64-encoded image string directly to the Image(value=...) constructor, but I receive a validation error indicating that the input is being interpreted as an Image object rather than a raw string/bytes/path.

What I did:

  • Constructed a base64-encoded string of an image.
  • Created an Image object with Image(value=my_base64_string).
  • Passed a list of such Image objects into Message(images=...).

Expected behavior:

The Message object should be successfully created with the provided Image objects, each initialized from a raw base64 string.

Actual behavior:

A pydantic validation error is raised:

3 validation errors for Image
value.str
  Input should be a valid string ...
value.bytes
  Input should be a valid bytes ...
value.lax-or-strict[path_validator()] 
  Input is not a valid path ...

Full error:

value.str
  Input should be a valid string [type=string_type, input_value=Image(value='iVBORw0KGgoA...nRTIIAAAAASUVORK5CYII='), input_type=Image]
    For further information visit https://errors.pydantic.dev/2.10/v/string_type
value.bytes
  Input should be a valid bytes [type=bytes_type, input_value=Image(value='iVBORw0KGgoA...nRTIIAAAAASUVORK5CYII='), input_type=Image]
    For further information visit https://errors.pydantic.dev/2.10/v/bytes_type
value.lax-or-strict[lax=union[json-or-python[json=function-after[path_validator(), str],python=is-instance[Path]],function-after[path_validator(), str]],strict=json-or-python[json=function-after[path_validator(), str],python=is-instance[Path]]]
  Input is not a valid path for <class 'pathlib.Path'> [type=path_type, input_value=Image(value='iVBORw0KGgoA...nRTIIAAAAASUVORK5CYII='), input_type=Image]

The error suggests that the Image constructor is somehow receiving another Image object instead of a base64 string.

Additional details:

  • I verified that images.values() returns a base64 str.
  • I am not double-wrapping the Image objects in my code.
  • Using Message(images=[Image(value=my_base64_string)]) triggers the error.

Environment:

Python version: 3.10.14
ollama version: 0.5.1
OS: Fedora 41

Code for reference

...
    async def generate(
        self,
        prompt: str,
        model: str,
        images: Optional[Dict[ImageKind, str]] = None,
        context: Optional[str] = None,
        debug: bool = False,
    ) -> Message:
        self.model = model

        # Prepare the system message
        system_message = Message(
            role="system",
            content=self.system_message(debug=debug),
        )
        if not self.messages:
            self.messages.append(system_message)
        else:
            self.messages[0] = system_message

        # Process images into a list of `Image` objects
        image_list: Sequence[Image] = (
            [Image(value=image_data) for image_data in images.values()]
            if images
            else []
        )

        # Add the user's message
        user_message = Message(
            role="user",
            content=prompt if not context else f"{context}\n{prompt}",
            images=image_list,
        )

        self.messages.append(user_message)
...
Originally created by @daniel-iliesh on GitHub (Dec 12, 2024). Originally assigned to: @ParthSareen on GitHub. I am encountering a validation error while trying to create a `Message` object with an attached image using the `ollama._types.Message` and `ollama._types.Image` classes. According to the documentation, the `Message` constructor’s images parameter should accept a `Sequence[Image]`, and the Image constructor should accept a `str`, `bytes`, or `Path`. I am passing a single base64-encoded image string directly to the `Image(value=...)` constructor, but I receive a validation error indicating that the input is being interpreted as an `Image` object rather than a raw `string/bytes/path`. ### What I did: - Constructed a base64-encoded string of an image. - Created an `Image` object with `Image(value=my_base64_string)`. - Passed a list of such `Image` objects into `Message(images=...)`. ### Expected behavior: The `Message` object should be successfully created with the provided `Image` objects, each initialized from a raw base64 string. ### Actual behavior: A pydantic validation error is raised: ``` 3 validation errors for Image value.str Input should be a valid string ... value.bytes Input should be a valid bytes ... value.lax-or-strict[path_validator()] Input is not a valid path ... ``` #### Full error: ``` value.str Input should be a valid string [type=string_type, input_value=Image(value='iVBORw0KGgoA...nRTIIAAAAASUVORK5CYII='), input_type=Image] For further information visit https://errors.pydantic.dev/2.10/v/string_type value.bytes Input should be a valid bytes [type=bytes_type, input_value=Image(value='iVBORw0KGgoA...nRTIIAAAAASUVORK5CYII='), input_type=Image] For further information visit https://errors.pydantic.dev/2.10/v/bytes_type value.lax-or-strict[lax=union[json-or-python[json=function-after[path_validator(), str],python=is-instance[Path]],function-after[path_validator(), str]],strict=json-or-python[json=function-after[path_validator(), str],python=is-instance[Path]]] Input is not a valid path for <class 'pathlib.Path'> [type=path_type, input_value=Image(value='iVBORw0KGgoA...nRTIIAAAAASUVORK5CYII='), input_type=Image] ``` The error suggests that the `Image` constructor is somehow receiving another `Image` object instead of a base64 string. ### Additional details: - I verified that images.values() returns a base64 str. - I am not double-wrapping the Image objects in my code. - Using Message(images=[Image(value=my_base64_string)]) triggers the error. ### Environment: Python version: 3.10.14 ollama version: 0.5.1 OS: Fedora 41 ### Code for reference ```python ... async def generate( self, prompt: str, model: str, images: Optional[Dict[ImageKind, str]] = None, context: Optional[str] = None, debug: bool = False, ) -> Message: self.model = model # Prepare the system message system_message = Message( role="system", content=self.system_message(debug=debug), ) if not self.messages: self.messages.append(system_message) else: self.messages[0] = system_message # Process images into a list of `Image` objects image_list: Sequence[Image] = ( [Image(value=image_data) for image_data in images.values()] if images else [] ) # Add the user's message user_message = Message( role="user", content=prompt if not context else f"{context}\n{prompt}", images=image_list, ) self.messages.append(user_message) ... ```
yindo closed this issue 2026-02-15 16:28:33 -05:00
Author
Owner

@ParthSareen commented on GitHub (Dec 12, 2024):

Hey @daniel-iliesh. Dug into this a bit - I think it is to be expected. I think the fix here would be passing in

image_list: Sequence[str] = [image_data for image_data in image.values()]

This is since the generate function accepts a sequence of string or bytes:

...
    images: Optional[Sequence[Union[str, bytes]]] = None,
...

Please lmk if you still run into it after this!

@ParthSareen commented on GitHub (Dec 12, 2024): Hey @daniel-iliesh. Dug into this a bit - I think it is to be expected. I think the fix here would be passing in ```py image_list: Sequence[str] = [image_data for image_data in image.values()] ``` This is since the generate function accepts a sequence of string or bytes: ```py ... images: Optional[Sequence[Union[str, bytes]]] = None, ... ``` Please lmk if you still run into it after this!
Author
Owner

@daniel-iliesh commented on GitHub (Dec 13, 2024):

Hey @ParthSareen, thank you for the response.

I did not quite understood the resolution.

I've checked the Message class and it seems that it requires a sequence of Image objects for images:

class Message(
    *,
    role: Literal['user', 'assistant', 'system', 'tool'],
    content: str | None = None,
    images: Sequence[Image] | None = None,
    tool_calls: Sequence[ToolCall] | None = None
)

So when i try to do this:

image_list: Sequence[str] = [image_data for image_data in image.values()]

i get this when trying to create a Message:

Argument of type "list[str]" cannot be assigned to parameter "images" of type "Sequence[Image] | None" in function "__init__"
  Type "list[str]" is not assignable to type "Sequence[Image] | None"
    "list[str]" is not assignable to "Sequence[Image]"
      Type parameter "_T_co@Sequence" is covariant, but "str" is not a subtype of "Image"
        "str" is not assignable to "Image"
    "list[str]" is not assignable to "None"

Also the generate function accepts

...
images: Optional[Dict[ImageKind, str]] = None,
...

Not this

...
    images: Optional[Sequence[Union[str, bytes]]] = None,
...

ImageKind being just an internal type of mine, it is not related to the problem as i use only the values from the dict which are base64 strings.

@daniel-iliesh commented on GitHub (Dec 13, 2024): Hey @ParthSareen, thank you for the response. I did not quite understood the resolution. I've checked the [`Message` class](https://github.com/ollama/ollama-python/blob/70dd0b7e639f5b6b39e15963521f1d4c6810464b/ollama/_types.py#L255) and it seems that it requires a sequence of `Image` objects for `images`: ```python class Message( *, role: Literal['user', 'assistant', 'system', 'tool'], content: str | None = None, images: Sequence[Image] | None = None, tool_calls: Sequence[ToolCall] | None = None ) ``` So when i try to do this: > ```python > image_list: Sequence[str] = [image_data for image_data in image.values()] > ``` i get this when trying to create a `Message`: ``` Argument of type "list[str]" cannot be assigned to parameter "images" of type "Sequence[Image] | None" in function "__init__"   Type "list[str]" is not assignable to type "Sequence[Image] | None"     "list[str]" is not assignable to "Sequence[Image]"       Type parameter "_T_co@Sequence" is covariant, but "str" is not a subtype of "Image"         "str" is not assignable to "Image"     "list[str]" is not assignable to "None" ``` Also the `generate` function accepts > ```python > ... > images: Optional[Dict[ImageKind, str]] = None, > ... > ``` Not this > ```python > ... > images: Optional[Sequence[Union[str, bytes]]] = None, > ... > ``` `ImageKind` being just an internal type of mine, it is not related to the problem as i use only the values from the `dict` which are `base64` strings.
Author
Owner

@ggozad commented on GitHub (Dec 29, 2024):

I also see the same issue. I have a list of base64 encoded images.
Constructing a message by

m = Message(
  role="user",
  content="...",
  images=[Image(i) for I in images]
)

leads to a model validation error not during the contraction of the message but rather later when calling chat. The culprit is _copy_messages() which seems to be constructing images again.

#386 seems to fix the problem.

@ggozad commented on GitHub (Dec 29, 2024): I also see the same issue. I have a list of base64 encoded images. Constructing a message by ``` m = Message( role="user", content="...", images=[Image(i) for I in images] ) ``` leads to a model validation error not during the contraction of the message but rather later when calling `chat`. The culprit is `_copy_messages()` which seems to be constructing images again. #386 seems to fix the problem.
Author
Owner

@ParthSareen commented on GitHub (Jan 21, 2025):

Closed with https://github.com/ollama/ollama-python/pull/390

@ParthSareen commented on GitHub (Jan 21, 2025): Closed with https://github.com/ollama/ollama-python/pull/390
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#185