windmill icon indicating copy to clipboard operation
windmill copied to clipboard

feat: Add Pydantic BaseModel and dataclass support for Python type inference

Open devdattatalele opened this issue 4 months ago • 0 comments

Summary

Adds Pydantic BaseModel and Python dataclass support to the Python parser, enabling automatic UI generation for complex data structures.

Closes #4700

Changes

Modified backend/parsers/windmill-parser-py/src/lib.rs to detect Pydantic models and dataclasses via AST analysis. The parser now recognizes these custom types and extracts their field schemas without executing any Python code.

Implementation

The parser uses rustpython AST to pattern match class inheritance and decorators, then recursively extracts field types. Added thread-safe module storage with RAII cleanup and security limits (200 fields max, 10 recursion levels).

Example

Before:

def main(name: str, age: int, email: str):
    return f"Hello {name}"

After:

from pydantic import BaseModel

class User(BaseModel):
    name: str
    age: int
    email: str

def main(user: User):
    return f"Hello {user.name}"

Windmill now automatically generates a form with name, age, and email fields from the User model.

Testing

Added 3 test cases covering basic models, dataclasses, and nested models. All 12 tests pass. No breaking changes to existing functionality.

Known Limitations

Field default values and validation metadata are not yet extracted. Forward references and union types are not supported. These can be addressed in follow-up PRs if needed.

devdattatalele avatar Dec 10 '25 13:12 devdattatalele