crewAIInc/crewAI · error · Exception

An error occurred while training the crew: {e}

Error message

An error occurred while training the crew: {e}

What it means

Wrapper in the crew template's train() function. It catches every exception from {{crew_name}}().crew().train(n_iterations, filename, inputs) and re-raises it as a generic Exception prefixed with 'An error occurred while training the crew'. Training runs the crew n_iterations times and stores pickled memories; failures can come from kickoff, memory pickling to the filename, or invalid arguments.

Source

Thrown at lib/cli/src/crewai_cli/templates/crew/main.py:39

    try:
        {{crew_name}}().crew().kickoff(inputs=inputs)
    except Exception as e:
        raise Exception(f"An error occurred while running the crew: {e}")


def train():
    """
    Train the crew for a given number of iterations.
    """
    inputs = {
        "topic": "AI LLMs",
        'current_year': str(datetime.now().year)
    }
    try:
        {{crew_name}}().crew().train(n_iterations=int(sys.argv[1]), filename=sys.argv[2], inputs=inputs)

    except Exception as e:
        raise Exception(f"An error occurred while training the crew: {e}")

def replay():
    """
    Replay the crew execution from a specific task.
    """
    try:
        {{crew_name}}().crew().replay(task_id=sys.argv[1])

    except Exception as e:
        raise Exception(f"An error occurred while replaying the crew: {e}")

def test():
    """
    Test the crew execution and returns the results.
    """
    inputs = {
        "topic": "AI LLMs",
        "current_year": str(datetime.now().year)

View on GitHub (pinned to 754d7323be)

Solutions

  1. Check the suffix after the colon for the real exception; it is usually IndexError (missing filename argv) or ValueError (non-int iterations) from int(sys.argv[1]).
  2. Invoke training with both positional args: `uv run train 5 memory.pkl`.
  3. Ensure the crew runs cleanly first (`uv run run`) — training reuses kickoff, so any run failure will surface here too.
  4. Confirm the output filename path is writable and ends with .pkl.

Example fix

# before
try:
    MyCrew().crew().train(n_iterations=int(sys.argv[1]), filename=sys.argv[2], inputs=inputs)
except Exception as e:
    raise Exception(f"An error occurred while training the crew: {e}")

# after
if len(sys.argv) < 3:
    raise SystemExit("Usage: train <n_iterations> <filename.pkl>")
MyCrew().crew().train(n_iterations=int(sys.argv[1]), filename=sys.argv[2], inputs=inputs)
Defensive patterns

Strategy: validation

Validate before calling

import sys
if len(sys.argv) < 3:
    raise SystemExit("Usage: train <n_iterations:int> <filename.pkl>")
n = int(sys.argv[1])
assert n > 0, "n_iterations must be positive"
assert sys.argv[2].endswith(".pkl")

Type guard

def is_valid_train_args(argv: list[str]) -> bool:
    return (
        len(argv) >= 3
        and argv[1].isdigit()
        and int(argv[1]) > 0
        and argv[2].endswith(".pkl")
    )

Try / catch

try:
    MyCrew().crew().train(n_iterations=n, filename=fn, inputs=inputs)
except Exception:
    logging.exception("training failed")
    raise

Prevention

When it happens

Trigger: Running `uv run train <n> <file>` where the underlying crew kickoff fails (LLM/API errors), the filename is not writable, n_iterations is not a valid int (int(sys.argv[1]) raises ValueError caught here), or sys.argv[2] is missing (IndexError caught here).

Common situations: Developer runs `uv run train 5` without the filename argument; passes a non-numeric iteration count; trains in an environment where the LLM key is unset; writes the pickle to a read-only directory.

Related errors


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