crewAIInc/crewAI · error · BedrockValidationError

number_of_results must be greater than 0

Error message

number_of_results must be greater than 0

What it means

Raised when number_of_results is an int but is less than 1 (e.g. 0 or negative). Bedrock's retrieve API requires a positive result count; the tool enforces this client-side with BedrockValidationError. Note there is no upper-bound check here, so values above the AWS limit surface later as a ClientError instead.

Source

Thrown at lib/crewai-tools/src/crewai_tools/aws/bedrock/knowledge_base/retriever_tool.py:119

                raise BedrockValidationError(
                    "knowledge_base_id must contain only alphanumeric characters"
                )

            if self.next_token:
                if not isinstance(self.next_token, str):
                    raise BedrockValidationError("next_token must be a string")
                if len(self.next_token) < 1 or len(self.next_token) > 2048:
                    raise BedrockValidationError(
                        "next_token must be between 1 and 2048 characters"
                    )
                if " " in self.next_token:
                    raise BedrockValidationError("next_token cannot contain spaces")

            if self.number_of_results is not None:
                if not isinstance(self.number_of_results, int):
                    raise BedrockValidationError("number_of_results must be an integer")
                if self.number_of_results < 1:
                    raise BedrockValidationError(
                        "number_of_results must be greater than 0"
                    )

        except BedrockValidationError as e:
            raise BedrockValidationError(f"Parameter validation failed: {e!s}") from e

    def _process_retrieval_result(self, result: dict[str, Any]) -> dict[str, Any]:
        """Process a single retrieval result from Bedrock Knowledge Base.

        Args:
            result (Dict[str, Any]): Raw result from Bedrock Knowledge Base

        Returns:
            Dict[str, Any]: Processed result with standardized format
        """
        content_obj = result.get("content", {})
        content = content_obj.get("text", "")
        content_type = content_obj.get("type", "text")

View on GitHub (pinned to 754d7323be)

Solutions

  1. Use None instead of 0 to mean 'not set'
  2. Clamp to a sane positive default: max(1, number_of_results)
  3. Fix the arithmetic that can produce 0/negative values

Example fix

# before
tool = BedrockKnowledgeBaseRetrievalTool(knowledge_base_id=kb_id, number_of_results=max(0, user_limit))
# after
tool = BedrockKnowledgeBaseRetrievalTool(knowledge_base_id=kb_id, number_of_results=max(1, user_limit))
Defensive patterns

Strategy: validation

Validate before calling

if number_of_results is not None:
    number_of_results = max(1, int(number_of_results))

Type guard

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

Try / catch

except BedrockValidationError as e:
    if 'greater than 0' in str(e):
        tool.number_of_results = 5  # sane default
        out = tool._run(q)
    else:
        raise

Prevention

When it happens

Trigger: Passing number_of_results=0 (often as an 'unset' sentinel), a negative value from config arithmetic, or a decrement bug producing 0.

Common situations: Using 0 as a default/unset value in config; computing the value as max(0, n) or len(results) - 1 which can go to 0; math rounding down small fractions to 0.

Related errors


AI-assisted analysis of crewAIInc/crewAI@754d7323be (2026-08-15). Data as JSON: /api/errors/6596f7f9344e4275. Report an issue: GitHub.