📁 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. |
--input-model |
Import a Python type or dict schema from a module. |
--input-model-ref-strategy |
Strategy for referenced types when using --input-model. |
--output |
Specify the destination path for generated Python code. |
--schema-version |
Schema version to use for parsing. |
--schema-version-mode |
Schema version validation mode. |
--url |
Fetch schema from URL with custom HTTP headers. |
--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 UTF-8, which is the standard encoding for JSON and most modern text files.
Usage
-
--encoding- the option documented here
Examples
Input Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "日本語Model",
"description": "モデルの説明文",
"type": "object",
"properties": {
"名前": {
"type": "string",
"description": "ユーザー名"
},
"年齢": {
"type": "integer"
}
}
}
Output:
--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.
Usage
-
--input- the option documented here
Examples
Input Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Pet",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
Output:
--input-file-type¶
Specify the input file type for code generation.
The --input-file-type flag explicitly sets the input format.
Important distinction:
- Use
jsonschema,openapi, orgraphqlfor schema definition files - Use
json,yaml, orcsvfor raw sample data to automatically infer a schema
For example, if you have a JSON Schema written in YAML format, use --input-file-type jsonschema,
not --input-file-type yaml. The yaml type treats the file as raw data and infers a schema from it.
Usage
-
--input-file-type- the option documented here
Examples
Input Schema:
Output:
--input-model¶
Import a Python type or dict schema from a module.
Use the format module:Object or path/to/file.py:Object to specify the type.
Usage
-
--input-model- the option documented here
Examples
Output:
--input-model-ref-strategy¶
Strategy for referenced types when using --input-model.
The --input-model-ref-strategy option determines whether to regenerate or import
referenced types. Use regenerate-all (default) to regenerate all types,
reuse-foreign to import types from different families (like enums when generating
dataclasses) while regenerating same-family types, or reuse-all to import all
referenced types directly.
Usage
-
--input-model-ref-strategy- the option documented here
Examples
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.
Usage
-
--output- the option documented here
Examples
Input Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Pet",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
Output:
--schema-version¶
Schema version to use for parsing.
The --schema-version option specifies the schema version to use instead of auto-detection.
Valid values depend on input type: JsonSchema (draft-04, draft-06, draft-07, 2019-09, 2020-12)
or OpenAPI (3.0, 3.1). Default is 'auto' (detected from $schema or openapi field).
Usage
-
--schema-version- the option documented here
Examples
Input Schema:
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {"s": {"type": ["string"]}},
"required": ["s"]
}
Output:
Error: File not found: jsonschema/simple_string.py
--schema-version-mode¶
Schema version validation mode.
The --schema-version-mode option controls how schema version validation is performed.
'lenient' (default): accept all features regardless of version.
'strict': warn on features outside the declared/detected version.
Usage
-
--schema-version-mode- the option documented here
Examples
Input Schema:
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {"s": {"type": ["string"]}},
"required": ["s"]
}
Output:
Error: File not found: jsonschema/simple_string.py
--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.
Usage
datamodel-codegen --input schema.json --url https://api.example.com/schema.json --http-headers "Authorization:Bearer token" # (1)!
-
--url- the option documented here
Examples
Input Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Pet",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
Output: