# 📊 Generate from JSON / YAML / CSV Data

Generate Pydantic models directly from raw JSON, YAML, or CSV data. Under the hood, the generator uses [GenSON](https://pypi.org/project/genson/) to infer a JSON Schema from JSON/YAML input, or infers a schema from CSV rows, then processes it the same way as [JSON Schema input](./jsonschema.md).

## 🚀 Quick Start

```bash
datamodel-codegen \
    --input pets.json \
    --input-file-type json \
    --output-model-type pydantic_v2.BaseModel \
    --output model.py
```

Use `--input-file-type yaml` for raw YAML sample data and `--input-file-type csv`
for CSV files. If a YAML file is a schema definition rather than sample data, use
`--input-file-type jsonschema`, `openapi`, or `asyncapi` instead.

## 📝 Example

**pets.json**
```json
{
  "pets": [
    {
      "name": "dog",
      "age": 2
    },
    {
      "name": "cat",
      "age": 1
    },
    {
      "name": "snake",
      "age": 3,
      "nickname": "python"
    }
  ],
  "status": 200
}
```


**✨ Generated model.py**
```python
# generated by datamodel-codegen:
#   filename:  pets.json
#   timestamp: 2020-04-27T16:08:21+00:00

from __future__ import annotations

from pydantic import BaseModel


class Pet(BaseModel):
    name: str
    age: int
    nickname: str | None = None


class Model(BaseModel):
    pets: list[Pet]
    status: int

```

## Supported Input Shapes

| Input type | Generation behavior |
|------------|---------------------|
| JSON object/array samples | Infers JSON Schema with GenSON, then generates models |
| YAML sample data | Parsed as sample data, then inferred like JSON |
| CSV files | Infers columns from rows and generates a tabular model |

## Limitations

Raw data input is inference-based. Optionality, unions, numeric ranges, and exact constraints depend on the sample
values you provide, so small or incomplete samples may produce broader models than a hand-authored schema.

---

## 📖 See Also

- 🚀 [Getting Started](getting-started.md) - Installation and first model
- 📋 [Generate from JSON Schema](jsonschema.md) - JSON Schema input documentation
- 🖥️ [CLI Reference](cli-reference/index.md) - Complete CLI options reference
- 📊 [Supported Data Types](supported-data-types.md) - Data type support details
