You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/json_schema.md
+62Lines changed: 62 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1414,6 +1414,68 @@ print(validation_schema)
1414
1414
"""
1415
1415
```
1416
1416
1417
+
### JSON schema sorting
1418
+
1419
+
By default, Pydantic recursively sorts JSON schemas by alphabetically sorting keys. Notably, Pydantic skips sorting the values of the `properties` key,
1420
+
to preserve the order of the fields as they were defined in the model.
1421
+
1422
+
If you would like to customize this behavior, you can override the `sort` method in your custom `GenerateJsonSchema` subclass. The below example
1423
+
uses a no-op `sort` method to disable sorting entirely, which is reflected in the preserved order of the model fields and `json_schema_extra` keys:
1424
+
1425
+
```py
1426
+
import json
1427
+
from typing import Optional
1428
+
1429
+
from pydantic import BaseModel, Field
1430
+
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue
0 commit comments