jax-ml/jax · error · TypeError

The format string expects {n_placeholders} argument{'' if n_

Error message

The format string expects {n_placeholders} argument{'' if n_placeholders == 1 else 's'}, but got {len(args)}

What it means

Thrown by check_debug_print_format when a Pallas debug_print (or DeviceArray.print in a kernel) format string contains a different number of placeholders than the number of array arguments passed. The formatter only supports positional {} placeholders, one per argument.

Source

Thrown at jax/_src/pallas/primitives.py:634

def check_debug_print_format(
    fmt: str, *args: jax_typing.ArrayLike
):
  n_placeholders = 0
  for _, field, spec, conversion in string.Formatter().parse(fmt):
    if field is not None:
      n_placeholders += 1
    if spec or conversion:
      raise ValueError(
          "The format string should not contain any format specs or conversions"
      )
    if field:
      raise ValueError(
          "The format string should not reference arguments by position or name"
      )

  if len(args) != n_placeholders:
    raise TypeError(
        f"The format string expects {n_placeholders} "
        f"argument{'' if n_placeholders == 1 else 's'}, but got {len(args)}"
    )


# All of those shenanigans are because we can't make TransformedRef a PyTree,
# because they should appear as atomic JAX values to the users.
# TODO(apaszke): This can be deleted once we make transforms in Mosaic GPU
# inferred by the compiler.
def wrap_with_transforms(
    fun: Callable,
    ref_transforms: tuple[tuple[state_types.Transform, ...], ...],
) -> Callable:
  if all(not t for t in ref_transforms):
    return fun
  def wrapped(*args, **kwargs):
    args_ft = ft.flatten(
        (args, kwargs), registry=tree_util.default_registry

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Make the number of {} placeholders in the format string exactly equal the number of positional arguments
  2. Do not use positional {0} or named {x} placeholders — they are rejected separately
  3. Rewrite f-string style messages as "value: {}" with the value passed as an argument

Example fix

// before
debug_print("x={x} y", x, y)
// after
debug_print("x={} y={}", x, y)
Defensive patterns

Strategy: validation

Validate before calling

fmt = "x={} y={}"
args = (x, y)
assert fmt.count("{}") == len(args), f"format expects {fmt.count('{}')} args, got {len(args)}"

Try / catch

try:
    debug_print(fmt, *args)
except (TypeError, ValueError) as e:
    if "format string" in str(e):
        raise ValueError(f"debug_print format/arg mismatch: {fmt!r} vs {len(args)} args") from e
    raise

Prevention

When it happens

Trigger: Calling debug_print("{} {}", x) inside a Pallas kernel, or passing extra/missing arguments relative to the {} count in the format string.

Common situations: Editing a kernel's print statement and forgetting to add/remove a {} or an argument; copying Python f-string style formatting into debug_print which only supports str.format-style placeholders.

Related errors


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