keras-team/keras · error · ValueError

Graph disconnected: cannot find parent for tensor {x} at ope

Error message

Graph disconnected: cannot find parent for tensor {x} at operation '{operation}'. The following previous operations were accessed without issue: {operations_with_complete_input}

What it means

When building a keras.ops.Function, map_graph walks the symbolic graph and requires every input tensor of every node to be producible from the function's declared inputs or constants. If a node consumes a tensor that was never connected to the provided inputs, the graph is disconnected and this ValueError names the orphan tensor and its operation.

Source

Thrown at keras/src/ops/function.py:368

    # Get sorted list of node depths.
    depth_keys = list(nodes_by_depth.keys())
    depth_keys.sort(reverse=True)

    # Check that all tensors required are computable.
    # computable_tensors: all tensors in the graph
    # that can be computed from the inputs provided.
    computable_tensors = set()
    for x in inputs:
        computable_tensors.add(x)

    operations_with_complete_input = []  # To provide a better error msg.
    for depth in depth_keys:
        for node in nodes_by_depth[depth]:
            for x in tree.flatten(node.input_tensors):
                if x not in computable_tensors:
                    operation = node.operation
                    raise ValueError(
                        "Graph disconnected: cannot find parent for "
                        f"tensor {x} at operation '{operation}'. "
                        "The following previous operations were accessed "
                        f"without issue: {operations_with_complete_input}"
                    )
                operations_with_complete_input.append(node.operation.name)

            for x in tree.flatten(node.outputs):
                computable_tensors.add(x)

    # Ensure name unicity, which will be crucial for serialization
    # (since serialized nodes refer to operations by their name).
    all_names = [operation.name for operation in operations]
    for name in all_names:
        if all_names.count(name) != 1:
            raise ValueError(
                f'The name "{name}" is used {all_names.count(name)} '
                "times in the model. All operation names should be unique."

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Trace every tensor listed in outputs back to the declared inputs and fix the branch that starts from an unrelated KerasTensor
  2. Pass all required source tensors in the inputs list
  3. If the orphan tensor comes from another model, re-declare it as an explicit input (keras.Input) or rebuild that part of the graph on top of your inputs

Example fix

# before
x = keras.Input((28,28,1))
y = some_other_model_output  # unrelated KerasTensor
out = layers.Add()([x, y])
fn = keras.ops.Function(x, out)  # Graph disconnected

# after
x2 = keras.Input((28,28,1))
y2 = layers.Add()([x, x2])
fn = keras.ops.Function([x, x2], y2)
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Creating keras.ops.Function(inputs, outputs) where an output depends on a KerasTensor that is not downstream of any input in inputs — typically an intermediate tensor from another model captured by mistake, or using a layer's output instead of the layer call on the input.

Common situations: Refactoring functional code and passing the wrong tensor into outputs; mixing shared layers across models so an output references another model's tensor; extracting intermediate outputs with the wrong variable; copy-paste wiring mistakes in multi-branch models.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/7fee0e8f4f11a6f7. Report an issue: GitHub.