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

benchmark root must be an object

Error message

benchmark root must be an object

What it means

Loader guard for the benchmark file: json.loads succeeded, but the parsed document is not a JSON object (likely a top-level array or a bare string/number), so field lookups like get('cases') would be meaningless. It fires immediately after reading the file, before any validation.

Solutions

  1. Make the benchmark file a top-level JSON object with the expected keys
  2. Check the loader points at benchmark.json and not an exported array or log file
  3. Include the parsed root type in the error to make the mismatch obvious
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at certifications/claude/lessons/02-model-selection-and-token-economics/code/main.py:313 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/e86ba091678dda93. Report an issue: GitHub.

Appendix: source

Thrown at certifications/claude/lessons/02-model-selection-and-token-economics/code/main.py:313

def _nonempty_strings(value: Any) -> bool:
    return isinstance(value, list) and bool(value) and all(isinstance(item, str) and item.strip() for item in value)


def _iso_date(value: Any) -> bool:
    if not isinstance(value, str):
        return False
    try:
        date.fromisoformat(value)
        return True
    except ValueError:
        return False


def load_benchmark(path: Path) -> dict[str, Any]:
    value = json.loads(path.read_text(encoding="utf-8"))
    if not isinstance(value, dict):
        raise ValueError("benchmark root must be an object")
    return value


def load_mode_trials(path: Path) -> dict[str, Any]:
    value = json.loads(path.read_text(encoding="utf-8"))
    if not isinstance(value, dict):
        raise ValueError("mode-trials root must be an object")
    return value


if __name__ == "__main__":
    outputs = Path(__file__).parents[1] / "outputs"
    benchmark = load_benchmark(outputs / "model-routing-benchmark.json")
    mode_trials = load_mode_trials(outputs / "mode-trials.json")
    errors = validate_benchmark(benchmark) + validate_mode_trials(mode_trials)
    print(
        json.dumps(
            {

View on GitHub (pinned to 39ea8a1c6d)