Skip to main content
The Python Tool step allows you to use Python to take outputs from other Tool steps and manipulate data for other Tool steps and LLMs.
In order to use this Tool step, we would recommend having a beginner level of understanding of Python programming, or use our Chat to walk you through implementing it at chat.relevanceai.com.

What is Python?

Python is a widely used programming language known for its clarity and simplicity. It’s popular among beginners and experts alike because it’s easy to read, quick to write, and versatile enough for tasks ranging from web development and automation to data analysis and machine learning. With a massive library ecosystem and strong community support, Python is a powerful tool for solving real-world problems efficiently.

Python Package Ecosystem

One of Python’s greatest strengths is its extensive package ecosystem. You can install and use any package available on the Python Package Index (PyPI), which hosts over 693,000 projects with millions of releases. This means you have access to virtually any functionality you need across countless domains.
  • pandas - Data manipulation and analysis
  • numpy - Numerical computing and arrays
  • matplotlib - Data visualization and plotting
  • seaborn - Statistical data visualization
  • plotly - Interactive data visualization
  • requests - HTTP library for making web requests
  • beautifulsoup4 - Web scraping and HTML parsing
  • scrapy - Web crawling framework
  • selenium - Browser automation and testing
  • scikit-learn - Machine learning library
  • tensorflow - Deep learning framework
  • pytorch - Deep learning and neural networks
  • transformers - Pre-trained language models
  • openai - OpenAI API client
  • openpyxl - Excel file manipulation
  • python-docx - Word document processing
  • PyPDF2 - PDF file manipulation
  • pillow - Image processing library
  • csv - Built-in CSV file handling
  • flask - Lightweight web framework
  • fastapi - Modern web API framework
  • requests-oauthlib - OAuth authentication
  • twilio - SMS and communication APIs
  • stripe - Payment processing
  • sqlalchemy - SQL toolkit and ORM
  • psycopg2 - PostgreSQL adapter
  • pymongo - MongoDB driver
  • redis - Redis database client
To find and explore available packages, visit PyPI.org where you can search through the vast collection of Python packages. Simply add any package you need to your Python Tool step using the ‘PyPI Packages’ section.

Adding the Python Tool step into your Tool

You can add the Python Tool step to your Tool by:
  1. Creating a new Tool, then clicking or searching for the Python Tool step
  2. Click ’+ New item’ under ‘PyPI Packages’ if you need to install any packages, and enter the package name you need.
  3. Enter your code in the ‘Code’ field, including any imports you need.
  4. Click ‘Run step’ to run the Tool step
In order to access the output of this codeblock, you will need to return a value in the return statement.

Accessing variables

You can access Tool input variables, other Tool step outputs, and API keys you’ve created in Relevance in your Python Tool step.

Input variables

You can access Input variables by simply using the variable name directly, where ‘name’ is the variable for your input.
name

Other Tool step outputs

You can access other Tool step outputs by using the variable name directly, where ‘llm’ is the variable for the Tool step you’re trying to access the output of.
llm
This returns the full output object. To access a specific field within that output, use dictionary notation:
llm["answer"]
For example, if an LLM step returns {"answer": "Hello!"}, then:
  • llm returns {"answer": "Hello!"}
  • llm["answer"] returns "Hello!"

API keys

You can access one of your saved API keys by using this syntax, where ‘api_key’ is the title of your API key.
"{{secrets.chains_api_key}}"

Advanced Settings

Several advanced settings (Session ID, Number of GPUs, Number of CPU Cores, Memory Size, and Session Timeout) are only available when the backend is set to ‘Modal Labs’.

Backend

You can choose between two backend services to run the code, ‘Modal Labs’ or ‘Daytona’.

Runtime Commands

You can include a list of Linux terminal commands to be run before running your code.

Session ID

Define a custom session that saves all local variables between Python executions.

Enable ‘Long Output’ Mode

Allow the output to be longer than 127,000 characters, in exchange for 2-4 seconds of extra latency.

Number of GPUs

Allows you to specify a number of GPUs for your code to use.

Number of CPU Cores

Allows you to specify a number of CPU cores for your code to use.

Memory Size

Allows you to specify an amount of RAM for your code to use.

Session Timeout

Allows you to specify (in seconds) how long the code should run before timing out.

Raise Error

Allows you to specify how errors are raised:
  • Error with Full Traceback - Shows the complete error traceback including file names, line numbers, and the full call stack. This is useful for debugging complex issues.
  • Error - Shows only the error message without the detailed traceback. This provides a cleaner output for production use.
  • Print Error to Stderr - Prints the error to stderr instead of raising it, allowing your code to continue execution even when errors occur.

Enable fallback

If the selected backend fails, when this is selected, your code will attempt to run via the other backend.

Common errors

This error indicates that the code took longer than the seconds allowed in the ‘Session timeout’ Advanced Setting, or exceeded our hard limit of four hours.
This error indicates that there is no return statement in the code. You will not be able to access the output of this codeblock unless you pass data in a return statement. If the transformed section of the output is showing null, it means that there is nothing being returned from your code.
WARNING: There is no return statement in this code. To access the output of this codeblock, return a value.

Additional resources