Skip to content

dspy.Image

dspy.Image

Bases: BaseModel

Functions

from_PIL(pil_image) classmethod

Source code in dspy/adapters/types/image.py
@classmethod
def from_PIL(cls, pil_image):
    return cls(url=encode_image(pil_image))

from_file(file_path: str) classmethod

Source code in dspy/adapters/types/image.py
@classmethod
def from_file(cls, file_path: str):
    return cls(url=encode_image(file_path))

from_url(url: str, download: bool = False) classmethod

Source code in dspy/adapters/types/image.py
@classmethod
def from_url(cls, url: str, download: bool = False):
    return cls(url=encode_image(url, download))

serialize_model()

Source code in dspy/adapters/types/image.py
@pydantic.model_serializer()
def serialize_model(self):
    return "<DSPY_IMAGE_START>" + self.url + "<DSPY_IMAGE_END>"

validate_input(values) classmethod

Source code in dspy/adapters/types/image.py
@pydantic.model_validator(mode="before")
@classmethod
def validate_input(cls, values):
    # Allow the model to accept either a URL string or a dictionary with a single 'url' key
    if isinstance(values, str):
        # if a string, assume it's the URL directly and wrap it in a dict
        return {"url": values}
    elif isinstance(values, dict) and set(values.keys()) == {"url"}:
        # if it's a dict, ensure it has only the 'url' key
        return values
    elif isinstance(values, cls):
        return values.model_dump()
    else:
        raise TypeError("Expected a string URL or a dictionary with a key 'url'.")