Skip to main content
The user asks for a computed result; the agent executes vetted Python and returns the output.
Prerequisites
  • Python 3.10 or higher
  • PraisonAI Agents package installed
  • Basic understanding of Python programming

Python Tools

Use Python Tools to execute and manage Python code with AI agents.
1

Install Dependencies

First, install the required package:
2

Import Components

Import the necessary components:
3

Create Agent

Create a Python execution agent:
4

Define Task

Define the Python execution task:
5

Run Agent

Initialize and run the agent:

Available Functions

Import Paths

Function Details

execute_code(code: str, globals_dict: Optional[Dict[str, Any]] = None, locals_dict: Optional[Dict[str, Any]] = None, timeout: int = 30, max_output_size: int = 10000)

Safely executes Python code:
  • Isolated execution environment
  • Output capture
  • Error handling
  • Timeout protection
  • Output size limits
When using the default in-process path, execute_code runs Python in the host process (with a timeout). That path is not a sandbox — do not pass untrusted code from end-users without isolating the host process yourself (subprocess, container, or VM). For untrusted code use Sandbox mode, which runs the snippet in a subprocess with enforced resource caps.

Sandbox Mode

Sandbox mode runs each snippet in a fresh subprocess whose resource limits are enforced by the kernel. Enable it with ExecutionConfig(code_execution=True, code_mode="safe"). On POSIX the subprocess applies the full ResourceLimits bundle via resource.setrlimit in a preexec_fn before your code runs, so untrusted code cannot exhaust host memory, CPU, processes, files, or disk. Caps applied per snippet: memory (RLIMIT_AS), CPU seconds (RLIMIT_CPU, derived from timeout_seconds), process count (RLIMIT_NPROC), open files (RLIMIT_NOFILE), and disk-write size (RLIMIT_FSIZE).
On non-POSIX platforms (Windows), only the wall-clock timeout applies — the SDK emits a logging.warning at launch so operators can see the gap. Run the sandbox on POSIX for untrusted code.

Configuration

Sandbox caps come from the ResourceLimits dataclass. Untrusted snippets use ResourceLimits.minimal() by default — the defaults below are that minimal profile. Constants absent on a given POSIX platform, and caps the platform rejects, are skipped without aborting the launch — the wall-clock timeout still applies as a backstop.

analyze_code(code: str)

Analyzes Python code structure:
  • Import statements
  • Function definitions
  • Class definitions
  • Variable usage
  • Code complexity

format_code(code: str, style: str = ‘black’, line_length: int = 88)

Formats Python code:
  • Multiple style options
  • Line length control
  • PEP 8 compliance
  • Consistent formatting

lint_code(code: str)

Lints Python code for issues:
  • Code quality checks
  • Style violations
  • Potential bugs
  • Best practices

disassemble_code(code: str)

Disassembles Python code to bytecode:
  • Bytecode inspection
  • Performance analysis
  • Code optimization
  • Debugging support

Example Agent Configuration

Dependencies

execute_code, analyze_code, and disassemble_code work out of the box — they only require the Python standard library. The remaining tools need optional packages installed manually: If the dependency is missing, the tool returns a clear error rather than crashing the agent.

Error Handling

All functions include comprehensive error handling:
  • Code execution errors
  • Syntax errors
  • Import errors
  • Timeout errors
  • Memory errors
Errors are handled consistently:
  • Success cases return expected data type
  • Error cases return None or error details
  • All errors are logged for debugging

Common Use Cases

  1. Code Testing:
  1. Code Quality:
  1. Code Analysis:

Understanding Python Tools

What are Python Tools?

Python Tools provide code execution capabilities for AI agents:
  • Code execution
  • Module management
  • Error handling
  • Output capture
  • Environment control

Examples

Basic Python Execution Agent

Advanced Python Operations with Multiple Agents

Best Practices

Configure agents with clear Python focus:
Define specific Python operations:

Common Patterns

Python Execution Pipeline