$ cat node-template.py

D

Document Chunk

// Fetches a document chunk by its ID. Returns the chunk text and source document reference for use with Knowledge Synthesizer nodes.

Input
Data
template.py
1import json2import sys3import os4import re5import urllib.request6import urllib.error789def main():10    try:11        raw = json.loads(sys.stdin.read())12        inputs = raw.get("inputs", {})13        chunk_id = inputs.get("chunkId", "")1415        if not chunk_id or not re.match(r'^[a-zA-Z0-9_-]+$', chunk_id):16            print(json.dumps({"error": "Invalid or missing chunkId"}), file=sys.stderr)17            sys.exit(1)1819        api_base = os.environ.get("EMBLEMA_API_BASE_URL", "")20        user_token = os.environ.get("USER_TOKEN", "")2122        if not api_base:23            print(json.dumps({"error": "EMBLEMA_API_BASE_URL not configured"}), file=sys.stderr)24            sys.exit(1)25        if not user_token:26            print(json.dumps({"error": "USER_TOKEN not configured — workspace needs an access token"}), file=sys.stderr)27            sys.exit(1)2829        url = f"{api_base}/api/v2/chunks/{chunk_id}"30        req = urllib.request.Request(url, headers={31            "Authorization": f"Bearer {user_token}",32            "Content-Type": "application/json",33        })3435        with urllib.request.urlopen(req, timeout=30) as resp:36            data = json.loads(resp.read().decode())3738        relevance = inputs.get("relevance", 0)39        doc_path = data.get("documentPath") or data.get("documentName", "")4041        # Prepend metadata header to chunk text for downstream context42        header = (43            f"[Document Path: {doc_path} | "44            f"Chunk ID: {chunk_id} | "45            f"Relevance: {relevance:.2f}]"46        )47        text = f"{header}\n\n{data.get('text', '')}"4849        print(json.dumps({50            "text": text,51            "chunkId": data.get("chunkId", chunk_id),52            "driveItemId": data.get("driveItemId", ""),53        }))5455    except urllib.error.HTTPError as e:56        body = e.read().decode()[:200] if e.fp else ""57        print(json.dumps({"error": f"API error {e.code}: {body}"}), file=sys.stderr)58        sys.exit(1)59    except Exception as e:60        print(json.dumps({"error": str(e), "errorType": type(e).__name__}), file=sys.stderr)61        sys.exit(1)626364if __name__ == "__main__":65    main()

$ git log --oneline

v2.1.1
HEAD
2026-05-07
v2.1.02026-04-09
v2.0.02026-04-08