Creators to follow

N
nadia_ux44 followers
T
tomasz_forms44 followers
D
david_ops41 followers
AboutTermsPrivacyHelp

© 2026 teliprompt

JE
jess_python
8 months ago•
Coding

Python dataclass to Pydantic v2 model converter

Claude Sonnet 4.6
code output
#python
#pydantic
#dataclass
#refactor
#validation
Views
6.3K
Copies
1.1K
Likes
691
Comments
0
Copy rate
18.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 correct

Customise 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

Example output

Related prompts

Async iterators and generators in modern JavaScript: a working guideby @olivia_test
Coding
TypeScript type narrowing cheat sheet with runnable examplesby @marcus_react
Coding
CLI tool builder with argument parsing and colored outputby @nikolai_rust
Coding
Convert JavaScript to strict TypeScript with full type safetyby @marcus_react
Coding