#!/usr/bin/env python3
"""Export your OpenAI Evals before the scheduled 30 Nov 2026 shutdown.

Saves every eval in the project your key belongs to, every run, every run's output items
and the JSONL data files the runs used, into ./openai-evals-export/. Python 3.8+, standard
library only. It only sends GET requests (read-only). Your key goes only to api.openai.com.

  export OPENAI_API_KEY=...      # a key for the project whose evals you want
  python3 export-openai-evals.py

Run it once per OpenAI project. Source: https://litmiq.com/migrate-from-openai-evals (MIT license).
"""
import json, os, pathlib, sys, urllib.error, urllib.parse, urllib.request

API = os.environ.get("OPENAI_BASE_URL", "https://api.openai.com/v1").rstrip("/")
KEY = os.environ.get("OPENAI_API_KEY") or sys.exit("Set OPENAI_API_KEY first.")
OUT = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else "openai-evals-export")


def get(path, params=None, raw=False):
    url = API + path + ("?" + urllib.parse.urlencode(params) if params else "")
    req = urllib.request.Request(url, headers={"Authorization": "Bearer " + KEY})
    with urllib.request.urlopen(req, timeout=120) as r:
        body = r.read()
    return body if raw else json.loads(body)


def pages(path):
    """Yield every object from a paginated list endpoint (has_more / last_id)."""
    params = {}
    while True:
        page = get(path, params)
        yield from page.get("data", [])
        if not page.get("has_more") or not page.get("last_id"):
            return
        params["after"] = page["last_id"]


def save(p, obj):
    p.parent.mkdir(parents=True, exist_ok=True)
    p.write_text(json.dumps(obj, indent=2, ensure_ascii=False), encoding="utf-8")


n_evals = n_runs = n_items = n_files = 0
for ev in pages("/evals"):
    n_evals += 1
    d = OUT / ev["id"]
    save(d / "eval.json", ev)  # name, data_source_config (item schema) and testing_criteria (graders)
    for run in pages(f"/evals/{ev['id']}/runs"):
        n_runs += 1
        rd = d / "runs" / run["id"]
        save(rd / "run.json", run)  # model, prompt template (input_messages), data source, result counts
        rd.mkdir(parents=True, exist_ok=True)
        with open(rd / "output_items.jsonl", "w", encoding="utf-8") as f:
            for item in pages(f"/evals/{ev['id']}/runs/{run['id']}/output_items"):
                n_items += 1
                f.write(json.dumps(item, ensure_ascii=False) + "\n")
        src = (run.get("data_source") or {}).get("source") or {}
        if src.get("type") == "file_id":
            fp = d / "files" / (src["id"] + ".jsonl")
            if not fp.exists():
                try:
                    fp.parent.mkdir(parents=True, exist_ok=True)
                    fp.write_bytes(get(f"/files/{src['id']}/content", raw=True))
                    n_files += 1
                except urllib.error.HTTPError as e:
                    print(f"  could not download file {src['id']} (HTTP {e.code}); its rows are still in output_items.jsonl")
        elif src.get("type") == "file_content":
            with open(rd / "file_content.jsonl", "w", encoding="utf-8") as f:
                for row in src.get("content", []):
                    f.write(json.dumps(row, ensure_ascii=False) + "\n")
    print(f"exported {ev.get('name') or ev['id']}")

print(f"Done: {n_evals} evals, {n_runs} runs, {n_items} output items, {n_files} data files in {OUT.resolve()}")
