CLI crewai train throws error 'The filename must not end with .pkl' even with .py file

Hey folks, how’s it going? I’m trying to use the CLI crewai train to train my medical agent, but I keep hitting a weird error I can’t figure out. The command I’m running is: crewai train -f medical_agent_train.py -n 3. But every time, I get this: Training the Crew for 3 iterations - An unexpected error occurred: The filename must not end with .pkl. The file medical_agent_train.py is a valid Python script, not a .pkl, so I don’t get why the CLI is rejecting it. I’ve tried a bunch of variations: crewai train -f medical_agent_train -n 3, crewai train -f “C:\Users\jaian\Desktop\cla\medical_agent_train.py” -n 3, renamed it to train_medico.py and ran crewai train -f train_medico -n 3, even tried python -m crewai.cli train -f C:\Users\jaian\Desktop\cla\medical_agent_train.py -n 3. Still, same error every time. The --help shows -f and -n are valid options: Usage: crewai train [OPTIONS] Options: -n, --n_iterations INTEGER Number of iterations to train the crew -f, --filename TEXT Path to a custom file for training --help Show this message and exit. Looks like a bug in the CLI to me, since the file is .py, not .pkl. The docs (Introduction - CrewAI) say crewai train -n <n_iterations> should work with a Python script, but it’s not happening. Anyone run into this or know how to fix it?

Try:

crewai train -n 3 -f train_medico.pkl

This work on my machine with v0.108.0

Training the Crew for 3 iterations
error: Failed to spawn: train
Caused by: program not found
An error occurred while training the crew: Command ‘[‘uv’, ‘run’, ‘train’, ‘3’, ‘train_medico.pkl’]’ returned non-zero exit status 2.

What version of CrewAI are you using and can you paste the contents of your pyproject.toml file?

Are you in a CrewAI CLI created crew?

0.108.0

import os
from dotenv import load_dotenv
from crewai import Agent, Task, Crew, Process

Load the .env file

load_dotenv()
print(“OpenAI API Key loaded:”, os.getenv(“OPENAI_API_KEY”))

Define the medical agent

medical_agent = Agent(
role=“Medical Specialist”,
goal=“Assist patients with medical queries and provide accurate guidance”,
backstory=“You are an experienced doctor with years of practice, specialized in diagnosis and treatment. Use a professional, empathetic, and clear tone.”,
verbose=True,
llm=“gpt-4o”,
memory=False,
allow_delegation=False
)

Define the task for training

task = Task(
description=“Respond to the patient’s query: ‘I have mild chest pain’. Provide an accurate and helpful medical response.”,
agent=medical_agent,
expected_output=“A clear and professional response to the patient”
)

Class to manage the Crew

class MedicalCrew:
def init(self):
self.agents = [medical_agent]
self.tasks = [task]

@crew
def crew(self) -> Crew:
    """Creates the Medical Crew for training"""
    crew_instance = Crew(
        agents=self.agents,
        tasks=self.tasks,
        process=Process.sequential,
        verbose=True,
        memory=False  # No Mem0, just behavior training
    )
    # Try to load trained data, if it exists
    output = self.agents[0]._use_trained_data(".")
    print("Trained data:", output)
    return crew_instance

Function to interact in the terminal after training

def interact_with_agent(crew):
print(“Welcome! I’m your trained medical specialist. How can I assist you today?”)
while True:
user_input = input(“You: “)
if user_input.lower() in [“exit”, “quit”, “bye”]:
print(“Goodbye! Take care!”)
break
task.description = f"Respond to the patient’s query: ‘{user_input}’. Provide an accurate and helpful medical response.”
response = crew.kickoff()
print(f"Doctor: {response}”)

Execute

if name == “main”:
# Create the Crew
medical_crew = MedicalCrew()
crew_instance = medical_crew.crew()
# Run the interaction
interact_with_agent(crew_instance)

Did you create the project via crewai create crew your_crew?

Yes