Python
Python 3.13 is the default language for Function tools and the most commonly used option in the Studio code editor.
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}| Parameter | Type | Description |
|---|---|---|
event | dict | The input payload sent by the caller |
context | object | Execution metadata |
The handler returns a dict with statusCode and body. Return body as a plain dict - the platform serializes it automatically.
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 alongside cortexone_function.py to install third-party packages:
requests==2.31.0pandas==2.2.0The platform installs your dependencies before executing your handler.
Common libraries are already available without a requirements.txt entry - for example requests, numpy, pandas, and the Python standard library.
Environment Secrets
Store credentials as Environment Secrets at /user/secrets, attach them to your tool from the Code step of the Tool Editor, and read them as regular 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}}See Environment Variables for the full workflow.
Resource limits
| Limit | Default |
|---|---|
| Max memory | 512 MB |
| Max runtime | 300 seconds |
| CPU limit | 2.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 execution metadata:
| Field | Description |
|---|---|
context.function_name | The tool’s function identifier |
context.memory_limit_in_mb | Allocated memory in MB |
context.remaining_time_in_millis | Milliseconds remaining before timeout |
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}}Testing your code
Save and run sample payloads from the Test panel inside the Code step of the Tool Editor. The panel shows the returned statusCode, body, and any logs from your handler. See Testing & Executing for the full workflow.
Notes
- Each run starts fresh - no files persist between runs and no globals carry over.
- The main file must be named
cortexone_function.pyand must containcortexone_handler. - JavaScript and Lua handlers do not receive a populated
contextparameter. - For lower-latency execution without outbound HTTP, consider Python 3.13 — Fast.