Python dataclass to Pydantic v2 model converter
Views
10.3K
Copies
3.5K
Likes
2.0K
Comments
0
Copy rate
34.0%
Prompt
You are an expert Python engineer. Convert the following Python dataclass to an equivalent Pydantic v2 model.
Source dataclass:
[source_dataclass]
Requirements:
- Target Pydantic version: [pydantic_version]
- Preserve all fields, types, and default values exactly
- Translate `field(default_factory=...)` to Pydantic's `Field(default_factory=...)`
- Translate `Optional[X]` / `X | None` correctly with proper defaults
- Add sensible `Field(...)` validation (min_length, ge, le, pattern) where the field name implies constraints (email, age, url, slug)
- Convert any `__post_init__` logic into `@model_validator(mode='after')`
- Add a `model_config = ConfigDict(...)` block with `str_strip_whitespace=True`, `validate_assignment=True`, and `extra='forbid'`
- Include a working usage example at the bottom that constructs the model from a dict, serializes it with `model_dump()`, and round-trips through `model_validate_json()`
Output format:
- Return only the complete Python file, no prose
- First line: `from __future__ import annotations`
- Include all necessary imports at the top
- Type hints must be complete and correctCustomise this prompt
Fill in 3 variables to personalise this prompt
Preview
You are an expert Python engineer. Convert the following Python dataclass to an equivalent Pydantic v2 model.
Source dataclass:
[source_dataclass]
Requirements:
- Target Pydantic version: [pydantic_version]
- Preserve all fields, types, and default values exactly
- Translate `field(default_factory=...)` to Pydantic's `Field(default_factory=...)`
- Translate `Optional[X]` / `X | None` correctly with proper defaults
- Add sensible `Field(...)` validation (min_length, ge, le, pattern) where the field name implies constraints (email, age, url, slug)
- Convert any `__post_init__` logic into `@model_validator(mode='after')`
- Add a `model_config = ConfigDict(...)` block with `str_strip_whitespace=True`, `validate_assignment=True`, and `extra='forbid'`
- Include a working usage example at the bottom that constructs the model from a dict, serializes it with `model_dump()`, and round-trips through `model_validate_json()`
Output format:
- Return only the complete Python file, no prose
- First line: `from __future__ import annotations`
- Include all necessary imports at the top
- Type hints must be complete and correct