Repository navigation
fix(cli): exit with the script's status when crewai train fails - #9572
Conversation
Strix Security Review✅ No open security findings. PR summary: Makes `crewai train` propagate a non-zero exit status: `train_crew` now raises `SystemExit` with the training script's own return code when `uv run train` fails, and `SystemExit(1)` when the subprocess cannot be started, matching how `crewai run` and `crewai install` behave. Two existing tests were updated to expect `SystemExit`, and new tests cover the exit codes through the `train` click command. Review summaryThe change is two lines: Ruled out (1)
Review 1 on this PR · Reviewed by Strix |
Vidit Ostwal (Vidit-Ostwal)
left a comment
There was a problem hiding this comment.
Thanks, this is a clean fix. Re-raising SystemExit(e.returncode) / SystemExit(1) in lib/cli/src/crewai_cli/train_crew.py matches how crewai install and crewai run behave, and the messages are unchanged.
I checked the new tests on both sides: with the PR's test file on the base branch 6 tests fail (including the CLI-level exit-42 test); on this branch all pass, and the success path still exits 0. Test coverage through the train click command is a nice touch.
Thanks for your first contribution to CrewAI, anilkr09!We really appreciate the time and care you put into this PR. Your work is now part of CrewAI — welcome to the contributor community. Want to share your contribution? Totally optional: Share on X · Share the PR on LinkedIn If the links don't work, feel free to copy and personalize this: If you share, we'd love to see it — tag crewAI (@crewAIInc) on X and CrewAI on LinkedIn. |
Related issue
Fixes #9571
Summary
crewai train -n Nrunsuv run train …; when the project's training script fails,train_crewprints the error and returns, so the command exits 0 and a CI job sees a success. Theexceptbranches only echo (lib/cli/src/crewai_cli/train_crew.py:29-34,mainat5f3fe6ff08).SystemExitafter the existing messages: with the script's own exit code for a failed script, and with 1 whenuvcannot be started, ascrewai run(lib/cli/src/crewai_cli/run_crew.py:1375-1383) andcrewai install(lib/cli/src/crewai_cli/cli.py:717-720, andlib/cli/src/crewai_cli/cli.py:722for the1) do.train_crewand expected a normal return now expectSystemExit.crewai …in a throwaway project)main5f3fe6ff08train -n 1, script exits 1 (A)train -n 1, script exits 42 (F)train -n 1,uvcannot be started (I)train -n 1, script SIGKILLs itself (K)train -n 1in an empty folder, realuv(N)train -n 1, script exits 0 (H, control)train -n 0, invalid argument (M, control)run, script exits 42 (G, control)Not changed:
crewai replayandcrewai testhave the same pattern and still exit 0 here (rows B, C); issues #9456 and #9457 track them. Scripts that relied oncrewai trainexiting 0 after a failed script will now see a non-zero code, as withcrewai run.Verification
Tests added or updated for the changed behavior
Relevant tests and quality checks pass locally
lib/cli/tests/test_train_crew.py: 4 of its 9 tests fail withmain's source (3 withDID NOT RAISE SystemExit; thetraincommand test withassert 0 == 42), all 9 pass here in file order on macOS, Python 3.13.15 and 3.10.21, and with the default random order and xdist on Python 3.13.15. No network or LLM.Neighbouring suites:
lib/cli/tests/test_cli.py,test_crew_test.py,test_install_crew.py,test_run_crew.py: 198 passed.ruff check,ruff format --checkandmypyonlib/cli/src/crewai_cli/train_crew.py: clean.Not run locally: the full suite, Linux and Windows; CI runs Python 3.10–3.13.
Additional context
None.
Written with an AI coding agent (Claude Code). CONTRIBUTING asks for the
llm-generatedlabel on AI-generated PRs; I can't add labels, so could a maintainer add it?