JuliusBrussee/caveman · error · ValueError

max_tools must be a positive integer

Error message

max_tools must be a positive integer

What it means

`tool_search()` validates its optional `max_tools` limit client-side: it must be a positive integer (>= 1) or `None`. Booleans are rejected despite being int subclasses. This value bounds how many tools the gateway-side search returns in the `ToolSearchResult`.

Solutions

  1. Pass a positive integer, e.g. `max_tools=5`, or omit it to use the gateway default.
  2. Normalize CLI/config input: `limit = max(int(raw), 1)` when a limit is requested.
  3. Treat "unlimited" as `None`, not 0.

Example fix

// before
result = client.tool_search(query, max_tools="0")
// after
result = client.tool_search(query, max_tools=None)  # or a positive int like 5
Defensive patterns

Strategy: validation

Validate before calling

if limit is not None:
    if isinstance(limit, bool) or not isinstance(limit, int) or limit < 1:
        limit = max(int(limit), 1)
result = client.tool_search(query, max_tools=limit)

Type guard

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

Try / catch

try:
    result = client.tool_search(query, max_tools=limit)
except ValueError as e:
    result = client.tool_search(query, max_tools=None)

Prevention

When it happens

Trigger: Calling `client.tool_search("find billing tools", max_tools=0)`, `max_tools=-1`, `max_tools=True`, or a float/string value passed straight from CLI args or JSON config.

Common situations: A `--max-tools 0` CLI flag meaning "unlimited" (here it means invalid), or unparsed string arguments from `sys.argv`.

Related errors


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

Appendix: source

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

        Args:
            tools: Full tool catalog to filter.
            query: The user's current intent / task description.
            context: Optional additional context for the search.
            max_tools: Optional cap on returned tools (server default applies if omitted).
            workflow: Override workflow label (defaults to Cave.default_workflow).
            ranker: Optional ranking algorithm passed through to the gateway
                ("bm25" default, or "embeddings" when the gateway has an embedding
                provider wired). The SDK passes it through; it never computes
                similarity itself.
            session_id: Optional server-side tool session id for later
                provider-request reinjection.

        Returns:
            ToolSearchResult with .tools, .sent_schema_tokens, .full_schema_tokens,
            .deferred_count, .method, .saved_tokens, .reduction_pct.
        """
        if max_tools is not None and (isinstance(max_tools, bool) or not isinstance(max_tools, int) or max_tools < 1):
            raise ValueError("max_tools must be a positive integer")
        body: dict[str, Any] = {
            "tools": [
                {
                    "name": t.name,
                    "description": t.description,
                    "input_schema": t.input_schema,
                    "read_only": t.read_only,
                    "idempotent": t.idempotent,
                    "always_load": t.always_load,
                }
                for t in tools
            ],
            "query": query,
        }
        if context is not None:
            body["context"] = context
        if max_tools is not None:
            body["max_tools"] = max_tools

View on GitHub (pinned to 3ee70a1026)