Overview
Tool Name
Purpose
The code_executor_tools enable secure execution of Python code within Genesis Data Agents. Run dynamic calculations, data transformations, API integrations, and custom logic without pre-defined functions. Perfect for ad-hoc analysis, prototyping, data science workflows, and extending agent capabilities with custom code on-the-fly.Functions Available
python_exec
:Execute Python code in a secure sandboxed environment with access to common libraries, data sources, and file systems.
Key Features
Secure Execution
Run code in isolated sandbox with resource limits and security constraints to prevent system abuse.
Rich Library Access
Pre-loaded with pandas, numpy, requests, and other common data science and utility libraries.
Data Source Integration
Direct access to configured databases, file systems, and APIs within executed code.
File System Access
Read and write files to Git repositories and storage locations from within executed code.
Dynamic Variables
Pass variables into code execution and retrieve results for further processing.
Error Handling
Comprehensive error messages with stack traces for debugging failed executions.
Input Parameters for Each Function
python_exec
Parameters
Name | Definition | Format |
---|---|---|
code | Python code to execute. Can be single line or multi-line block. | String (required) |
variables | Dictionary of variables to make available in the execution context. | Object |
timeout | Maximum execution time in seconds (default: 30, max: 300). | Integer |
return_var | Name of variable to return from execution context (default: returns all variables). | String |
libraries | Additional libraries to import (beyond pre-loaded defaults). | Array of Strings |
safe_mode | Enable additional security restrictions (default: True). | Boolean |
Use variables parameter to pass data from the agent context into your code. This is more efficient than embedding large data directly in code strings.
Use Cases
- Ad-Hoc Data Analysis Perform quick calculations, statistical analysis, or data exploration without creating permanent functions or scripts.
- Data Transformation Execute custom transformation logic on datasets that does not fit into standard SQL or built-in functions.
- API Integration Make HTTP requests, parse responses, and integrate with external APIs dynamically based on user requirements.
- Prototyping & Testing Test algorithms, validate assumptions, or prototype solutions before implementing them as permanent features.
- Custom Business Logic Implement organization-specific calculations, rules, or validations that vary by use case or customer.
Workflow/How It Works
-
Step 1: Simple Calculation
Execute basic Python for quick results:
-
Step 2: Data Analysis with Pandas
Analyze data using pre-loaded libraries:
-
Step 3: Pass Variables from Context
Use data from previous operations:
-
Step 4: API Integration
Make external API calls and process responses:
-
Step 5: File Operations
Read and write files within execution:
-
Step 6: Complex Data Processing
Implement multi-step algorithms:
Integration Relevance
- data_connector_tools to fetch data from databases, then process with custom Python code.
- file tools to read/write files before or after code execution.
- github_connector_tools / gitlab_connector_tools to version control generated scripts.
- image_tools to process images or generate visualizations from code execution results.
- web_access_tools to fetch data via HTTP within executed code.
- project_manager_tools to execute custom code as part of automated mission tasks.
Configuration Details
- Execution Environment: Python 3.9+ with isolated namespace per execution.
- Pre-loaded Libraries: pandas, numpy, requests, json, datetime, os, sys, re, math, statistics.
- Resource Limits: CPU time limit (default 30s), memory limit (default 512MB).
- File System Access: Read/write to designated workspace directories and Git repositories.
- Network Access: HTTP/HTTPS requests allowed; other protocols restricted.
- Security Mode: Restricted access to system operations, subprocess execution, and dangerous functions.
- Return Value: Can return primitive types, dictionaries, lists, or serializable objects.
Code execution happens in a sandboxed environment, but always validate and sanitize user-provided code to prevent malicious operations. Never execute untrusted code without review.
Limitations or Notes
- Execution Timeout: Default 30 seconds, maximum 300 seconds (5 minutes). Long-running operations may be terminated.
- Memory Limits: Default 512MB. Large dataset operations may exceed limits and fail.
- No Subprocess: Cannot spawn subprocesses or execute system commands for security.
- Library Restrictions: Cannot install arbitrary packages; limited to pre-loaded libraries.
- Network Limitations: Only HTTP/HTTPS allowed; no raw socket access or other protocols.
- File System Scope: Access limited to designated directories; cannot access system files.
- Serialization: Return values must be JSON-serializable or primitive types.
- State Persistence: Each execution is isolated; no state persists between calls.
- Standard Output: Print statements captured but may be truncated for very large outputs.
Supported Operations
✅ Basic Python operations - Variables, functions, loops, conditionals✅ Data manipulation - pandas DataFrames, numpy arrays, list/dict operations
✅ File I/O - Read/write text and binary files in workspace
✅ HTTP requests - GET, POST, PUT, DELETE with requests library
✅ JSON/CSV processing - Parse and generate structured data formats
✅ Date/time calculations - datetime, timedelta operations
✅ Regular expressions - Pattern matching and text processing
✅ Mathematical operations - Statistics, linear algebra, calculations
✅ String manipulation - Formatting, parsing, encoding
✅ Exception handling - Try/except blocks and error recovery
Not Supported
❌ Installing new packages (pip install) during execution❌ Subprocess execution (subprocess, os.system, etc.)
❌ System file access (outside workspace)
❌ Database connections (use data_connector_tools instead)
❌ Multi-threading or multiprocessing
❌ Raw socket programming
❌ Modifying Python environment or interpreter
❌ Accessing environment variables (except explicitly passed)
❌ Infinite loops (terminated by timeout)
❌ Heavy machine learning model training (use dedicated tools)
Output
- Success: Execution result with returned variable(s), stdout output, and execution time.
- Variables: Dictionary of all variables in execution context (unless specific return_var specified).
- Standard Output: Captured print statements and logging output.
- Execution Time: Time taken to execute code in seconds.
- Errors: Exception type, error message, and full stack trace for debugging.
- Warnings: Any resource warnings (approaching timeout, high memory usage).
Best Practices
Small & Focused
Keep code blocks focused on single tasks. Break complex operations into multiple executions.
Error Handling
Always use try/except blocks for operations that might fail (API calls, file I/O, parsing).
Resource Awareness
Monitor execution time and memory usage. Use sampling for large datasets.
Data Validation
Validate input data and function parameters before processing to avoid runtime errors.
Logging
Use print statements to track execution progress and debug issues.
Return Values
Always assign results to variables for retrieval. Do not rely solely on print output.
Example: Complete Data Analysis Workflow
Advanced Features
Statistical Analysis
Text Processing
Data Validation
API Pagination Handler
Troubleshooting
Execution Timeout
Execution Timeout
- Reduce dataset size or use sampling
- Optimize algorithms (vectorize operations with numpy/pandas)
- Increase timeout parameter (up to 300 seconds)
- Break into smaller execution steps
- Use generators or lazy evaluation for large data
Memory Error
Memory Error
- Process data in chunks instead of loading all at once
- Use pandas iterator options (chunksize parameter)
- Delete large intermediate variables with del variable
- Use more memory-efficient data types (int8 vs int64)
- Consider using data_connector_tools for heavy processing
Import Error - Module Not Found
Import Error - Module Not Found
- Verify library is in pre-loaded list
- Check for typos in import statement
- Use alternative built-in libraries when possible
- Request library addition from Genesis team
- Implement functionality manually if simple
File Not Found
File Not Found
- Verify file path is relative to workspace
- Check file exists using os.path.exists()
- Ensure file was created in previous step
- Use absolute paths within workspace directories
- Check file permissions and access rights
Serialization Error on Return
Serialization Error on Return
- Convert numpy arrays to lists: array.tolist()
- Convert pandas DataFrames to dicts: df.to_dict()
- Use float() for numpy float types
- Use int() for numpy integer types
- Convert datetime to ISO strings: dt.isoformat()
API Request Failed
API Request Failed
- Check internet connectivity (if applicable)
- Verify API endpoint URL is correct
- Check authentication credentials
- Add error handling with try/except
- Check for rate limiting and add delays
- Verify API response format matches expectations
Syntax Error in Code
Syntax Error in Code
- Check for proper indentation (use 4 spaces)
- Verify quotes are properly closed
- Check for balanced parentheses/brackets
- Test code in local Python environment first
- Use syntax highlighter to identify issues
Security Considerations
Code Execution Risks: Always validate user-provided code before execution. Never execute code from untrusted sources without thorough review.
Sandbox Protections
The code executor implements multiple security layers:- Restricted Imports: Cannot import subprocess, os.system, eval, exec, compile, or other dangerous functions
- File System Isolation: Access limited to designated workspace directories
- Network Filtering: Only HTTP/HTTPS; no raw sockets or other protocols
- Resource Limits: CPU time and memory caps prevent denial-of-service
- Namespace Isolation: Each execution has isolated variable scope
- No Privilege Escalation: Cannot access system files or escalate permissions
Safe Coding Practices
Input Validation
Validate and sanitize all user inputs before using them in code execution.
Error Containment
Use try/except blocks to prevent error propagation and information leakage.
Resource Monitoring
Monitor execution time and memory usage. Set appropriate timeouts.
Audit Logging
Log all code executions with user context for security auditing.