Repository navigation
Fix eager tf.while_loop corrupting shape for single-var loop - #125616
Merged
copybara-service[bot] merged 2 commits intoAug 31, 2026
Merged
copybara-service[bot] merged 2 commits into
copybara-service[bot] merged 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the eager execution path of while_loop to preserve the original container type (list or tuple) of the loop variables when they are packed, and adds a corresponding unit test. The review feedback correctly identifies a potential issue where using isinstance to determine the container type could fail or corrupt structures for subclasses like namedtuple, and suggests checking the exact type instead.
dmiltr3
approved these changes
Aug 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #125538.
tf.while_loop could corrupt the shape of a single-element loop_vars list when the body returned a bare Tensor, e.g. tf.while_loop(cond, lambda x: x + 1, [x]). In eager mode, the Tensor could be incorrectly iterated over between iterations, changing its shape from (1, 1) to (1,) and causing a later indexing error.
Root cause - The eager execution path did not re-pack a bare Tensor body result when called through the v2 tf.while_loop API. As a result, the loop variable lost its original list structure and was implicitly unpacked using Python iteration.
Graph mode and XLA were unaffected because they preserve the structured loop-variable signature during tracing.
Fix - Always re-pack bare Tensor/TensorArray body results using the original top-level container type (list or tuple). This preserves loop-variable structure and prevents shape corruption across iterations.
Malformed body functions now also fail fast with a clear structure-mismatch error.
Testing - Added a regression test covering a single loop variable with a bare-Tensor body.