jax-ml/jax · warning · ValueError

The return value of the policies should be a boolean. Got: {

Error message

The return value of the policies should be a boolean. Got: {out1} and {out2}. Please write a custom policy function directly, rather than using this helper function.

What it means

This is a DeprecationWarning (not yet an error) emitted when flattening encounters an object that is a Python iterable type unknown to the PyTree registry (e.g. a generator, set, frozenset, or custom __iter__ class). Such objects are currently treated as leaves, but a future JAX release will raise instead.

Source

Thrown at jax/_src/ad_checkpoint.py:193

        "The names should be exclusive and should not intersect in"
        " `names_which_can_be_saved` and `names_which_can_be_offloaded`. Got"
        f" names_which_can_be_saved={set(names_which_can_be_saved)},"
        f" names_which_can_be_offloaded={set(names_which_can_be_offloaded)} and"
        f" the intersection={set(intersection)}")
  return SaveAndOffloadOnlyTheseNames(
      names_which_can_be_saved, names_which_can_be_offloaded,
      offload_src, offload_dst)


def save_from_both_policies(policy_1, policy_2):
  """Logical OR of the given policies.

  A residual is saveable iff it is saveable according to either policy."""
  def policy(prim, *args, **params):
    out1 = policy_1(prim, *args, **params)
    out2 = policy_2(prim, *args, **params)
    if not (isinstance(out1, bool) and isinstance(out2, bool)):
      raise ValueError(
          "The return value of the policies should be a boolean. Got:"
          f" {out1} and {out2}. Please write a custom policy function directly,"
          " rather than using this helper function.")
    return out1 or out2
  return policy


# Please update the file docs/gradient-checkpointing.md with any new
# policies to keep the doc in sync.
checkpoint_policies = types.SimpleNamespace(
    SaveOnlyTheseNames=SaveOnlyTheseNames,
    SaveAnyNamesButThese=SaveAnyNamesButThese,
    SaveAndOffloadOnlyTheseNames=SaveAndOffloadOnlyTheseNames,
    everything_saveable=everything_saveable,
    nothing_saveable=nothing_saveable,
    dots_saveable=dots_saveable,
    checkpoint_dots=dots_saveable,
    dots_with_no_batch_dims_saveable=dots_with_no_batch_dims_saveable,

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Convert sets to sorted lists: sorted(s); generators to lists: list(g)
  2. Register the custom type with jax.tree_util.register_pytree_node if it should flatten
  3. Pass is_leaf=lambda x: isinstance(x, (set, frozenset)) to explicitly opt into leaf treatment and silence the warning
  4. Filter warnings with -W error::DeprecationWarning in CI to catch future breakage early

Example fix

# before
params = {'ids': {1, 2, 3}}
jax.tree.map(f, params)  # DeprecationWarning about iterable treated as leaf

# after
params = {'ids': [1, 2, 3]}
jax.tree.map(f, params)
Defensive patterns

Strategy: validation

Validate before calling

def no_unregistered_iterables(tree) -> bool:
    bad = (set, frozenset, type((x for x in [])))
    return not any(isinstance(l, bad) for l in jax.tree.leaves(tree))

Type guard

def is_leafable(x) -> bool:
    return not isinstance(x, (set, frozenset))

Try / catch

try:
    jax.tree.map(f, tree)
except DeprecationWarning as w:
    if 'treated as a leaf' in str(w):
        tree = jax.tree.map(lambda s: sorted(s) if isinstance(s, (set, frozenset)) else s,
                            tree, is_leaf=lambda x: isinstance(x, (set, frozenset)))

Prevention

When it happens

Trigger: Placing a set, generator, dict subclass, or custom iterable inside a structure passed to jax.tree.map / tree.flatten; because sets have no registered flatten rule, the C++ code warns and treats the whole iterable as one leaf.

Common situations: Passing {1,2,3} (a set) as a container, storing generators from comprehensions, migrating configs holding Python objects into JAX transforms; behavior change after upgrading JAX where previously it silently leaf-ified them.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/bd997f552a89108f. Report an issue: GitHub.