Skip to content

Commit 2ca6954

Browse files
authored
Add model metadata output (#3443)
* Add model metadata output * Document emitted model metadata * Add model metadata schema output * Lazy load model metadata helpers
1 parent 7b83709 commit 2ca6954

53 files changed

Lines changed: 1570 additions & 88 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/cli-reference/base-options.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
| Option | Description |
66
|--------|-------------|
7+
| [`--emit-model-metadata`](#emit-model-metadata) | Write a separate JSON map from source schema references to t... |
78
| [`--encoding`](#encoding) | Specify character encoding for input and output files. |
89
| [`--external-ref-mapping`](#external-ref-mapping) | Map external `$ref` files to Python packages. |
910
| [`--input`](#input) | Specify the input schema file path. |
@@ -18,6 +19,127 @@
1819

1920
---
2021

22+
## `--emit-model-metadata` {#emit-model-metadata}
23+
24+
Write a separate JSON map from source schema references to the final generated models, modules, fields, and type hints.
25+
26+
!!! tip "Usage"
27+
28+
```bash
29+
datamodel-codegen --input schema.json --emit-model-metadata model-map.json --module-split-mode single --disable-timestamp # (1)!
30+
```
31+
32+
1. :material-arrow-left: `--emit-model-metadata` - the option documented here
33+
34+
??? example "Examples"
35+
36+
**Input Schema:**
37+
38+
```json
39+
{
40+
"$schema": "https://json-schema.org/draft/2020-12/schema",
41+
"title": "Root title",
42+
"type": "object",
43+
"properties": {
44+
"method": {
45+
"const": "thread/started"
46+
},
47+
"payload": {
48+
"$ref": "#/$defs/MetadataPayload"
49+
}
50+
},
51+
"required": ["method", "payload"],
52+
"$defs": {
53+
"MetadataPayload": {
54+
"title": "Payload title",
55+
"type": "object",
56+
"properties": {
57+
"id": {
58+
"type": "integer"
59+
}
60+
},
61+
"required": ["id"]
62+
}
63+
}
64+
}
65+
```
66+
67+
**Output:**
68+
69+
```python
70+
# generated by datamodel-codegen:
71+
# filename: model_metadata.json
72+
73+
from __future__ import annotations
74+
75+
from typing import Literal
76+
77+
from pydantic import BaseModel
78+
79+
from . import metadata_payload
80+
81+
82+
class RootTitle(BaseModel):
83+
method: Literal['thread/started']
84+
payload: metadata_payload.MetadataPayload
85+
```
86+
87+
**Generated metadata (`model-map.json`):**
88+
89+
```json
90+
{
91+
"version": 1,
92+
"models": [
93+
{
94+
"class_name": "RootTitle",
95+
"name": "RootTitle",
96+
"module": "root_title",
97+
"source_ref": "model_metadata.json#",
98+
"source_path": [],
99+
"title": "Root title",
100+
"fields": [
101+
{
102+
"name": "method",
103+
"alias": "method",
104+
"original_name": "method",
105+
"type": "Literal['thread/started']",
106+
"required": true
107+
},
108+
{
109+
"name": "payload",
110+
"alias": "payload",
111+
"original_name": "payload",
112+
"type": "metadata_payload.MetadataPayload",
113+
"required": true
114+
}
115+
]
116+
},
117+
{
118+
"class_name": "MetadataPayload",
119+
"name": "MetadataPayload",
120+
"module": "metadata_payload",
121+
"source_ref": "model_metadata.json#/$defs/MetadataPayload",
122+
"source_path": [
123+
"$defs",
124+
"MetadataPayload"
125+
],
126+
"title": "Payload title",
127+
"fields": [
128+
{
129+
"name": "id",
130+
"alias": "id",
131+
"original_name": "id",
132+
"type": "int",
133+
"required": true
134+
}
135+
]
136+
}
137+
]
138+
}
139+
```
140+
141+
---
142+
21143
## `--encoding` {#encoding}
22144

23145
Specify character encoding for input and output files.

docs/cli-reference/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This documentation is auto-generated from test cases.
88

99
| Category | Options | Description |
1010
|----------|---------|-------------|
11-
| 📁 [Base Options](base-options.md) | 11 | Input/output configuration |
11+
| 📁 [Base Options](base-options.md) | 12 | Input/output configuration |
1212
| 🔧 [Typing Customization](typing-customization.md) | 30 | Type annotation and import behavior |
1313
| 🏷️ [Field Customization](field-customization.md) | 27 | Field naming and docstring behavior |
1414
| 🏗️ [Model Customization](model-customization.md) | 41 | Model generation behavior |
@@ -74,6 +74,7 @@ This documentation is auto-generated from test cases.
7474

7575
### E {#e}
7676

77+
- [`--emit-model-metadata`](base-options.md#emit-model-metadata)
7778
- [`--empty-enum-field-name`](field-customization.md#empty-enum-field-name)
7879
- [`--enable-command-header`](template-customization.md#enable-command-header)
7980
- [`--enable-faux-immutability`](model-customization.md#enable-faux-immutability)
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## `--output-format-json-schema` {#output-format-json-schema}
22

3-
Output JSON Schema for a structured command output format and exit.
3+
Output JSON Schema for a JSON output format and exit.
44

55
Use this when an LLM agent, tool call definition, or validation layer needs the
66
contract before consuming JSON output. The schema is emitted separately from the
@@ -11,6 +11,7 @@ Currently supported schema targets:
1111

1212
- `generate-prompt`: schema for `--generate-prompt --output-format json`
1313
- `generation`: schema for normal generation with `--output-format json`
14+
- `model-metadata`: schema for files emitted by `--emit-model-metadata`
1415
- `structured-output`: tagged union schema for all structured command outputs,
1516
discriminated by `kind`
1617

@@ -19,13 +20,15 @@ Currently supported schema targets:
1920
```bash
2021
datamodel-codegen --output-format-json-schema generate-prompt # (1)!
2122
datamodel-codegen --output-format-json-schema generation # (2)!
22-
datamodel-codegen --output-format-json-schema structured-output # (3)!
23-
datamodel-codegen --generate-prompt --output-format json # (4)!
24-
datamodel-codegen --input schema.json --output-format json # (5)!
23+
datamodel-codegen --output-format-json-schema model-metadata # (3)!
24+
datamodel-codegen --output-format-json-schema structured-output # (4)!
25+
datamodel-codegen --generate-prompt --output-format json # (5)!
26+
datamodel-codegen --input schema.json --emit-model-metadata model-map.json # (6)!
2527
```
2628

2729
1. :material-arrow-left: Emit the JSON Schema for structured prompt output
2830
2. :material-arrow-left: Emit the JSON Schema for generated-file output
29-
3. :material-arrow-left: Emit the JSON Schema for all structured command outputs
30-
4. :material-arrow-left: Emit prompt payloads that match the prompt schema
31-
5. :material-arrow-left: Emit generation payloads that match the generation schema
31+
3. :material-arrow-left: Emit the JSON Schema for generated model metadata
32+
4. :material-arrow-left: Emit the JSON Schema for all structured command outputs
33+
5. :material-arrow-left: Emit prompt payloads that match the prompt schema
34+
6. :material-arrow-left: Emit metadata payloads that match the model metadata schema

docs/cli-reference/manual/output-format.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ is the generated module path.
2424

2525
Use `--output-format json` with `--generate-prompt` to emit structured option
2626
metadata instead of Markdown. Use `--output-format-json-schema` when an LLM
27-
agent or tool needs the schema for a structured output payload.
27+
agent or tool needs the schema for a JSON payload.
2828

2929
Schema targets are intentionally scoped. `generate-prompt` emits the
3030
`PromptPayload` schema for `--generate-prompt --output-format json`.
3131
`generation` emits only the `GenerationPayload` schema for generated-file JSON.
32+
`model-metadata` emits the schema for files written by `--emit-model-metadata`.
3233
`structured-output` emits the broader `StructuredOutputPayload` schema, a union
3334
covering `GenerationPayload`, `PromptPayload`, `CommandOutputPayload`, and
3435
`CheckOutputPayload`. Structured payloads use `kind` as the discriminator.
@@ -41,15 +42,17 @@ covering `GenerationPayload`, `PromptPayload`, `CommandOutputPayload`, and
4142
datamodel-codegen --generate-prompt --output-format json # (3)!
4243
datamodel-codegen --output-format-json-schema generation # (4)!
4344
datamodel-codegen --output-format-json-schema generate-prompt # (5)!
44-
datamodel-codegen --output-format-json-schema structured-output # (6)!
45+
datamodel-codegen --output-format-json-schema model-metadata # (6)!
46+
datamodel-codegen --output-format-json-schema structured-output # (7)!
4547
```
4648

4749
1. :material-arrow-left: Emit the default generated Python text
4850
2. :material-arrow-left: Emit structured JSON containing generated files
4951
3. :material-arrow-left: Emit structured JSON with current options and argparse metadata
5052
4. :material-arrow-left: Emit JSON Schema for generated-file JSON output
5153
5. :material-arrow-left: Emit JSON Schema for structured prompt JSON
52-
6. :material-arrow-left: Emit JSON Schema for any structured command JSON output
54+
6. :material-arrow-left: Emit JSON Schema for generated model metadata JSON
55+
7. :material-arrow-left: Emit JSON Schema for any structured command JSON output
5356

5457
??? example "Generation JSON output"
5558

docs/cli-reference/quick-reference.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ datamodel-codegen [OPTIONS]
1616

1717
| Option | Description |
1818
|--------|-------------|
19+
| [`--emit-model-metadata`](base-options.md#emit-model-metadata) | Write a separate JSON map from source schema references to the final generated m... |
1920
| [`--encoding`](base-options.md#encoding) | Specify character encoding for input and output files. |
2021
| [`--external-ref-mapping`](base-options.md#external-ref-mapping) | Map external `$ref` files to Python packages. |
2122
| [`--input`](base-options.md#input) | Specify the input schema file path. |
@@ -221,7 +222,7 @@ datamodel-codegen [OPTIONS]
221222
| [`--list-experimental`](utility-options.md#list-experimental) | List registered experimental features |
222223
| [`--no-color`](utility-options.md#no-color) | Disable colorized output |
223224
| [`--output-format`](utility-options.md#output-format) | Choose the command output format |
224-
| [`--output-format-json-schema`](utility-options.md#output-format-json-schema) | Output JSON Schema for structured command output |
225+
| [`--output-format-json-schema`](utility-options.md#output-format-json-schema) | Output JSON Schema for JSON output payloads |
225226
| [`--profile`](utility-options.md#profile) | Use a named profile from pyproject.toml |
226227
| [`--version`](utility-options.md#version) | Show program version and exit |
227228

@@ -268,6 +269,7 @@ All options sorted alphabetically:
268269
- [`--disable-timestamp`](template-customization.md#disable-timestamp) - Disable timestamp in generated file header for reproducible ...
269270
- [`--disable-warnings`](general-options.md#disable-warnings) - Suppress warning messages during code generation.
270271
- [`--duplicate-name-suffix`](model-customization.md#duplicate-name-suffix) - Customize suffix for duplicate model names.
272+
- [`--emit-model-metadata`](base-options.md#emit-model-metadata) - Write a separate JSON map from source schema references to t...
271273
- [`--empty-enum-field-name`](field-customization.md#empty-enum-field-name) - Name for empty string enum field values.
272274
- [`--enable-command-header`](template-customization.md#enable-command-header) - Include command-line options in file header for reproducibil...
273275
- [`--enable-faux-immutability`](model-customization.md#enable-faux-immutability) - Enable faux immutability in Pydantic models (frozen=True).
@@ -329,7 +331,7 @@ All options sorted alphabetically:
329331
- [`--output-date-class`](typing-customization.md#output-date-class) - Specify date class type for date schema fields.
330332
- [`--output-datetime-class`](typing-customization.md#output-datetime-class) - Specify datetime class type for date-time schema fields.
331333
- [`--output-format`](utility-options.md#output-format) - Choose the command output format
332-
- [`--output-format-json-schema`](utility-options.md#output-format-json-schema) - Output JSON Schema for structured command output
334+
- [`--output-format-json-schema`](utility-options.md#output-format-json-schema) - Output JSON Schema for JSON output payloads
333335
- [`--output-model-type`](model-customization.md#output-model-type) - Select the output model type (Pydantic v2, Pydantic v2 datac...
334336
- [`--parent-scoped-naming`](model-customization.md#parent-scoped-naming) - Namespace models by their parent scope to avoid naming confl...
335337
- [`--preset`](base-options.md#preset) - Apply an immutable built-in option preset.

docs/cli-reference/utility-options.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
| [`--list-experimental`](#list-experimental) | List registered experimental features |
1212
| [`--no-color`](#no-color) | Disable colorized output |
1313
| [`--output-format`](#output-format) | Choose the command output format |
14-
| [`--output-format-json-schema`](#output-format-json-schema) | Output JSON Schema for structured command output |
14+
| [`--output-format-json-schema`](#output-format-json-schema) | Output JSON Schema for JSON output payloads |
1515
| [`--profile`](#profile) | Use a named profile from pyproject.toml |
1616
| [`--version`](#version) | Show program version and exit |
1717

@@ -227,11 +227,12 @@ is the generated module path.
227227

228228
Use `--output-format json` with `--generate-prompt` to emit structured option
229229
metadata instead of Markdown. Use `--output-format-json-schema` when an LLM
230-
agent or tool needs the schema for a structured output payload.
230+
agent or tool needs the schema for a JSON payload.
231231

232232
Schema targets are intentionally scoped. `generate-prompt` emits the
233233
`PromptPayload` schema for `--generate-prompt --output-format json`.
234234
`generation` emits only the `GenerationPayload` schema for generated-file JSON.
235+
`model-metadata` emits the schema for files written by `--emit-model-metadata`.
235236
`structured-output` emits the broader `StructuredOutputPayload` schema, a union
236237
covering `GenerationPayload`, `PromptPayload`, `CommandOutputPayload`, and
237238
`CheckOutputPayload`. Structured payloads use `kind` as the discriminator.
@@ -244,15 +245,17 @@ covering `GenerationPayload`, `PromptPayload`, `CommandOutputPayload`, and
244245
datamodel-codegen --generate-prompt --output-format json # (3)!
245246
datamodel-codegen --output-format-json-schema generation # (4)!
246247
datamodel-codegen --output-format-json-schema generate-prompt # (5)!
247-
datamodel-codegen --output-format-json-schema structured-output # (6)!
248+
datamodel-codegen --output-format-json-schema model-metadata # (6)!
249+
datamodel-codegen --output-format-json-schema structured-output # (7)!
248250
```
249251

250252
1. :material-arrow-left: Emit the default generated Python text
251253
2. :material-arrow-left: Emit structured JSON containing generated files
252254
3. :material-arrow-left: Emit structured JSON with current options and argparse metadata
253255
4. :material-arrow-left: Emit JSON Schema for generated-file JSON output
254256
5. :material-arrow-left: Emit JSON Schema for structured prompt JSON
255-
6. :material-arrow-left: Emit JSON Schema for any structured command JSON output
257+
6. :material-arrow-left: Emit JSON Schema for generated model metadata JSON
258+
7. :material-arrow-left: Emit JSON Schema for any structured command JSON output
256259

257260
??? example "Generation JSON output"
258261

@@ -285,7 +288,7 @@ covering `GenerationPayload`, `PromptPayload`, `CommandOutputPayload`, and
285288

286289
## `--output-format-json-schema` {#output-format-json-schema}
287290

288-
Output JSON Schema for a structured command output format and exit.
291+
Output JSON Schema for a JSON output format and exit.
289292

290293
Use this when an LLM agent, tool call definition, or validation layer needs the
291294
contract before consuming JSON output. The schema is emitted separately from the
@@ -296,6 +299,7 @@ Currently supported schema targets:
296299

297300
- `generate-prompt`: schema for `--generate-prompt --output-format json`
298301
- `generation`: schema for normal generation with `--output-format json`
302+
- `model-metadata`: schema for files emitted by `--emit-model-metadata`
299303
- `structured-output`: tagged union schema for all structured command outputs,
300304
discriminated by `kind`
301305

@@ -304,16 +308,18 @@ Currently supported schema targets:
304308
```bash
305309
datamodel-codegen --output-format-json-schema generate-prompt # (1)!
306310
datamodel-codegen --output-format-json-schema generation # (2)!
307-
datamodel-codegen --output-format-json-schema structured-output # (3)!
308-
datamodel-codegen --generate-prompt --output-format json # (4)!
309-
datamodel-codegen --input schema.json --output-format json # (5)!
311+
datamodel-codegen --output-format-json-schema model-metadata # (3)!
312+
datamodel-codegen --output-format-json-schema structured-output # (4)!
313+
datamodel-codegen --generate-prompt --output-format json # (5)!
314+
datamodel-codegen --input schema.json --emit-model-metadata model-map.json # (6)!
310315
```
311316

312317
1. :material-arrow-left: Emit the JSON Schema for structured prompt output
313318
2. :material-arrow-left: Emit the JSON Schema for generated-file output
314-
3. :material-arrow-left: Emit the JSON Schema for all structured command outputs
315-
4. :material-arrow-left: Emit prompt payloads that match the prompt schema
316-
5. :material-arrow-left: Emit generation payloads that match the generation schema
319+
3. :material-arrow-left: Emit the JSON Schema for generated model metadata
320+
4. :material-arrow-left: Emit the JSON Schema for all structured command outputs
321+
5. :material-arrow-left: Emit prompt payloads that match the prompt schema
322+
6. :material-arrow-left: Emit metadata payloads that match the model metadata schema
317323

318324
---
319325

0 commit comments

Comments
 (0)