deeplearning4j/deeplearning4j · error · UnsupportedOperationException
Only static Conditions are supported
Error message
Only static Conditions are supported
What it means
BooleanIndexing.and() only works with conditions extending BaseCondition (the framework's built-in condition types like Conditions.equals/greaterThan). The thrown check is `!(condition instanceof BaseCondition)`, because the op is delegated to MatchCondition, which can only interpret framework conditions, not arbitrary user implementations of the Condition interface. If you pass a custom or anonymous Condition that doesn't extend BaseCondition, the call fails immediately with this UnsupportedOperationException.
Solutions
- Use one of the factory methods in org.nd4j.linalg.factory.Conditions (e.g. Conditions.equals(v), Conditions.greaterThan(v), Conditions.lessThanOrEqual(v)) instead of a hand-written Condition.
- If custom logic is required, extend org.nd4j.linalg.conditions.BaseCondition (implementing apply/getValue) rather than implementing the Condition interface directly.
- If you only need a boolean per slice without the Condition machinery, compute it manually with Nd4j.getExecutioner().exec(new MatchCondition(...)) on a supported condition, or iterate with n.slice(d) and ElementWiseVariableOp-free plain checks.
Example fix
// before
Condition cond = new Condition() {
public boolean apply(Number input) { return input.doubleValue() > 0.5; }
};
boolean[] r = BooleanIndexing.and(arr, cond, 1);
// after
Condition cond = Conditions.greaterThan(0.5);
boolean[] r = BooleanIndexing.and(arr, cond, 1); Defensive patterns
Strategy: type-guard
Validate before calling
if (!(condition instanceof BaseCondition)) {
throw new IllegalArgumentException("condition must extend BaseCondition; use org.nd4j.linalg.factory.Conditions factories");
} Type guard
boolean isSupported(Condition c) {
return c instanceof BaseCondition;
} Try / catch
try {
result = BooleanIndexing.and(n, condition, dimension);
} catch (UnsupportedOperationException e) {
// condition was not a BaseCondition; rebuild it via Conditions factory or extend BaseCondition
} Prevention
- Always build conditions through org.nd4j.linalg.factory.Conditions factory methods.
- When writing custom predicates, extend BaseCondition, never implement the Condition interface directly.
- Add a unit test asserting your condition instances are instanceof BaseCondition.
When it happens
Trigger: Calling BooleanIndexing.and(n, condition, dimension...) where `condition` is a custom class implementing org.nd4j.linalg.api.ndarray.conditions.Condition directly (not extending BaseCondition), e.g. an anonymous `new Condition() {...}` or a lambda-like hand-rolled implementation.
Common situations: Developers implementing custom predicate logic (e.g. comparing to a moving threshold) as a raw Condition; copying older ND4J tutorial code where Condition was implemented directly; IDE auto-complete offering to implement the Condition interface instead of extending BaseCondition.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- 2D input is expected
- Append loading not supported for DataType
- Backprop not supported
- Backprop of gradient op not supported
- calculateOutputShape() is not supported for control flow…
AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07).
Data as JSON: /api/errors/3539a58802d305b6.
Report an issue: GitHub.
Appendix: source
Thrown at nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/indexing/BooleanIndexing.java:79
else
return false;
} else {
throw new RuntimeException("Can only execute BaseCondition conditions using this method");
}
}
/**
* And over the whole ndarray given some condition, with respect to dimensions
*
* @param n the ndarray to test
* @param condition the condition to test against
* @return true if all of the elements meet the specified
* condition false otherwise
*/
public static boolean[] and(final INDArray n, final Condition condition, long... dimension) {
if (!(condition instanceof BaseCondition))
throw new UnsupportedOperationException("Only static Conditions are supported");
MatchCondition op = new MatchCondition(n, condition, dimension);
INDArray arr = Nd4j.getExecutioner().exec(op);
boolean[] result = new boolean[(int) arr.length()];
long tadLength = Shape.getTADLength(n.shape(), dimension);
for (int i = 0; i < arr.length(); i++) {
if (arr.getDouble(i) == tadLength)
result[i] = true;
else
result[i] = false;
}
return result;
}
View on GitHub (pinned to 4c22ac5fe4)