Skip to content

Fix: Correct variable reference in flatten() function#44751

Closed
LincolnBurrows2017 wants to merge 3 commits intohuggingface:mainfrom
LincolnBurrows2017:fix-empty-list-handling
Closed

Fix: Correct variable reference in flatten() function#44751
LincolnBurrows2017 wants to merge 3 commits intohuggingface:mainfrom
LincolnBurrows2017:fix-empty-list-handling

Conversation

@LincolnBurrows2017
Copy link
Copy Markdown

What does this PR fix?

The flatten() function in tokenization_utils_base.py had a bug where it was checking arr[0] instead of sub_arr when determining if an element should be recursively flattened.

Bug Details

  • File: src/transformers/tokenization_utils_base.py
  • Function: flatten()
  • Issue: isinstance(arr[0], (list, tuple)) should be isinstance(sub_arr, (list, tuple))

Impact

This bug caused a TypeError: object of type int has no len() when processing mixed lists like [[1,2], 3, [4,5]] where non-list elements followed list elements.

Example

# Before fix - fails with TypeError
flatten([[1, 2], 3, [4, 5]])  # TypeError: object of type int has no len()

# After fix - works correctly
flatten([[1, 2], 3, [4, 5]])  # Returns [1, 2, 3, 4, 5]

LincolnBurrows2017 and others added 3 commits March 13, 2026 20:44
- Fixed is_batched() to handle empty lists without IndexError
- Fixed make_flat_list_of_images() to handle empty lists
- Fixed make_nested_list_of_images() to handle empty lists
- Fixed concatenate_list() to handle empty lists
- Added tests for all these cases

These functions were accessing list[0] without checking if the list
was empty, causing IndexError exceptions when empty lists were passed.
The flatten() function was incorrectly checking arr[0] instead of
sub_arr when determining if an element should be recursively flattened.
This caused a TypeError when processing mixed lists like [[1,2], 3, [4,5]]
where non-list elements followed list elements.

Changed isinstance(arr[0], (list, tuple)) to isinstance(sub_arr, (list, tuple))
to correctly check each element during iteration.
@github-actions
Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: doge

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants