From 37d4a814b78e520addfb3e4dd5ecea9629eb21e1 Mon Sep 17 00:00:00 2001 From: Lukas Geiger Date: Wed, 6 May 2026 14:27:29 +0100 Subject: [PATCH 1/2] Cache `merged_typed_dict` to not break `validate_typed_dict` caching --- src/transformers/processing_utils.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/transformers/processing_utils.py b/src/transformers/processing_utils.py index bb1344a43dcf..1b11ed10aff3 100644 --- a/src/transformers/processing_utils.py +++ b/src/transformers/processing_utils.py @@ -17,6 +17,7 @@ import bisect import copy +import functools import inspect import json import os @@ -571,6 +572,15 @@ def __getitem__(self, key): raise AttributeError(f"{self.__class__.__name__} has no attribute {key}") +@functools.lru_cache +def _merge_typed_dict(preprocessor_typed_dict: type, modality_typed_dict: type) -> type: + return TypedDict( + "merged_typed_dict", + {**preprocessor_typed_dict.__annotations__, **modality_typed_dict.__annotations__}, + total=False, + ) + + class ProcessorMixin(PushToHubMixin): """ This is a mixin used to provide saving/loading functionality for all processor classes. @@ -1369,11 +1379,7 @@ class MyProcessingKwargs(ProcessingKwargs, CommonKwargs, TextKwargs, ImagesKwarg if preprocessor is None or getattr(preprocessor, "valid_kwargs", None) is None: continue preprocessor_typed_dict_obj = getattr(preprocessor, "valid_kwargs") - typed_dict_obj = TypedDict( - "merged_typed_dict", - {**preprocessor_typed_dict_obj.__annotations__, **typed_dict_obj.__annotations__}, - total=False, - ) + typed_dict_obj = _merge_typed_dict(preprocessor_typed_dict_obj, typed_dict_obj) validate_typed_dict(typed_dict_obj, output_kwargs[key]) return output_kwargs From 02c53903ed493fefb7672d03905b5837e077519b Mon Sep 17 00:00:00 2001 From: Lukas Geiger Date: Thu, 7 May 2026 10:51:52 +0100 Subject: [PATCH 2/2] Set maxsize=8 --- src/transformers/processing_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/processing_utils.py b/src/transformers/processing_utils.py index 1b11ed10aff3..31360883dbf3 100644 --- a/src/transformers/processing_utils.py +++ b/src/transformers/processing_utils.py @@ -572,7 +572,7 @@ def __getitem__(self, key): raise AttributeError(f"{self.__class__.__name__} has no attribute {key}") -@functools.lru_cache +@functools.lru_cache(maxsize=8) def _merge_typed_dict(preprocessor_typed_dict: type, modality_typed_dict: type) -> type: return TypedDict( "merged_typed_dict",