Skip to content

Commit 6274b70

Browse files
authored
Docs: describe --keep-model-order as deterministic dependency-aware ordering (#3090)
1 parent 3e87a9a commit 6274b70

5 files changed

Lines changed: 49 additions & 25 deletions

File tree

docs/cli-reference/model-customization.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
| [`--enable-faux-immutability`](#enable-faux-immutability) | Enable faux immutability in Pydantic models (frozen=True). |
2121
| [`--force-optional`](#force-optional) | Force all fields to be Optional regardless of required statu... |
2222
| [`--frozen-dataclasses`](#frozen-dataclasses) | Generate frozen dataclasses with optional keyword-only field... |
23-
| [`--keep-model-order`](#keep-model-order) | Keep model definition order as specified in schema. |
23+
| [`--keep-model-order`](#keep-model-order) | Keep generated model order deterministic while respecting de... |
2424
| [`--keyword-only`](#keyword-only) | Generate dataclasses with keyword-only fields (Python 3.10+)... |
2525
| [`--model-extra-keys`](#model-extra-keys) | Add model-level schema extensions to ConfigDict json_schema_... |
2626
| [`--model-extra-keys-without-x-prefix`](#model-extra-keys-without-x-prefix) | Strip x- prefix from model-level schema extensions and add t... |
@@ -2691,11 +2691,18 @@ keyword-only arguments.
26912691

26922692
## `--keep-model-order` {#keep-model-order}
26932693

2694-
Keep model definition order as specified in schema.
2694+
Keep generated model order deterministic while respecting dependency constraints.
26952695

2696-
The `--keep-model-order` flag preserves the original definition order from the schema
2697-
instead of reordering models based on dependencies. This is useful when the order
2698-
of model definitions matters for documentation or readability.
2696+
The `--keep-model-order` flag produces a stable, deterministic output order. The
2697+
generator starts from class-name order and only moves models when required to
2698+
satisfy dependency or runtime constraints (for example, a base class must appear
2699+
before its subclass, and cyclic groups must stay together).
2700+
2701+
This option is not equivalent to preserving the raw definition order from the
2702+
input schema, and it does not guarantee a strict unconditional alphabetical
2703+
order either. Inheritance and other runtime ordering requirements can force a
2704+
non-alphabetical arrangement. The value of the flag is that repeated runs on the
2705+
same schema produce the same ordering, which keeps diffs stable.
26992706

27002707
**Related:** [`--collapse-root-models`](model-customization.md#collapse-root-models)
27012708

docs/cli-reference/quick-reference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ datamodel-codegen [OPTIONS]
110110
| [`--enable-faux-immutability`](model-customization.md#enable-faux-immutability) | Enable faux immutability in Pydantic models (frozen=True). |
111111
| [`--force-optional`](model-customization.md#force-optional) | Force all fields to be Optional regardless of required status. |
112112
| [`--frozen-dataclasses`](model-customization.md#frozen-dataclasses) | Generate frozen dataclasses with optional keyword-only fields. |
113-
| [`--keep-model-order`](model-customization.md#keep-model-order) | Keep model definition order as specified in schema. |
113+
| [`--keep-model-order`](model-customization.md#keep-model-order) | Keep generated model order deterministic while respecting dependency constraints... |
114114
| [`--keyword-only`](model-customization.md#keyword-only) | Generate dataclasses with keyword-only fields (Python 3.10+). |
115115
| [`--model-extra-keys`](model-customization.md#model-extra-keys) | Add model-level schema extensions to ConfigDict json_schema_extra. |
116116
| [`--model-extra-keys-without-x-prefix`](model-customization.md#model-extra-keys-without-x-prefix) | Strip x- prefix from model-level schema extensions and add to ConfigDict json_sc... |
@@ -284,7 +284,7 @@ All options sorted alphabetically:
284284
- [`--input-file-type`](base-options.md#input-file-type) - Specify the input file type for code generation.
285285
- [`--input-model`](base-options.md#input-model) - Import a Python type or dict schema from a module.
286286
- [`--input-model-ref-strategy`](base-options.md#input-model-ref-strategy) - Strategy for referenced types when using --input-model.
287-
- [`--keep-model-order`](model-customization.md#keep-model-order) - Keep model definition order as specified in schema.
287+
- [`--keep-model-order`](model-customization.md#keep-model-order) - Keep generated model order deterministic while respecting de...
288288
- [`--keyword-only`](model-customization.md#keyword-only) - Generate dataclasses with keyword-only fields (Python 3.10+)...
289289
- [`--model-extra-keys`](model-customization.md#model-extra-keys) - Add model-level schema extensions to ConfigDict json_schema_...
290290
- [`--model-extra-keys-without-x-prefix`](model-customization.md#model-extra-keys-without-x-prefix) - Strip x- prefix from model-level schema extensions and add t...

docs/llms-full.txt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ Source: https://datamodel-code-generator.koxudaxi.dev/cli-reference/model-custom
12511251
| [`--enable-faux-immutability`](#enable-faux-immutability) | Enable faux immutability in Pydantic models (frozen=True). |
12521252
| [`--force-optional`](#force-optional) | Force all fields to be Optional regardless of required statu... |
12531253
| [`--frozen-dataclasses`](#frozen-dataclasses) | Generate frozen dataclasses with optional keyword-only field... |
1254-
| [`--keep-model-order`](#keep-model-order) | Keep model definition order as specified in schema. |
1254+
| [`--keep-model-order`](#keep-model-order) | Keep generated model order deterministic while respecting de... |
12551255
| [`--keyword-only`](#keyword-only) | Generate dataclasses with keyword-only fields (Python 3.10+)... |
12561256
| [`--model-extra-keys`](#model-extra-keys) | Add model-level schema extensions to ConfigDict json_schema_... |
12571257
| [`--model-extra-keys-without-x-prefix`](#model-extra-keys-without-x-prefix) | Strip x- prefix from model-level schema extensions and add t... |
@@ -3922,11 +3922,18 @@ keyword-only arguments.
39223922

39233923
## `--keep-model-order` {#keep-model-order}
39243924

3925-
Keep model definition order as specified in schema.
3925+
Keep generated model order deterministic while respecting dependency constraints.
39263926

3927-
The `--keep-model-order` flag preserves the original definition order from the schema
3928-
instead of reordering models based on dependencies. This is useful when the order
3929-
of model definitions matters for documentation or readability.
3927+
The `--keep-model-order` flag produces a stable, deterministic output order. The
3928+
generator starts from class-name order and only moves models when required to
3929+
satisfy dependency or runtime constraints (for example, a base class must appear
3930+
before its subclass, and cyclic groups must stay together).
3931+
3932+
This option is not equivalent to preserving the raw definition order from the
3933+
input schema, and it does not guarantee a strict unconditional alphabetical
3934+
order either. Inheritance and other runtime ordering requirements can force a
3935+
non-alphabetical arrangement. The value of the flag is that repeated runs on the
3936+
same schema produce the same ordering, which keeps diffs stable.
39303937

39313938
**Related:** [`--collapse-root-models`](model-customization.md#collapse-root-models)
39323939

@@ -24015,7 +24022,7 @@ datamodel-codegen [OPTIONS]
2401524022
| [`--enable-faux-immutability`](model-customization.md#enable-faux-immutability) | Enable faux immutability in Pydantic models (frozen=True). |
2401624023
| [`--force-optional`](model-customization.md#force-optional) | Force all fields to be Optional regardless of required status. |
2401724024
| [`--frozen-dataclasses`](model-customization.md#frozen-dataclasses) | Generate frozen dataclasses with optional keyword-only fields. |
24018-
| [`--keep-model-order`](model-customization.md#keep-model-order) | Keep model definition order as specified in schema. |
24025+
| [`--keep-model-order`](model-customization.md#keep-model-order) | Keep generated model order deterministic while respecting dependency constraints... |
2401924026
| [`--keyword-only`](model-customization.md#keyword-only) | Generate dataclasses with keyword-only fields (Python 3.10+). |
2402024027
| [`--model-extra-keys`](model-customization.md#model-extra-keys) | Add model-level schema extensions to ConfigDict json_schema_extra. |
2402124028
| [`--model-extra-keys-without-x-prefix`](model-customization.md#model-extra-keys-without-x-prefix) | Strip x- prefix from model-level schema extensions and add to ConfigDict json_sc... |
@@ -24189,7 +24196,7 @@ All options sorted alphabetically:
2418924196
- [`--input-file-type`](base-options.md#input-file-type) - Specify the input file type for code generation.
2419024197
- [`--input-model`](base-options.md#input-model) - Import a Python type or dict schema from a module.
2419124198
- [`--input-model-ref-strategy`](base-options.md#input-model-ref-strategy) - Strategy for referenced types when using --input-model.
24192-
- [`--keep-model-order`](model-customization.md#keep-model-order) - Keep model definition order as specified in schema.
24199+
- [`--keep-model-order`](model-customization.md#keep-model-order) - Keep generated model order deterministic while respecting de...
2419324200
- [`--keyword-only`](model-customization.md#keyword-only) - Generate dataclasses with keyword-only fields (Python 3.10+)...
2419424201
- [`--model-extra-keys`](model-customization.md#model-extra-keys) - Add model-level schema extensions to ConfigDict json_schema_...
2419524202
- [`--model-extra-keys-without-x-prefix`](model-customization.md#model-extra-keys-without-x-prefix) - Strip x- prefix from model-level schema extensions and add t...

src/datamodel_code_generator/prompt_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"--input-file-type": "Specify the input file type for code generation.",
7373
"--input-model": "Import a Python type or dict schema from a module.",
7474
"--input-model-ref-strategy": "Strategy for referenced types when using --input-model.",
75-
"--keep-model-order": "Keep model definition order as specified in schema.",
75+
"--keep-model-order": "Keep generated model order deterministic while respecting dependency constraints.",
7676
"--keyword-only": "Generate dataclasses with keyword-only fields (Python 3.10+).",
7777
"--model-extra-keys": "Add model-level schema extensions to ConfigDict json_schema_extra.",
7878
"--model-extra-keys-without-x-prefix": "Strip x- prefix from model-level schema extensions and add to ConfigDic...",

tests/main/jsonschema/test_main_jsonschema.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,32 @@ def test_main_inheritance_forward_ref(output_file: Path, tmp_path: Path) -> None
9898
@pytest.mark.benchmark
9999
@pytest.mark.cli_doc(
100100
options=["--keep-model-order"],
101-
option_description="""Keep model definition order as specified in schema.
102-
103-
The `--keep-model-order` flag preserves the original definition order from the schema
104-
instead of reordering models based on dependencies. This is useful when the order
105-
of model definitions matters for documentation or readability.""",
101+
option_description="""Keep generated model order deterministic while respecting dependency constraints.
102+
103+
The `--keep-model-order` flag produces a stable, deterministic output order. The
104+
generator starts from class-name order and only moves models when required to
105+
satisfy dependency or runtime constraints (for example, a base class must appear
106+
before its subclass, and cyclic groups must stay together).
107+
108+
This option is not equivalent to preserving the raw definition order from the
109+
input schema, and it does not guarantee a strict unconditional alphabetical
110+
order either. Inheritance and other runtime ordering requirements can force a
111+
non-alphabetical arrangement. The value of the flag is that repeated runs on the
112+
same schema produce the same ordering, which keeps diffs stable.""",
106113
input_schema="jsonschema/inheritance_forward_ref.json",
107114
cli_args=["--keep-model-order"],
108115
golden_output="jsonschema/inheritance_forward_ref_keep_model_order.py",
109116
related_options=["--collapse-root-models"],
110117
)
111118
def test_main_inheritance_forward_ref_keep_model_order(output_file: Path, tmp_path: Path) -> None:
112-
"""Keep model definition order as specified in schema.
113-
114-
The `--keep-model-order` flag preserves the original definition order from the schema
115-
instead of reordering models based on dependencies. This is useful when the order
116-
of model definitions matters for documentation or readability.
119+
"""Keep generated model order deterministic while respecting dependency constraints.
120+
121+
The `--keep-model-order` flag produces a stable, deterministic output order.
122+
The generator starts from class-name order and only moves models when required
123+
to satisfy dependency or runtime constraints. It is not equivalent to
124+
preserving the raw schema/spec definition order, and it does not guarantee a
125+
strict unconditional alphabetical order either — inheritance and other
126+
runtime ordering requirements can force a non-alphabetical arrangement.
117127
"""
118128
run_main_and_assert(
119129
input_path=JSON_SCHEMA_DATA_PATH / "inheritance_forward_ref.json",

0 commit comments

Comments
 (0)