Pdf 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 Pdf data. For more details, refer to pdf types.
Pdf instances can be created from URLs, Base64 data, or local files. URL
processing is controlled by your client’s
media_url_handler configuration.
Please note that many websites will block requests to directly fetch PDFs.
Some models like Vertex AI require the media type to be explicitly specified.
Always provide the mediaType parameter when possible for better compatibility.
The PDF input may need to be put into the user message, not the system message in your prompt.
Usage Examples
Test Pdf in the Playground
To test a function that accepts a pdf in the VSCode playground using a local file, add a test block to your .baml file:
The path to the PDF file. Supports relative paths (resolved from the current BAML file) or absolute paths. The file does not need to be inside baml_src/.
API Reference
Python
TypeScript
Go
Ruby
Static Methods
Creates a Pdf object from a URL. The media type is automatically set to application/pdf.
Creates a Pdf object using Base64 encoded data. The media type is automatically set to application/pdf.
Instance Methods
Check if the Pdf is stored as a URL.
Get the URL if the Pdf is stored as a URL. Raises an exception if the Pdf is not stored as a URL.
Get the base64 data and media type if the Pdf is stored as base64. Returns [base64_data, media_type]. Raises an exception if the Pdf is not stored as base64.
Convert the Pdf to a dictionary representation. Returns either {"url": str} or {"base64": str, "media_type": str}.
URL Handling
PDF URLs are processed according to your client’s media_url_handler configuration:
- Anthropic: By default converts to base64 (
send_base64) as required by their API. - AWS Bedrock: By default converts to base64 (
send_base64). - OpenAI: By default keeps URLs as-is (
send_url). - Google AI: By default keeps URLs as-is (
send_url). - Vertex AI: By default keeps URLs as-is (
send_url).
Many websites block direct PDF fetching. If you encounter issues with URL-based PDFs, try:
- Using
media_url_handler.pdf = "send_base64"to fetch and embed the content - Downloading the PDF locally and using
from_file - Using a proxy or authenticated request
Configuring Media URL Handlers
You can customize how PDF URLs are processed by configuring the media_url_handler in your BAML client definition. This is useful when you need to override provider defaults or ensure compatibility with specific model requirements.
Available Modes
The media_url_handler.pdf setting accepts the following values:
-
send_base64: Fetch the PDF from the URL and convert it to base64 before sending to the model. Use this when the provider requires base64 encoding or when you want to ensure the content is embedded in the request. -
send_url: Keep the PDF as a URL and send it directly to the model. The model provider will fetch the content. Note that many providers don’t support direct URL fetching for PDFs. -
send_url_add_mime_type: Keep the PDF as a URL but add MIME type information (application/pdf). Useful for providers like Vertex AI that require explicit MIME types. -
send_base64_unless_google_url: Keep Google Cloud Storage URLs (gs://) as-is, but convert all other URLs to base64. Useful when working with Google AI models.
Provider-Specific Examples
You can configure different handling modes for each media type (image, audio, pdf, video) independently in the same client.
Model Compatibility
Different AI models have varying levels of support for PDF input methods (As of July 2025):
For most models, direct https URLs are not accepted (except Anthropic). Prefer using base64, file uploads, or the appropriate cloud storage/file upload mechanism for your provider. Always specify the correct MIME type (e.g., application/pdf) when required.