Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Evidentlyai Evidently Legacy IsValidPython Feature

From Leeroopedia
Knowledge Sources
Domains ML Monitoring, Data Validation, Code Analysis
Last Updated 2026-02-14 12:00 GMT

Overview

Provides a generated feature that validates whether each value in a specified text column is syntactically valid Python code.

Description

The IsValidPython class extends ApplyColumnGeneratedFeature to apply a per-row validation check that attempts to parse each cell value using Python's ast.parse function. If parsing succeeds without raising a SyntaxError or TypeError, the feature returns True; otherwise, it returns False. The feature type is ColumnType.Categorical since the output is a boolean classification.

The class uses a display_name_template class variable set to "Valid Python for {column_name}" which provides automatic display naming based on the target column. A custom display_name can also be provided at initialization.

Usage

Use this feature when monitoring or evaluating LLM outputs that are expected to produce valid Python code. It is useful for code generation quality checks in pipelines where models generate Python snippets.

Code Reference

Source Location

Signature

class IsValidPython(ApplyColumnGeneratedFeature):
    class Config:
        type_alias = "evidently:feature:IsValidPython"

    __feature_type__: ClassVar = ColumnType.Categorical
    display_name_template: ClassVar = "Valid Python for {column_name}"

    def __init__(self, column_name: str, display_name: Optional[str] = None): ...
    def apply(self, value: Any) -> bool: ...

Import

from evidently.legacy.features.is_valid_python_feature import IsValidPython

I/O Contract

Inputs

Name Type Required Description
column_name str Yes Name of the text column in the DataFrame to validate as Python code
display_name Optional[str] No Custom display name for the feature (defaults to template-based name)

Outputs

Name Type Description
return bool (per row) True if the value is syntactically valid Python, False otherwise

Usage Examples

from evidently.legacy.features.is_valid_python_feature import IsValidPython

# Create the feature for a column named "generated_code"
python_feature = IsValidPython(column_name="generated_code")

# With a custom display name
python_feature = IsValidPython(column_name="code_output", display_name="Code Output Validity")

Related Pages

Page Connections

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