From 740b52d27b0ae4eb7f6cbd95bf72e64963a5c9b1 Mon Sep 17 00:00:00 2001 From: Greyson LaLonde Date: Sat, 23 May 2026 13:02:48 -0700 Subject: [PATCH] fix(checkpoint): drop unroundtrippable callbacks and adapter state - callable_to_string returns None for lambdas/closures instead of an unresolvable dotted path; Crew filters Nones out of restored callback lists. - EventNode.event serializer honors info.mode so mode='json' calls cascade properly into nested event payloads. - RagTool.adapter serializes to None (post-validator rebuilds from config); concrete adapters hold runtime state that can't be round-tripped. --- .../src/crewai_tools/tools/rag/rag_tool.py | 30 +++++++++++++++++-- lib/crewai/src/crewai/crew.py | 9 ++++++ lib/crewai/src/crewai/state/event_record.py | 6 +++- lib/crewai/src/crewai/types/callback.py | 13 ++++---- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/lib/crewai-tools/src/crewai_tools/tools/rag/rag_tool.py b/lib/crewai-tools/src/crewai_tools/tools/rag/rag_tool.py index 8099443e2..be269aa78 100644 --- a/lib/crewai-tools/src/crewai_tools/tools/rag/rag_tool.py +++ b/lib/crewai-tools/src/crewai_tools/tools/rag/rag_tool.py @@ -1,6 +1,6 @@ from abc import ABC, abstractmethod import os -from typing import Any, Literal, cast +from typing import Annotated, Any, Literal, cast from crewai.rag.core.base_embeddings_callable import EmbeddingFunction from crewai.rag.embeddings.factory import build_embedder @@ -8,8 +8,10 @@ from crewai.rag.embeddings.types import ProviderSpec from crewai.tools import BaseTool from pydantic import ( BaseModel, + BeforeValidator, ConfigDict, Field, + PlainSerializer, TypeAdapter, ValidationError, field_validator, @@ -100,6 +102,26 @@ class Adapter(BaseModel, ABC): """Add content to the knowledge base.""" +def _resolve_adapter(value: Any) -> Any: + """Validate the ``adapter`` field, returning a placeholder for dict input. + + Adapter state is not round-tripped; the ``_ensure_adapter`` post-validator + rebuilds a fresh adapter from the tool's ``config``. + """ + if isinstance(value, Adapter): + return value + if not isinstance(value, dict): + return value + return RagTool._AdapterPlaceholder() + + +def _serialize_adapter(adapter: Any, info: Any) -> Any: + """Serialize the ``adapter`` field, dropping runtime state from the payload.""" + if not isinstance(adapter, Adapter): + return adapter + return None + + class RagTool(BaseTool): class _AdapterPlaceholder(Adapter): def query( @@ -123,7 +145,11 @@ class RagTool(BaseTool): similarity_threshold: float = 0.6 limit: int = 5 collection_name: str = "rag_tool_collection" - adapter: Adapter = Field(default_factory=_AdapterPlaceholder) + adapter: Annotated[ + Adapter, + BeforeValidator(_resolve_adapter), + PlainSerializer(_serialize_adapter), + ] = Field(default_factory=_AdapterPlaceholder) config: RagToolConfig = Field( default_factory=RagToolConfig, description="Configuration format accepted by RagTool.", diff --git a/lib/crewai/src/crewai/crew.py b/lib/crewai/src/crewai/crew.py index 0ffec4888..870049179 100644 --- a/lib/crewai/src/crewai/crew.py +++ b/lib/crewai/src/crewai/crew.py @@ -382,6 +382,15 @@ class Crew(FlowTrackable, BaseModel): checkpoint_train: bool | None = Field(default=None) checkpoint_kickoff_event_id: str | None = Field(default=None) + @field_validator( + "before_kickoff_callbacks", "after_kickoff_callbacks", mode="before" + ) + @classmethod + def _drop_unresolvable_callbacks(cls, value: Any) -> Any: + if isinstance(value, list): + return [v for v in value if v is not None] + return value + @classmethod def from_checkpoint(cls, config: CheckpointConfig) -> Crew: """Restore a Crew from a checkpoint, ready to resume via kickoff(). diff --git a/lib/crewai/src/crewai/state/event_record.py b/lib/crewai/src/crewai/state/event_record.py index f0b15b48f..2ca78a396 100644 --- a/lib/crewai/src/crewai/state/event_record.py +++ b/lib/crewai/src/crewai/state/event_record.py @@ -67,7 +67,11 @@ class EventNode(BaseModel): event: Annotated[ BaseEvent, BeforeValidator(_resolve_event), - PlainSerializer(lambda v: v.model_dump()), + PlainSerializer( + lambda v, info: ( + v.model_dump(mode="json") if info.mode == "json" else v.model_dump() + ), + ), ] edges: dict[EdgeType, list[str]] = Field(default_factory=dict) diff --git a/lib/crewai/src/crewai/types/callback.py b/lib/crewai/src/crewai/types/callback.py index ea89effdb..a6fb2d101 100644 --- a/lib/crewai/src/crewai/types/callback.py +++ b/lib/crewai/src/crewai/types/callback.py @@ -130,18 +130,15 @@ def _resolve_dotted_path(path: str) -> Callable[..., Any]: raise ValueError(f"Cannot resolve callback {path!r}") -def callable_to_string(fn: Callable[..., Any]) -> str: - """Serialize a callable to its dotted-path string representation. - - Uses ``fn.__module__`` and ``fn.__qualname__`` to produce a string such - as ``"builtins.print"``. Lambdas and closures produce paths that contain - ```` and cannot be round-tripped via :func:`string_to_callable`. +def callable_to_string(fn: Callable[..., Any]) -> str | None: + """Serialize a module-level callable as a ``"module.qualname"`` string. Args: fn: The callable to serialize. Returns: - A dotted string of the form ``"module.qualname"``. + The dotted path, or ``None`` for lambdas and closures (not + resolvable by :func:`string_to_callable`). """ module = getattr(fn, "__module__", None) qualname = getattr(fn, "__qualname__", None) @@ -150,6 +147,8 @@ def callable_to_string(fn: Callable[..., Any]) -> str: f"Cannot serialize {fn!r}: missing __module__ or __qualname__. " "Use a module-level named function for checkpointable callbacks." ) + if "" in qualname or qualname == "": + return None return f"{module}.{qualname}"