File size: 9,184 Bytes
8143f06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""run a single document through the Extend (extend.ai) extraction API.

transforms a JSON Schema into Extend's accepted subset (recursively — these schemas are
nested arrays-of-objects), uploads the source file with a FRESH file_id per run (the first
/extract on a file_id caches the parse output — the parse-cache trap), creates an extractor,
runs /extract, polls, and maps the output back into the original field shape.

set EXTEND_API_KEY for your own Extend workspace.

    python scripts/run_extend.py <document_path> <schemas/doc_id.json> <output.json>
"""
import json
import os
import sys
import time
from datetime import datetime
from pathlib import Path
from typing import Any, Optional

import requests

EXTEND_API_BASE = "https://api.extend.ai"
EXTEND_API_VERSION = "2026-02-09"
EXTEND_PER_CREDIT_USD = 0.0125
EXTEND_BASE_PROCESSOR = "extraction_performance"
EXTEND_BASE_VERSION = "4.1.1"
EXTEND_PARSE_ENGINE = "parse_performance"
POLL_INTERVAL_SEC = 3
POLL_TIMEOUT_SEC = 600

PRIMITIVE_TYPES = {"string", "number", "integer", "boolean"}
RESERVED_PROPERTY_NAMES = {"id"}
RENAME_SUFFIX = "__renamed"


# --- schema transform: our JSON schema -> Extend's accepted subset (RECURSIVE) ---

def transform_node(spec: dict) -> dict:
    """recursively transform one schema node into Extend's subset:
      - strip x_* keys and $schema; primitives -> nullable union; enums include null
      - format:"date" -> extend:type:"date"; objects recurse + additionalProperties:false; arrays recurse into items
    """
    # extend's schema subset rejects standard json-schema keywords like "examples"/"default"/"title"
    drop = {"$schema", "examples", "default", "title"}
    out: dict[str, Any] = {k: v for k, v in spec.items() if not k.startswith("x_") and k not in drop}
    if out.pop("format", None) == "date":
        out["extend:type"] = "date"
    type_val = out.get("type")
    type_list = [type_val] if isinstance(type_val, str) else list(type_val or [])

    if "object" in type_list:
        out["type"] = "object"
        if "properties" in out:
            out["properties"] = {n: transform_node(p) for n, p in out["properties"].items()}
        out["additionalProperties"] = False
        return out
    if "array" in type_list:
        out["type"] = "array"
        if "items" in out:
            out["items"] = transform_node(out["items"])
        return out
    if isinstance(type_val, str) and type_val in PRIMITIVE_TYPES:
        out["type"] = [type_val, "null"]
    elif isinstance(type_val, list) and "null" not in type_val:
        out["type"] = type_val + ["null"]
    if "enum" in out and None not in out["enum"]:
        out["enum"] = list(out["enum"]) + [None]
    return out


def transform_schema(json_schema: dict):
    """transform a full JSON schema to an Extend-compatible payload.
    returns (extend_schema, field_renames) mapping extend-side name -> original (for reserved keys).
    """
    base = {k: v for k, v in json_schema.items() if not k.startswith("x_") and k != "$schema"}
    field_renames: dict[str, str] = {}
    if base.get("type") == "object" and "properties" in base:
        new_props: dict = {}
        for name, spec in base["properties"].items():
            target = f"{name}{RENAME_SUFFIX}" if name in RESERVED_PROPERTY_NAMES else name
            if target != name:
                field_renames[target] = name
            new_props[target] = transform_node(spec)
        base["properties"] = new_props
    base["additionalProperties"] = False
    return base, field_renames


# --- api ---

def headers() -> dict:
    key = os.environ.get("EXTEND_API_KEY")
    if not key:
        raise RuntimeError("EXTEND_API_KEY not set")
    return {"Authorization": f"Bearer {key}", "x-extend-api-version": EXTEND_API_VERSION}


CONTENT_TYPES = {".pdf": "application/pdf", ".jpeg": "image/jpeg", ".jpg": "image/jpeg", ".png": "image/png",
                 ".webp": "image/webp", ".tiff": "image/tiff", ".tif": "image/tiff", ".txt": "text/plain",
                 ".csv": "text/csv", ".xml": "text/xml", ".html": "text/html",
                 ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                 ".doc": "application/msword",
                 ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".xls": "application/vnd.ms-excel"}


def upload_file(file_path: Path, h: dict) -> str:
    ctype = CONTENT_TYPES.get(file_path.suffix.lower(), "application/octet-stream")
    with open(file_path, "rb") as f:
        resp = requests.post(f"{EXTEND_API_BASE}/files/upload", headers=h, files={"file": (file_path.name, f, ctype)}, timeout=180)
    if resp.status_code >= 300:
        raise RuntimeError(f"/files/upload failed {resp.status_code}: {resp.text[:400]}")
    return resp.json()["id"]


def create_extractor(name: str, schema: dict, h: dict) -> str:
    cfg = {"baseProcessor": EXTEND_BASE_PROCESSOR, "baseVersion": EXTEND_BASE_VERSION, "schema": schema, "parseConfig": {"engine": EXTEND_PARSE_ENGINE}}
    resp = requests.post(f"{EXTEND_API_BASE}/extractors", headers={**h, "Content-Type": "application/json"}, json={"name": name, "config": cfg}, timeout=60)
    if resp.status_code >= 300:
        raise RuntimeError(f"/extractors failed {resp.status_code}: {resp.text[:600]}")
    return resp.json()["id"]


def run_extract(file_id: str, extractor_id: str, h: dict) -> Optional[dict]:
    resp = requests.post(f"{EXTEND_API_BASE}/extract", headers={**h, "Content-Type": "application/json"},
                         json={"extractor": {"id": extractor_id}, "file": {"id": file_id}}, timeout=120)
    if resp.status_code >= 300:
        print(f"    POST /extract failed {resp.status_code}: {resp.text[:300]}")
        return None
    initial = resp.json()
    run_id = initial.get("id") or initial.get("extractRun", {}).get("id")
    if not run_id:
        print(f"    /extract returned no run id: {json.dumps(initial)[:300]}")
        return None
    start = time.time()
    while True:
        if time.time() - start > POLL_TIMEOUT_SEC:
            print(f"    extract run {run_id} timed out")
            return None
        time.sleep(POLL_INTERVAL_SEC)
        r = requests.get(f"{EXTEND_API_BASE}/extract_runs/{run_id}", headers=h, timeout=30)
        if r.status_code >= 300:
            print(f"    GET /extract_runs/{run_id} failed {r.status_code}")
            return None
        run = r.json().get("extractRun") or r.json()
        status = run.get("status")
        if status == "PROCESSED":
            return run
        if status == "FAILED":
            print(f"    extract run {run_id} FAILED: {run.get('failureReason')} {run.get('failureMessage')}")
            return None


def run(doc_id: str, file_path: Path, json_schema: dict) -> Optional[dict]:
    """full per-doc flow: transform schema, upload file (fresh file_id), create extractor, extract, map back.
    returns {"data", "cost", "time_sec", "meta"} or None.
    """
    h = headers()
    extend_schema, field_renames = transform_schema(json_schema)
    try:
        file_id = upload_file(file_path, h)  # fresh file_id per run (parse-cache trap)
        # unique name per run: extend rejects duplicate processor names, and a fresh extractor guarantees the CURRENT schema
        extractor_id = create_extractor(name=f"public__{doc_id}__{int(time.time() * 1000)}", schema=extend_schema, h=h)
    except Exception as e:
        print(f"    extend setup FAILED on {doc_id}: {e}")
        return None
    rec = run_extract(file_id=file_id, extractor_id=extractor_id, h=h)
    if rec is None:
        return None
    data = (rec.get("output") or {}).get("value", {}) or {}
    if field_renames and isinstance(data, dict):
        data = {field_renames.get(k, k): v for k, v in data.items()}
    usage = rec.get("usage") or {}
    credits = usage.get("totalCredits") or usage.get("credits") or 0
    cost = credits * EXTEND_PER_CREDIT_USD
    try:
        t0 = datetime.fromisoformat((rec.get("createdAt") or "").replace("Z", "+00:00"))
        t1 = datetime.fromisoformat((rec.get("updatedAt") or "").replace("Z", "+00:00"))
        tsec = max(0.0, (t1 - t0).total_seconds())
    except (ValueError, TypeError):
        tsec = 0.0
    return {"data": data, "cost": cost, "time_sec": tsec,
            "meta": {"file_id": file_id, "extractor_id": extractor_id, "run_id": rec.get("id"), "credits": credits}}


def main():
    if len(sys.argv) != 4:
        print("usage: python scripts/run_extend.py <document_path> <schemas/doc_id.json> <output.json>")
        sys.exit(1)

    file_path = Path(sys.argv[1])
    schema_path = Path(sys.argv[2])
    output_path = Path(sys.argv[3])

    json_schema = json.load(open(schema_path, encoding="utf-8"))
    doc_id = schema_path.stem
    result = run(doc_id=doc_id, file_path=file_path, json_schema=json_schema)
    if result is None:
        print("extraction failed")
        sys.exit(1)

    output_path.parent.mkdir(parents=True, exist_ok=True)
    with open(output_path, "w", encoding="utf-8") as f:
        json.dump(result, f, ensure_ascii=False, indent=2)
    print(f"wrote {output_path}")


if __name__ == "__main__":
    main()