Skip to content

Fix tf.tuple() to reject nested structures consistently - #124931

Merged
copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
AnupamKumar-1:fix-tuple-nested-input-124879
Aug 11, 2026
Merged

copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
AnupamKumar-1:fix-tuple-nested-input-124879

Conversation

@AnupamKumar-1

Copy link
Copy Markdown
Contributor

Fixes #124879

tf.tuple() only supports a flat list of tensors (per its docstring), but
eager mode skips validation entirely while graph/XLA mode implicitly stacks
nested elements via convert_to_tensor, causing a confusing Pack shape error.
Both eager and graph modes now raise the same clear TypeError for nested input
via a nest.is_nested() check applied before the eager/graph branch. Flat-list
(documented) usage is unaffected.

Added TupleTest in control_flow_ops_test.py covering nested tuple, nested list,
and dict-element cases in both eager and graph mode.

@google-ml-butler google-ml-butler Bot added the size:XL CL Change Size:Extra Large label Aug 8, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Note

Gemini is unable to generate a review for this pull request due to the file types involved not being currently supported.

@google-ml-butler
google-ml-butler Bot requested a review from cantonios August 8, 2026 16:37
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Aug 8, 2026
@google-cla

google-cla Bot commented Aug 8, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@AnupamKumar-1
AnupamKumar-1 force-pushed the fix-tuple-nested-input-124879 branch from 745dc73 to 86277c2 Compare August 8, 2026 17:05
@nithyak0204
nithyak0204 requested a review from a team August 10, 2026 08:33
@nithyak0204 nithyak0204 added comp:ops OPs related issues prtype:bugfix PR to fix a bug labels Aug 10, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Aug 10, 2026
@AnupamKumar-1
AnupamKumar-1 force-pushed the fix-tuple-nested-input-124879 branch from 86277c2 to 6d79433 Compare August 10, 2026 10:31
@dmiltr3
dmiltr3 self-requested a review August 10, 2026 15:22

@dmiltr3 dmiltr3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review Feedback

1. Missing Test Class Declaration and Indentation in tensorflow/python/ops/control_flow_ops_test.py

In control_flow_ops_test.py, the new test methods are added directly after WithDependenciesTestCase.testListDependencies without a class declaration (class TupleTestCase(test_util.TensorFlowTestCase): or class TupleTest), causing the tests to be appended to WithDependenciesTestCase with 4-space indentation rather than in their own test class with the standard 2-space indentation.

Please define a dedicated test class TupleTestCase (or TupleTest) using 2-space indentation.

2. Remove Unused Variable in testNestedListRaisesTypeError

In testNestedListRaisesTypeError, c = constant_op.constant(3.0) is defined but not used in the test assertion.

3. Recommended Test Suite Structure

Here is the updated test case implementation verifying nested structures as well as valid flat sequences:

class TupleTestCase(test_util.TensorFlowTestCase):

  @test_util.run_in_graph_and_eager_modes
  def testNestedTupleRaisesTypeError(self):
    a = constant_op.constant(1.0)
    b = constant_op.constant(2.0)
    c = constant_op.constant(3.0)
    with self.assertRaises(TypeError):
      control_flow_ops.tuple(((a, b), c))

  @test_util.run_in_graph_and_eager_modes
  def testNestedListRaisesTypeError(self):
    a = constant_op.constant(1.0)
    b = constant_op.constant(2.0)
    with self.assertRaises(TypeError):
      control_flow_ops.tuple([[a, b]])

  @test_util.run_in_graph_and_eager_modes
  def testDictElementRaisesTypeError(self):
    a = constant_op.constant(1.0)
    b = constant_op.constant(2.0)
    with self.assertRaises(TypeError):
      control_flow_ops.tuple([{"x": a}, b])

  @test_util.run_in_graph_and_eager_modes
  def testFlatSequenceSucceeds(self):
    a = constant_op.constant(1.0)
    b = constant_op.constant(2.0)
    res = control_flow_ops.tuple([a, b])
    self.assertLen(res, 2)

@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Reviewer Requested Changes in PR Queue Aug 10, 2026
@AnupamKumar-1
AnupamKumar-1 force-pushed the fix-tuple-nested-input-124879 branch from 6d79433 to fa75856 Compare August 10, 2026 20:15
@AnupamKumar-1

Copy link
Copy Markdown
Contributor Author

@dmiltr3 Addressed all three points:

  1. TupleTestCase is now a proper top-level class, 2-space indented.
  2. Removed the unused c in testNestedListRaisesTypeError.
  3. Matches the recommended structure, including testFlatSequenceSucceeds.

Verified: 4/4 tests pass.

@github-project-automation github-project-automation Bot moved this from Reviewer Requested Changes to Approved by Reviewer in PR Queue Aug 10, 2026
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Aug 10, 2026
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Aug 10, 2026
@nithyak0204 nithyak0204 removed the awaiting review Pull request awaiting review label Aug 11, 2026
@copybara-service
copybara-service Bot merged commit 6eb8b68 into tensorflow:master Aug 11, 2026
17 of 18 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Aug 11, 2026
@AnupamKumar-1
AnupamKumar-1 deleted the fix-tuple-nested-input-124879 branch August 11, 2026 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:ops OPs related issues prtype:bugfix PR to fix a bug ready to pull PR ready for merge process size:XL CL Change Size:Extra Large

Projects

Status: Merged

5 participants