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 use
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
¶
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 ¶
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
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 300 301 302 303 304 305 |
|
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() -> dict[
CoreSchemaOrFieldType,
Callable[[CoreSchemaOrField], JsonSchemaValue],
]
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
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
generate_definitions ¶
generate_definitions(
inputs: Sequence[
tuple[JsonSchemaKeyT, JsonSchemaMode, CoreSchema]
]
) -> tuple[
dict[
tuple[JsonSchemaKeyT, JsonSchemaMode],
JsonSchemaValue,
],
dict[DefsRef, JsonSchemaValue],
]
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
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 386 387 388 389 390 391 |
|
generate ¶
generate(
schema: CoreSchema, mode: JsonSchemaMode = "validation"
) -> JsonSchemaValue
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
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 438 439 440 441 442 443 |
|
generate_inner ¶
generate_inner(
schema: CoreSchemaOrField,
) -> JsonSchemaValue
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
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 552 553 554 555 556 557 |
|
any_schema ¶
any_schema(schema: AnySchema) -> JsonSchemaValue
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
560 561 562 563 564 565 566 567 568 569 |
|
none_schema ¶
none_schema(schema: NoneSchema) -> JsonSchemaValue
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
571 572 573 574 575 576 577 578 579 580 |
|
bool_schema ¶
bool_schema(schema: BoolSchema) -> JsonSchemaValue
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
582 583 584 585 586 587 588 589 590 591 |
|
int_schema ¶
int_schema(schema: IntSchema) -> JsonSchemaValue
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
593 594 595 596 597 598 599 600 601 602 603 604 605 |
|
float_schema ¶
float_schema(schema: FloatSchema) -> JsonSchemaValue
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
607 608 609 610 611 612 613 614 615 616 617 618 619 |
|
decimal_schema ¶
decimal_schema(schema: DecimalSchema) -> JsonSchemaValue
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
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 647 648 649 650 651 652 |
|
str_schema ¶
str_schema(schema: StringSchema) -> JsonSchemaValue
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
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
|
bytes_schema ¶
bytes_schema(schema: BytesSchema) -> JsonSchemaValue
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
670 671 672 673 674 675 676 677 678 679 680 681 |
|
date_schema ¶
date_schema(schema: DateSchema) -> JsonSchemaValue
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
683 684 685 686 687 688 689 690 691 692 693 694 |
|
time_schema ¶
time_schema(schema: TimeSchema) -> JsonSchemaValue
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
696 697 698 699 700 701 702 703 704 705 |
|
datetime_schema ¶
datetime_schema(schema: DatetimeSchema) -> JsonSchemaValue
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
707 708 709 710 711 712 713 714 715 716 |
|
timedelta_schema ¶
timedelta_schema(
schema: TimedeltaSchema,
) -> JsonSchemaValue
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
718 719 720 721 722 723 724 725 726 727 728 729 |
|
literal_schema ¶
literal_schema(schema: LiteralSchema) -> JsonSchemaValue
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
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
|
enum_schema ¶
enum_schema(schema: EnumSchema) -> JsonSchemaValue
Generates a JSON schema that matches an Enum value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
EnumSchema
|
The core schema. |
required |
Returns:
Type | Description |
---|---|
JsonSchemaValue
|
The generated JSON schema. |
Source code in pydantic/json_schema.py
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 |
|
is_instance_schema ¶
is_instance_schema(
schema: IsInstanceSchema,
) -> JsonSchemaValue
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
801 802 803 804 805 806 807 808 809 810 811 812 |
|
is_subclass_schema ¶
is_subclass_schema(
schema: IsSubclassSchema,
) -> JsonSchemaValue
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
814 815 816 817 818 819 820 821 822 823 824 825 826 |
|
callable_schema ¶
callable_schema(schema: CallableSchema) -> JsonSchemaValue
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
828 829 830 831 832 833 834 835 836 837 838 839 |
|
list_schema ¶
list_schema(schema: ListSchema) -> JsonSchemaValue
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
841 842 843 844 845 846 847 848 849 850 851 852 853 |
|
tuple_positional_schema ¶
tuple_positional_schema(
schema: TupleSchema,
) -> JsonSchemaValue
Replaced by tuple_schema
.
Source code in pydantic/json_schema.py
855 856 857 858 859 860 861 862 863 864 |
|
tuple_variable_schema ¶
tuple_variable_schema(
schema: TupleSchema,
) -> JsonSchemaValue
Replaced by tuple_schema
.
Source code in pydantic/json_schema.py
866 867 868 869 870 871 872 873 874 875 |
|
tuple_schema ¶
tuple_schema(schema: TupleSchema) -> JsonSchemaValue
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
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 |
|
set_schema ¶
set_schema(schema: SetSchema) -> JsonSchemaValue
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
912 913 914 915 916 917 918 919 920 921 |
|
frozenset_schema ¶
frozenset_schema(
schema: FrozenSetSchema,
) -> JsonSchemaValue
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
923 924 925 926 927 928 929 930 931 932 |
|
generator_schema ¶
generator_schema(
schema: GeneratorSchema,
) -> JsonSchemaValue
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
940 941 942 943 944 945 946 947 948 949 950 951 952 |
|
dict_schema ¶
dict_schema(schema: DictSchema) -> JsonSchemaValue
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
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 |
|
function_before_schema ¶
function_before_schema(
schema: BeforeValidatorFunctionSchema,
) -> JsonSchemaValue
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
993 994 995 996 997 998 999 1000 1001 1002 |
|
function_after_schema ¶
function_after_schema(
schema: AfterValidatorFunctionSchema,
) -> JsonSchemaValue
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
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 |
|
function_plain_schema ¶
function_plain_schema(
schema: PlainValidatorFunctionSchema,
) -> JsonSchemaValue
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
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 |
|
function_wrap_schema ¶
function_wrap_schema(
schema: WrapValidatorFunctionSchema,
) -> JsonSchemaValue
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
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 |
|
default_schema ¶
default_schema(
schema: WithDefaultSchema,
) -> JsonSchemaValue
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
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 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 |
|
nullable_schema ¶
nullable_schema(schema: NullableSchema) -> JsonSchemaValue
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
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 |
|
union_schema ¶
union_schema(schema: UnionSchema) -> JsonSchemaValue
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
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 |
|
tagged_union_schema ¶
tagged_union_schema(
schema: TaggedUnionSchema,
) -> JsonSchemaValue
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
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 |
|
chain_schema ¶
chain_schema(schema: ChainSchema) -> JsonSchemaValue
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
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 |
|
lax_or_strict_schema ¶
lax_or_strict_schema(
schema: LaxOrStrictSchema,
) -> JsonSchemaValue
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
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 |
|
json_or_python_schema ¶
json_or_python_schema(
schema: JsonOrPythonSchema,
) -> JsonSchemaValue
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
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 |
|
typed_dict_schema ¶
typed_dict_schema(
schema: TypedDictSchema,
) -> JsonSchemaValue
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
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 |
|
typed_dict_field_schema ¶
typed_dict_field_schema(
schema: TypedDictField,
) -> JsonSchemaValue
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
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 |
|
dataclass_field_schema ¶
dataclass_field_schema(
schema: DataclassField,
) -> JsonSchemaValue
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
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 |
|
model_field_schema ¶
model_field_schema(schema: ModelField) -> JsonSchemaValue
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
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 |
|
computed_field_schema ¶
computed_field_schema(
schema: ComputedField,
) -> JsonSchemaValue
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
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 |
|
model_schema ¶
model_schema(schema: ModelSchema) -> JsonSchemaValue
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
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 |
|
resolve_schema_to_update ¶
resolve_schema_to_update(
json_schema: JsonSchemaValue,
) -> JsonSchemaValue
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
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 |
|
model_fields_schema ¶
model_fields_schema(
schema: ModelFieldsSchema,
) -> JsonSchemaValue
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
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 |
|
field_is_present ¶
field_is_present(field: CoreSchemaField) -> bool
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
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 |
|
field_is_required ¶
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
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 |
|
dataclass_args_schema ¶
dataclass_args_schema(
schema: DataclassArgsSchema,
) -> JsonSchemaValue
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
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 |
|
dataclass_schema ¶
dataclass_schema(
schema: DataclassSchema,
) -> JsonSchemaValue
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
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 |
|
arguments_schema ¶
arguments_schema(
schema: ArgumentsSchema,
) -> JsonSchemaValue
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
1603 1604 1605 1606 1607 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 |
|
kw_arguments_schema ¶
kw_arguments_schema(
arguments: list[ArgumentsParameter],
var_kwargs_schema: CoreSchema | None,
) -> JsonSchemaValue
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
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 |
|
p_arguments_schema ¶
p_arguments_schema(
arguments: list[ArgumentsParameter],
var_args_schema: CoreSchema | None,
) -> JsonSchemaValue
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
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 |
|
get_argument_name ¶
get_argument_name(argument: ArgumentsParameter) -> str
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
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 |
|
call_schema ¶
call_schema(schema: CallSchema) -> JsonSchemaValue
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
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 |
|
custom_error_schema ¶
custom_error_schema(
schema: CustomErrorSchema,
) -> JsonSchemaValue
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
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 |
|
json_schema ¶
json_schema(schema: JsonSchema) -> JsonSchemaValue
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
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 |
|
url_schema ¶
url_schema(schema: UrlSchema) -> JsonSchemaValue
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
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 |
|
multi_host_url_schema ¶
multi_host_url_schema(
schema: MultiHostUrlSchema,
) -> JsonSchemaValue
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
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 |
|
uuid_schema ¶
uuid_schema(schema: UuidSchema) -> JsonSchemaValue
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
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 |
|
definitions_schema ¶
definitions_schema(
schema: DefinitionsSchema,
) -> JsonSchemaValue
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
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 |
|
definition_ref_schema ¶
definition_ref_schema(
schema: DefinitionReferenceSchema,
) -> JsonSchemaValue
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
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 |
|
ser_schema ¶
ser_schema(
schema: (
SerSchema | IncExSeqSerSchema | IncExDictSerSchema
),
) -> JsonSchemaValue | None
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
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 |
|
get_title_from_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
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 |
|
field_title_should_be_set ¶
field_title_should_be_set(
schema: CoreSchemaOrField,
) -> bool
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
1881 1882 1883 1884 1885 1886 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 |
|
normalize_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
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 |
|
get_defs_ref ¶
get_defs_ref(core_mode_ref: CoreModeRef) -> DefsRef
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
1927 1928 1929 1930 1931 1932 1933 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 |
|
get_cache_defs_ref_schema ¶
get_cache_defs_ref_schema(
core_ref: CoreRef,
) -> tuple[DefsRef, JsonSchemaValue]
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
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 |
|
handle_ref_overrides ¶
handle_ref_overrides(
json_schema: JsonSchemaValue,
) -> JsonSchemaValue
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
2003 2004 2005 2006 2007 2008 2009 2010 2011 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 2039 2040 2041 2042 |
|
encode_default ¶
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
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 |
|
update_with_validations ¶
update_with_validations(
json_schema: JsonSchemaValue,
core_schema: CoreSchema,
mapping: dict[str, str],
) -> None
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
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 |
|
get_json_ref_counts ¶
get_json_ref_counts(
json_schema: JsonSchemaValue,
) -> dict[JsonRef, int]
Get all values corresponding to the key '$ref' anywhere in the json_schema.
Source code in pydantic/json_schema.py
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 |
|
emit_warning ¶
emit_warning(
kind: JsonSchemaWarningKind, detail: str
) -> None
This method simply emits PydanticJsonSchemaWarnings based on handling in the warning_message
method.
Source code in pydantic/json_schema.py
2175 2176 2177 2178 2179 |
|
render_warning_message ¶
render_warning_message(
kind: JsonSchemaWarningKind, detail: str
) -> str | None
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
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 |
|
WithJsonSchema
dataclass
¶
WithJsonSchema(
json_schema: JsonSchemaValue | None,
mode: (
Literal["validation", "serialization"] | None
) = None,
)
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
¶
Examples(
examples: dict[str, Any],
mode: (
Literal["validation", "serialization"] | None
) = None,
)
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
¶
SkipJsonSchema()
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: JsonSchemaValue, updates: dict[str, Any]
) -> JsonSchemaValue
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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
model_json_schema ¶
model_json_schema(
cls: type[BaseModel] | type[PydanticDataclass],
by_alias: bool = True,
ref_template: str = DEFAULT_REF_TEMPLATE,
schema_generator: type[
GenerateJsonSchema
] = GenerateJsonSchema,
mode: JsonSchemaMode = "validation",
) -> dict[str, Any]
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
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 |
|
models_json_schema ¶
models_json_schema(
models: Sequence[
tuple[
type[BaseModel] | type[PydanticDataclass],
JsonSchemaMode,
]
],
*,
by_alias: bool = True,
title: str | None = None,
description: str | None = None,
ref_template: str = DEFAULT_REF_TEMPLATE,
schema_generator: type[
GenerateJsonSchema
] = GenerateJsonSchema
) -> tuple[
dict[
tuple[
type[BaseModel] | type[PydanticDataclass],
JsonSchemaMode,
],
JsonSchemaValue,
],
JsonSchemaValue,
]
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
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 |
|