keras-team/keras · error · TypeError

Unrecognized keyword arguments

Error message

Unrecognized keyword arguments: {str(kwargs)}

What it means

The legacy Tokenizer __init__ accepts only a fixed set of keyword arguments; after handling the deprecated nb_words alias and popping document_count, any remaining kwargs raise TypeError listing the offending names, so misspelled or removed arguments are never silently ignored.

Solutions

  1. Fix each name printed in the message against the real signature (num_words, filters, lower, split, char_level, oov_token, document_count, analyzer)
  2. Replace nb_words with num_words (only that alias is auto-handled)
  3. Drop arguments removed in this Keras version

Example fix

# before
Tokenizer(num_word=5000, lower=True)
# after
Tokenizer(num_words=5000, lower=True)
Defensive patterns

Strategy: type-guard

Validate before calling

allowed = {'num_words','filters','lower','split','char_level','oov_token','document_count','analyzer'}
bad = set(kwargs) - allowed
if bad:
    raise TypeError(f'typo(s): {bad}')

Type guard

def valid_tokenizer_kwargs(kw):
    allowed = {'num_words','filters','lower','split','char_level','oov_token','document_count','analyzer'}
    return not (set(kw) - allowed)

Prevention

When it happens

Trigger: Constructing Tokenizer with a misspelled or outdated kwarg such as num_word=100, or Keras 1.x-era names other than nb_words.

Common situations: Copy-pasting Tokenizer code from old tutorials; IDE autocompletion picking the wrong name; version migrations from Keras 1.x.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at keras/src/legacy/preprocessing/text.py:105

        num_words=None,
        filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n',
        lower=True,
        split=" ",
        char_level=False,
        oov_token=None,
        analyzer=None,
        **kwargs,
    ):
        # Legacy support
        if "nb_words" in kwargs:
            warnings.warn(
                "The `nb_words` argument in `Tokenizer` "
                "has been renamed `num_words`."
            )
            num_words = kwargs.pop("nb_words")
        document_count = kwargs.pop("document_count", 0)
        if kwargs:
            raise TypeError(f"Unrecognized keyword arguments: {str(kwargs)}")

        self.word_counts = collections.OrderedDict()
        self.word_docs = collections.defaultdict(int)
        self.filters = filters
        self.split = split
        self.lower = lower
        self.num_words = num_words
        self.document_count = document_count
        self.char_level = char_level
        self.oov_token = oov_token
        self.index_docs = collections.defaultdict(int)
        self.word_index = {}
        self.index_word = {}
        self.analyzer = analyzer

    def fit_on_texts(self, texts):
        for text in texts:
            self.document_count += 1

View on GitHub (pinned to 7a34a03db6)