Generate pydantic models using prompts
Project description
Promptantic
An interactive CLI tool for populating Pydantic models using prompt-toolkit.
Features
- Interactive prompts for populating Pydantic models
- Rich formatting and syntax highlighting
- Type-aware input with validation
- Autocompletion for paths, timezones, and custom values
- Support for all common Python and Pydantic types
- Nested model support
- Union type handling via selection dialogs
- Sequence input (lists, sets, tuples)
- Customizable styling
Installation
pip install promptantic
Quick Start
from pydantic import BaseModel, Field
from promptantic import ModelGenerator
class Person(BaseModel):
name: str = Field(description="Person's full name")
age: int = Field(description="Age in years", gt=0)
email: str = Field(description="Email address", pattern=r"[^@]+@[^@]+\.[^@]+")
# Create and use the generator
generator = ModelGenerator()
person = await generator.apopulate(Person)
print(person)
Supported Types
Basic Types
str,int,float,bool,decimal.Decimal- Constrained types (e.g.,
constr,conint) EnumclassesLiteraltypes
Complex Types
list,set,tuple(with nested type support)Uniontypes (with interactive type selection)- Nested Pydantic models
Special Types
Path(with path autocompletion)UUIDSecretStr(masked input)datetime,date,time,timedeltaZoneInfo(with timezone autocompletion)IPv4Address,IPv6Address,IPv4Network,IPv6Network- Email addresses (with validation)
- URLs (with validation)
Advanced Usage
Custom Completions
from pydantic import BaseModel, Field
from pathlib import Path
class Config(BaseModel):
environment: str = Field(
description="Select environment",
completions=["development", "staging", "production"]
)
config_path: Path = Field(description="Path to config file") # Has path completion
Nested Models
class Address(BaseModel):
street: str = Field(description="Street name")
city: str = Field(description="City name")
country: str = Field(description="Country name")
class Person(BaseModel):
name: str = Field(description="Full name")
address: Address = Field(description="Person's address")
# Will prompt for all fields recursively
person = await ModelGenerator().apopulate(Person)
Union Types
class Student(BaseModel):
student_id: int
class Teacher(BaseModel):
teacher_id: str
subject: str
class Person(BaseModel):
name: str
role: Student | Teacher # Will show selection dialog
# Will prompt for type selection before filling fields
person = await ModelGenerator().apopulate(Person)
Styling
from prompt_toolkit.styles import Style
from promptantic import ModelGenerator
custom_style = Style.from_dict({
"field-name": "bold #00aa00", # Green bold
"field-description": "italic #888888", # Gray italic
"error": "bold #ff0000", # Red bold
})
generator = ModelGenerator(style=custom_style)
Options
generator = ModelGenerator(
show_progress=True, # Show field progress
allow_back=True, # Allow going back to previous fields
retry_on_validation_error=True # Retry on validation errors
)
Error Handling
from promptantic import ModelGenerator, PromptanticError
try:
result = await ModelGenerator().apopulate(MyModel)
except KeyboardInterrupt:
print("Operation cancelled by user")
except PromptanticError as e:
print(f"Error: {e}")
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Credits
Built with prompt-toolkit and Pydantic.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file promptantic-0.4.0.tar.gz.
File metadata
- Download URL: promptantic-0.4.0.tar.gz
- Upload date:
- Size: 24.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
077acd3eb22e3a57a031e3631727995ed1621d06561450c4b53438ff0c454ec0
|
|
| MD5 |
97990eddb25195a1de1d2894d3a18fb9
|
|
| BLAKE2b-256 |
8a0d4de7906c1fc15e1b84eea4d6acbe4a6d1f552b989ae0c0f6e6fb4d88b278
|
File details
Details for the file promptantic-0.4.0-py3-none-any.whl.
File metadata
- Download URL: promptantic-0.4.0-py3-none-any.whl
- Upload date:
- Size: 28.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92dc3578808ec8d1918191935c724821d594525df44e34fc88a4ce1757e1da33
|
|
| MD5 |
1191dd373d6c5b25adb241cd8528aa5f
|
|
| BLAKE2b-256 |
94df56c8dd02225f9c25b2b757c18228851f5969669abe92eb21c81765ae02e2
|