deeplearning4j/deeplearning4j · error · IllegalStateException
Graph not initialized.
Error message
Graph not initialized.
What it means
GraphRunner.runTfTensor(Map<String,TF_Tensor>) requires the native TF_Graph to have been initialized (via a constructor that loaded the graph or a prior init call). If the internal graph field is null it throws IllegalStateException("Graph not initialized."). This is a lifecycle misuse: run was called before the runner was initialized, or initialization failed silently/through a different path.
Solutions
- Ensure the GraphRunner is constructed with a valid graph (bytes, file, or SavedModelConfig) and the constructor completed without exception.
- Initialize explicitly before running if your code path defers loading (call the init path first).
- Handle constructor failures: if init threw, do not reuse the instance — create a new GraphRunner.
- Add an isInitialized()/graph != null check before calling runTfTensor in application code.
- Log/inspect earlier stack traces: the null graph usually traces back to a failed or skipped load (see 'Unable to parse protobuf').
Example fix
// before
GraphRunner runner = new GraphRunner();
runner.output(inputs); // IllegalStateException: Graph not initialized.
// after
GraphRunner runner = new GraphRunner(Files.readAllBytes(Paths.get("frozen_graph.pb")));
if (runner.isInitialized()) {
Map<String, TF_Tensor> out = runner.output(inputs);
} Defensive patterns
Strategy: type-guard
Validate before calling
if (runner == null || runner.isInitialized() == false) {
runner = new GraphRunner(graphBytes); // re-create with valid graph before running
} Type guard
boolean canRun(GraphRunner r) { return r != null && r.isInitialized(); } Try / catch
try {
Map<String, TF_Tensor> out = runner.runTfTensor(inputs);
} catch (IllegalStateException e) {
if ("Graph not initialized.".equals(e.getMessage())) {
runner = new GraphRunner(graphBytes); // re-initialize once
return runner.runTfTensor(inputs);
}
throw e;
} Prevention
- Construct GraphRunner with a valid graph source; never run on a default/uninitialized instance.
- Check initialization (graph != null / isInitialized()) before the first inference call.
- If the constructor throws, discard the instance — it is not recoverable.
- Initialize once at startup and share the runner rather than lazily constructing per request.
When it happens
Trigger: Calling runTfTensor (directly or via output()/outputTensors()) on a GraphRunner built through a path that never set graph — e.g., default-constructed then run before initialization, or after a failed constructor left the object in an unusable state.
Common situations: Constructing GraphRunner with a graph-loading constructor that was given an empty/absent graph source; running inference on a runner whose init threw and was swallowed; using the runner before async/deferred initialization completes.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- Illegal output found
- Number of inputs specified do not match number of arrays…
- Unable to parse protobuf
- Assertion failed: expected workspace for array type " +…
- AtomicAllocator is NULL
AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07).
Data as JSON: /api/errors/39eb002243be366a.
Report an issue: GitHub.
Appendix: source
Thrown at nd4j/nd4j-tensorflow/src/main/java/org/nd4j/tensorflow/conversion/graphrunner/GraphRunner.java:253
currInput = castTensor(currInput, fromDType, inputDataTypes.get(inputOrder.get(i)));
TF_DeleteTensor(oldTensor);
}
ret.put(inputOrder.get(i),currInput);
}
return ret;
}
/**
* Run the graph definition with the given inputs
* in native tensorflow
* @param inputs the inputs to run
* @return the outputSchema from the native tensorflow wrapper
*/
public Map<String, TF_Tensor> runTfTensor(Map<String, TF_Tensor> inputs) {
if(graph == null) {
throw new IllegalStateException("Graph not initialized.");
}
if(!inputs.isEmpty() && inputOrder != null && inputs.size() != inputOrder.size()) {
throw new IllegalArgumentException("Number of inputs specified do not match number of arrays specified.");
}
if(inputDataTypes == null) {
inputDataTypes = new LinkedHashMap<>();
if(inputOrder != null)
for(int i = 0; i < inputOrder.size(); i++) {
inputDataTypes.put(inputOrder.get(i),TensorDataType.values()[TF_TensorType(inputs.get(inputOrder.get(i)))]);
}
}
for(Map.Entry<String, org.bytedeco.tensorflow.TF_Tensor> entry : inputs.entrySet()) {
Preconditions.checkNotNull(entry.getValue(),"Entry " + entry.getKey() + " was null!");
}View on GitHub (pinned to 4c22ac5fe4)