Image

Image values to BAML functions can be created in client libraries. This document explains how to use these functions both at compile time and runtime to handle image data. For more details, refer to image types.

Usage Examples

1from baml_py import Image
2from baml_client import b
3
4async def test_image_input():
5 # Create an Image from a URL
6 img = Image.from_url("https://upload.wikimedia.org/wikipedia/en/4/4d/Shrek_%28character%29.png")
7 res = await b.TestImageInput(img=img)
8
9 # Create an Image from Base64 data
10 image_b64 = "iVB0xyz..."
11 img = Image.from_base64("image/png", image_b64)
12 res = await b.TestImageInput(img=img)

Static Methods

fromUrl
(url: string, mediaType?: string) => Image

Creates an Image object from a URL. Optionally specify the media type, otherwise it will be inferred from the URL.

fromBase64
(mediaType: string, base64: string) => Image

Creates an Image object using Base64 encoded data along with the given MIME type.

fromFile
(file: File) => Promise<Image>

Only available in browser environments. @boundaryml/baml/browser
Creates an Image object from a File object. Available in browser environments only.

fromBlob
(blob: Blob, mediaType?: string) => Promise<Image>

Only available in browser environments. @boundaryml/baml/browser
Creates an Image object from a Blob object. Available in browser environments only.

fromUrlToBase64
(url: string) => Promise<Image>

Only available in browser environments.
Creates an Image object by fetching from a URL. Available in browser environments only.

Instance Methods

isUrl
() => boolean

Check if the image is stored as a URL.

asUrl
() => string

Get the URL of the image if it’s stored as a URL. Throws an Error if the image is not stored as a URL.

asBase64
() => [string, string]

Get the base64 data and media type if the image is stored as base64. Returns [base64Data, mediaType]. Throws an Error if the image is not stored as base64.

toJSON
() => { url: string } | { base64: string; media_type: string }

Convert the image to a JSON representation. Returns either a URL object or a base64 object with media type.

URL Handling

When you create an Image using from_url, BAML processes the URL according to your client’s media_url_handler configuration:

  • OpenAI: By default keeps URLs as-is (send_url). Set to send_base64 to convert to base64.
  • Anthropic: By default keeps URLs as-is (send_url). The provider accepts both formats.
  • Google AI: By default uses send_base64_unless_google_url to preserve gs:// URLs while converting others.
  • Vertex AI: By default uses send_url_add_mime_type to include MIME type information.
  • AWS Bedrock: By default converts to base64 (send_base64).

You can override these defaults in your client configuration. See the provider-specific documentation linked above for details.