Skip to content

export_schemas

src.models.export_schemas

Export JSON Schema files from Pydantic models.

Generates one .schema.json file per model, matching the layout in the data-schemas repository. Run this after modifying any model in src/models/ to keep schemas in sync.

Usage

python -m src.models.export_schemas --output_dir ../data-schemas/schemas

export_all(output_dir: str) -> list[str]

Export all registered schemas. Returns list of written file paths.

Source code in src/models/export_schemas.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def export_all(output_dir: str) -> list[str]:
    """Export all registered schemas. Returns list of written file paths."""
    os.makedirs(output_dir, exist_ok=True)
    written = []

    for filename, is_array, module_path, class_name in SCHEMA_REGISTRY:
        cls = _import_class(module_path, class_name)
        schema = cls.model_json_schema()
        schema_id = f"{BASE_URL}/{filename}"

        if is_array:
            title = schema.get("title", class_name)
            description = schema.get("description", "")
            # Use the class docstring if model description is empty
            if not description and cls.__doc__:
                description = cls.__doc__.strip().split("\n")[0]
            final = _make_array_schema(schema, f"{title} Collection", description, schema_id)
        else:
            final = _make_object_schema(schema, schema_id)

        path = os.path.join(output_dir, filename)
        with open(path, "w", encoding="utf-8") as f:
            json.dump(final, f, indent=2, ensure_ascii=False)
            f.write("\n")

        written.append(path)
        logger.info(f"  {filename}")

    return written