Required code structure
To ensure your script works with the Atlas AI agent, you must define specific entry points and follow the required function signatures.The handle() function
Every Python script must include ahandle() function that serves as the entry point. The function must be named handle and must have type annotations for all parameters. The function must also include a docstring following the Google Python Style Guide.
- The function must be named
handle. - All parameters must have type annotations. See Supported parameter types.
- The function should include a docstring following the Google Python Style Guide.
- The return type annotation is recommended but not required.
The test() function
Include atest() function for validation and testing. Use the Test code button in the Atlas AI agent builder to trigger the function.
- The function must be named
test. - No parameters are allowed.
- The function can use assertions or return values for display.
- The function is run when you click the Test code button in the UI.
Supported parameter types
Parameters can be of primitive types,DateTime, NodeId and lists of these. Primitive types can have a default value.
Primitive types
| Python type | JSON schema type | Example usage | Runtime value |
|---|---|---|---|
str | "string" | name: str | "John Doe" |
int | "integer" | age: int | 25 |
float | "number" | temperature: float | 23.5 |
bool | "boolean" | is_active: bool | true |
Default values
Primitive types support default values:List types
We support lists of primitive types:DateTime type
For temporal data usedatetime:
NodeId type
To work with Cognite Data Fusion (CDF) instances from data models, useNodeId to identify the instances:
NodeId parameters are passed to the language model as objects with space and externalId properties:
The
NodeId class uniquely identifies nodes in Cognite’s data modeling framework, combining a space (workspace identifier) and external_id to form a unique node reference. See the Cognite Python SDK data modeling documentation for details.Limitations and restrictions
The following types are not supported as arguments tohandle():
- Custom classes and objects
- Nested lists (
list[list[str]]) - Complex generics beyond
list[T] - Union types (
str | int) - Optional types (
Optional[str]) - Tuples, sets, and other collections
- String literal types (e.g.,
Literal["A"],Literal["A", "B"])
handle():
- Default values are only supported for primitive types (
str,int,float,bool). - Lists cannot have default values.
NodeIdanddatetimeparameters cannot have default values.
Supported return types
Yourhandle() function can return any JSON-serializable data. The following return types are supported:
Primitive return types
Dictionary returns
List returns
None/null returns
Unsupported return types
The following return types are not supported:- Raw Cognite SDK objects
- Custom class instances
- Functions or callable objects
- File handles or binary data
- Complex objects that can’t be serialized to JSON
Cognite SDK and authentication
The Cognite Python SDK is pre-installed and automatically configured with your session credentials. The current user credentials are used and no additional configuration is needed.Cognite SDK return types
We don’t support direct serialization of Cognite SDK objects. To reduce the number of LLM tokens the result will require and to make the data understandable to the agent, we recommend extracting relevant values and potentially reshaping the data.Best practices
We recommend the following best practices when writing Python code for the Run Python code tool.Error handling
Use descriptive error messages:Documentation
- Always include docstrings following the Google Python Style Guide.
- Parameter descriptions are critical: Only the
Args:section descriptions are passed to the AI agent as part of the JSON schema. - Write clear, specific parameter descriptions that help the agent understand what values to provide.
- Include examples in the parameter descriptions when helpful.
Testing
Write comprehensive test functions:Troubleshooting
Below are some common errors you might encounter when using the Run Python code tool, and how to fix them.”Function ‘handle’ not found”
- Ensure your function is named
handle. - Check for indentation errors.
”Missing type annotation for parameter(s)”
- Make sure all parameters have type annotations.
- Use supported types only.
”Duplicate argument name(s) found”
- Make sure the parameter names are unique.
- Check for copy-paste errors.
”Unsupported type for parameter”
- Use only supported types.
- Check for typos in type annotations.
Serialization errors
- Use the
.dump()method on Cognite SDK objects. - Make sure the return values are serializable to JSON.
Code execution environment capabilities and limitations
The Run Python code tool runs in a controlled WebAssembly environment with specific capabilities and limitations:- Runtime: Pyodide (Python 3.11 in WebAssembly).
- Execution context: Runs in a web worker for isolation.
- Performance: WebAssembly execution may be slower than native Python.
- Memory: Limited by browser memory constraints.
Pre-installed packages
The following packages are automatically available:- Cognite Python SDK (
cognite-sdk): Pre-configured with authentication. - Standard library: Full Python 3.11 standard library.
- Core scientific packages (via Pyodide):
numpy: Numerical computingpandas: Data analysis and manipulationjson: JSON encoding/decodingdatetime: Date and time handlinguuid: UUID generation
- Custom package installation via
requirements.txt - Longer execution times
- More memory and compute resources
- Full Python ecosystem access
System limitations
The execution environment has certain constraints that you should be aware of when writing your scripts.- File system: No access to local file system or file I/O operations. The scripts have access to a virtual memory-based file system.
- Network access: Only API calls to Cognite Data Fusion APIs are supported. Network calls to other servers might get blocked.
- Process control: No subprocess or system command execution.
- Threading: Limited threading capabilities in WebAssembly environment.
- Graphics: Matplotlib backend set to ‘AGG’ (non-interactive).
How the AI agent uses your code
The Atlas AI agent processes your Python code through these steps to understand and execute your tool functions:- JSON schema as tool descriptor: The generated JSON schema is passed to the AI agent’s language model as a tool descriptor. This tells the agent exactly what parameters are available, their types, and what they’re for.
-
Parameter descriptions guide the agent: The parameter descriptions from your docstring’s
Args:section are included in the schema and used by the agent to understand what values to provide. - Type validation: The schema ensures the language model provides correctly typed arguments that match your function signature.
-
Runtime execution: When the agent calls your tool, the arguments are validated against the schema and then transformed (e.g., NodeId objects are created from dictionaries) before being passed to your
handle()function.
Your parameter docstrings are critical. They directly influence how the agent understands and uses your tool.