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
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 |
__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 |
required |
Returns:
Type | Description |
---|---|
CoreSchema
|
The Pydantic core schema. |
Source code in .venv/lib/python3.13/site-packages/pydantic/functional_serializers.py
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
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
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. |