JSON Schema
Usage Documentation
The json_schema
module contains classes and functions to allow the way JSON Schema
is generated to be customized.
In general you shouldn't need to use this module directly; instead, you can
BaseModel.model_json_schema
and
TypeAdapter.json_schema
.
CoreSchemaOrFieldType
module-attribute
¶
CoreSchemaOrFieldType = Literal[
CoreSchemaType, CoreSchemaFieldType
]
A type alias for defined schema types that represents a union of
core_schema.CoreSchemaType
and
core_schema.CoreSchemaFieldType
.
JsonSchemaValue
module-attribute
¶
JsonSchemaValue = Dict[str, Any]
A type alias for a JSON schema value. This is a dictionary of string keys to arbitrary JSON values.
JsonSchemaMode
module-attribute
¶
JsonSchemaMode = Literal['validation', 'serialization']
A type alias that represents the mode of a JSON schema; either 'validation' or 'serialization'.
For some types, the inputs to validation differ from the outputs of serialization. For example, computed fields will only be present when serializing, and should not be provided when validating. This flag provides a way to indicate whether you want the JSON schema required for validation inputs, or that will be matched by serialization outputs.
JsonSchemaWarningKind
module-attribute
¶
JsonSchemaWarningKind = Literal[
"skipped-choice", "non-serializable-default"
]
A type alias representing the kinds of warnings that can be emitted during JSON schema generation.
See GenerateJsonSchema.render_warning_message
for more details.
DEFAULT_REF_TEMPLATE
module-attribute
¶
DEFAULT_REF_TEMPLATE = '#/$defs/{model}'
The default format string used to generate reference names.
PydanticJsonSchemaWarning ¶
Bases: UserWarning
This class is used to emit warnings produced during JSON schema generation.
See the GenerateJsonSchema.emit_warning
and
GenerateJsonSchema.render_warning_message
methods for more details; these can be overridden to control warning behavior.
GenerateJsonSchema ¶
GenerateJsonSchema(
by_alias=True, ref_template=DEFAULT_REF_TEMPLATE
)
Usage Documentation
A class for generating JSON schemas.
This class generates JSON schemas based on configured parameters. The default schema dialect
is https://json-schema.org/draft/2020-12/schema.
The class uses by_alias
to configure how fields with
multiple names are handled and ref_template
to format reference names.
Attributes:
Name | Type | Description |
---|---|---|
schema_dialect |
The JSON schema dialect used to generate the schema. See Declaring a Dialect in the JSON Schema documentation for more information about dialects. |
|
ignored_warning_kinds |
set[JsonSchemaWarningKind]
|
Warnings to ignore when generating the schema. |
by_alias |
Whether to use field aliases when generating the schema. |
|
ref_template |
The format string used when generating reference names. |
|
core_to_json_refs |
dict[CoreModeRef, JsonRef]
|
A mapping of core refs to JSON refs. |
core_to_defs_refs |
dict[CoreModeRef, DefsRef]
|
A mapping of core refs to definition refs. |
defs_to_core_refs |
dict[DefsRef, CoreModeRef]
|
A mapping of definition refs to core refs. |
json_to_defs_refs |
dict[JsonRef, DefsRef]
|
A mapping of JSON refs to definition refs. |
definitions |
dict[DefsRef, JsonSchemaValue]
|
Definitions in the schema. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
by_alias |
bool
|
Whether to use field aliases in the generated schemas. |
True
|
ref_template |
str
|
The format string to use when generating reference names. |
DEFAULT_REF_TEMPLATE
|
Raises:
Type | Description |
---|---|
JsonSchemaError
|
If the instance of the class is inadvertently re-used after generating a schema. |
Source code in pydantic/json_schema.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
ValidationsMapping ¶
This class just contains mappings from core_schema attribute names to the corresponding JSON schema attribute names. While I suspect it is unlikely to be necessary, you can in principle override this class in a subclass of GenerateJsonSchema (by inheriting from GenerateJsonSchema.ValidationsMapping) to change these mappings.
build_schema_type_to_method ¶
build_schema_type_to_method()
Builds a dictionary mapping fields to methods for generating JSON schemas.
Returns:
Type | Description |
---|---|
dict[CoreSchemaOrFieldType, Callable[[CoreSchemaOrField], JsonSchemaValue]]
|
A dictionary containing the mapping of |
Raises:
Type | Description |
---|---|
TypeError
|
If no method has been defined for generating a JSON schema for a given pydantic core schema type. |
Source code in pydantic/json_schema.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
|
generate_definitions ¶
generate_definitions(inputs)
Generates JSON schema definitions from a list of core schemas, pairing the generated definitions with a mapping that links the input keys to the definition references.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
Sequence[tuple[JsonSchemaKeyT, JsonSchemaMode, CoreSchema]]
|
A sequence of tuples, where:
|
required |
Returns:
Type | Description |
---|---|
tuple[dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], dict[DefsRef, JsonSchemaValue]]
|
A tuple where:
|
Raises:
Type | Description |
---|---|
PydanticUserError
|
Raised if the JSON schema generator has already been used to generate a JSON schema. |
Source code in pydantic/json_schema.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
|
generate ¶
generate(schema, mode='validation')
Generates a JSON schema for a specified schema in a specified mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchema
|
A Pydantic model. |
required |
mode |
JsonSchemaMode
|
The mode in which to generate the schema. Defaults to 'validation'. |
'validation'
|
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
A JSON schema representing the specified schema. |
Raises:
Type | Description |
---|---|
PydanticUserError
|
If the JSON schema generator has already been used to generate a JSON schema. |
Source code in pydantic/json_schema.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
|
generate_inner ¶
generate_inner(schema)
Generates a JSON schema for a given core schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchemaOrField
|
The given core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
|
any_schema ¶
any_schema(schema)
Generates a JSON schema that matches any value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
AnySchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
554 555 556 557 558 559 560 561 562 563 |
|
none_schema ¶
none_schema(schema)
Generates a JSON schema that matches None
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
NoneSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
565 566 567 568 569 570 571 572 573 574 |
|
bool_schema ¶
bool_schema(schema)
Generates a JSON schema that matches a bool value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
BoolSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
576 577 578 579 580 581 582 583 584 585 |
|
int_schema ¶
int_schema(schema)
Generates a JSON schema that matches an int value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
IntSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
587 588 589 590 591 592 593 594 595 596 597 598 599 |
|
float_schema ¶
float_schema(schema)
Generates a JSON schema that matches a float value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
FloatSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
601 602 603 604 605 606 607 608 609 610 611 612 613 |
|
decimal_schema ¶
decimal_schema(schema)
Generates a JSON schema that matches a decimal value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DecimalSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 |
|
str_schema ¶
str_schema(schema)
Generates a JSON schema that matches a string value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
StringSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
648 649 650 651 652 653 654 655 656 657 658 659 |
|
bytes_schema ¶
bytes_schema(schema)
Generates a JSON schema that matches a bytes value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
BytesSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
661 662 663 664 665 666 667 668 669 670 671 672 |
|
date_schema ¶
date_schema(schema)
Generates a JSON schema that matches a date value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DateSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
674 675 676 677 678 679 680 681 682 683 684 685 |
|
time_schema ¶
time_schema(schema)
Generates a JSON schema that matches a time value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
TimeSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
687 688 689 690 691 692 693 694 695 696 |
|
datetime_schema ¶
datetime_schema(schema)
Generates a JSON schema that matches a datetime value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DatetimeSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
698 699 700 701 702 703 704 705 706 707 |
|
timedelta_schema ¶
timedelta_schema(schema)
Generates a JSON schema that matches a timedelta value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
TimedeltaSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
709 710 711 712 713 714 715 716 717 718 719 720 |
|
literal_schema ¶
literal_schema(schema)
Generates a JSON schema that matches a literal value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
LiteralSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
|
is_instance_schema ¶
is_instance_schema(schema)
Handles JSON schema generation for a core schema that checks if a value is an instance of a class.
Unless overridden in a subclass, this raises an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
IsInstanceSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
754 755 756 757 758 759 760 761 762 763 764 765 |
|
is_subclass_schema ¶
is_subclass_schema(schema)
Handles JSON schema generation for a core schema that checks if a value is a subclass of a class.
For backwards compatibility with v1, this does not raise an error, but can be overridden to change this.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
IsSubclassSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
767 768 769 770 771 772 773 774 775 776 777 778 779 |
|
callable_schema ¶
callable_schema(schema)
Generates a JSON schema that matches a callable value.
Unless overridden in a subclass, this raises an error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CallableSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
781 782 783 784 785 786 787 788 789 790 791 792 |
|
list_schema ¶
list_schema(schema)
Returns a schema that matches a list schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ListSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
794 795 796 797 798 799 800 801 802 803 804 805 806 |
|
tuple_positional_schema ¶
tuple_positional_schema(schema)
Replaced by tuple_schema
.
Source code in pydantic/json_schema.py
808 809 810 811 812 813 814 815 816 817 |
|
tuple_variable_schema ¶
tuple_variable_schema(schema)
Replaced by tuple_schema
.
Source code in pydantic/json_schema.py
819 820 821 822 823 824 825 826 827 828 |
|
tuple_schema ¶
tuple_schema(schema)
Generates a JSON schema that matches a tuple schema e.g. Tuple[int,
str, bool]
or Tuple[int, ...]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
TupleSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 |
|
set_schema ¶
set_schema(schema)
Generates a JSON schema that matches a set schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
SetSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
865 866 867 868 869 870 871 872 873 874 |
|
frozenset_schema ¶
frozenset_schema(schema)
Generates a JSON schema that matches a frozenset schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
FrozenSetSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
876 877 878 879 880 881 882 883 884 885 |
|
generator_schema ¶
generator_schema(schema)
Returns a JSON schema that represents the provided GeneratorSchema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
GeneratorSchema
|
The schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
893 894 895 896 897 898 899 900 901 902 903 904 905 |
|
dict_schema ¶
dict_schema(schema)
Generates a JSON schema that matches a dict schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 |
|
function_before_schema ¶
function_before_schema(schema)
Generates a JSON schema that matches a function-before schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
BeforeValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
946 947 948 949 950 951 952 953 954 955 |
|
function_after_schema ¶
function_after_schema(schema)
Generates a JSON schema that matches a function-after schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
AfterValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
957 958 959 960 961 962 963 964 965 966 |
|
function_plain_schema ¶
function_plain_schema(schema)
Generates a JSON schema that matches a function-plain schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
PlainValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
968 969 970 971 972 973 974 975 976 977 |
|
function_wrap_schema ¶
function_wrap_schema(schema)
Generates a JSON schema that matches a function-wrap schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
WrapValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
979 980 981 982 983 984 985 986 987 988 |
|
default_schema ¶
default_schema(schema)
Generates a JSON schema that matches a schema with a default value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
WithDefaultSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 |
|
nullable_schema ¶
nullable_schema(schema)
Generates a JSON schema that matches a schema that allows null values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
NullableSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 |
|
union_schema ¶
union_schema(schema)
Generates a JSON schema that matches a schema that allows values matching any of the given schemas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
UnionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 |
|
tagged_union_schema ¶
tagged_union_schema(schema)
Generates a JSON schema that matches a schema that allows values matching any of the given schemas, where the schemas are tagged with a discriminator field that indicates which schema should be used to validate the value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
TaggedUnionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 |
|
chain_schema ¶
chain_schema(schema)
Generates a JSON schema that matches a core_schema.ChainSchema.
When generating a schema for validation, we return the validation JSON schema for the first step in the chain. For serialization, we return the serialization JSON schema for the last step in the chain.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ChainSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 |
|
lax_or_strict_schema ¶
lax_or_strict_schema(schema)
Generates a JSON schema that matches a schema that allows values matching either the lax schema or the strict schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
LaxOrStrictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 |
|
json_or_python_schema ¶
json_or_python_schema(schema)
Generates a JSON schema that matches a schema that allows values matching either the JSON schema or the Python schema.
The JSON schema is used instead of the Python schema. If you want to use the Python schema, you should override this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
JsonOrPythonSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 |
|
typed_dict_schema ¶
typed_dict_schema(schema)
Generates a JSON schema that matches a schema that defines a typed dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
TypedDictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 |
|
typed_dict_field_schema ¶
typed_dict_field_schema(schema)
Generates a JSON schema that matches a schema that defines a typed dict field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
TypedDictField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 |
|
dataclass_field_schema ¶
dataclass_field_schema(schema)
Generates a JSON schema that matches a schema that defines a dataclass field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DataclassField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 |
|
model_field_schema ¶
model_field_schema(schema)
Generates a JSON schema that matches a schema that defines a model field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ModelField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 |
|
computed_field_schema ¶
computed_field_schema(schema)
Generates a JSON schema that matches a schema that defines a computed field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ComputedField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 |
|
model_schema ¶
model_schema(schema)
Generates a JSON schema that matches a schema that defines a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ModelSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 |
|
resolve_schema_to_update ¶
resolve_schema_to_update(json_schema)
Resolve a JsonSchemaValue to the non-ref schema if it is a $ref schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_schema |
JsonSchemaValue
|
The schema to resolve. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The resolved schema. |
Source code in pydantic/json_schema.py
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 |
|
model_fields_schema ¶
model_fields_schema(schema)
Generates a JSON schema that matches a schema that defines a model's fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ModelFieldsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 |
|
field_is_present ¶
field_is_present(field)
Whether the field should be included in the generated JSON schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
CoreSchemaField
|
The schema for the field itself. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in pydantic/json_schema.py
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 |
|
field_is_required ¶
field_is_required(field, total)
Whether the field should be marked as required in the generated JSON schema. (Note that this is irrelevant if the field is not present in the JSON schema.).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
ModelField | DataclassField | TypedDictField
|
The schema for the field itself. |
required |
total |
bool
|
Only applies to |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in pydantic/json_schema.py
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 |
|
dataclass_args_schema ¶
dataclass_args_schema(schema)
Generates a JSON schema that matches a schema that defines a dataclass's constructor arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DataclassArgsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 |
|
dataclass_schema ¶
dataclass_schema(schema)
Generates a JSON schema that matches a schema that defines a dataclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DataclassSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 |
|
arguments_schema ¶
arguments_schema(schema)
Generates a JSON schema that matches a schema that defines a function's arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
ArgumentsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 |
|
kw_arguments_schema ¶
kw_arguments_schema(arguments, var_kwargs_schema)
Generates a JSON schema that matches a schema that defines a function's keyword arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arguments |
list[ArgumentsParameter]
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 |
|
p_arguments_schema ¶
p_arguments_schema(arguments, var_args_schema)
Generates a JSON schema that matches a schema that defines a function's positional arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arguments |
list[ArgumentsParameter]
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 |
|
get_argument_name ¶
get_argument_name(argument)
Retrieves the name of an argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
argument |
ArgumentsParameter
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
str
|
The name of the argument. |
Source code in pydantic/json_schema.py
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 |
|
call_schema ¶
call_schema(schema)
Generates a JSON schema that matches a schema that defines a function call.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CallSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 |
|
custom_error_schema ¶
custom_error_schema(schema)
Generates a JSON schema that matches a schema that defines a custom error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CustomErrorSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 |
|
json_schema ¶
json_schema(schema)
Generates a JSON schema that matches a schema that defines a JSON object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
JsonSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 |
|
url_schema ¶
url_schema(schema)
Generates a JSON schema that matches a schema that defines a URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
UrlSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 |
|
multi_host_url_schema ¶
multi_host_url_schema(schema)
Generates a JSON schema that matches a schema that defines a URL that can be used with multiple hosts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
MultiHostUrlSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 |
|
uuid_schema ¶
uuid_schema(schema)
Generates a JSON schema that matches a UUID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
UuidSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 |
|
definitions_schema ¶
definitions_schema(schema)
Generates a JSON schema that matches a schema that defines a JSON object with definitions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DefinitionsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 |
|
definition_ref_schema ¶
definition_ref_schema(schema)
Generates a JSON schema that matches a schema that references a definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
DefinitionReferenceSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 |
|
ser_schema ¶
ser_schema(schema)
Generates a JSON schema that matches a schema that defines a serialized object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
SerSchema | IncExSeqSerSchema | IncExDictSerSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue | None
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 |
|
get_title_from_name ¶
get_title_from_name(name)
Retrieves a title from a name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name to retrieve a title from. |
required |
Returns:
Type | Description |
---|---|
str
|
The title. |
Source code in pydantic/json_schema.py
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 |
|
field_title_should_be_set ¶
field_title_should_be_set(schema)
Returns true if a field with the given schema should have a title set based on the field name.
Intuitively, we want this to return true for schemas that wouldn't otherwise provide their own title (e.g., int, float, str), and false for those that would (e.g., BaseModel subclasses).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
CoreSchemaOrField
|
The schema to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in pydantic/json_schema.py
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 |
|
normalize_name ¶
normalize_name(name)
Normalizes a name to be used as a key in a dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name to normalize. |
required |
Returns:
Type | Description |
---|---|
str
|
The normalized name. |
Source code in pydantic/json_schema.py
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 |
|
get_defs_ref ¶
get_defs_ref(core_mode_ref)
Override this method to change the way that definitions keys are generated from a core reference.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
core_mode_ref |
CoreModeRef
|
The core reference. |
required |
Returns:
Type | Description |
---|---|
DefsRef
|
The definitions key. |
Source code in pydantic/json_schema.py
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 |
|
get_cache_defs_ref_schema ¶
get_cache_defs_ref_schema(core_ref)
This method wraps the get_defs_ref method with some cache-lookup/population logic, and returns both the produced defs_ref and the JSON schema that will refer to the right definition.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
core_ref |
CoreRef
|
The core reference to get the definitions reference for. |
required |
Returns:
Type | Description |
---|---|
tuple[DefsRef, JsonSchemaValue]
|
A tuple of the definitions reference and the JSON schema that will refer to it. |
Source code in pydantic/json_schema.py
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 |
|
handle_ref_overrides ¶
handle_ref_overrides(json_schema)
It is not valid for a schema with a top-level $ref to have sibling keys.
During our own schema generation, we treat sibling keys as overrides to the referenced schema, but this is not how the official JSON schema spec works.
Because of this, we first remove any sibling keys that are redundant with the referenced schema, then if any remain, we transform the schema from a top-level '$ref' to use allOf to move the $ref out of the top level. (See bottom of https://swagger.io/docs/specification/using-ref/ for a reference about this behavior)
Source code in pydantic/json_schema.py
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 |
|
encode_default ¶
encode_default(dft)
Encode a default value to a JSON-serializable value.
This is used to encode default values for fields in the generated JSON schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dft |
Any
|
The default value to encode. |
required |
Returns:
Type | Description |
---|---|
Any
|
The encoded default value. |
Source code in pydantic/json_schema.py
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 |
|
update_with_validations ¶
update_with_validations(json_schema, core_schema, mapping)
Update the json_schema with the corresponding validations specified in the core_schema, using the provided mapping to translate keys in core_schema to the appropriate keys for a JSON schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_schema |
JsonSchemaValue
|
The JSON schema to update. |
required |
core_schema |
CoreSchema
|
The core schema to get the validations from. |
required |
mapping |
dict[str, str]
|
A mapping from core_schema attribute names to the corresponding JSON schema attribute names. |
required |
Source code in pydantic/json_schema.py
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 |
|
get_json_ref_counts ¶
get_json_ref_counts(json_schema)
Get all values corresponding to the key '$ref' anywhere in the json_schema.
Source code in pydantic/json_schema.py
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 |
|
emit_warning ¶
emit_warning(kind, detail)
This method simply emits PydanticJsonSchemaWarnings based on handling in the warning_message
method.
Source code in pydantic/json_schema.py
2095 2096 2097 2098 2099 |
|
render_warning_message ¶
render_warning_message(kind, detail)
This method is responsible for ignoring warnings as desired, and for formatting the warning messages.
You can override the value of ignored_warning_kinds
in a subclass of GenerateJsonSchema
to modify what warnings are generated. If you want more control, you can override this method;
just return None in situations where you don't want warnings to be emitted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kind |
JsonSchemaWarningKind
|
The kind of warning to render. It can be one of the following:
|
required |
detail |
str
|
A string with additional details about the warning. |
required |
Returns:
Type | Description |
---|---|
str | None
|
The formatted warning message, or |
Source code in pydantic/json_schema.py
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 |
|
WithJsonSchema
dataclass
¶
Usage Documentation
Add this as an annotation on a field to override the (base) JSON schema that would be generated for that field. This provides a way to set a JSON schema for types that would otherwise raise errors when producing a JSON schema, such as Callable, or types that have an is-instance core schema, without needing to go so far as creating a custom subclass of pydantic.json_schema.GenerateJsonSchema. Note that any modifications to the schema that would normally be made (such as setting the title for model fields) will still be performed.
If mode
is set this will only apply to that schema generation mode, allowing you
to set different json schemas for validation and serialization.
Examples
dataclass
¶
Add examples to a JSON schema.
Examples should be a map of example names (strings) to example values (any valid JSON).
If mode
is set this will only apply to that schema generation mode,
allowing you to add different examples for validation and serialization.
SkipJsonSchema
dataclass
¶
Usage Documentation
Add this as an annotation on a field to skip generating a JSON schema for that field.
Example
from typing import Union
from pydantic import BaseModel
from pydantic.json_schema import SkipJsonSchema
from pprint import pprint
class Model(BaseModel):
a: Union[int, None] = None # (1)!
b: Union[int, SkipJsonSchema[None]] = None # (2)!
c: SkipJsonSchema[Union[int, None]] = None # (3)!
pprint(Model.model_json_schema())
'''
{
'properties': {
'a': {
'anyOf': [
{'type': 'integer'},
{'type': 'null'}
],
'default': None,
'title': 'A'
},
'b': {
'default': None,
'title': 'B',
'type': 'integer'
}
},
'title': 'Model',
'type': 'object'
}
'''
- The integer and null types are both included in the schema for
a
. - The integer type is the only type included in the schema for
b
. - The entirety of the
c
field is omitted from the schema.
update_json_schema ¶
update_json_schema(schema, updates)
Update a JSON schema in-place by providing a dictionary of updates.
This function sets the provided key-value pairs in the schema and returns the updated schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
JsonSchemaValue
|
The JSON schema to update. |
required |
updates |
dict[str, Any]
|
A dictionary of key-value pairs to set in the schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The updated JSON schema. |
Source code in pydantic/json_schema.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
model_json_schema ¶
model_json_schema(
cls,
by_alias=True,
ref_template=DEFAULT_REF_TEMPLATE,
schema_generator=GenerateJsonSchema,
mode="validation",
)
Utility function to generate a JSON Schema for a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
type[BaseModel] | type[PydanticDataclass]
|
The model class to generate a JSON Schema for. |
required |
by_alias |
bool
|
If |
True
|
ref_template |
str
|
The template to use for generating JSON Schema references. |
DEFAULT_REF_TEMPLATE
|
schema_generator |
type[GenerateJsonSchema]
|
The class to use for generating the JSON Schema. |
GenerateJsonSchema
|
mode |
JsonSchemaMode
|
The mode to use for generating the JSON Schema. It can be one of the following:
|
'validation'
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The generated JSON Schema. |
Source code in pydantic/json_schema.py
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 |
|
models_json_schema ¶
models_json_schema(
models,
*,
by_alias=True,
title=None,
description=None,
ref_template=DEFAULT_REF_TEMPLATE,
schema_generator=GenerateJsonSchema
)
Utility function to generate a JSON Schema for multiple models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
models |
Sequence[tuple[type[BaseModel] | type[PydanticDataclass], JsonSchemaMode]]
|
A sequence of tuples of the form (model, mode). |
required |
by_alias |
bool
|
Whether field aliases should be used as keys in the generated JSON Schema. |
True
|
title |
str | None
|
The title of the generated JSON Schema. |
None
|
description |
str | None
|
The description of the generated JSON Schema. |
None
|
ref_template |
str
|
The reference template to use for generating JSON Schema references. |
DEFAULT_REF_TEMPLATE
|
schema_generator |
type[GenerateJsonSchema]
|
The schema generator to use for generating the JSON Schema. |
GenerateJsonSchema
|
Returns:
Type | Description |
---|---|
tuple[dict[tuple[type[BaseModel] | type[PydanticDataclass], JsonSchemaMode], JsonSchemaValue], JsonSchemaValue]
|
A tuple where: - The first element is a dictionary whose keys are tuples of JSON schema key type and JSON mode, and whose values are the JSON schema corresponding to that pair of inputs. (These schemas may have JsonRef references to definitions that are defined in the second returned element.) - The second element is a JSON schema containing all definitions referenced in the first returned element, along with the optional title and description keys. |
Source code in pydantic/json_schema.py
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 |
|