Skip to content

@rx.page and add_meta reject State Vars for title/description (VarTypeError) #6923

Description

@tinztwins

Summary

Passing a State Var as title or description to @rx.page raises VarTypeError. The same failure can occur later at compile time in add_meta() when description is a Var.

Error

reflex_base.utils.exceptions.VarTypeError: Cannot convert Var '…' to bool
for use with `if`, `and`, `or`, and `not`. Instead use `rx.cond` and
bitwise operators `&` (and), `|` (or), `~` (invert).

Affected locations

1. reflex/page.py (@rx.page decorator)

if title:
    kwargs["title"] = title
if description:
    kwargs["description"] = description

Any non-None Var hits Var.__bool__ and raises immediately when the decorator runs.

2. reflex/compiler/utils.py (add_meta)

children: list[Any] = [Title.create(title)]
if description:
    children.append(Description.create(content=description))

If a Var description reaches the compiler, compilation fails here.

Expected behavior

title and description should accept str | Var | None consistently for both @rx.page and app.add_page, matching the documented page metadata API for dynamic SEO titles and descriptions (especially on dynamic routes).

Suggested fix

Replace truthiness checks with explicit None checks:

# reflex/page.py
if title is not None:
    kwargs["title"] = title
if description is not None:
    kwargs["description"] = description

# reflex/compiler/utils.py (add_meta)
if description is not None:
    children.append(Description.create(content=description))

Optionally tighten type annotations to str | Var | None where appropriate.

Minimal reproduction

import reflex as rx


class State(rx.State):
    page_title: str = "Hello"
    page_description: str = "A dynamic description"


@rx.page(
    route="/",
    title=State.page_title,  # raises VarTypeError in page.py
    description=State.page_description,  # raises VarTypeError in page.py
)
def index():
    return rx.text("Hi")


app = rx.App()

Environment

  • Reflex: 0.9.4 (also present in 0.9.8 source for both files)
  • Python: 3.13

Impact

Dynamic per-page titles and descriptions for SEO cannot be set reliably with State vars through @rx.page.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions