pub struct ImageBlock { /* private fields */ }Expand description
Image content block for vision-capable models.
Supports both URL-based images and base64-encoded images.
§Examples
use open_agent::{ImageBlock, ImageDetail};
// From URL
let image = ImageBlock::from_url("https://example.com/image.jpg")?;
// From base64 (use properly formatted base64)
let image = ImageBlock::from_base64("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", "image/png")?;
// With detail level
let image = ImageBlock::from_url("https://example.com/image.jpg")?
.with_detail(ImageDetail::High);Implementations§
Source§impl ImageBlock
impl ImageBlock
Sourcepub fn from_url(url: impl Into<String>) -> Result<Self>
pub fn from_url(url: impl Into<String>) -> Result<Self>
Creates a new image block from a URL.
§Arguments
url- The image URL (must be HTTP, HTTPS, or data URI)
§Errors
Returns Error::InvalidInput if:
- URL is empty
- URL contains control characters (newline, tab, null, etc.)
- URL scheme is not
http://,https://, ordata: - Data URI is malformed (missing MIME type or base64 encoding)
- Data URI base64 portion has invalid characters, length, or padding
§Warnings
- Logs a warning to stderr if URL exceeds 2000 characters
§Example
use open_agent::ImageBlock;
let image = ImageBlock::from_url("https://example.com/cat.jpg")?;
assert_eq!(image.url(), "https://example.com/cat.jpg");Sourcepub fn from_base64(
base64_data: impl AsRef<str>,
mime_type: impl AsRef<str>,
) -> Result<Self>
pub fn from_base64( base64_data: impl AsRef<str>, mime_type: impl AsRef<str>, ) -> Result<Self>
Creates a new image block from base64-encoded data.
§Arguments
base64_data- The base64-encoded image datamime_type- The MIME type (e.g., “image/jpeg”, “image/png”)
§Errors
Returns Error::InvalidInput if:
- Base64 data is empty
- Base64 contains invalid characters (only A-Z, a-z, 0-9, +, /, = allowed)
- Base64 length is not a multiple of 4
- Base64 has invalid padding (more than 2 ‘=’ characters or not at end)
- MIME type is empty
- MIME type does not start with “image/”
- MIME type contains injection characters (;, \n, \r, ,)
§Warnings
- Logs a warning to stderr if base64 data exceeds 10MB (~7.5MB decoded)
§Example
use open_agent::ImageBlock;
let base64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";
let image = ImageBlock::from_base64(base64, "image/png")?;
assert!(image.url().starts_with("data:image/png;base64,"));Sourcepub fn from_file_path(path: impl AsRef<Path>) -> Result<Self>
pub fn from_file_path(path: impl AsRef<Path>) -> Result<Self>
Creates a new image block from a local file path.
This is a convenience method that reads the file from disk, encodes it as base64, and creates an ImageBlock with a data URI. The MIME type is inferred from the file extension.
§Arguments
path- Path to the image file on the local filesystem
§Errors
Returns Error::InvalidInput if:
- File cannot be read
- File extension is missing or unsupported
- File is too large (>10MB warning)
§Supported Formats
.jpg,.jpeg→image/jpeg.png→image/png.gif→image/gif.webp→image/webp.bmp→image/bmp.svg→image/svg+xml
§Example
use open_agent::ImageBlock;
let image = ImageBlock::from_file_path("/path/to/photo.jpg")?;§Security Note
This method reads files from the local filesystem. Ensure the path comes from a trusted source to prevent unauthorized file access.
Sourcepub fn with_detail(self, detail: ImageDetail) -> Self
pub fn with_detail(self, detail: ImageDetail) -> Self
Sets the image detail level.
§Example
use open_agent::{ImageBlock, ImageDetail};
let image = ImageBlock::from_url("https://example.com/image.jpg")?
.with_detail(ImageDetail::High);Sourcepub fn detail(&self) -> ImageDetail
pub fn detail(&self) -> ImageDetail
Returns the image detail level.
Trait Implementations§
Source§impl Clone for ImageBlock
impl Clone for ImageBlock
Source§fn clone(&self) -> ImageBlock
fn clone(&self) -> ImageBlock
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ImageBlock
impl Debug for ImageBlock
Source§impl<'de> Deserialize<'de> for ImageBlock
impl<'de> Deserialize<'de> for ImageBlock
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for ImageBlock
impl RefUnwindSafe for ImageBlock
impl Send for ImageBlock
impl Sync for ImageBlock
impl Unpin for ImageBlock
impl UnwindSafe for ImageBlock
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more