rohitg00/ai-engineering-from-scratch · error · ValueError
mode-trials root must be an object
Error message
mode-trials root must be an object
What it means
Loader guard for the mode-trials file: the JSON parsed successfully but the root value is not an object (an array or scalar), so nested access to trials and decision would fail. It fires right after read, before schema checks.
Solutions
- Use a top-level object wrapping the trials list and decision
- Confirm the path points at the mode-trials JSON, not a raw results array
- Load and shape-check all input files once in setUp so failures name the offending file
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at certifications/claude/lessons/02-model-selection-and-token-economics/code/main.py:320 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/40f3be6f9ca74737.
Report an issue: GitHub.
Appendix: source
Thrown at certifications/claude/lessons/02-model-selection-and-token-economics/code/main.py:320
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(
{
"valid": not errors,
"summary": summarize(benchmark),
"modeTrials": summarize_mode_trials(mode_trials),
},
indent=2,
)
)View on GitHub (pinned to 39ea8a1c6d)