#!/usr/bin/env python3 """Generate JSON Schema files from specs.ir Pydantic models. Writes one JSON file per model name into the output directory. Useful for external adapters to consume a stable IR schema artifact. """ import json import argparse from pathlib import Path import specs.ir as ir def main(out_dir: Path): out_dir.mkdir(parents=True, exist_ok=True) # ir.__all__ lists model names exported by the module for name in getattr(ir, "__all__", []): cls = getattr(ir, name, None) if cls is None: continue # pydantic v1/v2 compat: prefer model_json_schema if available schema = None if hasattr(cls, "model_json_schema"): try: schema = cls.model_json_schema() except TypeError: schema = cls.schema() else: schema = cls.schema() out_path = out_dir / f"{name}.json" with out_path.open("w", encoding="utf8") as f: json.dump(schema, f, indent=2, sort_keys=True) print(f"Wrote {out_path}") if __name__ == "__main__": p = argparse.ArgumentParser() p.add_argument("--out", dest="out", default="specs/schemas", help="Output directory") args = p.parse_args() main(Path(args.out))