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

unexpected fields

Error message

unexpected fields: {', '.join(sorted(unexpected))}

What it means

Strictness enforcement: the argument object contains fields not declared under 'properties' while the schema sets additionalProperties to false; the message lists the unexpected names. The schema forbids extra fields, so the call is rejected before the handler runs.

Solutions

  1. Remove the extra fields or rename them to the declared ones
  2. Add the field to 'properties' if it is legitimate input
  3. Relax to additionalProperties true only if extra fields are truly harmless
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at certifications/claude/lessons/10-tool-use-and-agentic-loops/code/main.py:174 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/675ece0355443a0b. Report an issue: GitHub.

Appendix: source

Thrown at certifications/claude/lessons/10-tool-use-and-agentic-loops/code/main.py:174

        if maximum is not None and len(value) > maximum:
            raise ValueError(f"invalid item count for {location}: above maxItems {maximum}")
        if "items" in schema:
            for index, item in enumerate(value):
                _validate_schema_value(item, schema["items"], f"{location}[{index}]")

    if isinstance(value, dict):
        properties = schema.get("properties", {})
        if not isinstance(properties, dict):
            raise ValueError(f"schema properties for {location} must be an object")
        required = schema.get("required", [])
        if not isinstance(required, list) or not all(isinstance(name, str) for name in required):
            raise ValueError(f"schema required for {location} must be a string list")
        missing = [name for name in required if name not in value]
        if missing:
            raise ValueError(f"missing required fields: {', '.join(missing)}")
        unexpected = set(value) - set(properties)
        if unexpected and schema.get("additionalProperties") is False:
            raise ValueError(f"unexpected fields: {', '.join(sorted(unexpected))}")
        for name, item in value.items():
            if name in properties:
                _validate_schema_value(item, properties[name], f"{location}.{name}")


@dataclass(frozen=True)
class Tool:
    name: str
    description: str
    input_schema: dict[str, Any]
    handler: Callable[[dict[str, Any]], Any]
    mutates: bool = False

    def validate(self, arguments: dict[str, Any]) -> None:
        if not isinstance(arguments, dict):
            raise ValueError("tool input must be an object")
        _validate_schema_value(arguments, self.input_schema, "tool input")

View on GitHub (pinned to 39ea8a1c6d)