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

tool already registered; pass override=True to replace

Error message

tool {name!r} already registered; pass override=True to replace

What it means

Duplicate-name guard in ToolRegistry.register: the name already exists in the registry's records and the call did not pass override=True. Registration is deliberately non-destructive so a late registration cannot silently replace a live tool's schema or handler.

Solutions

  1. Pass override=True when intentionally replacing an existing tool definition
  2. Pick a distinct name or namespace segment for the new tool
  3. Unregister first if the registry exposes removal
  4. Audit startup code for double registration of the same tool
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/21-tool-registry-schema-validation/code/main.py:83 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/f3ed171c65bf2d31. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/21-tool-registry-schema-validation/code/main.py:83

    def __init__(self) -> None:
        self._records: dict[str, ToolRecord] = {}
        self._order: list[str] = []

    def register(
        self,
        name: str,
        schema: dict,
        handler: Callable[..., Any],
        description: str = "",
        idempotent: bool = False,
        timeout_ms: int = 30_000,
        override: bool = False,
    ) -> ToolRecord:
        if not self._NAME_RE.match(name):
            raise ValueError(f"tool name {name!r} must match {self._NAME_RE.pattern}")
        if name in self._records and not override:
            raise ValueError(f"tool {name!r} already registered; pass override=True to replace")
        validate_schema_shape(schema)
        rec = ToolRecord(
            name=name, description=description, schema=schema, handler=handler,
            idempotent=idempotent, timeout_ms=timeout_ms,
        )
        if name not in self._records:
            self._order.append(name)
        self._records[name] = rec
        return rec

    def get(self, name: str) -> ToolRecord:
        if name not in self._records:
            raise KeyError(f"unknown tool {name!r}")
        return self._records[name]

    def names(self) -> list[str]:
        return list(self._order)

View on GitHub (pinned to 39ea8a1c6d)