Skip to content

DeclarativeBase serializer renders blanks silently: hybrid_property values are never sent, expired instances serialize as {} #7831

Open

Description

@masenf

Problem

reflex 0.11 serializes SQLAlchemy DeclarativeBase instances held in state with a built-in serializer that sends only loaded attributes. Two common cases render empty UI with no error or log line:

  1. hybrid_property. A @hybrid_property typed str on the model compiles (State.user.upper_name is accepted as a Var), but the serializer never includes it, so the text is always blank. A plain @property in the same position now raises a clear VarAttributeError at compile time ("... is not included in serialized state ... register an rx.serializer"), so the hybrid case is the odd one out.
  2. Expired instances. With the SQLAlchemy default async_sessionmaker(engine) (expire_on_commit=True), an object assigned to state after await session.commit() has every attribute expired. It serializes as {} and the page shows nothing for it. 0.10.1 raised (MissingGreenlet / DetachedInstanceError) in this situation, so the mistake was at least visible. The docs recommend expire_on_commit=False, but nothing tells a user who missed that.

Repro

import reflex as rx
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass, mapped_column


class Base(MappedAsDataclass, DeclarativeBase):
    pass


class User(Base):
    __tablename__ = "user"
    id: Mapped[int] = mapped_column(init=False, primary_key=True)
    name: Mapped[str]

    @hybrid_property
    def upper_name(self) -> str:
        return self.name.upper()


class S(rx.State):
    user: User | None = None

    @rx.event
    async def load(self):
        async with rx.asession() as session:  # or any async_sessionmaker with the default expire_on_commit=True
            user = User(name="alice")
            session.add(user)
            await session.commit()
            self.user = user  # expired after commit -> serialized as {}


def index():
    return rx.vstack(
        rx.button("load", on_click=S.load),
        rx.text("name: ", S.user.name),          # blank when the instance is expired
        rx.text("upper: ", S.user.upper_name),   # always blank (hybrid_property never sent)
    )

Expected vs actual

  • hybrid_property: expected either the value is serialized (instance-level evaluation of the hybrid, like other loaded attributes), or a compile-time VarAttributeError like the one for @property. Actual: compiles and is always blank.
  • Expired instance: expected a warning (at least in dev) that names the model and says the instance was expired, pointing at expire_on_commit=False or session.refresh(). Actual: {}, no log.

Fix direction

  • Serializer: packages/reflex-base/src/reflex_base/utils/serializers.py (_get_sqlalchemy_layout, _serialize_sqlalchemy_model). An instance that has an identity key but whose column attributes are all expired (instance_state(m).expired_attributes covers every column, or state.dict holds none of the columns) is the case to detect; log a warning once per model class, in dev mode only if the check costs anything on the hot path.
  • hybrid_property: decide between serializing it (add hybrid attributes to the layout and read them with getattr on the instance, only when the attribute's inputs are loaded) and rejecting it at Var-access time the same way @property is rejected. The rejection is the smaller change and matches the existing rule that only declared fields are serialized.
  • Non-goals: do not lazy-load or refresh attributes from the serializer (it must never run a query); do not change the behaviour for instances that are loaded.
  • Tests: serializer unit tests for an expired instance (warning emitted, {} result unchanged or documented) and for a hybrid property (value sent, or compile error raised).

Not a regression for the hybrid case (0.10.1 also rendered blank); the expired case changed from an exception on 0.10.1 to a silent {} with the new serializer.

Activity

  1. added
    bug
    release-train
    on Nov 11, 2026
  2. linear-code commented on Nov 11, 2026

    @linear-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugrelease-train

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions