Skip to content

Latest commit

 

History

History
305 lines (219 loc) · 6.72 KB

File metadata and controls

305 lines (219 loc) · 6.72 KB

📁 Base Options

📋 Options

Option Description
--encoding Specify character encoding for input and output files.
--input Specify the input schema file path.
--input-file-type Specify the input file type for code generation.
--output Specify the destination path for generated Python code.
--url Fetch schema from URL with custom HTTP headers.

--encoding {#encoding}

Specify character encoding for input and output files.

The --encoding flag sets the character encoding used when reading the schema file and writing the generated Python code. This is useful for schemas containing non-ASCII characters (e.g., Japanese, Chinese). Default is the system's preferred encoding (via locale.getpreferredencoding()), which is typically UTF-8 on Linux/macOS but may differ on Windows (e.g., cp1252). For consistent cross-platform behavior, explicitly specify --encoding utf-8.

!!! tip "Usage"

```bash
datamodel-codegen --input schema.json --encoding utf-8 # (1)!
```

1. :material-arrow-left: `--encoding` - the option documented here

??? example "Examples"

**Input Schema:**

```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "日本語Model",
  "description": "モデルの説明文",
  "type": "object",
  "properties": {
    "名前": {
      "type": "string",
      "description": "ユーザー名"
    },
    "年齢": {
      "type": "integer"
    }
  }
}
```

**Output:**

```python
# generated by datamodel-codegen:
#   filename:  encoding_test.json
#   timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel, Field


class 日本語Model(BaseModel):
    名前: str | None = Field(None, description='ユーザー名')
    年齢: int | None = None
```

--input {#input}

Specify the input schema file path.

The --input flag specifies the path to the schema file (JSON Schema, OpenAPI, GraphQL, etc.). Multiple input files can be specified to merge schemas. Required unless using --url to fetch schema from a URL.

!!! tip "Usage"

```bash
datamodel-codegen --input schema.json --input pet_simple.json --output output.py # (1)!
```

1. :material-arrow-left: `--input` - the option documented here

??? example "Examples"

**Input Schema:**

```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Pet",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    },
    "tag": {
      "type": "string"
    }
  }
}
```

**Output:**

```python
# generated by datamodel-codegen:
#   filename:  pet_simple.json
#   timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel


class Pet(BaseModel):
    id: int | None = None
    name: str | None = None
    tag: str | None = None
```

--input-file-type {#input-file-type}

Specify the input file type for code generation.

The --input-file-type flag explicitly sets the input format when it cannot be auto-detected from the file extension. Supported types: openapi, jsonschema, json, yaml, csv, graphql.

!!! tip "Usage"

```bash
datamodel-codegen --input schema.json --input-file-type json # (1)!
```

1. :material-arrow-left: `--input-file-type` - the option documented here

??? example "Examples"

**Input Schema:**

```json
{
  "Pet": {
    "name": "dog",
    "age": 2
  }
}
```

**Output:**

```python
# generated by datamodel-codegen:
#   filename:  pet.json
#   timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel


class Pet(BaseModel):
    name: str
    age: int


class Model(BaseModel):
    Pet: Pet
```

--output {#output}

Specify the destination path for generated Python code.

The --output flag specifies where to write the generated Python code. It can be either a file path (single-file output) or a directory path (multi-file output for modular schemas). If omitted, the generated code is written to stdout.

!!! tip "Usage"

```bash
datamodel-codegen --input schema.json --input pet_simple.json --output output.py # (1)!
```

1. :material-arrow-left: `--output` - the option documented here

??? example "Examples"

**Input Schema:**

```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Pet",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    },
    "tag": {
      "type": "string"
    }
  }
}
```

**Output:**

```python
# generated by datamodel-codegen:
#   filename:  pet_simple.json
#   timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel


class Pet(BaseModel):
    id: int | None = None
    name: str | None = None
    tag: str | None = None
```

--url {#url}

Fetch schema from URL with custom HTTP headers.

The --url flag specifies a remote URL to fetch the schema from instead of a local file. The --http-headers flag adds custom HTTP headers to the request, useful for authentication (e.g., Bearer tokens) or custom API requirements. Format: HeaderName:HeaderValue.

!!! tip "Usage"

```bash
datamodel-codegen --input schema.json --url https://api.example.com/schema.json --http-headers "Authorization:Bearer token" # (1)!
```

1. :material-arrow-left: `--url` - the option documented here

??? example "Examples"

**Input Schema:**

```json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Pet",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    },
    "tag": {
      "type": "string"
    }
  }
}
```

**Output:**

```python
# generated by datamodel-codegen:
#   filename:  https://api.example.com/schema.json
#   timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel


class Pet(BaseModel):
    id: int | None = None
    name: str | None = None
    tag: str | None = None
```