From 1f0dded390bf8156b1aad577d2ae1d85cab6b624 Mon Sep 17 00:00:00 2001 From: yonigozlan Date: Mon, 23 Feb 2026 21:44:15 +0000 Subject: [PATCH 1/2] Improve bc with remote code in image_processing_base --- src/transformers/image_processing_base.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/transformers/image_processing_base.py b/src/transformers/image_processing_base.py index f59fd41f7b85..583490dbe688 100644 --- a/src/transformers/image_processing_base.py +++ b/src/transformers/image_processing_base.py @@ -369,10 +369,16 @@ def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): image_processor_dict.update({k: v for k, v in kwargs.items() if k in cls.valid_kwargs.__annotations__}) image_processor = cls(**image_processor_dict) - # Remove kwargs that are used to initialize the image processor attributes - for key in list(kwargs): - if hasattr(image_processor, key): - kwargs.pop(key) + # Apply extra kwargs to instance (BC for remote code, e.g. phi4_multimodal) + to_remove = [] + for key, value in kwargs.items(): + # Set on instance if attribute exists and not in valid_kwargs + if hasattr(image_processor, key) and key not in cls.valid_kwargs.__annotations__: + setattr(image_processor, key, value) + to_remove.append(key) + # Don't return consumed kwargs as unused when return_unused_kwargs=True + for key in to_remove: + kwargs.pop(key, None) logger.info(f"Image processor {image_processor}") if return_unused_kwargs: From c32639653661e7f9b8ef2fe975552e1500e96cad Mon Sep 17 00:00:00 2001 From: yonigozlan Date: Tue, 24 Feb 2026 14:48:33 +0000 Subject: [PATCH 2/2] add warning and fancy reversed loop on kwargs --- src/transformers/image_processing_base.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/transformers/image_processing_base.py b/src/transformers/image_processing_base.py index 583490dbe688..72db8fcc9bec 100644 --- a/src/transformers/image_processing_base.py +++ b/src/transformers/image_processing_base.py @@ -370,15 +370,17 @@ def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): image_processor = cls(**image_processor_dict) # Apply extra kwargs to instance (BC for remote code, e.g. phi4_multimodal) - to_remove = [] - for key, value in kwargs.items(): - # Set on instance if attribute exists and not in valid_kwargs + extra_keys = [] + for key in reversed(list(kwargs.keys())): if hasattr(image_processor, key) and key not in cls.valid_kwargs.__annotations__: - setattr(image_processor, key, value) - to_remove.append(key) - # Don't return consumed kwargs as unused when return_unused_kwargs=True - for key in to_remove: - kwargs.pop(key, None) + setattr(image_processor, key, kwargs.pop(key, None)) + extra_keys.append(key) + if extra_keys: + logger.warning_once( + f"Image processor {cls.__name__}: kwargs {extra_keys} were applied for backward compatibility. " + f"To avoid this warning, add them to valid_kwargs: create a custom TypedDict extending " + f"ImagesKwargs with these keys and set it as the `valid_kwargs` class attribute." + ) logger.info(f"Image processor {image_processor}") if return_unused_kwargs: