reflex-dev/reflex · error · ValueError

The component `{comp_name}` only allows the components: {val

Error message

The component `{comp_name}` only allows the components: {valid_child_list} as children. Got `{child_name}` instead.

What it means

BaseState.get_value only accepts string keys (attribute names) when reading state values; any other key type raises TypeError.

Source

Thrown at packages/reflex-base/src/reflex_base/components/component.py:1633

            if isinstance(child, Match):
                for cases in child.match_cases:
                    validate_child(cases[-1])
                validate_child(child.default)

            if self._invalid_children and child_name in self._invalid_children:
                msg = f"The component `{comp_name}` cannot have `{child_name}` as a child component"
                raise ValueError(msg)

            if self._valid_children and child_name not in [
                *self._valid_children,
                *allowed_components,
            ]:
                valid_child_list = ", ".join([
                    f"`{v_child}`" for v_child in self._valid_children
                ])
                msg = f"The component `{comp_name}` only allows the components: {valid_child_list} as children. Got `{child_name}` instead."
                raise ValueError(msg)

            if child._valid_parents and all(
                clz_name not in [*child._valid_parents, *allowed_components]
                for clz_name in self._iter_parent_classes_names()
            ):
                valid_parent_list = ", ".join([
                    f"`{v_parent}`" for v_parent in child._valid_parents
                ])
                msg = f"The component `{child_name}` can only be a child of the components: {valid_parent_list}. Got `{comp_name}` instead."
                raise ValueError(msg)

        for child in children:
            validate_child(child)

    @staticmethod
    def _get_vars_from_event_triggers(
        event_triggers: dict[str, EventChain | Var],
    ) -> Iterator[tuple[str, list[Var]]]:

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Pass the attribute name as a string: `state.get_value("count")
  2. If you hold a Var, use `var._var_data.field_name` to obtain the string name first
  3. Use `state.dict()` when you need the whole mapping

Example fix

// before
state.get_value(idx)
// after
state.get_value("count")
Defensive patterns

Strategy: type-guard

Validate before calling

assert isinstance(key, str), "get_value expects an attribute name"

Type guard

def get_value_safe(state: rx.State, key: str) -> Any:
    if not isinstance(key, str):
        raise TypeError("key must be str")
    return state.get_value(key)

Prevention

When it happens

Trigger: Calling `state.get_value(0)`, `state.get_value(some_var)` or any non-str key instead of `state.get_value("fieldname")`.

Common situations: Code that treats State like a dict/sequence and passes indexes or Var objects; generic serialization helpers calling get(key) with arbitrary keys.

Related errors


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