set_model missing 1 required positional argument 'model'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The error TypeError: set_model() missing 1 required positional argument: 'model' occurs when you call an instance method without an instance, or when you pass one fewer argument than the method signature expects. In Python, instance methods automatically receive self as the first argument when called on an object. If you call the method on the class itself (e.g., MyClass.set_model(m) instead of instance.set_model(m)), Python does not inject self, so your model argument fills the self slot and the actual model parameter is left empty. This error commonly appears with Keras/scikit-learn wrappers, ORMs, and any code using a set_model method.
The Root Cause
When you call Trainer.set_model(my_model), Python treats my_model as self and has nothing left for model. The fix is to call it on an instance.
Keras Callback Example
Keras internally calls callback.set_model(self) on each callback. If you pass the class CustomCallback instead of an instance CustomCallback(), Keras tries to call set_model as an unbound method, causing the error.
Scikit-Learn Wrapper Example
Inheritance and super() Issues
When calling a parent class method directly (without super()), you must pass self explicitly. Using super() is cleaner and handles MRO correctly.
Static Method vs Instance Method
If set_model does not need instance state, consider making it a @staticmethod or @classmethod.
Debugging the Error
Common Pitfalls
- Missing parentheses during instantiation: Writing
callback = MyCallbackassigns the class itself, not an instance. Always useMyCallback()with parentheses. This is the most common cause of the error, especially in Keras callback lists. - Calling parent methods without self:
BaseClass.method(arg)treatsargasself. Usesuper().method(arg)orBaseClass.method(self, arg)to pass bothselfand the argument correctly. - Confusing class and instance in factory functions: A factory that returns
MyClassinstead ofMyClass()silently gives you a class reference. Downstream method calls then fail because there is no instance to bindselfto. - Decorating with @staticmethod when self is needed: If you accidentally add
@staticmethodto a method that accessesself.model, the first argument is no longerself— it becomes themodelparameter. Remove the decorator or adjust the method signature. - Passing class references in configuration dictionaries: Frameworks like Keras accept callbacks as a list of instances. Passing
[CustomCallback](class) instead of[CustomCallback()](instance) causes the framework's internalset_model()call to fail with this exact error.
Summary
- The error means Python received one fewer argument than expected because
selfwas not automatically provided - Most common cause: calling an instance method on a class instead of an instance (missing
()during instantiation) - Use
super().method(arg)instead ofParentClass.method(arg)in subclasses - Check
type(obj)orinspect.isclass(obj)to verify you have an instance, not a class - In Keras, always pass callback instances (
MyCallback()) not classes (MyCallback) in the callbacks list

