Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve error message for SQLitePersister #417 #418

Merged
8 changes: 8 additions & 0 deletions burr/core/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,14 @@ def with_state_persister(
"""
if on_every != "step":
raise ValueError(f"on_every {on_every} not supported")

# Check if 'is_initialized' exists and whether it returns False, indicating the persister is uninitialized
if hasattr(persister, 'is_initialized'):
if not persister.is_initialized():
raise RuntimeError(
"RuntimeError: Uninitialized persister. Make sure to call .initialize() before passing it to the ApplicationBuilder"
)
# If the persister is valid and initialized, add it to lifecycle adapters
arpitgupta-it marked this conversation as resolved.
Show resolved Hide resolved
if not isinstance(persister, persistence.BaseStateSaver):
self.lifecycle_adapters.append(persister)
else:
Expand Down
12 changes: 11 additions & 1 deletion burr/core/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ class BaseStatePersister(BaseStateLoader, BaseStateSaver, metaclass=ABCMeta):
Extend this class if you want an easy way to implement custom state storage.
"""

pass
def is_initialized(self) -> bool:
"""Check if the persister has been initialized. Default behavior is False.
Persisters that require initialization can override this method.
"""
return False
arpitgupta-it marked this conversation as resolved.
Show resolved Hide resolved


class PersisterHook(PostRunStepHook):
Expand Down Expand Up @@ -164,6 +168,7 @@ def __init__(
db_path, **connect_kwargs if connect_kwargs is not None else {}
)
self.serde_kwargs = serde_kwargs or {}
self._initialized = False

def create_table_if_not_exists(self, table_name: str):
"""Helper function to create the table where things are stored if it doesn't exist."""
Expand Down Expand Up @@ -192,6 +197,11 @@ def initialize(self):
"""Creates the table if it doesn't exist"""
# Usage
self.create_table_if_not_exists(self.table_name)
self._initialized = True

def is_initialized(self) -> bool:
skrawcz marked this conversation as resolved.
Show resolved Hide resolved
"""Check if the persister is initialized."""
return self._initialized

def list_app_ids(self, partition_key: Optional[str], **kwargs) -> list[str]:
partition_key = (
Expand Down