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

: expected , got

Error message

{path}: expected {schema['type']}, got {type(value).__name__}

What it means

SchemaError from the mini JSON-Schema validator's type branch in validate(): the value at the given JSON path failed _check_type against the schema's declared type (or list of allowed types). Note that booleans never satisfy "integer" and the error names the concrete Python type actually found.

Solutions

  1. Correct the value at the reported path to the schema's declared type
  2. Watch for JSON booleans where integers are expected — they are rejected on purpose
  3. Fix the schema if the type declaration does not match intended data
  4. Validate before load/commit so bad state never reaches the store
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/14-agent-engineering/34-repo-memory-and-state/code/main.py:78 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/f303784ed0866aca. Report an issue: GitHub.

Appendix: source

Thrown at phases/14-agent-engineering/34-repo-memory-and-state/code/main.py:78

def _check_type(value: Any, types: str | list[str]) -> bool:
    type_list = [types] if isinstance(types, str) else types
    for t in type_list:
        if t == "object" and isinstance(value, dict):
            return True
        if t == "array" and isinstance(value, list):
            return True
        if t == "string" and isinstance(value, str):
            return True
        if t == "integer" and isinstance(value, int) and not isinstance(value, bool):
            return True
        if t == "null" and value is None:
            return True
    return False


def validate(value: Any, schema: dict[str, Any], path: str = "$") -> None:
    if "type" in schema and not _check_type(value, schema["type"]):
        raise SchemaError(f"{path}: expected {schema['type']}, got {type(value).__name__}")
    if "enum" in schema and value not in schema["enum"]:
        raise SchemaError(f"{path}: {value!r} not in {schema['enum']}")
    if "pattern" in schema and isinstance(value, str) and not re.match(schema["pattern"], value):
        raise SchemaError(f"{path}: {value!r} does not match /{schema['pattern']}/")
    if isinstance(value, dict):
        for key in schema.get("required", []):
            if key not in value:
                raise SchemaError(f"{path}: missing required field {key!r}")
        properties = schema.get("properties", {})
        unexpected = sorted(set(value.keys()) - set(properties.keys()))
        if unexpected:
            raise SchemaError(f"{path}: unexpected fields {unexpected}")
        for key, sub in properties.items():
            if key in value:
                validate(value[key], sub, f"{path}.{key}")
    if isinstance(value, list) and "items" in schema:
        for idx, item in enumerate(value):
            validate(item, schema["items"], f"{path}[{idx}]")

View on GitHub (pinned to 39ea8a1c6d)