Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
refactor: make BaseProvider a BaseModel with provider_type discriminator
Greyson LaLonde75f162fd3c10
diff --git a/lib/crewai/src/crewai/state/checkpoint_config.py b/lib/crewai/src/crewai/state/checkpoint_config.pyindex 84c48bd4e..38c6b0490 100644--- a/lib/crewai/src/crewai/state/checkpoint_config.py+++ b/lib/crewai/src/crewai/state/checkpoint_config.py@@ -2,12 +2,12 @@from __future__ import annotations-from typing import Any, Literal+from typing import Annotated, Any, Literalfrom pydantic import BaseModel, Field, model_validator-from crewai.state.provider.core import BaseProviderfrom crewai.state.provider.json_provider import JsonProvider+from crewai.state.provider.sqlite_provider import SqliteProviderCheckpointEventType = Literal[@@ -189,7 +189,10 @@ class CheckpointConfig(BaseModel):description="Event types that trigger a checkpoint write. "'Use ["*"] to checkpoint on every event.',)- provider: BaseProvider = Field(+ provider: Annotated[+ JsonProvider | SqliteProvider,+ Field(discriminator="provider_type"),+ ] = Field(default_factory=JsonProvider,description="Storage backend. Defaults to JsonProvider.",)diff --git a/lib/crewai/src/crewai/state/provider/core.py b/lib/crewai/src/crewai/state/provider/core.pyindex 46f079444..0b12364c0 100644--- a/lib/crewai/src/crewai/state/provider/core.py+++ b/lib/crewai/src/crewai/state/provider/core.py@@ -1,39 +1,22 @@-"""Base protocol for state providers."""+"""Base class for state providers."""from __future__ import annotations-from typing import Any, Protocol, runtime_checkable+from abc import ABC, abstractmethod-from pydantic import GetCoreSchemaHandler-from pydantic_core import CoreSchema, core_schema+from pydantic import BaseModel-@runtime_checkable-class BaseProvider(Protocol):- """Interface for persisting and restoring runtime state checkpoints.+class BaseProvider(BaseModel, ABC):+ """Base class for persisting and restoring runtime state checkpoints.Implementations handle the storage backend — filesystem, cloud, database,etc. — while ``RuntimeState`` handles serialization."""- @classmethod- def __get_pydantic_core_schema__(- cls, source_type: Any, handler: GetCoreSchemaHandler- ) -> CoreSchema:- """Allow Pydantic to validate any ``BaseProvider`` instance."""-- def _validate(v: Any) -> BaseProvider:- if isinstance(v, BaseProvider):- return v- raise TypeError(f"Expected a BaseProvider instance, got {type(v)}")-- return core_schema.no_info_plain_validator_function(- _validate,- serialization=core_schema.plain_serializer_function_ser_schema(- lambda v: type(v).__name__, info_arg=False- ),- )+ provider_type: str = "base"+ @abstractmethoddef checkpoint(self, data: str, location: str) -> str:"""Persist a snapshot synchronously.@@ -46,6 +29,7 @@ class BaseProvider(Protocol):"""...+ @abstractmethodasync def acheckpoint(self, data: str, location: str) -> str:"""Persist a snapshot asynchronously.@@ -58,6 +42,7 @@ class BaseProvider(Protocol):"""...+ @abstractmethoddef prune(self, location: str, max_keep: int) -> None:"""Remove old checkpoints, keeping at most *max_keep*.@@ -67,6 +52,7 @@ class BaseProvider(Protocol):"""...+ @abstractmethoddef from_checkpoint(self, location: str) -> str:"""Read a snapshot synchronously.@@ -78,6 +64,7 @@ class BaseProvider(Protocol):"""...+ @abstractmethodasync def afrom_checkpoint(self, location: str) -> str:"""Read a snapshot asynchronously.diff --git a/lib/crewai/src/crewai/state/provider/json_provider.py b/lib/crewai/src/crewai/state/provider/json_provider.pyindex d2ac75d9c..f9763e6f3 100644--- a/lib/crewai/src/crewai/state/provider/json_provider.py+++ b/lib/crewai/src/crewai/state/provider/json_provider.py@@ -7,6 +7,7 @@ import globimport loggingimport osfrom pathlib import Path+from typing import Literalimport uuidimport aiofiles@@ -21,6 +22,8 @@ logger = logging.getLogger(__name__)class JsonProvider(BaseProvider):"""Persists runtime state checkpoints as JSON files on the local filesystem."""+ provider_type: Literal["json"] = "json"+def checkpoint(self, data: str, location: str) -> str:"""Write a JSON checkpoint file.diff --git a/lib/crewai/src/crewai/state/provider/sqlite_provider.py b/lib/crewai/src/crewai/state/provider/sqlite_provider.pyindex ae014dda3..e54f56180 100644--- a/lib/crewai/src/crewai/state/provider/sqlite_provider.py+++ b/lib/crewai/src/crewai/state/provider/sqlite_provider.py@@ -5,6 +5,7 @@ from __future__ import annotationsfrom datetime import datetime, timezonefrom pathlib import Pathimport sqlite3+from typing import Literalimport uuidimport aiosqlite@@ -47,6 +48,8 @@ class SqliteProvider(BaseProvider):used as the database file path."""+ provider_type: Literal["sqlite"] = "sqlite"+def checkpoint(self, data: str, location: str) -> str:"""Write a checkpoint to the SQLite database.