reflex-dev/reflex · error · ValueError

Only one of `data` or `fp` may be provided, not both.

Error message

Only one of `data` or `fp` may be provided, not both.

What it means

BaseStateToken.deserialize accepts either raw bytes (data) or a file-like object (fp), but not both at once — passing both is ambiguous about which source to load, so ValueError is raised. This is a guard on a classmethod API used internally by load_state and get_state to unpickle persisted state.

Source

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

    @classmethod
    def deserialize(
        cls, data: bytes | None = None, fp: BinaryIO | None = None
    ) -> TOKEN_TYPE:
        """Deserialize the state from redis/disk.

        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.

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Pass exactly one source: data=raw_bytes if you already have bytes, else fp=file_obj
  2. If reading from a file, use fp=open(path,'rb') or read the bytes first and pass only data
  3. Add a unit test asserting your wrapper forwards exactly one argument

Example fix

# before
state = BaseStateToken.deserialize(data=raw, fp=fh)

# after
state = BaseStateToken.deserialize(data=raw)
Defensive patterns

Strategy: validation

Validate before calling

assert (data is None) != (fp is None), "pass exactly one of data / fp"

Prevention

When it happens

Trigger: Calling BaseStateToken.deserialize(data=pickled_bytes, fp=some_file) with both arguments non-None. Happens in custom state persistence/middleware code that reads a file into bytes but also keeps passing the open file handle.

Common situations: Adapters that wrap deserialize in custom storage backends; refactoring from fp-based to bytes-based loading and leaving both arguments in the call; copy-pasted plumbing that defaults both parameters.

Related errors


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