Skip to content

tf.tuple() with a nested-tuple argument is silently returned unchanged under eager execution, but raises a shape ValueError under tf.function tracing (jit_compile=True and autoclustering) [Recent/StarCoder] #124879

Closed

Description

@MRiffiAslett

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

No

Source

source

TensorFlow version

2.20.0-dev0

Custom code

Yes

OS platform and distribution

Ubuntu 24.04.4 LTS (x86_64)

Mobile device

No response

Python version

3.12.3

Bazel version

No response

GCC/compiler version

gcc 13.3.0

CUDA/cuDNN version

N/A, CPU-only

GPU model and memory

N/A, CPU-only

Current behavior?

Calling tf.tuple(y) where y is a nested Python tuple of Tensors ((x, (y1, y2)), z), not a flat list, behaves differently depending on execution mode.

Under eager execution, no error is raised and the nested structure is returned unchanged:

((<tf.Tensor: shape=(), dtype=float32, numpy=1.0>, (<tf.Tensor: shape=(), dtype=float32, numpy=1.0>, <tf.Tensor: shape=(), dtype=float32, numpy=2.0>)), <tf.Tensor: shape=(), dtype=float32, numpy=0.0>)

tf.tuple doesn't appear to perform its documented grouping/control-dependency behavior here at all — it's an identity pass-through.

Under tf.function tracing — both explicit jit_compile=True and autoclustering (TF_XLA_FLAGS=--tf_xla_auto_jit=2) — the same call raises:

ValueError: Shapes must be equal rank, but are 0 and 1
From merging shape 0 with other shapes. for '{{node tuple/packed}} = Pack[N=2, ...

This happens because tracing attempts to convert the nested structure into a graph node, implicitly packing a scalar-shaped element against a (2,)-shaped element.

I haven't traced tf.tuple's internal implementation to say definitively whether eager's silent pass-through or graph mode's Pack-based conversion is the "correct" behavior for a nested (non-flat) input — flagging the inconsistency, not asserting which side is wrong.

Standalone code to reproduce the issue

import tensorflow as tf

print("tf:", tf.__version__)

def create_tuple(op):
    z = tf.constant(0.0)
    y1 = tf.constant(1.0)
    y2 = tf.constant(2.0)
    t1 = (y1, y2)
    t2 = (op, t1)
    t3 = (t2, z)
    return t3

def compute(x):
    y = create_tuple(x)
    return tf.tuple(y)

x1 = tf.constant(1.0)

print("--- naive (eager) ---")
try:
    print(compute(x1))
except Exception as e:
    print("FAILED:", str(e)[:300])

print("--- xla (jit_compile=True) ---")
try:
    print(tf.function(compute, jit_compile=True)(x1))
except Exception as e:
    print("FAILED:", str(e)[:300])

print("--- autocluster (run with TF_XLA_FLAGS=--tf_xla_auto_jit=2) ---")
try:
    print(tf.function(compute)(x1))
except Exception as e:
    print("FAILED:", str(e)[:300])

Relevant log output

tf    : 2.20.0-dev0+selfbuilt

--- naive (eager) ---
naive out: ((<tf.Tensor: shape=(), dtype=float32, numpy=1.0>, (<tf.Tensor: shape=(), dtype=float32, numpy=1.0>, <tf.Tensor: shape=(), dtype=float32, numpy=2.0>)), <tf.Tensor: shape=(), dtype=float32, numpy=0.0>)

--- xla (jit_compile=True) ---
FAILED: in user code:

    File "repro.py", line 17, in compute  *
        return tf.tuple(y)

    ValueError: Shapes must be equal rank, but are 0 and 1
      From merging shape 0 with other shapes. for '{{node tuple/packed}} = Pack[N=2, T

--- autocluster (TF_XLA_FLAGS=--tf_xla_auto_jit=2) ---
FAILED: in user code:

    File "repro.py", line 17, in compute  *
        return tf.tuple(y)

    ValueError: Shapes must be equal rank, but are 0 and 1
      From merging shape 0 with other shapes. for '{{node tuple/packed}} = Pack[N=2, T

Activity

  1. AnupamKumar-1 commented on Aug 8, 2026

    @AnupamKumar-1
    Contributor

    Saw that this was assigned to someone else after I’d already put up this fix. Happy to close this if they’re actively working on it; otherwise, this is ready for review.

  2. added
    2.20.0
    on Aug 10, 2026
  3. google-ml-butler commented on Aug 11, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

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

Metadata

Metadata

Assignees

Labels

2.20.0comp:xlatype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions