Skip to content

Python

Python 3.13 is the default runtime and the most commonly used option on the platform.


Handler signature

Your main file must be named cortexone_function.py and must define a function called cortexone_handler:

def cortexone_handler(event, context):
result = {"message": "Hello, World!"}
return {"statusCode": 200, "body": result}

When your data contains types that don’t serialize natively (e.g. numpy arrays, int64), wrap with json.dumps():

import json
import numpy as np
def cortexone_handler(event, context):
arr = np.array([1, 2, 3])
return {"statusCode": 200, "body": json.dumps({"values": arr.tolist()})}
ParameterTypeDescription
eventdictThe input payload sent by the caller
contextobjectExecution metadata

The event object

event is whatever JSON the caller passes. For example, if the caller sends:

{ "name": "Alice", "numbers": [1, 2, 3] }

Your handler receives:

def cortexone_handler(event, context):
name = event["name"]
numbers = event["numbers"]
total = sum(numbers)
return {"statusCode": 200, "body": {"greeting": f"Hello, {name}!", "total": total}}

Multiple files

You can upload multiple files. The main entry file must be cortexone_function.py containing cortexone_handler. Additional .py files can be imported using standard imports.

Supported file types: .py, .csv, .md, .txt, .json, .yaml, .yml


Dependencies

Add a requirements.txt file to your project to install third-party packages:

requests==2.31.0
pandas==2.2.0

The platform installs your dependencies at runtime before executing your handler.


Environment variables

import os
def cortexone_handler(event, context):
api_key = os.environ.get("MY_API_KEY", "")
return {"statusCode": 200, "body": {"ready": len(api_key) > 0}}

Resource limits

LimitDefault
Max memory512 MB
Max runtime300 seconds
CPU limit2.0 cores

Example: Processing numbers

import pandas as pd
def cortexone_handler(event, context):
s = pd.Series(event["num"])
return {"statusCode": 200, "body": {"sum": float(s.sum()), "mean": float(s.mean())}}

Example: HTTP request

import requests
def cortexone_handler(event, context):
url = event.get("url")
response = requests.get(url, timeout=10)
return {
"statusCode": 200,
"body": {
"status_code": response.status_code,
"text": response.text[:500]
}
}

The context object

Python handlers receive a context object with Lambda-compatible metadata:

FieldDescription
context.function_nameThe tool’s function identifier
context.function_versionAlways "$LATEST"
context.memory_limit_in_mbAllocated memory in MB
context.remaining_time_in_millisMilliseconds remaining before timeout
context.aws_request_idUnique request ID for this execution
context.get_remaining_time_in_millis()Method — same as the field above

Error responses

If your handler raises an exception, the platform catches it and returns:

{
"statusCode": 500,
"body": "{\"error\": \"Runtime error: <message>\"}"
}

Timeouts return statusCode: 408. You can also return non-200 status codes intentionally:

def cortexone_handler(event, context):
if not event.get("id"):
return {"statusCode": 400, "body": {"error": "id is required"}}
return {"statusCode": 200, "body": {"ok": True}}

Notes

  • Python tools run in isolated Docker containers. Each execution gets a fresh container.
  • There is no persistent state between executions.
  • The main file must be named cortexone_function.py and must contain cortexone_handler.
  • JavaScript and Lua handlers do not receive a context parameter.