Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(processing): Filter kwargs in ProcessorMixin call to prevent Type…#41606

Open
CodersAcademy006 wants to merge 1 commit into
huggingface:mainfrom
CodersAcademy006:fix-processor-kwargs-bug
Open

fix(processing): Filter kwargs in ProcessorMixin call to prevent Type…#41606
CodersAcademy006 wants to merge 1 commit into
huggingface:mainfrom
CodersAcademy006:fix-processor-kwargs-bug

Conversation

@CodersAcademy006

Copy link
Copy Markdown

This PR fixes an issue where multimodal processors could raise a TypeError when called with arguments specific to one modality. The ProcessorMixin.__call__ method was passing all keyword arguments to each sub-processor (tokenizer, feature extractor, etc.), causing a crash if a processor received an argument it didn't recognize (e.g., EncodecFeatureExtractor receiving pad_to_multiple_of from the tokenizer).

This fix resolves the problem by filtering the keyword arguments before they are passed to each sub-processor. It uses Python's inspect module to get the valid parameters for each processor's __call__ method and only passes the arguments that match. This prevents TypeErrors and makes the processor logic more robust.

Fixes #41598

@Rocketknight1

Copy link
Copy Markdown
Member

cc @zucchini-nlp! I'm not sure about this because some of the submodules may just have **kwargs

Comment on lines 628 to -629
input_data, input_kwargs = attribute_to_kwargs[attribute_name]
if input_data is not None and attribute is not None:
attribute_output = attribute(input_data, **kwargs[input_kwargs])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this line kwargs are filtered already and we are obtaining kwargs[input_kwargs]. That should work for all models if if there's a TypeError anywhere, I'd suggest to see what has gone wrong with specific model

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The model where this issue is occurring is CSM-1B, so it might be related to how that particular architecture handles its input kwargs.

…cit signatures

`_merge_kwargs` routes kwargs into per-modality buckets based on TypedDict
membership. Several kwargs (e.g. `pad_to_multiple_of`) are declared in more
than one modality TypedDict (TextKwargs and AudioKwargs), so they land in
every matching bucket. When a concrete sub-processor has an explicit `__call__`
signature without a **kwargs catch-all it then receives unexpected keyword
arguments and raises a TypeError.

Fix: before calling each sub-processor, inspect its `__call__` signature.
- If a VAR_KEYWORD parameter exists the processor already handles unknown keys
  itself, so all modality kwargs are forwarded unchanged.
- Otherwise, filter modality kwargs to the set of params the sub-processor
  explicitly accepts.

Adds two regression tests in ProcessorMixinKwargsFilterTest that reproduce
the failure with a strict-signature feature extractor and confirm the
**kwargs path continues to work unchanged.

Fixes huggingface#41598
@Qodo-Free-For-OSS

Copy link
Copy Markdown

Hi, ProcessorMixin.__call__ unconditionally calls inspect.signature(attribute.__call__), which can raise TypeError/ValueError for some callable implementations and would crash processing before invoking the sub-processor.

Severity: remediation recommended | Category: reliability

How to fix: Add signature() fallback path

Agent prompt to fix - you can give this to your LLM of choice:

Issue description

ProcessorMixin.__call__ now relies on inspect.signature(attribute.__call__) to decide whether to filter kwargs. If inspect.signature() raises for a particular callable implementation, processing crashes before the sub-processor is called.

Issue Context

Some sub-processors may be dynamically loaded or implemented in ways that are not inspectable.

Fix Focus Areas

  • src/transformers/processing_utils.py[689-703]

Suggested approach

  • Wrap inspect.signature(attribute.__call__) in try/except (TypeError, ValueError).
  • On exception, fall back to a safe behavior. Recommended fallback:
    • Attempt calling the sub-processor with full modality_kwargs (preserves current behavior for processors that tolerate extras via internal handling).
    • If you want to preserve the original goal of avoiding TypeError, alternatively fall back to filtering using a conservative allowlist (e.g., empty filter) only if that won’t break required args.
  • Add/adjust a unit test that simulates an uninspectable __call__ (e.g., a callable object whose __call__ is a built-in or raises when inspected) and assert the processor call does not crash.

Found by Qodo code review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EncodecFeatureExtractor.__call__() got an unexpected keyword argument 'pad_to_multiple_of'

6 participants