Python Sandbox
The Python sandbox lets you deploy any Python function as a callable tool. The function runs in an isolated environment and can be used standalone or attached to an agent.
Setup
pip install aixplain
from aixplain import Aixplain
import inspect
import time
aix = Aixplain(api_key="YOUR_API_KEY")
Quick start
def add_numbers(a: int, b: int):
return a + b
script_tool = aix.Tool(
name=f"Addition Tool {int(time.time())}",
integration="688779d8bfb8e46c273982ca", # Python Sandbox
config={
"code": inspect.getsource(add_numbers),
"function_name": "add_numbers",
},
)
script_tool.save()
result = script_tool.run(data={"a": 4, "b": 6}, action="add_numbers")
print(result.data) # 10
Show output
Create a script tool
1. Define and extract the function
Use inspect.getsource() to capture the function source as a string:
def calculate_statistics(numbers: list):
import statistics
return {
"mean": statistics.mean(numbers),
"median": statistics.median(numbers),
"stdev": statistics.stdev(numbers) if len(numbers) > 1 else 0,
"count": len(numbers),
}
script_content = inspect.getsource(calculate_statistics)
To load from a file instead:
with open("my_function.py") as f:
script_content = f.read()
Function requirements:
Most legacy authoring rules have been relaxed — multi-line def signatures, short parameter names, default values, missing type hints, top-level imports, and zero-argument functions all work. The only hard constraints today are:
function_namemust exactly match a function defined incode. Helper functions in the same file are fine — only the one named byfunction_nameis registered as the tool- Do not use
boolparameters. The runtime serializer emits JSONtrue/false(lowercase), which then errors inside Python withNameError: name 'true' is not defined. Useint(0/1) instead until this is fixed - Avoid returning tuples or unpacking multiple values. Tuple returns round-trip as a string repr (e.g.
"(2, 3)") rather than structured data. Return adictorlistwhen you need more than one value - Return values must be JSON-serialisable (dicts, lists, strings, numbers)
Type hints are no longer enforced, but we still recommend them — they help the agent infer a clean tool schema and make the function easier to call correctly.