The message “is an invalid keyword argument for this function” is a Python TypeError raised when you pass a keyword argument that the called function does not accept—most often a misspelled parameter name, an argument that belongs to a different function, a parameter removed or renamed in a newer library version, or a keyword used where the callable only takes positional arguments. The wording itself is stable across recent Python 3 versions: the full text usually reads like TypeError: some_function() got an unexpected keyword argument 'foo', and in some contexts—notably functools.partial, certain C-implemented builtins, and some framework wrappers—it appears as TypeError: 'foo' is an invalid keyword argument for this function. Both point to the same underlying mismatch: the name you used does not exist in that callable’s signature.

Because the exact phrasing varies by Python version, callable type, and library, the first thing to establish is which form you actually saw and which call produced it. The traceback names the file and line; that line, plus the function’s documented signature, is what determines the fix.

Where the two phrasings come from

Python’s own argument binding, implemented in CPython’s ceval.c, produces messages of the form %s() got an unexpected keyword argument '%s' for ordinary Python functions and most builtins. The variant '%s' is an invalid keyword argument for this function historically appears for certain built-in and C-level callables, and it is also the message you get from functools.partial objects in some situations. The distinction matters only diagnostically: both mean the keyword name is not in the accepted set.

A minimal reproduction of the common form:

The typo nam is not a parameter of greet. Python does not silently ignore unknown keywords (unlike, say, **kwargs-accepting functions), so the call fails before the function body runs.

The four usual causes

1: A misspelled or wrong parameter name

The most common cause by far. Parameter names are case-sensitive and must match the definition exactly. user_id is not userId, userid, or user-ID. If you are calling a library function, copy the name from the signature in the docs or from inspect.signature.

inspect.signature works for pure-Python callables and many wrapped ones; it can fail or give a generic signature for some C extensions, in which case the documentation or help() output is the fallback.

2: The argument belongs to a different function or version

A frequent real-world case: you upgrade a library and a parameter is renamed or removed. For example, older code that passed sort=True to some function may break when a later release changes the name or drops the option. The error is not a bug in your environment; it is a signal that your call no longer matches the installed API. Check the version you have installed (pip show package, or package.version) against the documentation for that version, not the latest docs.

3: Passing a keyword to a positional-only parameter

Since Python 3.8, function definitions can mark parameters as positional-only with /:

Note this produces a different message (“passed as keyword arguments”), because the name exists but is not usable as a keyword. Many builtins are effectively positional-only and raise the “invalid keyword argument” style when you try to name their arguments—for instance, naming parameters of some C functions whose Python-visible signature does not expose names. If the parameter is documented as positional, pass it positionally.

4: A framework or wrapper that forwards keywords strictly

Decorators and wrappers can change the effective signature. A wrapper defined as def wrapper(*args, **kwargs) will accept anything and then fail inside when it forwards to the real function. Frameworks that introspect signatures—ORMs, web request handlers, dependency-injection systems, CLI parsers, plotting libraries—may reject a keyword before your code runs because the framework’s own signature for the callback does not include it. In these cases the traceback often points into library code, and the fix is to match the framework’s expected parameter names, which may differ from what you would name the argument yourself.

Diagnosing efficiently

Work from the traceback outward. The last frame that is your code is the call to inspect; the frames above it tell you which function rejected the keyword.

1: Read the exact name in the error. It is quoted; compare it character by character with the signature. Watch for trailing underscores (class_, id_, type_) used to avoid shadowing builtins—these are easy to omit. 2: Print the signature. Use inspect.signature(func) for Python callables, or help(func) and the official docs for others. 3: Check the installed version against the docs for that version. 4: Distinguish “unexpected keyword” from “positional-only.” The messages differ, and the fixes differ: rename/remove the keyword in the first case; pass positionally in the second. 5: Look for wrappers. If the failing frame is inside a decorator, framework, or functools.partial, investigate the wrapper’s accepted keywords rather than the innermost function.

Distinctions that prevent wrong fixes

“Unexpected keyword” versus “missing required argument.” These are opposite errors. got an unexpected keyword argument means you supplied a name that does not exist. missing 1 required positional argument means you omitted one that does. Adding **kwargs to silence the first can hide real bugs and is rarely the right fix for library calls; it is sometimes acceptable in your own flexible APIs, but only when you genuinely intend to accept arbitrary keywords, and you should still validate them.

kwargs absorbs everything, including typos. A function defined with kwargs will never raise this error for an unknown name—it will receive the typo silently. That is why this error is a good signal: it means the target function has a definite, checkable signature. If you are debugging a call that “should” fail but does not, check whether the callee uses **kwargs and is swallowing a misspelled key.

Keyword-only parameters. Parameters after * in a definition must be passed by keyword. Forgetting this produces a different error (missing required keyword-only argument), but the inverse mistake—passing a keyword-only parameter’s name to a function that does not define it—produces the error in question. Know which parameters are keyword-only by reading the signature.

Bound methods and self. When calling a bound method, do not pass self; it is supplied automatically. Passing self=... explicitly to a bound method is a classic source of “unexpected keyword argument 'self'.” On the other hand, calling an unbound function from a class with obj.method(...) versus Class.method(obj, ...) changes what is positional, not what keywords are valid.

Examples that clarify the boundary

The last call fails because bcc was never defined. The fix is either to add bcc to the signature (if you control it) or to stop passing it. Adding **kwargs to send_email would make the call succeed but would silently discard bcc unless you also handle it—a behavior change, not a fix.

A version-drift counterexample: code written against an older release might pass an option like delimiter=',' to a function that later renamed that parameter. On the old version the call works; on the new one it raises the unexpected-keyword error. The correct response is to read the changelog for the installed version and update the call, not to pin the old version indefinitely unless you have a compatibility reason.

When the answer stops applying

This explanation covers Python’s keyword-argument binding and the standard error text. It does not cover other languages’ messages that happen to use similar words, nor does it cover runtime errors raised inside a function that validates its own **kwargs and then raises a custom exception with similar wording—those are application-defined and must be read from that code. It also does not determine which specific library or version you are using; without the traceback, the exact function, and the installed version, only the general principle is answerable, and the precise corrected call must be checked against that function’s signature for your version.

Practical checklist

Copy the offending keyword from the error message exactly. Obtain the callable’s signature (inspect.signature, help, or version-matched docs). Confirm spelling, case, and any trailing underscore. Confirm the installed version and whether the parameter was renamed or removed. Check whether the parameter is positional-only (pass positionally) or keyword-only (must be named). If a decorator, functools.partial, or framework wraps the call, inspect the wrapper’s accepted keywords. Fix the call to match the signature; avoid blanket **kwargs unless the API is intentionally permissive and you validate keys.