Do not import reflex.model while transforming an event payload
Fixes FINDING-045 from the reflex 0.11.0a1 pre-release testing (artifacts on branch claude/vibrant-pasteur-3921g3, prerelease-testing/runs/2026-11-11-v0.11.0a1/compile_memo/ and enterprise/).
All Submissions:
- Have you followed the guidelines stated in CONTRIBUTING.md?
- Have you checked to ensure there aren't any other open Pull Requests for the desired changed?
Type of change
- Bug fix (non-breaking change which fixes an issue)
Changes To Core Features:
- Have you added an explanation of what your changes do and why you'd like us to include them?
- Have you written new tests for your core changes, as applicable?
- Have you successfully ran tests with your changes locally?
Defect
The first event whose handler takes a dict, dataclass, TypedDict or Pydantic model argument (e.g. on_upload_progress=State.on_progress with def on_progress(self, prog: dict)) ran from reflex.model import Model inside _transform_event_arg. With the db extra installed that imports SQLAlchemy / SQLModel / alembic (0.22–0.56 s, ~143 modules) inside the event task, and the dev event-loop blocking monitor (#7672) reported the user's trivial handler as blocking the loop, with a stack through SQLAlchemy's import. Seen on an upload page and on the reflex-enterprise flow demo.
Fix
A class can only subclass rx.Model once reflex.model has run, so the payload transform now looks the module up in sys.modules instead of importing it (the pattern telemetry_accounting._get_db_model_count already uses). Apps that never use rx.Model never load SQLAlchemy, at startup or per event; apps that do loaded it when they defined their models. Per-event cost unchanged (~120 ns).
Every lazy import reachable from the event path was checked, statically and with an import audit hook on a dev server: this was the only one that ran inside an event (the next largest, plotly's to_json on the first Figure, costs 1–16 ms). Warming reflex.model at worker startup was considered and rejected: +0.22–0.32 s on every worker start and dev reload for every app with the db extra, used or not (reflex-enterprise depends on reflex[db]), and warming it in a thread can still block the first event on the import lock.
Test plan
tests/units/reflex_base/event/processor/test_base_state_processor.py:test_transform_event_arg_dict_does_not_import_the_model_module[dict|_TagsDict|_TagsRecord]— withsys.modules["reflex.model"] = None; before:ModuleNotFoundError: import of reflex.model halted; None in sys.modules×3.test_dict_arg_handler_first_event_does_not_block_on_the_model_import— realBaseStateEventProcessorin dev with a 0.05 s threshold and a fakereflex.modelwhose import sleeps 0.3 s; before: the fake import ran and the monitor logged the finding's warning; after: no import, no warning, the dict reaches the handler.- The existing
rx.Modelpath test still passes. The processor directory: 478 passed; the touched module: 375 passed.
- Full unit suite: 15638 passed, 66 skipped, 81.42% (the Playwright browser-driver module excluded locally for lack of a Chromium binary).
ruff,pyright: clean. - End to end (dev, audit hook): PyPI 0.11.0a1 logged the blocking warning on the first
on_progresswith 147 imports inside the event; this branch: 0 warnings, 0 imports inside events for dict / dataclass / TypedDict / Pydantic / background / upload handlers, with and without the db extra; anrx.Modelargument still drops extra keys.
Notes
/_healthlazily importsreflex.modelon the loop whendb_urlis set (not an event); left as is.- Attributing import / third-party frames in the monitor is left to FINDING-046.
News fragment: packages/reflex-base/news/+dict-arg-no-model-import.bugfix.md.
Generated by Claude Code