Skip to main content
One of the most common use cases for AI agents is working with files. Your agent should be able to read files from user uploads and generate new files as outputs. The Agent Stack makes this seamless through the A2A protocol’s FilePart.

Example of File Processing

Here’s how to build an agent that can accept and modify files:
1

Enable file uploads in your agent (optional)

Add the default_input_modes parameter to your agent decorator only if you want users to upload files to your agent. This specifies which file types users can upload.
2

Inject the Platform API extension

Import and use the PlatformApiExtensionServer to access file creation capabilities. This extension provides your agent with the proper context and authentication needed to use the Agent Stack API for creating and managing files. If not provided, your agent will receive unauthorized responses when working with files.
3

Process uploaded files

Iterate through message parts to find FilePart objects and load their content using load_file helper.
4

Generate new files

Use the File.create() method to generate new files and yield them as FilePart objects with to_file_part().

How to work with files

Here’s what you need to know to add file processing capabilities to your agent: Enable file uploads: Add default_input_modes to your agent decorator with a list of MIME types you want to accept (e.g., ["text/plain", "application/pdf", "image/jpeg"]). Enable producing of files: Add default_output_modes to your agent decorator with a list of MIME types that your agent can potentially produce (e.g., ["text/plain", "application/pdf", "image/jpeg"]). Access the Platform API: Import and use PlatformApiExtensionServer to get access to file manipulation capabilities. Process message parts: Iterate through input.parts to find FilePart objects that represent uploaded files. Load file content: Use load_file() with an async context manager to safely access file content. Create new files: Use File.create() to generate new files with custom names, content types, and content. Yield file outputs: The File object created by the SDK can be easily converted to a FilePart using the to_file_part() method and then yielded as agent outputs.

File Upload Configuration

The default_input_modes parameter controls which file types users can upload:
The default_output_modes parameter controls which file agent can produce:
Common MIME types you might want to support:

Handling Text vs Binary Files

When processing files, it’s important to handle text and binary files differently. The load_file helper provides both .text and .content properties:
Key differences:
  • Text files (text/plain, text/markdown, application/json, etc.): Use loaded_content.text.encode() to get bytes.
  • Binary files (application/pdf, image/*, etc.): Use loaded_content.content directly to preserve binary content.