json_schema
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[
core_schema.CoreSchemaType,
core_schema.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 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
)
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 or not to use field names 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. |
collisions |
dict[DefsRef, JsonSchemaValue]
|
Definitions with colliding names. When collisions are detected, we choose a non-colliding name during generation, but we also track the colliding tag so that it can be remapped for the first occurrence at the end of the process. |
defs_ref_fallbacks |
dict[DefsRef, JsonSchemaValue]
|
Core refs to fallback definitions refs. |
_schema_type_to_method |
A mapping of schema types to generator methods. |
|
_used |
Set to |
|
mode |
JsonSchemaMode
|
The schema mode. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
by_alias |
bool
|
Whether or not to include field names. |
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
259 260 261 262 263 264 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 |
|
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
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
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, core_schema.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
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 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 |
|
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
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 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 |
|
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
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 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 |
|
any_schema ¶
any_schema(schema)
Generates a JSON schema that matches any value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.AnySchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
536 537 538 539 540 541 542 543 544 545 |
|
none_schema ¶
none_schema(schema)
Generates a JSON schema that matches a None value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.NoneSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
547 548 549 550 551 552 553 554 555 556 |
|
bool_schema ¶
bool_schema(schema)
Generates a JSON schema that matches a bool value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.BoolSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
558 559 560 561 562 563 564 565 566 567 |
|
int_schema ¶
int_schema(schema)
Generates a JSON schema that matches an Int value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.IntSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
569 570 571 572 573 574 575 576 577 578 579 580 581 |
|
float_schema ¶
float_schema(schema)
Generates a JSON schema that matches a float value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.FloatSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
583 584 585 586 587 588 589 590 591 592 593 594 595 |
|
decimal_schema ¶
decimal_schema(schema)
Generates a JSON schema that matches a decimal value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DecimalSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 |
|
str_schema ¶
str_schema(schema)
Generates a JSON schema that matches a string value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.StringSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
630 631 632 633 634 635 636 637 638 639 640 641 |
|
bytes_schema ¶
bytes_schema(schema)
Generates a JSON schema that matches a bytes value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.BytesSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
643 644 645 646 647 648 649 650 651 652 653 654 |
|
date_schema ¶
date_schema(schema)
Generates a JSON schema that matches a date value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DateSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
656 657 658 659 660 661 662 663 664 665 666 667 |
|
time_schema ¶
time_schema(schema)
Generates a JSON schema that matches a time value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.TimeSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
669 670 671 672 673 674 675 676 677 678 |
|
datetime_schema ¶
datetime_schema(schema)
Generates a JSON schema that matches a datetime value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DatetimeSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
680 681 682 683 684 685 686 687 688 689 |
|
timedelta_schema ¶
timedelta_schema(schema)
Generates a JSON schema that matches a timedelta value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.TimedeltaSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
691 692 693 694 695 696 697 698 699 700 |
|
literal_schema ¶
literal_schema(schema)
Generates a JSON schema that matches a literal value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.LiteralSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 |
|
is_instance_schema ¶
is_instance_schema(schema)
Generates a JSON schema that checks if a value is an instance of a class, equivalent to Python's
isinstance
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.IsInstanceSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
734 735 736 737 738 739 740 741 742 743 744 |
|
is_subclass_schema ¶
is_subclass_schema(schema)
Generates a JSON schema that checks if a value is a subclass of a class, equivalent to Python's issubclass
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.IsSubclassSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
746 747 748 749 750 751 752 753 754 755 756 757 |
|
callable_schema ¶
callable_schema(schema)
Generates a JSON schema that matches a callable value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.CallableSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
759 760 761 762 763 764 765 766 767 768 |
|
list_schema ¶
list_schema(schema)
Returns a schema that matches a list schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.ListSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
770 771 772 773 774 775 776 777 778 779 780 781 782 |
|
tuple_positional_schema ¶
tuple_positional_schema(schema)
Generates a JSON schema that matches a positional tuple schema e.g. Tuple[int, str, bool]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.TuplePositionalSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 |
|
tuple_variable_schema ¶
tuple_variable_schema(schema)
Generates a JSON schema that matches a variable tuple schema e.g. Tuple[int, ...]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.TupleVariableSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
805 806 807 808 809 810 811 812 813 814 815 816 817 |
|
set_schema ¶
set_schema(schema)
Generates a JSON schema that matches a set schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.SetSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
819 820 821 822 823 824 825 826 827 828 |
|
frozenset_schema ¶
frozenset_schema(schema)
Generates a JSON schema that matches a frozenset schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.FrozenSetSchema
|
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 |
|
generator_schema ¶
generator_schema(schema)
Returns a JSON schema that represents the provided GeneratorSchema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.GeneratorSchema
|
The schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
847 848 849 850 851 852 853 854 855 856 857 858 859 |
|
dict_schema ¶
dict_schema(schema)
Generates a JSON schema that matches a dict schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 |
|
function_before_schema ¶
function_before_schema(schema)
Generates a JSON schema that matches a function-before schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.BeforeValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
900 901 902 903 904 905 906 907 908 909 |
|
function_after_schema ¶
function_after_schema(schema)
Generates a JSON schema that matches a function-after schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.AfterValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
911 912 913 914 915 916 917 918 919 920 |
|
function_plain_schema ¶
function_plain_schema(schema)
Generates a JSON schema that matches a function-plain schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.PlainValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
922 923 924 925 926 927 928 929 930 931 |
|
function_wrap_schema ¶
function_wrap_schema(schema)
Generates a JSON schema that matches a function-wrap schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.WrapValidatorFunctionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
933 934 935 936 937 938 939 940 941 942 |
|
default_schema ¶
default_schema(schema)
Generates a JSON schema that matches a schema with a default value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.WithDefaultSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 |
|
nullable_schema ¶
nullable_schema(schema)
Generates a JSON schema that matches a schema that allows null values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.NullableSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 |
|
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 |
core_schema.UnionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 |
|
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 |
core_schema.TaggedUnionSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 |
|
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 |
core_schema.ChainSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 |
|
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 |
core_schema.LaxOrStrictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 |
|
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 |
core_schema.JsonOrPythonSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 |
|
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 |
core_schema.TypedDictSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 |
|
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 |
core_schema.TypedDictField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 |
|
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 |
core_schema.DataclassField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 |
|
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 |
core_schema.ModelField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 |
|
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 |
core_schema.ComputedField
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 |
|
model_schema ¶
model_schema(schema)
Generates a JSON schema that matches a schema that defines a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.ModelSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 |
|
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
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 |
|
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 |
core_schema.ModelFieldsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 |
|
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
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 |
|
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 |
core_schema.ModelField | core_schema.DataclassField | core_schema.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
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 |
|
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 |
core_schema.DataclassArgsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 |
|
dataclass_schema ¶
dataclass_schema(schema)
Generates a JSON schema that matches a schema that defines a dataclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.DataclassSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 |
|
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 |
core_schema.ArgumentsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 |
|
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[core_schema.ArgumentsParameter]
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 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 |
|
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[core_schema.ArgumentsParameter]
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 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 |
|
get_argument_name ¶
get_argument_name(argument)
Retrieves the name of an argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
argument |
core_schema.ArgumentsParameter
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
str
|
The name of the argument. |
Source code in pydantic/json_schema.py
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 |
|
call_schema ¶
call_schema(schema)
Generates a JSON schema that matches a schema that defines a function call.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.CallSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 |
|
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 |
core_schema.CustomErrorSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 |
|
json_schema ¶
json_schema(schema)
Generates a JSON schema that matches a schema that defines a JSON object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.JsonSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 |
|
url_schema ¶
url_schema(schema)
Generates a JSON schema that matches a schema that defines a URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.UrlSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 |
|
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 |
core_schema.MultiHostUrlSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 |
|
uuid_schema ¶
uuid_schema(schema)
Generates a JSON schema that matches a UUID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.UuidSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 |
|
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 |
core_schema.DefinitionsSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 |
|
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 |
core_schema.DefinitionReferenceSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 |
|
ser_schema ¶
ser_schema(schema)
Generates a JSON schema that matches a schema that defines a serialized object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
core_schema.SerSchema | core_schema.IncExSeqSerSchema | core_schema.IncExDictSerSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue | None
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 |
|
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
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 |
|
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
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 |
|
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
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 |
|
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
1811 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 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 |
|
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
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 |
|
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
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 |
|
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
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 |
|
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
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 |
|
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
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 |
|
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
2043 2044 2045 2046 2047 |
|
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
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 |
|
WithJsonSchema
dataclass
¶
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
¶
Add this as an annotation on a field to skip generating a JSON schema for that field.
Example
from pydantic import BaseModel
from pydantic.json_schema import SkipJsonSchema
class Model(BaseModel):
a: int | SkipJsonSchema[None] = None
print(Model.model_json_schema())
#> {'properties': {'a': {'default': None, 'title': 'A', 'type': 'integer'}}, 'title': 'Model', 'type': 'object'}
update_json_schema ¶
update_json_schema(schema, updates)
Update a JSON schema 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
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
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 |
|
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
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 |
|