JuliusBrussee/caveman · error · ValueError

initial_tool_count must be a non-negative integer

Error message

initial_tool_count must be a non-negative integer

What it means

`tools()` validates that `initial_tool_count` is a true non-negative `int`. Because `bool` is a subclass of `int` in Python, booleans are explicitly rejected too. This count controls how many non-always-load tools are included in the initial payload for the "deferred" strategy.

Solutions

  1. Pass a non-negative integer, e.g. `initial_tool_count=10`.
  2. Coerce and validate before the call: `count = int(raw); assert count >= 0 and not isinstance(raw, bool)`.
  3. If the value comes from config, parse it with `int(value)` inside try/except and fall back to a sane default.

Example fix

// before
handle = client.tools(catalog, initial_tool_count="10")
// after
handle = client.tools(catalog, initial_tool_count=int("10"))
Defensive patterns

Strategy: validation

Validate before calling

raw = cfg.get("initial_tool_count", 10)
if isinstance(raw, bool) or not isinstance(raw, int) or raw < 0:
    raise TypeError(f"initial_tool_count must be a non-negative int, got {raw!r}")
handle = client.tools(catalog, initial_tool_count=raw)

Type guard

def is_nonneg_int(v: object) -> TypeGuard[int]:
    return isinstance(v, int) and not isinstance(v, bool) and v >= 0

Try / catch

try:
    handle = client.tools(catalog, initial_tool_count=int(raw))
except (ValueError, TypeError) as e:
    handle = client.tools(catalog, initial_tool_count=10)

Prevention

When it happens

Trigger: Calling `tools(catalog, initial_tool_count=-1)`, `initial_tool_count=5.0` (float), `initial_tool_count="10"` (string), or `initial_tool_count=True`, or passing `None`/unset config where an int is required.

Common situations: Counts parsed from JSON/YAML staying as strings, arithmetic yielding floats (`len(x)/2`), or a boolean flag accidentally wired into the count parameter.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/d8c548ce31477158. Report an issue: GitHub.

Appendix: source

Thrown at packages/sdk/python/caveman_cloud/core.py:678

        """Build a tool-catalog handle with server-side search via
        ``/sdk/v1/tool-search``.

        ``strategy="all"``      → ``initial`` is the whole catalog; ``search()``
                                  still calls the server.
        ``strategy="deferred"`` → ``initial`` is the ``always_load`` tools plus
                                  the first ``initial_tool_count`` of the catalog
                                  (a subset sent on the first turn); ``search()``
                                  calls the server to load relevant tools on demand.

        Returns a handle with ``.strategy``, ``.initial`` (a ``list[CaveTool]``),
        and ``.search(query, *, max_tools, context, workflow, ranker)`` which
        hits the gateway and returns a :class:`ToolSearchResult`. Mirrors the
        TypeScript ``cave.tools({ catalog, strategy })``.
        """
        if strategy not in {"all", "deferred"}:
            raise ValueError("strategy must be all or deferred")
        if isinstance(initial_tool_count, bool) or not isinstance(initial_tool_count, int) or initial_tool_count < 0:
            raise ValueError("initial_tool_count must be a non-negative integer")
        if max_loaded_tools is not None and (isinstance(max_loaded_tools, bool) or not isinstance(max_loaded_tools, int) or max_loaded_tools < 1):
            raise ValueError("max_loaded_tools must be a positive integer")
        if strategy == "all":
            initial = list(catalog)
        else:
            always = [t for t in catalog if t.always_load]
            if max_loaded_tools is not None and len(always) > max_loaded_tools:
                raise ValueError("max_loaded_tools must be at least the always_load tool count")
            lazy = [t for t in catalog if not t.always_load]
            lazy_limit = initial_tool_count if max_loaded_tools is None else min(initial_tool_count, max_loaded_tools - len(always))
            initial = always + lazy[:lazy_limit]
        return _ToolsHandle(self, catalog, strategy, initial, max_loaded_tools)

    def tool_search(
        self,
        tools: list[CaveTool],
        query: str,
        *,

View on GitHub (pinned to 3ee70a1026)