rohitg00/ai-engineering-from-scratch · error · ValueError

bad json at

Error message

bad json at {path}:{line_num}: {exc}

What it means

Task-file parser guard: a non-blank, non-comment line in the JSONL task file failed json.loads, so the task dataset is malformed at the reported line and cannot be deserialised into an Example.

Solutions

  1. Fix the JSON syntax at the reported line (the underlying json error is included)
  2. Remove stray commas/quotes or trailing garbage on that line
  3. Validate task files with a JSONL linter before running the harness
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/49-lm-eval-harness/code/main.py:265 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/b5611bfcfdb680de. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/49-lm-eval-harness/code/main.py:265

    "exact_match": lambda p, t, e: metric_exact_match(p, t),
    "substring_contains": lambda p, t, e: metric_substring_contains(p, t),
    "multiple_choice": lambda p, t, e: metric_multiple_choice(p, t),
    "rouge_l": lambda p, t, e: metric_rouge_l(p, t),
    "code_exec": lambda p, t, e: metric_code_exec(p, t, e),
}


def load_task_jsonl(path: Path) -> List[Example]:
    examples: List[Example] = []
    with path.open("r", encoding="utf-8") as f:
        for line_num, raw in enumerate(f, start=1):
            raw = raw.strip()
            if not raw or raw.startswith("#"):
                continue
            try:
                obj = json.loads(raw)
            except json.JSONDecodeError as exc:
                raise ValueError(f"bad json at {path}:{line_num}: {exc}") from exc
            examples.append(Example(
                id=str(obj.get("id", f"ex-{line_num}")),
                prompt=obj["prompt"],
                targets=list(obj["targets"]),
                metric=obj["metric"],
                extras=dict(obj.get("extras", {})),
            ))
    return examples


def write_task_jsonl(examples: Iterable[Example], path: Path) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w", encoding="utf-8") as f:
        for ex in examples:
            f.write(json.dumps({
                "id": ex.id,
                "prompt": ex.prompt,
                "targets": ex.targets,

View on GitHub (pinned to 39ea8a1c6d)