MCPcopy Index your code
hub / github.com/msgspec/msgspec / schema

Function schema

src/msgspec/_json_schema.py:15–61  ·  view source on GitHub ↗

Generate a JSON Schema for a given type. Any schemas for (potentially) shared components are extracted and stored in a top-level ``"$defs"`` field. If you want to generate schemas for multiple types, or to have more control over the generated schema you may want to use ``schema_com

(
    type: Any,
    *,
    schema_hook: Optional[Callable[[type], dict[str, Any]]] = None,
    ref_template: str = _REF_TEMPLATE,
)

Source from the content-addressed store, hash-verified

13
14
15def schema(
16 type: Any,
17 *,
18 schema_hook: Optional[Callable[[type], dict[str, Any]]] = None,
19 ref_template: str = _REF_TEMPLATE,
20) -> dict[str, Any]:
21 """Generate a JSON Schema for a given type.
22
23 Any schemas for (potentially) shared components are extracted and stored in
24 a top-level ``"$defs"`` field.
25
26 If you want to generate schemas for multiple types, or to have more control
27 over the generated schema you may want to use ``schema_components`` instead.
28
29 Parameters
30 ----------
31 type : type
32 The type to generate the schema for.
33 schema_hook : callable, optional
34 An optional callback to use for generating JSON schemas of custom
35 types. Will be called with the custom type, and should return a dict
36 representation of the JSON schema for that type.
37 ref_template : str, optional
38 A template to use when generating ``"$ref"`` fields. This template is
39 formatted with the type name as ``template.format(name=name)``. This
40 can be useful if you intend to store the ``components`` mapping
41 somewhere other than a top-level ``"$defs"`` field. For example, you
42 might use ``ref_template="#/components/{name}"`` if generating an
43 OpenAPI schema.
44
45 Returns
46 -------
47 schema : dict
48 The generated JSON Schema.
49
50 See Also
51 --------
52 schema_components
53 """
54 (out,), components = schema_components(
55 (type,),
56 schema_hook=schema_hook,
57 ref_template=ref_template,
58 )
59 if components:
60 out["$defs"] = components
61 return out
62
63
64def schema_components(

Callers

nothing calls this directly

Calls 1

schema_componentsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…