Add custom Python code
Agent Builder provides two ways to add custom Python code to your agents: using Python code blocks in your blueprint for inline logic, and using the code editor to create reusable functions and manage files.
When to use custom Python code
Custom Python code is ideal for:
- Complex data processing that goes beyond built-in blocks
- API integrations with external services
- Custom business logic specific to your use case
- File processing and data transformation
- Mathematical calculations and statistical analysis
- Reusable functions across multiple blocks
Variables available in Python code and blocks
The following variables and libraries are available in Python code blocks and other Python files. You can reference them in your code directly.
- State variables: Access your agent’s state using
state["variable_name"]
. - Execution environment: Access environment variables like
result
,payload
,session
. - Pre-installed libraries: Use any of the pre-installed Python libraries like
pandas
,requests
, ornumpy
. - Custom functions: Call functions you’ve defined in your
main.py
file from Python code blocks. - Secrets: Access sensitive data from Vault using
vault["SECRET_NAME"]
.
Python blocks in blueprints
Python code blocks allow you to run custom code directly within your blueprint workflow. They’re useful for one-off operations or processing steps that need to happen at specific points in your agent’s logic.
How to use Python code blocks
- Add a Python block to your blueprint from the blocks library
- Write your code in the Code field
- Access variables directly from the execution environment, state, and secrets
- Return results using the
set_output()
function
Returning values
Use set_output(value)
to pass data to the next block:
The value becomes available as @{result}
in subsequent blocks.
Code editor for custom functions
The code editor lets you create reusable Python functions and manage files for your agent. This is where you build the foundation that your Python blocks can use. Any functions you define in the code editor are available to use in Python code blocks.
Accessing the code editor
- Open your agent in Agent Builder
- Look for the code editor at the bottom of the interface
- Click to expand and start editing your
main.py
file
File structure
Your agent starts with these files:
main.py
: your main Python code file.requirements.txt
: shows the version of thewriter
package that your agent is using.README.md
: documentation for your agent.static/
: folder for static assets like images.
Managing files
Add new files: click ”+ Add file” to create Python modules, data files, or configuration files.
Upload files: click “Upload” to add external files like CSV data, images, or documents.
Organize code: create folders to organize your code by functionality.
Example file structure:
You can’t install additional Python packages in the Agent Builder code editor. Use the pre-installed libraries that are already available. Request new packages in the feature request form.
Common patterns
Initialize state variables
Data validation and cleaning
Using secrets in custom functions
File processing
Debugging
Use print or logger statements to debug your code. The logger
object is globally available in Python blocks.
See more general troubleshooting tips in Troubleshooting.
Next steps
- Review pre-installed Python libraries to see what’s available
- Learn about storing secrets for secure API key management
- Check out the Python block reference for detailed field information
- Learn about state management for better data handling
- Review execution environment variables for accessing block results