MCPcopy
hub / github.com/pydantic/pydantic / generate

Method generate

pydantic/json_schema.py:401–448  ·  view source on GitHub ↗

Generates a JSON schema for a specified schema in a specified mode. Args: schema: A Pydantic model. mode: The mode in which to generate the schema. Defaults to 'validation'. Returns: A JSON schema representing the specified schema. Raise

(self, schema: CoreSchema, mode: JsonSchemaMode = 'validation')

Source from the content-addressed store, hash-verified

399 return json_schemas_map, self.sort(json_schema['$defs']) # type: ignore
400
401 def generate(self, schema: CoreSchema, mode: JsonSchemaMode = 'validation') -> JsonSchemaValue:
402 """Generates a JSON schema for a specified schema in a specified mode.
403
404 Args:
405 schema: A Pydantic model.
406 mode: The mode in which to generate the schema. Defaults to 'validation'.
407
408 Returns:
409 A JSON schema representing the specified schema.
410
411 Raises:
412 PydanticUserError: If the JSON schema generator has already been used to generate a JSON schema.
413 """
414 self._mode = mode
415 if self._used:
416 raise PydanticUserError(
417 'This JSON schema generator has already been used to generate a JSON schema. '
418 f'You must create a new instance of {type(self).__name__} to generate a new JSON schema.',
419 code='json-schema-already-used',
420 )
421
422 json_schema: JsonSchemaValue = self.generate_inner(schema)
423 json_ref_counts = self.get_json_ref_counts(json_schema)
424
425 ref = cast(JsonRef, json_schema.get('$ref'))
426 while ref is not None: # may need to unpack multiple levels
427 ref_json_schema = self.get_schema_from_definitions(ref)
428 if json_ref_counts[ref] == 1 and ref_json_schema is not None and len(json_schema) == 1:
429 # "Unpack" the ref since this is the only reference and there are no sibling keys
430 json_schema = ref_json_schema.copy() # copy to prevent recursive dict reference
431 json_ref_counts[ref] -= 1
432 ref = cast(JsonRef, json_schema.get('$ref'))
433 ref = None
434
435 self._garbage_collect_definitions(json_schema)
436 definitions_remapping = self._build_definitions_remapping()
437
438 if self.definitions:
439 json_schema['$defs'] = self.definitions
440
441 json_schema = definitions_remapping.remap_json_schema(json_schema)
442
443 # For now, we will not set the $schema key. However, if desired, this can be easily added by overriding
444 # this method and adding the following line after a call to super().generate(schema):
445 # json_schema['$schema'] = self.schema_dialect
446
447 self._used = True
448 return self.sort(json_schema)
449
450 def generate_inner(self, schema: CoreSchemaOrField) -> JsonSchemaValue: # noqa: C901
451 """Generates a JSON schema for a given core schema.

Callers 3

model_json_schemaFunction · 0.45
json_schemaMethod · 0.45

Calls 10

generate_innerMethod · 0.95
get_json_ref_countsMethod · 0.95
sortMethod · 0.95
PydanticUserErrorClass · 0.85
remap_json_schemaMethod · 0.80
getMethod · 0.45
copyMethod · 0.45

Tested by 1