crewAIInc/crewAI · error · ValueError

The number of iterations must be a positive integer.

Error message

The number of iterations must be a positive integer.

What it means

Raised by evaluate_crew (the `crewai test` flow) when n_iterations is zero or negative. It is a pre-flight argument check before the `uv run test <n> <model>` subprocess is launched; the ValueError is raised inside the try block, and since the except clauses handle CalledProcessError and generic Exception separately, the click echo path depends on the generic handler — the run never starts. It exists to stop wasted evaluation runs with nonsensical iteration counts.

Source

Thrown at lib/cli/src/crewai_cli/evaluate_crew.py:27

def evaluate_crew(
    n_iterations: int, model: str, trained_agents_file: str | None = None
) -> None:
    """Test and Evaluate the crew by running a command in the UV environment.

    Args:
        n_iterations: The number of iterations to test the crew.
        model: The model to test the crew with.
        trained_agents_file: Optional trained-agents pickle path forwarded to
            the subprocess via the ``CREWAI_TRAINED_AGENTS_FILE`` env var.
    """
    command = ["uv", "run", "test", str(n_iterations), model]
    env = build_env_with_all_tool_credentials()
    if trained_agents_file:
        env[CREWAI_TRAINED_AGENTS_FILE_ENV] = trained_agents_file

    try:
        if n_iterations <= 0:
            raise ValueError("The number of iterations must be a positive integer.")

        result = subprocess.run(  # noqa: S603
            command, capture_output=False, text=True, check=True, env=env
        )

        if result.stderr:
            click.echo(result.stderr, err=True)

    except subprocess.CalledProcessError as e:
        click.echo(f"An error occurred while testing the crew: {e}", err=True)
        click.echo(e.output, err=True)

    except Exception as e:
        click.echo(f"An unexpected error occurred: {e}", err=True)

View on GitHub (pinned to 754d7323be)

Solutions

  1. Pass a positive integer: `crewai test -n 3 gpt-4o-mini`
  2. If the count comes from a script/env var, default it to a sane positive value (e.g. 2 or 3) when unset
  3. Validate the argument in your wrapper before invoking the CLI or evaluate_crew

Example fix

# before
crewai test -n 0 gpt-4o-mini
# after
crewai test -n 3 gpt-4o-mini
Defensive patterns

Strategy: validation

Validate before calling

def valid_iterations(n: int) -> bool:
    return isinstance(n, int) and not isinstance(n, bool) and n > 0

Type guard

def is_positive_int(value: object) -> bool:
    return isinstance(value, int) and not isinstance(value, bool) and value > 0

Try / catch

if n_iterations <= 0:
    raise SystemExit("--n_iterations must be a positive integer")
evaluate_crew(n_iterations=n_iterations, model=model)

Prevention

When it happens

Trigger: Calling `crewai test -n 0` (or a negative --n_iterations value), or calling evaluate_crew(n_iterations=0, model=...) programmatically. The check is `n_iterations <= 0`, so both 0 and negatives fail.

Common situations: CI scripts parameterizing iteration count where a variable defaults to 0, shell expansion of an unset variable producing 0, or users misunderstanding -n as verbosity rather than iteration count.

Related errors


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