mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-07-05 06:59:23 +00:00
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.
This commit is contained in:
@@ -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.",
|
||||
|
||||
@@ -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().
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
``<locals>`` 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 "<locals>" in qualname or qualname == "<lambda>":
|
||||
return None
|
||||
return f"{module}.{qualname}"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user