Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

fix(cli): correct train filename error message (#7903)

Sathyajith MCe836a191cfe5
diff --git a/lib/cli/src/crewai_cli/train_crew.py b/lib/cli/src/crewai_cli/train_crew.py
index bab568ea3..66d39fc6e 100644
--- a/lib/cli/src/crewai_cli/train_crew.py
+++ b/lib/cli/src/crewai_cli/train_crew.py
@@ -10,15 +10,17 @@ def train_crew(n_iterations: int, filename: str) -> None:
Args:
n_iterations (int): The number of iterations to train the crew.
"""
- command = ["uv", "run", "train", str(n_iterations), filename]
+ if n_iterations <= 0:
+ raise click.ClickException(
+ "The number of iterations must be a positive integer."
+ )
- try:
- if n_iterations <= 0:
- raise ValueError("The number of iterations must be a positive integer.")
+ if not filename.endswith(".pkl"):
+ raise click.ClickException("The filename must end with .pkl")
- if not filename.endswith(".pkl"):
- raise ValueError("The filename must not end with .pkl")
+ command = ["uv", "run", "train", str(n_iterations), filename]
+ try:
result = subprocess.run(command, capture_output=False, text=True, check=True) # noqa: S603
if result.stderr:
diff --git a/lib/cli/tests/test_train_crew.py b/lib/cli/tests/test_train_crew.py
index 47263032e..2dd984875 100644
--- a/lib/cli/tests/test_train_crew.py
+++ b/lib/cli/tests/test_train_crew.py
@@ -1,6 +1,9 @@
import subprocess
from unittest import mock
+import click
+import pytest
+
from crewai_cli.train_crew import train_crew
@@ -24,22 +27,33 @@ def test_train_crew_positive_iterations(mock_subprocess_run):
)
[email protected]("crewai_cli.train_crew.click")
-def test_train_crew_zero_iterations(click):
- train_crew(0, "trained_agents_data.pkl")
- click.echo.assert_called_once_with(
- "An unexpected error occurred: The number of iterations must be a positive integer.",
- err=True,
- )
[email protected]("crewai_cli.train_crew.subprocess.run")
+def test_train_crew_zero_iterations(mock_subprocess_run):
+ with pytest.raises(
+ click.ClickException,
+ match="The number of iterations must be a positive integer.",
+ ):
+ train_crew(0, "trained_agents_data.pkl")
+ mock_subprocess_run.assert_not_called()
[email protected]("crewai_cli.train_crew.click")
-def test_train_crew_negative_iterations(click):
- train_crew(-2, "trained_agents_data.pkl")
- click.echo.assert_called_once_with(
- "An unexpected error occurred: The number of iterations must be a positive integer.",
- err=True,
- )
[email protected]("crewai_cli.train_crew.subprocess.run")
+def test_train_crew_negative_iterations(mock_subprocess_run):
+ with pytest.raises(
+ click.ClickException,
+ match="The number of iterations must be a positive integer.",
+ ):
+ train_crew(-2, "trained_agents_data.pkl")
+ mock_subprocess_run.assert_not_called()
+
+
[email protected]("crewai_cli.train_crew.subprocess.run")
+def test_train_crew_invalid_filename(mock_subprocess_run):
+ with pytest.raises(
+ click.ClickException, match=r"The filename must end with \.pkl"
+ ):
+ train_crew(5, "myfile.txt")
+ mock_subprocess_run.assert_not_called()
@mock.patch("crewai_cli.train_crew.click")