{"record":{"id":"810aae5b7a687da7","repo":"crewAIInc/crewAI","slug":"an-error-occurred-while-running-the-crew-e","errorCode":null,"errorMessage":"An error occurred while running the crew: {e}","messagePattern":"An error occurred while running the crew: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"lib/cli/src/crewai_cli/templates/crew/main.py","lineNumber":24,"sourceCode":"\nfrom {{folder_name}}.crew import {{crew_name}}\n\nwarnings.filterwarnings(\"ignore\", category=SyntaxWarning, module=\"pysbd\")\n\n\ndef run():\n    \"\"\"\n    Run the crew.\n    \"\"\"\n    inputs = {\n        'topic': 'AI LLMs',\n        'current_year': str(datetime.now().year)\n    }\n\n    try:\n        {{crew_name}}().crew().kickoff(inputs=inputs)\n    except Exception as e:\n        raise Exception(f\"An error occurred while running the crew: {e}\")\n\n\ndef train():\n    \"\"\"\n    Train the crew for a given number of iterations.\n    \"\"\"\n    inputs = {\n        \"topic\": \"AI LLMs\",\n        'current_year': str(datetime.now().year)\n    }\n    try:\n        {{crew_name}}().crew().train(n_iterations=int(sys.argv[1]), filename=sys.argv[2], inputs=inputs)\n\n    except Exception as e:\n        raise Exception(f\"An error occurred while training the crew: {e}\")\n\ndef replay():\n    \"\"\"","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/crewAIInc/crewAI/blob/754d7323beb2fd042e33444a115ea2d5a47193f0/lib/cli/src/crewai_cli/templates/crew/main.py#L6-L42","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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).","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.","Verify all env vars listed in .env.example are set (OPENAI_API_KEY, SERPER_API_KEY, etc.) before kickoff.","Validate config files (config/agents.yaml, config/tasks.yaml) reference agents/tasks consistently and all tools import cleanly."],"exampleFix":"# before\ntry:\n    MyCrew().crew().kickoff(inputs=inputs)\nexcept Exception as e:\n    raise Exception(f\"An error occurred while running the crew: {e}\")\n\n# after\ntry:\n    MyCrew().crew().kickoff(inputs=inputs)\nexcept Exception:\n    import traceback\n    traceback.print_exc()\n    raise","handlingStrategy":"try-catch","validationCode":"import os\nrequired = [\"OPENAI_API_KEY\"]\nmissing = [k for k in required if not os.getenv(k)]\nif missing:\n    raise SystemExit(f\"Missing env vars: {missing}\")","typeGuard":null,"tryCatchPattern":"try:\n    result = MyCrew().crew().kickoff(inputs=inputs)\nexcept Exception as e:\n    # inspect e.__cause__ / full text — wrapper loses the original type\n    logging.exception(\"crew kickoff failed\")\n    raise","preventionTips":["Populate .env from .env.example before first run","Smoke-test kickoff in a scratch script without the template wrapper so tracebacks stay intact","Wrap scheduled runs with logging that captures the full traceback, not just str(e)"],"tags":["crewai","cli","template","kickoff","catch-all"],"backgroundTag":null,"analyzedSha":"754d7323beb2fd042e33444a115ea2d5a47193f0","analyzedAt":"2026-08-15T04:06:56.746Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}