{"record":{"id":"39eb002243be366a","repo":"deeplearning4j/deeplearning4j","slug":"graph-not-initialized","errorCode":null,"errorMessage":"Graph not initialized.","messagePattern":"Graph not initialized\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"nd4j/nd4j-tensorflow/src/main/java/org/nd4j/tensorflow/conversion/graphrunner/GraphRunner.java","lineNumber":253,"sourceCode":"                    currInput = castTensor(currInput, fromDType, inputDataTypes.get(inputOrder.get(i)));\n                    TF_DeleteTensor(oldTensor);\n                }\n\n                ret.put(inputOrder.get(i),currInput);\n            }\n\n        return ret;\n    }\n\n    /**\n     * Run the graph definition with the given inputs\n     * in native tensorflow\n     * @param inputs the inputs to run\n     * @return the outputSchema from the native tensorflow wrapper\n     */\n    public Map<String, TF_Tensor> runTfTensor(Map<String, TF_Tensor> inputs) {\n        if(graph == null) {\n            throw new IllegalStateException(\"Graph not initialized.\");\n        }\n\n\n        if(!inputs.isEmpty() && inputOrder != null && inputs.size() != inputOrder.size()) {\n            throw new IllegalArgumentException(\"Number of inputs specified do not match number of arrays specified.\");\n        }\n\n        if(inputDataTypes == null) {\n            inputDataTypes = new LinkedHashMap<>();\n            if(inputOrder != null)\n                for(int i = 0; i < inputOrder.size(); i++) {\n                    inputDataTypes.put(inputOrder.get(i),TensorDataType.values()[TF_TensorType(inputs.get(inputOrder.get(i)))]);\n                }\n        }\n\n        for(Map.Entry<String, org.bytedeco.tensorflow.TF_Tensor> entry : inputs.entrySet()) {\n            Preconditions.checkNotNull(entry.getValue(),\"Entry \" + entry.getKey() + \" was null!\");\n        }","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/nd4j/nd4j-tensorflow/src/main/java/org/nd4j/tensorflow/conversion/graphrunner/GraphRunner.java#L235-L271","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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')."],"exampleFix":"// before\nGraphRunner runner = new GraphRunner();\nrunner.output(inputs); // IllegalStateException: Graph not initialized.\n// after\nGraphRunner runner = new GraphRunner(Files.readAllBytes(Paths.get(\"frozen_graph.pb\")));\nif (runner.isInitialized()) {\n    Map<String, TF_Tensor> out = runner.output(inputs);\n}","handlingStrategy":"type-guard","validationCode":"if (runner == null || runner.isInitialized() == false) {\n    runner = new GraphRunner(graphBytes); // re-create with valid graph before running\n}","typeGuard":"boolean canRun(GraphRunner r) { return r != null && r.isInitialized(); }","tryCatchPattern":"try {\n    Map<String, TF_Tensor> out = runner.runTfTensor(inputs);\n} catch (IllegalStateException e) {\n    if (\"Graph not initialized.\".equals(e.getMessage())) {\n        runner = new GraphRunner(graphBytes); // re-initialize once\n        return runner.runTfTensor(inputs);\n    }\n    throw e;\n}","preventionTips":["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."],"tags":["tensorflow","graphrunner","initialization","lifecycle"],"backgroundTag":"internal-invariant-violation","analyzedSha":"4c22ac5fe4a8350d05d224e7f4499429f7f69c93","analyzedAt":"2026-09-07T05:27:04.714Z","contentChangedAt":"2026-09-07T05:27:04.714Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}