Skip to content

Your code in flows

Python Code lets you write your own logic in Python inside a flow, when no component does exactly what you need: calculate, clean or combine data, apply your business rules, read and save files, or go through a whole list in one go.

In this video you will see all its uses: packages, storage volumes, tools and models called from the code, and the component as an agent’s tool.

How it works

The component receives the data from the previous step, runs your code in isolation, and delivers whatever your code returns.

# The input data arrives in "data"
total = data["quantity"] * data["price"]
print("Order", data["order"], "calculated") # shows up in the run's log
return {"order": data["order"], "total": round(total, 2)} # goes on to the next component
In your codeWhat it is
dataThe data arriving through Input Data: a record with its fields, or a list of records.
returnWhat you hand to the next component. Without return, the component delivers nothing.
print()Messages that appear in the run’s log, without being part of the result.
envThe variables of the environment assigned to the flow, for example env.get("CURRENCY").
volumeThe folder of your storage where files are kept. See Packages and volumes.
tools and modelsThe connected tools and models. See Tools and models.
argsThe data an agent sends when the component is its tool. See As an agent’s tool.
componentsWhat other components of the flow returned in this run, by their id.

Dates and amounts coming from a database become text on their own, and you can use await directly if your code is asynchronous.

Writing the code

  • In the component panel, the Python Code field opens full screen with its expand button.
  • If you drag a field of the previous run into the editor, the way to read it is written for you, for example data.get("amount", None). The same with an environment variable: env.get("KEY", None) is written.
  • To see how your data arrives, start with return data and run the component.

Where it runs

Each run is isolated from the rest: your code cannot touch other flows or the platform, but it can reach the internet to call other services. Files saved outside the volume are deleted when it finishes. Each run has limited memory and time, up to one hour.

An execution profile can block the use of Python Code in your account’s runs.

Keep reading

To…Read
Install packages and keep files between runsPackages and volumes
Use tools and AI models from the codeTools and models
Give your code to an agent as a toolAs an agent’s tool