Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:ANative Lab EvoAgentX Sanitize

From Leeroopedia
Knowledge Sources
Domains Utils, Code_Safety
Last Updated 2026-10-21 00:00 GMT

Overview

Concrete code sanitization utility using tree-sitter AST parsing to validate and analyze LLM-generated Python code.

Description

The `evoagentx/utils/sanitize.py` module uses `tree_sitter_python` to parse Python code into an AST. It defines a `NodeType` enum (class, function, import, etc.) and a `traverse_tree` generator for AST traversal. Sanitizes generated code by analyzing and validating AST structure. Modified from MetaGPT. Used by operators and code generation workflows to validate LLM-generated Python code before execution.

Usage

  • Code validation: Use to validate LLM-generated Python code structure before execution.
  • AST analysis: Use `traverse_tree` to analyze code structure programmatically.
  • Code generation safety: Used by AFlow operators to sanitize generated code.

Code Reference

Source Location

Signature

class NodeType(enum.Enum):
    CLASS = "class"
    FUNCTION = "function"
    IMPORT = "import"
    # ...

def traverse_tree(tree) -> Generator
def sanitize(code: str) -> str

Import

from evoagentx.utils.sanitize import sanitize, NodeType, traverse_tree

I/O Contract

Inputs

Name Type Required Description
code str Yes │ Python code to sanitize

Outputs

Name Type Description
sanitize returns str │ Sanitized code
traverse_tree returns Generator │ AST node generator

Usage Examples

Sanitize Generated Code

from evoagentx.utils.sanitize import sanitize

code = "def hello():\n    print('hello')"
clean_code = sanitize(code)

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment