TypeError: unexpected keyword argument (Fix)

This error happens when you call a function, method, or class constructor with a keyword name that it does not accept.

In Python, a keyword argument is a named argument like name="Sam". If the function expects name but you pass username, Python raises a TypeError.

The good news is that this is usually easy to fix:

  • Use the exact parameter name the function expects
  • Or pass the value without a keyword if positional arguments are allowed
  • Or update your own function definition if needed

Quick fix

def greet(name):
    print("Hello", name)

greet(name="Sam")   # works
# greet(username="Sam")  # TypeError: unexpected keyword argument 'username'

Use the exact parameter name from the function definition or documentation.

What this error means

Python found a keyword argument name that the function does not accept.

This usually means:

  • You passed something like username="Sam"
  • But the function does not have a parameter named username
  • The function may still accept the value in another way, such as a positional argument
  • The traceback usually points to the line where you called the function

If you are new to this topic, it helps to understand how function parameters and arguments in Python work.

A simple example that causes the error

Here is a small example:

def greet(name):
    print("Hello", name)

greet(username="Sam")

Output:

TypeError: greet() got an unexpected keyword argument 'username'

Why this fails:

  • The function defines one parameter: name
  • The call uses username
  • username does not match name

Correct version:

def greet(name):
    print("Hello", name)

greet(name="Sam")

Output:

Hello Sam

Why it happens

This error usually happens for one of these reasons:

  • You used the wrong parameter name
  • You guessed the keyword instead of checking the function definition
  • The function only accepts positional arguments
  • You passed a keyword to a built-in function or method that does not support it
  • You are calling a class or library function with an old or incorrect argument name

How to fix it

Start with the simplest checks:

  • Look at the function definition
  • Compare the accepted parameter names with your function call
  • Fix spelling and capitalization
  • Remove the keyword if positional arguments are allowed
  • Update your own function definition if you want that keyword to be accepted

If you want a deeper explanation of named arguments, see default and keyword arguments explained.

Fix 1: Use the correct parameter name

This is the most common fix.

Compare the function definition and the call side by side:

def greet(name):
    print("Hello", name)

greet(username="Sam")   # wrong

The function accepts name, not username.

Corrected version:

def greet(name):
    print("Hello", name)

greet(name="Sam")   # correct

Important details:

  • Spelling must match exactly
  • Parameter names are case-sensitive
  • Name and name are different

Example with capitalization:

def show_age(age):
    print(age)

# show_age(Age=20)   # wrong
show_age(age=20)     # correct

Fix 2: Pass the value without a keyword

Some functions accept positional arguments, which means you pass values by order instead of by name.

Example:

def greet(name):
    print("Hello", name)

greet("Sam")

Output:

Hello Sam

This is useful when:

  • The function accepts positional arguments
  • You are not sure whether the keyword form is supported
  • You keep the argument order correct

For example, with print() you usually do this:

print("hello")

Not this:

# print(msg="hello")  # TypeError in Python

Be careful: positional arguments must be in the correct order.

Fix 3: Change the function definition

Only do this if the function is your own code.

If you wrote the function and want it to accept username, you can rename the parameter:

def greet(username):
    print("Hello", username)

greet(username="Sam")

Or add another parameter if the function should support more values:

def greet(name, title=None):
    if title is None:
        print("Hello", name)
    else:
        print("Hello", title, name)

greet(name="Sam")
greet(name="Sam", title="Mr.")

Output:

Hello Sam
Hello Mr. Sam

Do not change parameter names carelessly if other parts of your program already depend on them.

If you are still learning to write functions, see how to create a simple function in Python.

Fix 4: Accept extra keyword arguments with **kwargs

Use **kwargs when your own function should accept extra named arguments.

Beginner-friendly explanation:

  • **kwargs collects extra keyword arguments into a dictionary
  • That means your function will not crash just because it receives an extra named value
  • But this can also hide mistakes if you use it too freely

Example:

def greet(name, **kwargs):
    print("Hello", name)
    print("Extra values:", kwargs)

greet(name="Sam", age=20, city="Boston")

Output:

Hello Sam
Extra values: {'age': 20, 'city': 'Boston'}

You can also check for expected keys:

def greet(name, **kwargs):
    print("Hello", name)

    if "title" in kwargs:
        print("Title:", kwargs["title"])

greet(name="Sam", title="Dr.")

Output:

Hello Sam
Title: Dr.

Use **kwargs only when flexibility is helpful. If you want strict parameter checking, regular parameters are usually better.

Methods and class constructors

The same error can happen with methods and class constructors.

Example with a class:

class Person:
    def __init__(self, name):
        self.name = name

# person = Person(name="Sam", age=20)  # TypeError
person = Person(name="Sam")
print(person.name)

Output:

Sam

Why the error happens:

  • __init__ accepts only name
  • The call also passes age
  • Python does not know what to do with age

This is common when using library classes, because their constructor argument names are often strict.

How to debug this error step by step

When you see this error, follow these steps:

  1. Read the function call shown in the traceback
  2. Find the keyword named in the error message
  3. Find the function, method, or class definition
  4. Compare the accepted parameters with the keyword you passed
  5. Check for spelling mistakes and wrong capitalization
  6. Look at the documentation if needed

Useful tools:

help(function_name)
print(function_name.__doc__)
print(type(obj))
dir(obj)

Example:

def greet(name):
    """Print a greeting."""
    print("Hello", name)

help(greet)
print(greet.__doc__)

If you want to learn more about help(), see Python help() function explained.

Common beginner cases

These are very common situations where beginners see this error:

  • Using msg= with print() instead of just passing a normal value
  • Using the wrong keyword with list, string, or dictionary methods
  • Passing a keyword to your own function after renaming its parameters
  • Copying example code from a different version of a library

Another common related error is giving too few arguments. If that happens, see TypeError: missing required positional argument.

Common causes

Here are the most common causes in one list:

  • Wrong parameter name in the function call
  • Misspelled keyword argument
  • Wrong capitalization in the argument name
  • Passing a keyword to a function that only accepts positional arguments
  • Using old documentation or code from another version of a library
  • Calling a class constructor with unsupported named arguments

FAQ

What is a keyword argument in Python?

It is an argument passed by name, like age=20, instead of only by position.

Can I fix this by changing the function call only?

Usually yes. The most common fix is to use the correct parameter name or pass the value positionally.

When should I use **kwargs?

Use it when your own function should accept flexible extra named arguments. Avoid it if you want strict parameter checking.

Why does this happen with library code?

Libraries often require exact argument names. A typo or version difference can cause this error.

See also