Skip to content

Conversation

@devdattatalele
Copy link
Contributor

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.

…ference

- Add AST-based detection of Pydantic BaseModel inheritance patterns
- Add AST-based detection of @DataClass decorator (all variants)
- Implement recursive field schema extraction with type inference
- Add thread-safe stack-based module storage for nested parsing
- Add RAII cleanup guard to ensure memory safety on all code paths
- Add security limits: 200 fields max, 10 recursion levels max
- Add comprehensive test coverage: 3 new tests for Pydantic/dataclass
- Maintain 100% backward compatibility with existing type system

This enables ML/AI practitioners to use Pydantic models as function
parameters with automatic UI generation from model schemas.

Implementation highlights:
- Zero code execution: Pure AST analysis for safety
- Thread-safe: Stack-based storage prevents race conditions
- Memory-safe: RAII pattern guarantees cleanup
- Security-hardened: Field count and recursion depth limits
- Performance-optimized: Depth-limited recursion, lazy parsing

Test results: All 12 tests passing (9 existing + 3 new)

Closes windmill-labs#4700

🤖 Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: Support Pydantic / dataclasses

1 participant