Repository navigation
Open
Labels
bugSomething isn't workingrelease-trainIssues found during agentic /prerelease-testing fanout, not necessarily validated bugs.
Description
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:
hybrid_property. A@hybrid_propertytypedstron the model compiles (State.user.upper_nameis accepted as a Var), but the serializer never includes it, so the text is always blank. A plain@propertyin the same position now raises a clearVarAttributeErrorat compile time ("... is not included in serialized state ... register anrx.serializer"), so the hybrid case is the odd one out.- Expired instances. With the SQLAlchemy default
async_sessionmaker(engine)(expire_on_commit=True), an object assigned to state afterawait 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 recommendexpire_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-timeVarAttributeErrorlike 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=Falseorsession.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_attributescovers every column, orstate.dictholds 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 withgetattron the instance, only when the attribute's inputs are loaded) and rejecting it at Var-access time the same way@propertyis 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.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingrelease-trainIssues found during agentic /prerelease-testing fanout, not necessarily validated bugs.
Activity
linear-code commentedon Nov 11, 2026
ENG-13979