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

packet root must be an object

Error message

packet root must be an object

What it means

Loader guard for the prompt-packet file: the parsed JSON root is not an object, so downstream accesses like packet.get('contract') could not work. The file most likely holds an array or scalar at the top level.

Solutions

  1. Wrap the packet content in a top-level JSON object with contract, stages, and evaluationCases
  2. Verify the file path — an exported list is the usual culprit
  3. Fail at load time with the actual root type included in the message
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at certifications/claude/lessons/03-prompting-and-task-decomposition/code/main.py:72 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/a74bb67ffc23ee99. Report an issue: GitHub.

Appendix: source

Thrown at certifications/claude/lessons/03-prompting-and-task-decomposition/code/main.py:72

            errors.append("every evaluation case needs an expected behavior")
    return errors


def score_packet(packet: dict[str, Any]) -> dict[str, Any]:
    errors = validate_packet(packet)
    return {
        "passed": not errors,
        "errors": errors,
        "contractFields": len(set(packet.get("contract", {})) & CONTRACT_FIELDS) if isinstance(packet.get("contract"), dict) else 0,
        "caseCoverage": sorted({case.get("type") for case in packet.get("evaluationCases", []) if isinstance(case, dict)}),
        "stageCount": len(packet.get("stages", [])) if isinstance(packet.get("stages"), list) else 0,
    }


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


if __name__ == "__main__":
    path = Path(__file__).parents[1] / "outputs" / "prompt-contract-packet.json"
    print(json.dumps(score_packet(load_packet(path)), indent=2))

View on GitHub (pinned to 39ea8a1c6d)