π Automatically generate Pydantic models from API responses, JSON files, or curl commands.
β¨ Smart Type Inference - Automatically detects correct Python types from sample data
π Pattern Recognition - Identifies emails, URLs, UUIDs, datetimes, and more
π¦ Nested Structures - Handles complex nested objects and arrays
β
Validators - Generates Field validators based on data patterns
π― Optional Detection - Identifies optional fields from null values
π Enum Recognition - Detects limited value sets and creates enums
π Documentation - Adds helpful docstrings to generated models
pip install api2pydanticOr install from source:
git clone https://github.com/codedev1992/api2pydantic.git
cd api2pydantic
pip install -e .api2pydantic https://api.example.com/userscurl https://api.example.com/users | api2pydanticapi2pydantic curl https://api.example.com/users -H "Authorization: Bearer token"api2pydantic file data.jsonecho '{"name": "John", "age": 30}' | api2pydanticapi2pydantic https://api.example.com/users --output models.pyapi2pydantic https://api.example.com/users --model-name UserResponseInput JSON:
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"email": "user@example.com",
"name": "John Doe",
"age": 30,
"is_active": true,
"created_at": "2024-01-15T10:30:00Z",
"tags": ["python", "pydantic"],
"profile": {
"bio": "Software Developer",
"website": "https://example.com"
}
}Generated Pydantic Model:
from pydantic import BaseModel, Field, EmailStr, HttpUrl
from typing import Optional, List
from datetime import datetime
from uuid import UUID
class Profile(BaseModel):
"""Profile model"""
bio: str = Field(..., description="Software Developer")
website: HttpUrl = Field(..., description="https://example.com")
class RootModel(BaseModel):
"""RootModel model"""
id: UUID = Field(..., description="123e4567-e89b-12d3-a456-426614174000")
email: EmailStr = Field(..., description="user@example.com")
name: str = Field(..., min_length=1, description="John Doe")
age: int = Field(..., ge=0, description="30")
is_active: bool = Field(..., description="True")
created_at: datetime = Field(..., description="2024-01-15T10:30:00Z")
tags: List[str] = Field(..., description="['python', 'pydantic']")
profile: Profile = Field(..., description="Profile model")# Analyze multiple API responses to improve type detection
api2pydantic https://api.example.com/users/1 \
https://api.example.com/users/2 \
https://api.example.com/users/3api2pydantic --help
Options:
--output, -o Output file path
--model-name, -m Custom model name (default: RootModel)
--array-item-name Name for array item models
--no-validators Skip generating validators
--no-descriptions Skip adding descriptions
--force-optional Make all fields optional- Fetch - Retrieves JSON data from URL, file, or curl command
- Analyze - Examines values to infer types and detect patterns
- Generate - Creates Pydantic model with proper type hints
- Validate - Adds field validators based on data constraints
- Format - Outputs clean, formatted Python code
The tool intelligently detects:
- Primitives: str, int, float, bool, None
- Collections: List, Dict, Set
- Special Types: UUID, datetime, date, time
- Validated Types: EmailStr, HttpUrl
- Patterns: Enum detection from limited value sets
- Optionals: Fields with null values
- Unions: Mixed types in arrays
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/your_feature_name) - Commit your changes (
git commit -m 'Add some your_feature_name') - Push to the branch (
git push origin feature/your_feature_name) - Open a Pull Request
# Clone the repo
git clone https://github.com/codedev1992/api2pydantic.git
cd api2pydantic
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytest
# Run linter
flake8 api2pydantic testsThis project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by the amazing Pydantic library
- Built to solve the tedious task of manually writing models
- Support for JSON Schema output
- GraphQL schema support
- OpenAPI spec generation
- VS Code extension
- Web UI for online conversion
- Support for more validation patterns
β Star this repo if you find it helpful!
π Report bugs
π‘ Request features
Made with β€οΈ