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 logreturn {"order": data["order"], "total": round(total, 2)} # goes on to the next component| In your code | What it is |
|---|---|
data | The data arriving through Input Data: a record with its fields, or a list of records. |
return | What 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. |
env | The variables of the environment assigned to the flow, for example env.get("CURRENCY"). |
volume | The folder of your storage where files are kept. See Packages and volumes. |
tools and models | The connected tools and models. See Tools and models. |
args | The data an agent sends when the component is its tool. See As an agent’s tool. |
components | What 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 dataand 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 runs | Packages and volumes |
| Use tools and AI models from the code | Tools and models |
| Give your code to an agent as a tool | As an agent’s tool |