reflex-dev/reflex · error · ValueError

At least one of `data` or `fp` must be provided.

Error message

At least one of `data` or `fp` must be provided.

What it means

BaseStateToken.deserialize requires exactly one source of serialized state: either data (bytes) or fp (file object). If neither is provided there is nothing to unpickle, so it raises ValueError. It is the counterpart guard to the 'both provided' check and fires when the caller forwards empty/None payloads.

Source

Thrown at reflex/istate/manager/token.py:106

        data and fp are mutually exclusive, but one must be provided.

        Args:
            data: The serialized state data.
            fp: The file pointer to the serialized state data.

        Returns:
            The deserialized state instance.
        """
        if data is not None and fp is not None:
            msg = "Only one of `data` or `fp` may be provided, not both."
            raise ValueError(msg)
        if data is not None:
            return pickle.loads(data)
        if fp is not None:
            return pickle.load(fp)
        msg = "At least one of `data` or `fp` must be provided."
        raise ValueError(msg)

    @classmethod
    def get_and_reset_touched_state(cls, state: TOKEN_TYPE) -> bool:
        """Get the touched state and reset the touched flag.

        This is used to determine if a state has been modified since it was last serialized.

        Args:
            state: The state to check for modifications.

        Returns:
            The touched state of the state.
        """
        # Default implementation is always to write the state.
        return True


class BaseStateToken(StateToken["BaseState"]):

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Check for empty payload before calling deserialize and handle 'no state' by creating fresh state
  2. If reading from storage, branch: data = store.get(key); if data is None: fresh_state() else deserialize(data=data)
  3. Log the token/key when the payload is empty to identify expired or missing state

Example fix

# before
state = BaseStateToken.deserialize(data=data)  # data is None -> ValueError

# after
if data is None:
    state = root_state_cls(_reflex_internal_init=True)
else:
    state = BaseStateToken.deserialize(data=data)
Defensive patterns

Strategy: validation

Validate before calling

if data is None and fp is None:
    # no persisted state; create fresh instead of calling deserialize
    state = root_state_cls(_reflex_internal_init=True)
else:
    state = BaseStateToken.deserialize(data=data, fp=fp)

Try / catch

try:
    state = BaseStateToken.deserialize(data=data)
except ValueError as e:
    if "At least one" in str(e):
        state = root_state_cls(_reflex_internal_init=True)
    else:
        raise

Prevention

When it happens

Trigger: Calling BaseStateToken.deserialize() with no arguments, or with data=None and fp=None — commonly when a caller reads a token store, gets no bytes back (missing/expired key), and passes the None through instead of short-circuiting.

Common situations: Custom storage backends returning None for missing keys; race where the Redis/disk token expired between existence check and read; refactors that drop the argument accidentally; code branching that forgets the third case (neither).

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28). Data as JSON: /api/errors/6727799f74e55fd9. Report an issue: GitHub.