dspy.Image¶
dspy.Image(source: Any = None, /, **data) ¶
Bases: Type
Create an Image.
Parameters¶
source: The positional-only image source. Supported values include
- ``str``: HTTP(S)/GS URL or an encoded data URI
- ``bytes``: raw image bytes
- ``PIL.Image.Image``: a PIL image instance
- ``dict`` with a single ``{"url": value}`` entry (legacy form)
- already encoded data URI
Any additional keyword arguments are passed to :class:pydantic.BaseModel.
Ordinary construction never touches the filesystem or network. Local files and remote resources must be loaded explicitly with :meth:from_path and :meth:from_url. The removed download and verify constructor keywords raise :class:TypeError; :meth:from_url still accepts verify.
Source code in dspy/adapters/types/image.py
Methods:¶
adapt_to_native_lm_feature(signature: type[Signature], field_name: str, lm: BaseLM, lm_kwargs: dict[str, Any]) -> type[Signature] classmethod ¶
Adapt the custom type to the native LM feature if possible.
When the LM and configuration supports the related native LM feature, e.g., native tool calling, native reasoning, etc., we adapt the signature and lm_kwargs to enable the native LM feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature | type[Signature] | The DSPy signature for the LM call. | required |
field_name | str | The name of the field in the signature to adapt to the native LM feature. | required |
lm | BaseLM | The LM instance. | required |
lm_kwargs | dict[str, Any] | The keyword arguments for the LM call, subject to in-place updates if adaptation if required. | required |
Returns:
| Type | Description |
|---|---|
type[Signature] | The adapted signature. If the custom type is not natively supported by the LM, return the original |
type[Signature] | signature. |
Source code in dspy/adapters/types/base_type.py
description() -> str classmethod ¶
extract_custom_type_from_annotation(annotation) classmethod ¶
Extract all custom types from the annotation.
This is used to extract all custom types from the annotation of a field, while the annotation can have arbitrary level of nesting. For example, we detect Tool is in list[dict[str, Tool]].
Source code in dspy/adapters/types/base_type.py
format() -> list[ImagePart] | str cached ¶
Source code in dspy/adapters/types/image.py
from_path(file_path: str) -> Image classmethod ¶
Read a local file and encode it as a data URI.
Source code in dspy/adapters/types/image.py
from_url(url: str, verify: bool = True, timeout: float | None = 30.0) -> Image classmethod ¶
Download an HTTP(S) resource and encode it as a data URI.
Security: this performs an explicit, caller-initiated fetch and applies no SSRF protection beyond requiring an HTTP(S) scheme. Like requests.get, it will reach private, loopback, or cloud-metadata hosts and follow redirects to them. When url is derived from untrusted input, the caller is responsible for validating the host against an allowlist before calling this method.
Source code in dspy/adapters/types/image.py
is_streamable() -> bool classmethod ¶
parse_lm_response(response: Response) -> Optional[Type] classmethod ¶
Read the native representation of this type out of an lm15 Response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response | Response | The model’s | required |
Returns:
| Type | Description |
|---|---|
Optional[Type] | A custom type object, or None when the response carries nothing for it. |
Source code in dspy/adapters/types/base_type.py
parse_stream_chunk(chunk) -> Optional[Type] classmethod ¶
Parse a stream chunk into the custom type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk | A listener-facing stream chunk ( | required |
Returns:
| Type | Description |
|---|---|
Optional[Type] | A custom type object or None if the chunk is not for this custom type. |