{"record":{"id":"d8c548ce31477158","repo":"JuliusBrussee/caveman","slug":"initial-tool-count-must-be-a-non-negative-integer","errorCode":null,"errorMessage":"initial_tool_count must be a non-negative integer","messagePattern":"initial_tool_count must be a non-negative integer","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/sdk/python/caveman_cloud/core.py","lineNumber":678,"sourceCode":"        \"\"\"Build a tool-catalog handle with server-side search via\n        ``/sdk/v1/tool-search``.\n\n        ``strategy=\"all\"``      → ``initial`` is the whole catalog; ``search()``\n                                  still calls the server.\n        ``strategy=\"deferred\"`` → ``initial`` is the ``always_load`` tools plus\n                                  the first ``initial_tool_count`` of the catalog\n                                  (a subset sent on the first turn); ``search()``\n                                  calls the server to load relevant tools on demand.\n\n        Returns a handle with ``.strategy``, ``.initial`` (a ``list[CaveTool]``),\n        and ``.search(query, *, max_tools, context, workflow, ranker)`` which\n        hits the gateway and returns a :class:`ToolSearchResult`. Mirrors the\n        TypeScript ``cave.tools({ catalog, strategy })``.\n        \"\"\"\n        if strategy not in {\"all\", \"deferred\"}:\n            raise ValueError(\"strategy must be all or deferred\")\n        if isinstance(initial_tool_count, bool) or not isinstance(initial_tool_count, int) or initial_tool_count < 0:\n            raise ValueError(\"initial_tool_count must be a non-negative integer\")\n        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):\n            raise ValueError(\"max_loaded_tools must be a positive integer\")\n        if strategy == \"all\":\n            initial = list(catalog)\n        else:\n            always = [t for t in catalog if t.always_load]\n            if max_loaded_tools is not None and len(always) > max_loaded_tools:\n                raise ValueError(\"max_loaded_tools must be at least the always_load tool count\")\n            lazy = [t for t in catalog if not t.always_load]\n            lazy_limit = initial_tool_count if max_loaded_tools is None else min(initial_tool_count, max_loaded_tools - len(always))\n            initial = always + lazy[:lazy_limit]\n        return _ToolsHandle(self, catalog, strategy, initial, max_loaded_tools)\n\n    def tool_search(\n        self,\n        tools: list[CaveTool],\n        query: str,\n        *,","sourceCodeStart":660,"sourceCodeEnd":696,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/sdk/python/caveman_cloud/core.py#L660-L696","documentation":"`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.","triggerScenarios":"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.","commonSituations":"Counts parsed from JSON/YAML staying as strings, arithmetic yielding floats (`len(x)/2`), or a boolean flag accidentally wired into the count parameter.","solutions":["Pass a non-negative integer, e.g. `initial_tool_count=10`.","Coerce and validate before the call: `count = int(raw); assert count >= 0 and not isinstance(raw, bool)`.","If the value comes from config, parse it with `int(value)` inside try/except and fall back to a sane default."],"exampleFix":"// before\nhandle = client.tools(catalog, initial_tool_count=\"10\")\n// after\nhandle = client.tools(catalog, initial_tool_count=int(\"10\"))","handlingStrategy":"validation","validationCode":"raw = cfg.get(\"initial_tool_count\", 10)\nif isinstance(raw, bool) or not isinstance(raw, int) or raw < 0:\n    raise TypeError(f\"initial_tool_count must be a non-negative int, got {raw!r}\")\nhandle = client.tools(catalog, initial_tool_count=raw)","typeGuard":"def is_nonneg_int(v: object) -> TypeGuard[int]:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 0","tryCatchPattern":"try:\n    handle = client.tools(catalog, initial_tool_count=int(raw))\nexcept (ValueError, TypeError) as e:\n    handle = client.tools(catalog, initial_tool_count=10)","preventionTips":["Parse numeric config with int() immediately at load time, not at call time.","Avoid float arithmetic for counts; use integer division or ceil deliberately.","Remember bool is an int in Python — reject bools explicitly in your validators."],"tags":["python","validation","type-error","tools"],"backgroundTag":"invalid-argument-value","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}