crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG]Restoring a hierarchical crew from a checkpoint fails with "This field is not to be set by the user"

Open subhashpolisetti opened this issue 1 month ago • 3 comments

Description

Crew.from_checkpoint raises a ValidationError for any checkpoint of a hierarchical crew. The manager agent holds the delegation tools (Delegate work to coworker, Ask question to coworker), and those tools store the coworker agents. On restore, the coworker agents' saved id is rejected with "This field is not to be set by the user". The same happens for any agent given AgentTools(agents=[...]).tools() directly. Writing the checkpoint works; only restoring it fails.

The BaseTool validator rebuilds a serialized tool with tool_cls.model_validate(data) and drops the validation context, so the nested agents never see from_checkpoint and their id guard fires. BaseAgentTool.agents also has no _resolve_agent hook, unlike Task.agent and Crew.manager_agent.

Steps to Reproduce

  1. Configure a hierarchical crew with checkpoint=CheckpointConfig(location=<dir>, on_events=["task_completed"]).
  2. Run crew.kickoff() so a checkpoint is written.
  3. Restore it with Crew.from_checkpoint(CheckpointConfig(restore_from=<checkpoint file>)).

Expected behavior

The crew restores, the same as a sequential crew does.

Screenshots/Code snippets

import glob
import os
import tempfile
from typing import Any

from crewai import Agent, Crew, Process, Task
from crewai.llms.base_llm import BaseLLM
from crewai.state.checkpoint_config import CheckpointConfig


class FinalLLM(BaseLLM):
    def __init__(self) -> None:
        super().__init__(model="offline")

    def supports_function_calling(self) -> bool:
        return False

    def call(self, messages: Any, **kwargs: Any) -> str:
        return "Thought: done\nFinal Answer: ok"

    async def acall(self, messages: Any, **kwargs: Any) -> str:
        return self.call(messages, **kwargs)


location = tempfile.mkdtemp()
writer = Agent(role="Writer", goal="w", backstory="b", llm=FinalLLM())
crew = Crew(
    agents=[writer],
    tasks=[Task(description="Say ok", expected_output="ok")],
    process=Process.hierarchical,
    manager_llm=FinalLLM(),
    checkpoint=CheckpointConfig(location=location, on_events=["task_completed"]),
)
crew.kickoff()

[checkpoint] = glob.glob(os.path.join(location, "**", "*.json"), recursive=True)
Crew.from_checkpoint(CheckpointConfig(restore_from=checkpoint))

Operating System

macOS Monterey

Python Version

3.12

crewAI Version

1.15.30 (main @ bb4d892e6)

crewAI Tools Version

N/A (not used in the repro)

Virtual Environment

Venv

Evidence

pydantic_core._pydantic_core.ValidationError: 6 validation errors for RuntimeState 0.crew.tasks.0.agent.tools.0.agents.0.id This field is not to be set by the user. [type=may_not_set_field, input_value='61ea8809-...', input_type=str] 0.crew.tasks.0.agent.tools.1.agents.0.id This field is not to be set by the user. [type=may_not_set_field, input_value='61ea8809-...', input_type=str] ...

The same crew run sequentially (task assigned to `writer`) restores without error.

### Possible Solution

Pass the validation context through when `BaseTool` rebuilds a serialized tool (`model_validate(data,
context=info.context)`), and resolve `BaseAgentTool.agents` with `_resolve_agent`, the way `Task.agent` and
`Crew.manager_agent` already do.

### Additional context

Reproduced on Python 3.13 with a uv-managed venv (3.13 is not in the version dropdown).

subhashpolisetti avatar Oct 27 '26 05:10 subhashpolisetti

Until the BaseTool validator passes the validation context through, a workaround (untested) is to keep the delegation tools out of the restore and rebuild them afterwards:

  1. In the checkpoint JSON, strip the manager's tools entries for "Delegate work to coworker" / "Ask question to coworker" (check the exact shape in your file first), so the nested coworker agents never hit the id guard.
  2. After crew = Crew.from_checkpoint(...), reattach them yourself:
from crewai.tools.agent_tools.agent_tools import AgentTools
crew.manager_agent.tools = AgentTools(agents=crew.agents).tools()

Step 2 matters: on current main, _create_manager_agent() only builds AgentTools(agents=self.agents).tools() when manager_agent is None. A restored crew already has a manager, so kickoff keeps whatever tools it was restored with (an empty list if you stripped them). The coworkers come from the crew itself, so their ids never go through the tool validator. The same approach works for agents given AgentTools(...).tools() directly.

For the fix, giving BaseAgentTool.agents a BeforeValidator(_resolve_agents) like Crew.agents has, or forwarding info.context in the BaseTool rebuild, would let the from_checkpoint context reach the nested agents.

Rafay121 avatar Oct 28 '26 15:10 Rafay121

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Nov 28 '26 12:11 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Dec 03 '26 12:12 github-actions[bot]