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

serialize default value for disk state manager #4008

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2592,16 +2592,24 @@ def _serialize_type(type_: Any) -> str:
return f"{type_.__module__}.{type_.__qualname__}"


def is_serializable(value: Any) -> bool:
"""Check if a value is serializable.

Args:
value: The value to check.

Returns:
Whether the value is serializable.
"""
try:
return bool(dill.dumps(value))
except Exception:
return False


def state_to_schema(
state: BaseState,
) -> List[
Tuple[
str,
str,
Any,
Union[bool, None],
]
]:
) -> List[Tuple[str, str, Any, Union[bool, None], Any]]:
"""Convert a state to a schema.

Args:
Expand All @@ -2621,6 +2629,7 @@ def state_to_schema(
if isinstance(model_field.required, bool)
else None
),
(model_field.default if is_serializable(model_field.default) else None),
)
for field_name, model_field in state.__fields__.items()
)
Expand Down
Loading