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 <noreply@anthropic.com>
This commit addresses critical bugs and improves error handling in the
Python parser for Pydantic BaseModel and dataclass support.

## Critical Fixes

1. **Thread-local storage RAII pattern**: Fixed bug where parse failures
   could leave the module stack in an inconsistent state. Now uses proper
   functional composition with .ok().map() to ensure cleanup always happens.

2. **Recursion depth warnings**: Added explicit warning messages when the
   recursion depth limit (10 levels) is reached during type extraction.
   Made the limit a named constant for clarity.

3. **Unsupported type warnings**: Added informative warning messages for
   unsupported type annotations (Union types and forward references) to
   help users understand why their types aren't being inferred.

## Improvements

- Added 10 comprehensive test cases covering:
  - Empty Pydantic models
  - List[T] and Optional[T] types
  - Dataclass with decorator arguments
  - Dict types
  - Regular classes (non-model types)
  - Invalid syntax handling
  - Datetime fields
  - Multiple model definitions
  - Nested models

- All 21 tests pass successfully

## Testing

Verified that:
- Parser handles malformed code gracefully
- RAII cleanup works correctly with early returns
- Warning messages are clear and actionable
- No memory leaks or panics

Closes windmill-labs#4700
…ated src/pydantic_parser.rs with thread-local storage, model detection, and type extraction logic. Moved 12 Pydantic tests to tests/pydantic_tests.rs and removed duplicate code from lib.rs. All 21 tests passing.
@hugocasa
Copy link
Collaborator

hugocasa commented Jan 6, 2026

moved to #7497

@hugocasa hugocasa closed this Jan 6, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Jan 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: Support Pydantic / dataclasses

2 participants