crewai - ✅(Solved) Fix Agents always load trained_agents_data.pkl, ignoring custom filename; custom training file not respected on inference [9 pull requests, 1 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
crewAIInc/crewAI#4905Fetched 2026-04-08 00:52:22
View on GitHub
Comments
1
Participants
1
Timeline
23
Reactions
0
Author
Participants
Timeline (top)
referenced ×11cross-referenced ×9assigned ×1commented ×1

When training a CrewAI crew with a custom file name (-f <custom_file>.pkl), the consolidated agent guidance is correctly written to the given path (e.g., my_custom_trained.pkl). However, during normal (non-training) execution, agents always load from the hardcoded trained_agents_data.pkl, ignoring the custom file supplied at training time.


Relevant code:

    if data := CrewTrainingHandler(TRAINED_AGENTS_DATA_FILE).load():  # ❌ HARDCODED!

From agent/core.py

Error Message

  • At minimum, clearly warn users and suggest renaming the file post-training.

Root Cause

When training a CrewAI crew with a custom file name (-f <custom_file>.pkl), the consolidated agent guidance is correctly written to the given path (e.g., my_custom_trained.pkl). However, during normal (non-training) execution, agents always load from the hardcoded trained_agents_data.pkl, ignoring the custom file supplied at training time.


Relevant code:

    if data := CrewTrainingHandler(TRAINED_AGENTS_DATA_FILE).load():  # ❌ HARDCODED!

From agent/core.py

Fix Action

Fix / Workaround

Trained models saved to custom path are not respected in inference; always loads trained_agents_data.pkl by default. Only workaround is to rename the file manually.

PR fix notes

PR #4906: fix: respect custom trained_agents_data_file during inference (#4905)

Description (problem / solution / changelog)

Summary

Fixes #4905. Agents always loaded trained suggestions from the hardcoded trained_agents_data.pkl during inference, ignoring any custom filename supplied at training time via crewai train -f <custom>.pkl.

Changes:

  • Added trained_agents_data_file field to Crew (defaults to "trained_agents_data.pkl") so users can specify which file to load during inference
  • Updated Agent._use_trained_data() to accept an optional filename parameter
  • Updated apply_training_data() to propagate the crew's trained_agents_data_file to the agent

Usage:

crew = Crew(
    agents=[...],
    tasks=[...],
    trained_agents_data_file="my_custom_trained.pkl",
)
crew.kickoff()  # agents now load suggestions from my_custom_trained.pkl

Review & Testing Checklist for Human

  • Training/inference gap: Crew.train(filename=...) saves to the custom file but does NOT auto-set self.trained_agents_data_file. Users must explicitly set both. Verify this partial approach is acceptable vs. having train() automatically propagate the filename.
  • Crew.copy() preservation: The new field is a regular Pydantic Field, so model_dump() should include it. Confirm copies created during train() preserve the custom filename.
  • Recommended manual test: Train a crew with crewai train -n 2 -f custom.pkl, then create a Crew(trained_agents_data_file="custom.pkl") and run kickoff() — verify suggestions from the custom file are applied to agent prompts.

Notes

  • The or fallback in _use_trained_data means passing an empty string also falls back to the default; this matches the existing convention.
  • When agent.crew is None (standalone agent usage), the default trained_agents_data.pkl is used, preserving backward compatibility.

Link to Devin session: https://app.devin.ai/sessions/4575684ad75643a2b583fcd290cc4258

<!-- CURSOR_SUMMARY -->

[!NOTE] Medium Risk Changes how inference-time prompts are augmented with trained suggestions by threading a configurable filename through Crewapply_training_data()Agent._use_trained_data(), which could subtly alter agent outputs if misconfigured.

Overview Fixes inference-time loading of trained suggestions to respect a crew-configured trained-data filename instead of always using the default trained_agents_data.pkl.

Adds Crew.trained_agents_data_file (defaulting to TRAINED_AGENTS_DATA_FILE) and updates apply_training_data()/Agent._use_trained_data() to propagate and use this value, with new unit tests covering custom vs default behavior and the no-crew fallback.

<sup>Reviewed by Cursor Bugbot for commit d79c4a62a1cb7e1fab1c78f3d8bcba51bbc78d2b. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

<!-- /CURSOR_SUMMARY -->

Changed files

  • lib/crewai/src/crewai/agent/core.py (modified, +13/-3)
  • lib/crewai/src/crewai/agent/utils.py (modified, +7/-1)
  • lib/crewai/src/crewai/crew.py (modified, +13/-1)
  • lib/crewai/tests/agents/test_agent.py (modified, +53/-0)
  • lib/crewai/tests/test_crew.py (modified, +69/-0)

PR #4914: fix(agent): respect custom trained_agents_file during inference

Description (problem / solution / changelog)

Problem

When training a Crew with a custom filename (e.g., crewai train -n 5 -f my_custom_trained.pkl), the trained data is correctly saved to the specified file. However, during inference (normal execution), agents always load from the hardcoded trained_agents_data.pkl, ignoring the custom filename.

Solution

This PR adds a trained_agents_file parameter to the Crew class that allows users to specify which trained agents data file to load during inference.

Changes

  1. Crew class: Added trained_agents_file field (defaults to trained_agents_data.pkl for backward compatibility)
  2. Agent._use_trained_data: Modified to use the crew's configured trained_agents_file when available, falling back to the default when no crew is associated

Usage

# Use custom trained data file during inference
crew = Crew(
    agents=[...],
    tasks=[...],
    trained_agents_file="my_custom_trained.pkl"  # Load from custom file
)
crew.kickoff()

Backward Compatibility

  • Default behavior unchanged: if no trained_agents_file is specified, uses trained_agents_data.pkl
  • Agents without a crew still use the default file

Fixes #4905

<!-- CURSOR_SUMMARY -->

[!NOTE] Low Risk Low risk: small, backward-compatible change to which trained-data file is loaded during prompt construction, plus targeted unit tests.

Overview Fixes trained-agent inference to respect a crew-level trained data filename. Crew now exposes trained_agents_file (defaulting to TRAINED_AGENTS_DATA_FILE), and Agent._use_trained_data loads suggestions from that configured path when the agent is attached to a crew, falling back to the default otherwise.

Adds unit coverage to verify custom-file usage, default fallback behavior, and Crew field defaults/overrides.

<sup>Written by Cursor Bugbot for commit b60ef5f1744d4da83aa8144a74d0bca4ca8f07e3. This will update automatically on new commits. Configure here.</sup>

<!-- /CURSOR_SUMMARY -->

Changed files

  • lib/crewai/src/crewai/agent/core.py (modified, +5/-1)
  • lib/crewai/src/crewai/crew.py (modified, +5/-1)
  • lib/crewai/tests/test_trained_agents_file.py (added, +82/-0)

PR #4916: fix(agent): add trained_agents_data_file field to respect custom training output

Description (problem / solution / changelog)

Problem

When training with crewai train -f my_custom_trained.pkl, the trained data is correctly written to the custom file. However, during inference Agent._use_trained_data() always loads from the hardcoded constant TRAINED_AGENTS_DATA_FILE ("trained_agents_data.pkl"), completely ignoring the custom filename used at training time.

# Before — hardcoded, ignores custom training file
if data := CrewTrainingHandler(TRAINED_AGENTS_DATA_FILE).load():

The only workaround was to manually rename the custom file to trained_agents_data.pkl before each run.

Solution

Add a trained_agents_data_file: str | None = None field to BaseAgent. In Agent._use_trained_data(), use self.trained_agents_data_file or TRAINED_AGENTS_DATA_FILE so the custom filename is respected when set.

agent = Agent(
    role="Researcher",
    goal="...",
    backstory="...",
    trained_agents_data_file="my_custom_trained.pkl",  # ← new field
)
  • If trained_agents_data_file is not set (default None), behaviour is unchanged — falls back to trained_agents_data.pkl.
  • If set, agents load trained guidance from the specified file.

Testing

  • Syntax verified with Python AST parser for both modified files
  • Backward compatible: existing agents without the field work exactly as before

Fixes #4905

<!-- CURSOR_SUMMARY -->

[!NOTE] Low Risk Low risk: small, backward-compatible config addition and a minor tool-call parsing fix; potential impact is limited to which .pkl file is read and how Bedrock-style tool inputs are interpreted.

Overview Fixes trained-guidance loading to respect custom training output filenames by adding trained_agents_data_file to BaseAgent and using it in Agent._use_trained_data() (fallback remains TRAINED_AGENTS_DATA_FILE).

Also corrects native tool-call parsing in CrewAgentExecutor._parse_native_tool_call() so Bedrock-style input is used when OpenAI-style function.arguments is explicitly absent, avoiding the previous or short-circuit bug.

<sup>Written by Cursor Bugbot for commit 937da507a2f60cbb7b7610640d11cbe8dc01020c. This will update automatically on new commits. Configure here.</sup>

<!-- /CURSOR_SUMMARY -->

Changed files

  • lib/crewai/src/crewai/agent/core.py (modified, +5/-1)
  • lib/crewai/src/crewai/agents/agent_builder/base_agent.py (modified, +9/-0)
  • lib/crewai/src/crewai/flow/visualization/renderers/interactive.py (modified, +15/-10)

PR #4920: fix: respect custom training filename during agent inference

Description (problem / solution / changelog)

Problem

When training a crew with a custom filename via crew.train(filename="custom.pkl"), agents were always loading from the hardcoded 'trained_agents_data.pkl' instead of the custom filename during inference.

Solution

  • Add _train_filename attribute to Crew class to store the training filename
  • Store the custom filename during _setup_for_training()
  • Modify Agent._use_trained_data() to use the crew's stored filename when available, falling back to the default constant otherwise

Changes

  • lib/crewai/src/crewai/crew.py: Added _train_filename attribute and set it during training setup
  • lib/crewai/src/crewai/agent/core.py: Updated _use_trained_data() to check for custom filename from crew
  • lib/crewai/tests/agents/test_agent.py: Added test for custom filename functionality

Testing

  • Added new test test_agent_use_trained_data_with_custom_filename that verifies the fix
  • All existing training tests pass

Fixes #4905

<!-- CURSOR_SUMMARY -->

[!NOTE] Low Risk Small behavioral change in prompt augmentation: inference now reads trained suggestions from a crew-provided file path when set, which could affect output prompts but is well-scoped and covered by a unit test.

Overview Fixes trained-data loading during agent inference to respect a crew-specific training output file. Crew now stores the training filename in a new private _train_filename set during _setup_for_training(), and Agent._use_trained_data() uses that value when present (falling back to TRAINED_AGENTS_DATA_FILE).

Adds a unit test ensuring CrewTrainingHandler is called with the custom filename when the agent is associated with a crew.

<sup>Written by Cursor Bugbot for commit 789e75e4c4c2cff9fc98bc6e9add0d752bd5ffc8. This will update automatically on new commits. Configure here.</sup>

<!-- /CURSOR_SUMMARY -->

Changed files

  • lib/crewai/src/crewai/agent/core.py (modified, +7/-1)
  • lib/crewai/src/crewai/crew.py (modified, +2/-0)
  • lib/crewai/tests/agents/test_agent.py (modified, +43/-0)

PR #4926: fix: respect custom trained agents file during inference

Description (problem / solution / changelog)

Problem

When training a crew with a custom filename (crew.train(n_iterations=5, filename='my_custom_trained.pkl')), the trained suggestions are correctly saved to the custom file. However, during inference, Agent._use_trained_data() always loads from the hardcoded TRAINED_AGENTS_DATA_FILE (trained_agents_data.pkl), completely ignoring the custom file.

This means users who train with a custom filename will never see their trained suggestions applied during normal execution.

Closes #4905

Solution

  1. Added a trained_agents_file optional parameter to the Crew class
  2. Updated Agent._use_trained_data() to check self.crew.trained_agents_file first, falling back to the default TRAINED_AGENTS_DATA_FILE
  3. Added a test to verify the custom file path is used when configured

Usage

# Train with custom file
crew.train(n_iterations=5, filename='my_custom_trained.pkl')

# Use the same file during inference
crew = Crew(
    agents=[...],
    tasks=[...],
    trained_agents_file='my_custom_trained.pkl',
)
crew.kickoff()

Changes

  • lib/crewai/src/crewai/crew.py: Added trained_agents_file field
  • lib/crewai/src/crewai/agent/core.py: Updated _use_trained_data() to use crew's custom file
  • lib/crewai/tests/agents/test_agent.py: Added test for custom file path
<!-- CURSOR_SUMMARY -->

[!NOTE] Low Risk Low risk: small, backward-compatible change to which .pkl file is read for trained suggestions during inference, plus a test covering the new configuration path.

Overview Fixes inference to respect a crew-provided trained agents data file.

Crew gains an optional trained_agents_file field, and Agent._use_trained_data() now loads suggestions from this path when set, falling back to the default TRAINED_AGENTS_DATA_FILE. Adds a unit test ensuring the custom filename is used.

<sup>Written by Cursor Bugbot for commit c1fb5b7505ee8ac750d3008d471004a6e91804b0. This will update automatically on new commits. Configure here.</sup>

<!-- /CURSOR_SUMMARY -->

Changed files

  • lib/crewai/src/crewai/agent/core.py (modified, +14/-2)
  • lib/crewai/src/crewai/crew.py (modified, +9/-0)
  • lib/crewai/tests/agents/test_agent.py (modified, +42/-0)

PR #4946: fix: respect custom trained_file path in Agent._use_trained_data (fixes #4905)

Description (problem / solution / changelog)

Summary

Fixes #4905

When training a crew with a custom filename (crewai train -f my_custom.pkl), the trained data was never applied during inference because _use_trained_data always loaded from the hardcoded TRAINED_AGENTS_DATA_FILE constant (trained_agents_data.pkl).

Root cause

# agent/core.py — before this fix
def _use_trained_data(self, task_prompt: str) -> str:
    if data := CrewTrainingHandler(TRAINED_AGENTS_DATA_FILE).load():  # ❌ always hardcoded
        ...

Solution

Introduce a new optional field trained_file on the Agent model. When set, it is used as the source file for trained agent suggestions; otherwise it falls back to TRAINED_AGENTS_DATA_FILE (unchanged default behaviour).

# agent/core.py — after this fix
def _use_trained_data(self, task_prompt: str) -> str:
    trained_data_file = self.trained_file or TRAINED_AGENTS_DATA_FILE  # ✅
    if data := CrewTrainingHandler(trained_data_file).load():
        ...

Usage

agent = Agent(
    role='researcher',
    goal='...',
    backstory='...',
    trained_file='my_custom_trained.pkl',  # matches -f arg used during training
)

Changes

FileChange
src/crewai/agent/core.pyAdd trained_file: str | None = None field; update _use_trained_data to use it; update docstrings
tests/agents/test_agent.pyAdd test_agent_use_trained_data_custom_file unit test

Testing

The existing test_agent_use_trained_data test still passes (default path unchanged).
The new test_agent_use_trained_data_custom_file test verifies that CrewTrainingHandler is called with the custom filename instead of the default.

<!-- CURSOR_SUMMARY -->

[!NOTE] Low Risk Low risk: small, backward-compatible change that only affects how _use_trained_data selects the training data file, covered by a new unit test.

Overview Fixes trained-data inference to respect a new optional Agent.trained_file path, falling back to TRAINED_AGENTS_DATA_FILE when unset.

Updates _use_trained_data to load suggestions from the selected file and adds a unit test asserting CrewTrainingHandler is invoked with the custom filename.

<sup>Written by Cursor Bugbot for commit bb74adbca09f199ee3b6df6b915900b670848f2e. This will update automatically on new commits. Configure here.</sup>

<!-- /CURSOR_SUMMARY -->

Changed files

  • lib/crewai/src/crewai/agent/core.py (modified, +21/-2)
  • lib/crewai/tests/agents/test_agent.py (modified, +31/-0)

PR #4948: fix(agent): respect custom training data file path instead of hardcoded default

Description (problem / solution / changelog)

Fixes #4905

Problem

Agents always load from the hardcoded constant TRAINED_AGENTS_DATA_FILE (trained_agents_data.pkl), completely ignoring any custom file path passed with -f during crewai train.

Steps to reproduce:

crewai train -n 3 -f my_custom_training.pkl
# Training saves to my_custom_training.pkl ✓
# But at runtime, agents load from trained_agents_data.pkl ✗

Root Cause

Agent._use_trained_data() in agent/core.py hardcodes the file path:

# Before (broken)
if data := CrewTrainingHandler(TRAINED_AGENTS_DATA_FILE).load():

Fix

  1. Add trained_agents_data_file field to Agent with default value of TRAINED_AGENTS_DATA_FILE (fully backward compatible)
  2. Use self.trained_agents_data_file in _use_trained_data() instead of the module-level constant
  3. Add test verifying custom file path is used correctly
# After (fixed)
if data := CrewTrainingHandler(self.trained_agents_data_file).load():

Users can now specify the custom file when creating agents:

agent = Agent(
    role="researcher",
    goal="...",
    backstory="...",
    trained_agents_data_file="my_custom_training.pkl",  # matches -f flag
)

Changes

  • src/crewai/agent/core.py: add trained_agents_data_file field, fix _use_trained_data()
  • tests/agents/test_agent.py: add test_agent_use_trained_data_custom_file test

Changed files

  • lib/crewai/src/crewai/agent/core.py (modified, +9/-1)
  • lib/crewai/tests/agents/test_agent.py (modified, +31/-0)

PR #4968: fix: Respect custom trained data file during inference

Description (problem / solution / changelog)

Summary

When training a crew with a custom filename via train -f <custom_file>.pkl, the crew now correctly loads from that same file during inference, instead of always loading from the hardcoded trained_agents_data.pkl.

Problem

The _use_trained_data() method in Agent always loaded from TRAINED_AGENTS_DATA_FILE constant, ignoring any custom filename provided during training.

Solution

  1. Added _trained_data_file attribute to Crew class to store the custom filename
  2. Store the filename during _setup_for_training()
  3. Pass the stored filename to _use_trained_data() via the utility function
  4. Fall back to default trained_agents_data.pkl when no custom file is specified

Changes

  • src/crewai/crew.py: Add _trained_data_file attribute and store filename during setup
  • src/crewai/agent/core.py: Add trained_data_file parameter to _use_trained_data()
  • src/crewai/agent/utils.py: Retrieve and pass trained_data_file from crew

Backwards Compatibility

Fully backwards compatible. Existing code using default filename continues to work unchanged.

Fixes #4905

Changed files

  • lib/crewai/src/crewai/agent/core.py (modified, +3/-2)
  • lib/crewai/src/crewai/agent/utils.py (modified, +2/-1)
  • lib/crewai/src/crewai/crew.py (modified, +2/-0)

PR #5004: fix(agent): honour custom trained_agents_data_file during inference

Description (problem / solution / changelog)

Summary

Fixes #4905

Problem

Agent._use_trained_data() always loads from the constant TRAINED_AGENTS_DATA_FILE (trained_agents_data.pkl), ignoring any custom filename used with crewai train -f <file>.

Fix

Adds trained_agents_data_file: str | None to BaseAgent. When set, the agent loads trained suggestions from that path:

trained_file = self.trained_agents_data_file or TRAINED_AGENTS_DATA_FILE

Default behaviour (loads trained_agents_data.pkl) is unchanged.

# $ crewai train -n 5 -f my_crew_trained.pkl
agent = Agent(..., trained_agents_data_file="my_crew_trained.pkl")

Changed files

  • lib/crewai/src/crewai/agent/core.py (modified, +5/-1)
  • lib/crewai/src/crewai/agents/agent_builder/base_agent.py (modified, +9/-0)
  • lib/crewai/src/crewai/flow/visualization/renderers/interactive.py (modified, +15/-10)

Code Example

if data := CrewTrainingHandler(TRAINED_AGENTS_DATA_FILE).load():  # ❌ HARDCODED!

---

crewai train -n 5 -f my_custom_trained.pkl

---

if data := CrewTrainingHandler(TRAINED_AGENTS_DATA_FILE).load():  # ❌ HARDCODED!
RAW_BUFFERClick to expand / collapse

Description

When training a CrewAI crew with a custom file name (-f <custom_file>.pkl), the consolidated agent guidance is correctly written to the given path (e.g., my_custom_trained.pkl). However, during normal (non-training) execution, agents always load from the hardcoded trained_agents_data.pkl, ignoring the custom file supplied at training time.


Relevant code:

    if data := CrewTrainingHandler(TRAINED_AGENTS_DATA_FILE).load():  # ❌ HARDCODED!

From agent/core.py

Steps to Reproduce

  1. Train a crew with a custom file:
    crewai train -n 5 -f my_custom_trained.pkl
  2. Observe that my_custom_trained.pkl is created and populated after training.
  3. Run the crew (or agents) for inference without renaming the file.
  4. The agents will not use your custom file; only trained_agents_data.pkl is loaded.

Expected behavior

Agents should be able to load and apply trained suggestions from the actual file used in training, not just trained_agents_data.pkl. Ideally, the system should either:

  • Track and propagate the custom path used (e.g., as Crew(..., trained_file="my_custom_trained.pkl")), or
  • Provide an API, environment variable, or config to specify the trained data file to read during inference, or
  • At minimum, clearly warn users and suggest renaming the file post-training.

Screenshots/Code snippets

    if data := CrewTrainingHandler(TRAINED_AGENTS_DATA_FILE).load():  # ❌ HARDCODED!

File: agent/core.py (lines 3-3)

Operating System

Other (specify in additional context)

Python Version

3.12

crewAI Version

N/A

crewAI Tools Version

N/A

Virtual Environment

Venv

Evidence

Trained models saved to custom path are not respected in inference; always loads trained_agents_data.pkl by default. Only workaround is to rename the file manually.

Possible Solution

  • Accept a trained_file parameter when instantiating a Crew or agent so this path is respected everywhere.
  • Fall back to trained_agents_data.pkl only when no custom filename is provided.
  • Add documentation warnings if this is intentional

Additional context

This is especially confusing for multi-experiment workflows, CI, cloud, or production runs where explicit filenames are essential.

extent analysis

Fix Plan

To resolve the issue, we need to make the following changes:

  • Introduce a trained_file parameter in the Crew class to accept custom file names.
  • Update the CrewTrainingHandler to use the provided trained_file path.
  • Add a fallback to trained_agents_data.pkl when no custom file name is provided.

Code Changes

# In agent/core.py
class Crew:
    def __init__(self, ..., trained_file=None):
        self.trained_file = trained_file

    def load_trained_data(self):
        if self.trained_file:
            return CrewTrainingHandler(self.trained_file).load()
        else:
            return CrewTrainingHandler(TRAINED_AGENTS_DATA_FILE).load()
# In crewai train command
crew = Crew(..., trained_file="my_custom_trained.pkl")

Configuration Changes

No configuration changes are required. The trained_file parameter can be passed when creating a Crew instance.

Verification

To verify the fix, train a crew with a custom file name and then run the crew without renaming the file. The agents should now load the trained data from the custom file.

# Train a crew with a custom file
crewai train -n 5 -f my_custom_trained.pkl

# Run the crew
crewai run

Extra Tips

  • Consider adding documentation warnings to inform users about the importance of specifying the trained_file parameter.
  • In a multi-experiment workflow, CI, cloud, or production environment, it's essential to use explicit filenames to avoid conflicts.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Agents should be able to load and apply trained suggestions from the actual file used in training, not just trained_agents_data.pkl. Ideally, the system should either:

  • Track and propagate the custom path used (e.g., as Crew(..., trained_file="my_custom_trained.pkl")), or
  • Provide an API, environment variable, or config to specify the trained data file to read during inference, or
  • At minimum, clearly warn users and suggest renaming the file post-training.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING