Skip to content

mistral_common.multimodal

BeforeValidator(func, json_schema_input_type=PydanticUndefined) dataclass

!!! abstract "Usage Documentation" field before validators

A metadata class that indicates that a validation should be applied before the inner validation logic.

Attributes:

Name Type Description
func NoInfoValidatorFunction | WithInfoValidatorFunction

The validator function.

json_schema_input_type Any

The input type of the function. This is only used to generate the appropriate JSON Schema (in validation mode).

Example
from typing import Annotated

from pydantic import BaseModel, BeforeValidator

MyInt = Annotated[int, BeforeValidator(lambda v: v + 1)]

class Model(BaseModel):
    a: MyInt

print(Model(a=1).a)
#> 2

try:
    Model(a='a')
except TypeError as e:
    print(e)
    #> can only concatenate str (not "int") to str

PlainSerializer(func, return_type=PydanticUndefined, when_used='always') dataclass

Plain serializers use a function to modify the output of serialization.

This is particularly helpful when you want to customize the serialization for annotated types. Consider an input of list, which will be serialized into a space-delimited string.

from typing import Annotated

from pydantic import BaseModel, PlainSerializer

CustomStr = Annotated[
    list, PlainSerializer(lambda x: ' '.join(x), return_type=str)
]

class StudentModel(BaseModel):
    courses: CustomStr

student = StudentModel(courses=['Math', 'Chemistry', 'English'])
print(student.model_dump())
#> {'courses': 'Math Chemistry English'}

Attributes:

Name Type Description
func SerializerFunction

The serializer function.

return_type Any

The return type for the function. If omitted it will be inferred from the type annotation.

when_used WhenUsed

Determines when this serializer should be used. Accepts a string with values 'always', 'unless-none', 'json', and 'json-unless-none'. Defaults to 'always'.

__get_pydantic_core_schema__(source_type, handler)

Gets the Pydantic core schema.

Parameters:

Name Type Description Default
source_type Any

The source type.

required
handler GetCoreSchemaHandler

The GetCoreSchemaHandler instance.

required

Returns:

Type Description
CoreSchema

The Pydantic core schema.

Source code in .venv/lib/python3.13/site-packages/pydantic/functional_serializers.py
def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
    """Gets the Pydantic core schema.

    Args:
        source_type: The source type.
        handler: The `GetCoreSchemaHandler` instance.

    Returns:
        The Pydantic core schema.
    """
    schema = handler(source_type)
    if self.return_type is not PydanticUndefined:
        return_type = self.return_type
    else:
        try:
            # Do not pass in globals as the function could be defined in a different module.
            # Instead, let `get_callable_return_type` infer the globals to use, but still pass
            # in locals that may contain a parent/rebuild namespace:
            return_type = _decorators.get_callable_return_type(
                self.func,
                localns=handler._get_types_namespace().locals,
            )
        except NameError as e:
            raise PydanticUndefinedAnnotation.from_name_error(e) from e

    return_schema = None if return_type is PydanticUndefined else handler.generate_schema(return_type)
    schema['serialization'] = core_schema.plain_serializer_function_ser_schema(
        function=self.func,
        info_arg=_decorators.inspect_annotated_serializer(self.func, 'plain'),
        return_schema=return_schema,
        when_used=self.when_used,
    )
    return schema

download_image(url)

Download an image from a URL and return it as a PIL Image.

Parameters:

Name Type Description Default
url str

The URL of the image to download.

required

Returns:

Type Description
Image

The downloaded image as a PIL Image object.

Source code in src/mistral_common/image.py
def download_image(url: str) -> Image.Image:
    r"""Download an image from a URL and return it as a PIL Image.

    Args:
        url: The URL of the image to download.

    Returns:
       The downloaded image as a PIL Image object.
    """
    headers = {"User-Agent": f"mistral-common/{__version__}"}
    try:
        # Make a request to download the image
        response = requests.get(url, headers=headers)
        response.raise_for_status()  # Raise an error for bad responses (4xx, 5xx)

        # Convert the image content to a PIL Image
        img = Image.open(io.BytesIO(response.content))
        return img

    except requests.exceptions.RequestException as e:
        raise RuntimeError(f"Error downloading the image from {url}: {e}.")
    except Exception as e:
        raise RuntimeError(f"Error converting to PIL image: {e}")

maybe_load_image_from_str_or_bytes(x)

Load an image from a string or bytes.

If the input is already a PIL Image, return it as is.

Parameters:

Name Type Description Default
x Union[Image, str, bytes]

The input to load the image from. Can be a PIL Image, a string, or bytes. If it's a string, it's assumed to be a base64 encoded string of bytes.

required

Returns:

Type Description
Image

The loaded image as a PIL Image object.

Source code in src/mistral_common/image.py
def maybe_load_image_from_str_or_bytes(x: Union[Image.Image, str, bytes]) -> Image.Image:
    r"""Load an image from a string or bytes.

    If the input is already a PIL Image, return it as is.

    Args:
        x: The input to load the image from. Can be a PIL Image, a string, or bytes.
            If it's a string, it's assumed to be a base64 encoded string of bytes.

    Returns:
       The loaded image as a PIL Image object.
    """
    if isinstance(x, Image.Image):
        return x
    if isinstance(x, bytes):
        try:
            return Image.open(io.BytesIO(x))
        except Exception:
            raise RuntimeError("Encountered an error when loading image from bytes.")

    try:
        image = Image.open(io.BytesIO(base64.b64decode(x.encode("ascii"))))
        return image
    except Exception as e:
        raise RuntimeError(
            f"Encountered an error when loading image from bytes starting "
            f"with '{x[:20]}'. Expected either a PIL.Image.Image or a base64 "
            f"encoded string of bytes."
        ) from e

serialize_image_to_byte_str(im, info)

Serialize an image to a base64 encoded string of bytes.

Parameters:

Name Type Description Default
im Image

The image to serialize.

required
info SerializationInfo

The serialization info.

required

Returns:

Type Description
str

The serialized image as a base64 encoded string of bytes.

Source code in src/mistral_common/image.py
def serialize_image_to_byte_str(im: Image.Image, info: SerializationInfo) -> str:
    r"""Serialize an image to a base64 encoded string of bytes.

    Args:
        im: The image to serialize.
        info: The serialization info.

    Returns:
        The serialized image as a base64 encoded string of bytes.
    """
    if hasattr(info, "context"):
        context = info.context or {}
    else:
        context = {}

    stream = io.BytesIO()
    im_format = im.format or "PNG"
    im.save(stream, format=im_format)
    im_b64 = base64.b64encode(stream.getvalue()).decode("ascii")
    if context and (max_image_b64_len := context.get("max_image_b64_len")):
        return im_b64[:max_image_b64_len] + "..."
    if context and context.get("add_format_prefix"):
        im_b64 = f"data:image/{im_format.lower()};base64," + im_b64
    return im_b64