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
execute_code executes Python in the host process (with a timeout). It is not a sandbox — do not pass untrusted code from end-users without isolating the host process yourself (subprocess, container, or VM).

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