Skip to content

ImageBase64Content

Base classes

Name Children Inherits
BaseImageContent
llmling_agent.models.content
Base for image content.

⋔ Inheritance diagram

graph TD
  94060275456640["content.ImageBase64Content"]
  94060279283504["content.BaseImageContent"]
  94060279310192["content.BaseContent"]
  94060260503136["schema.Schema"]
  94060251496624["main.BaseModel"]
  140544442243552["builtins.object"]
  94060279283504 --> 94060275456640
  94060279310192 --> 94060279283504
  94060260503136 --> 94060279310192
  94060251496624 --> 94060260503136
  140544442243552 --> 94060251496624

🛈 DocStrings

Bases: BaseImageContent

Image from base64 data.

Source code in src/llmling_agent/models/content.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class ImageBase64Content(BaseImageContent):
    """Image from base64 data."""

    type: Literal["image_base64"] = Field("image_base64", init=False)
    """Base64-encoded image."""

    data: str
    """Base64-encoded image data."""

    mime_type: str = "image/jpeg"
    """MIME type of the image."""

    def to_pydantic_ai(self) -> BinaryImage:
        binary_data = base64.b64decode(self.data)
        return BinaryImage(data=binary_data, media_type=self.mime_type or "image/jpeg")

    @classmethod
    def from_bytes(
        cls,
        data: bytes,
        *,
        detail: DetailLevel | None = None,
        description: str | None = None,
    ) -> ImageBase64Content:
        """Create image content from raw bytes.

        Args:
            data: Raw image bytes
            detail: Optional detail level for processing
            description: Optional description of the image
        """
        content = base64.b64encode(data).decode()
        return cls(data=content, detail=detail, description=description)

    @classmethod
    def from_pil_image(cls, image: PIL.Image.Image) -> ImageBase64Content:
        """Create content from PIL Image."""
        with io.BytesIO() as buffer:
            image.save(buffer, format="PNG")
            return cls(data=base64.b64encode(buffer.getvalue()).decode())

data instance-attribute

data: str

Base64-encoded image data.

mime_type class-attribute instance-attribute

mime_type: str = 'image/jpeg'

MIME type of the image.

type class-attribute instance-attribute

type: Literal['image_base64'] = Field('image_base64', init=False)

Base64-encoded image.

from_bytes classmethod

from_bytes(
    data: bytes, *, detail: DetailLevel | None = None, description: str | None = None
) -> ImageBase64Content

Create image content from raw bytes.

Parameters:

Name Type Description Default
data bytes

Raw image bytes

required
detail DetailLevel | None

Optional detail level for processing

None
description str | None

Optional description of the image

None
Source code in src/llmling_agent/models/content.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
@classmethod
def from_bytes(
    cls,
    data: bytes,
    *,
    detail: DetailLevel | None = None,
    description: str | None = None,
) -> ImageBase64Content:
    """Create image content from raw bytes.

    Args:
        data: Raw image bytes
        detail: Optional detail level for processing
        description: Optional description of the image
    """
    content = base64.b64encode(data).decode()
    return cls(data=content, detail=detail, description=description)

from_pil_image classmethod

from_pil_image(image: Image) -> ImageBase64Content

Create content from PIL Image.

Source code in src/llmling_agent/models/content.py
129
130
131
132
133
134
@classmethod
def from_pil_image(cls, image: PIL.Image.Image) -> ImageBase64Content:
    """Create content from PIL Image."""
    with io.BytesIO() as buffer:
        image.save(buffer, format="PNG")
        return cls(data=base64.b64encode(buffer.getvalue()).decode())

Show source on GitHub