reflex-dev/reflex · error · ValueError
For conditional vars, the second argument must be set.
Error message
For conditional vars, the second argument must be set.
What it means
When `rx.cond(condition, c1, c2)` is used with non-component return values (i.e. building a conditional Var rather than a Cond component), the second argument c2 is mandatory because a ternary Var needs both branches. Only component rendering allows an unset c2.
Source
Thrown at packages/reflex-components-core/src/reflex_components_core/core/cond.py:204
# Convert the condition to a Var.
cond_var = LiteralVar.create(condition)
if cond_var is None:
msg = "The condition must be set."
raise ValueError(msg)
# If the first component is a component, create a Cond component.
if isinstance(c1, BaseComponent):
if not isinstance(c2, types.Unset) and not isinstance(c2, BaseComponent):
return Cond.create(cond_var.bool(), c1, Fragment.create(c2))
return Cond.create(cond_var.bool(), c1, c2)
# Otherwise, create a conditional Var.
# Check that the second argument is valid.
if isinstance(c2, BaseComponent):
return Cond.create(cond_var.bool(), Fragment.create(c1), c2)
if isinstance(c2, types.Unset):
msg = "For conditional vars, the second argument must be set."
raise ValueError(msg)
# convert the truth and false cond parts into vars so the _var_data can be obtained.
c1_var = Var.create(c1)
c2_var = Var.create(c2)
if c1_var is cond_var or c1_var.equals(cond_var):
c1_var = c1_var.to(types.value_inside_optional(c1_var._var_type))
# Create the conditional var.
return ternary_operation(
cond_var.bool(),
c1_var,
c2_var,
)
@overload
def color_mode_cond(light: Component, dark: Component | None = None) -> Component: ... # pyright: ignore [reportOverlappingOverload]View on GitHub (pinned to 45b8ed5ab7)
Solutions
- Supply the second branch: rx.cond(State.flag, 'yes', 'no')
- Use rx.cond(State.flag, rx.text('yes')) if you truly want component-only rendering (unset else renders nothing)
- Use an empty-string default explicitly: rx.cond(flag, 'yes', '')
Example fix
// before rx.text(rx.cond(State.is_pro, 'Pro')) // after rx.text(rx.cond(State.is_pro, 'Pro', 'Free'))
Defensive patterns
Strategy: validation
Validate before calling
val = rx.cond(flag, 'yes', 'no' if not isinstance(c2_given, str) else c2_given) # simply: always pass the third argument for value-returning conds
Prevention
- Always give both branches when rx.cond returns text/values
- Use rx.match with a default for multi-branch value selection
When it happens
Trigger: `rx.cond(State.flag, 'yes')` with no third argument, where c1 is a string/Var rather than a component.
Common situations: Using rx.cond to produce dynamic text or values and forgetting the else branch; assuming the else defaults to empty string.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- The condition must be set.
- Missing 'tag' keyword-argument for Icon
- IconButton requires a child icon. Pass a string as the first
- For `{template_name}` template, `template_code_dir_name` and
- ChildrenTypeError(component=cls.__name__, child=child)
AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28).
Data as JSON: /api/errors/2d52701c66901e10.
Report an issue: GitHub.