crewAIInc/crewAI · error · Exception

An error occurred while running the crew: {e}

Error message

An error occurred while running the crew: {e}

What it means

This is a catch-all wrapper in the generated crew project template (main.py). Any exception raised during {{crew_name}}().crew().kickoff(inputs) — LLM auth failures, missing API keys, task agent errors — is re-raised as a generic Exception with the message 'An error occurred while running the crew: {e}'. The original exception is chained implicitly but its type is lost, so callers cannot catch specific failure modes.

Source

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

from {{folder_name}}.crew import {{crew_name}}

warnings.filterwarnings("ignore", category=SyntaxWarning, module="pysbd")


def run():
    """
    Run the crew.
    """
    inputs = {
        'topic': 'AI LLMs',
        'current_year': str(datetime.now().year)
    }

    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():
    """

View on GitHub (pinned to 754d7323be)

Solutions

  1. Read the text after the colon — it contains the original exception's str(); fix that underlying error (most often a missing API key in .env).
  2. Re-run with the inner traceback visible: temporarily replace the except block with `except Exception: import traceback; traceback.print_exc(); raise` or just remove the try/except in your local main.py.
  3. Verify all env vars listed in .env.example are set (OPENAI_API_KEY, SERPER_API_KEY, etc.) before kickoff.
  4. Validate config files (config/agents.yaml, config/tasks.yaml) reference agents/tasks consistently and all tools import cleanly.

Example fix

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

# after
try:
    MyCrew().crew().kickoff(inputs=inputs)
except Exception:
    import traceback
    traceback.print_exc()
    raise
Defensive patterns

Strategy: try-catch

Validate before calling

import os
required = ["OPENAI_API_KEY"]
missing = [k for k in required if not os.getenv(k)]
if missing:
    raise SystemExit(f"Missing env vars: {missing}")

Try / catch

try:
    result = MyCrew().crew().kickoff(inputs=inputs)
except Exception as e:
    # inspect e.__cause__ / full text — wrapper loses the original type
    logging.exception("crew kickoff failed")
    raise

Prevention

When it happens

Trigger: Running `crewai run` (or `uv run run`) on a scaffolded crew where kickoff() fails: missing OPENAI_API_KEY, invalid model name, misconfigured agent/backstory YAML, tool import errors in crewai_agents_config, or an LLM rate limit during the first task.

Common situations: Freshly scaffolded project where .env is not populated; YAML config referencing a non-existent agent key; model provider env var misspelled; network outage mid-kickoff. The generic message hides the root cause, so developers paste the whole wrapper message into searches instead of the inner error.

Related errors


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