keras-team/keras · error · ValueError

`ngrams` must be None, an integer, or a tuple of integers…

Error message

`ngrams` must be None, an integer, or a tuple of integers. Received: ngrams={ngrams}

What it means

Constructor guard in TextVectorization.__init__: the ngrams argument is not None, a single int, nor a tuple of ints (e.g. a list, a string, or a tuple containing non-ints). The layer can only generate n-grams for a fixed specification of n values.

Solutions

  1. Pass ngrams=None (no n-grams), ngrams=2 (single n), or ngrams=(1, 2) (tuple of ints)
  2. Convert lists to tuples: ngrams=tuple(ngrams)
  3. Do not pass strings or nested structures
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/layers/preprocessing/text_vectorization.py:294 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/bd8100a7e268d9fd. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/layers/preprocessing/text_vectorization.py:294

            allowable_strings=(
                "int",
                "one_hot",
                "multi_hot",
                "count",
                "tf_idf",
            ),
            caller_name=self.__class__.__name__,
            arg_name="output_mode",
        )

        # 'ngrams' must be one of (None, int, tuple(int))
        if not (
            ngrams is None
            or isinstance(ngrams, int)
            or isinstance(ngrams, tuple)
            and all(isinstance(item, int) for item in ngrams)
        ):
            raise ValueError(
                "`ngrams` must be None, an integer, or a tuple of "
                f"integers. Received: ngrams={ngrams}"
            )

        # 'output_sequence_length' must be one of (None, int) and is only
        # set if output_mode is "int"".
        if output_mode == "int" and not (
            isinstance(output_sequence_length, int)
            or (output_sequence_length is None)
        ):
            raise ValueError(
                "`output_sequence_length` must be either None or an "
                "integer when `output_mode` is 'int'. Received: "
                f"output_sequence_length={output_sequence_length}"
            )

        if output_mode != "int" and output_sequence_length is not None:
            raise ValueError(

View on GitHub (pinned to 7a34a03db6)