{"record":{"id":"814dcddc67c6681c","repo":"PaddlePaddle/PaddleOCR","slug":"recognition-model-session-is-not-initialized","errorCode":null,"errorMessage":"Recognition model session is not initialized.","messagePattern":"Recognition model session is not initialized\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"paddleocr-js/packages/core/src/models/rec.ts","lineNumber":130,"sourceCode":"  });\n  const config = parseRecModelConfigText(configText);\n  const defaultBatchSize = Math.max(1, batchSizeArg ?? 1);\n  let sessionState: SessionState | null = await createRecModelSession(\n    ort,\n    modelBytes,\n    backend,\n    webgpuState\n  );\n\n  return {\n    kind: \"rec\",\n    config,\n    get provider() {\n      return sessionState?.provider || \"\";\n    },\n    async predict(cv, mats, overrides) {\n      if (!sessionState?.session) {\n        throw new Error(\"Recognition model session is not initialized.\");\n      }\n      const batchSize = resolveRuntimeBatchSize(overrides?.batchSize, defaultBatchSize);\n      const ctx = { cv, config };\n      const samples = preprocess(ctx, mats);\n      const charDict = config.charDict;\n      const ordered = samples.slice().sort((a, b) => a.width - b.width);\n      const decoded: Array<{ inputIndex: number; text: string; score: number }> = [];\n      const targetH = config.imageShape[1];\n\n      for (const batch of chunkArray(ordered, batchSize)) {\n        const inputTensor = packRecBatchTensor(ort, batch, targetH);\n        const output = await runInference(sessionState.session, inputTensor);\n        const batchResults = postprocess(output, charDict);\n        for (let index = 0; index < batchResults.length; index += 1) {\n          decoded.push({\n            inputIndex: batch[index].inputIndex,\n            ...batchResults[index]\n          });","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/paddleocr-js/packages/core/src/models/rec.ts#L112-L148","documentation":"Thrown by the recognition model's predict() when the ONNX Runtime session object is missing. Like the detection twin, the rec model object is returned before (or independently of) session creation; predict() guards on sessionState.session and fails if initialization never ran, is still in flight, or errored earlier. The provider getter returning \"\" is the matching symptom.","triggerScenarios":"Calling recModel.predict(...) right after creating the model without awaiting the session load; a failed model fetch or ORT backend init leaving sessionState null; using the model after teardown.","commonSituations":"Missing await on the async load path; parallel init code where predict races the loader; environments where WASM/WebGPU session creation fails silently (wrong MIME type for the .wasm, cross-origin model fetch blocked).","solutions":["Await the rec model load/initialize call before predict()","Treat model.provider === \"\" as not-ready and skip or queue the prediction","Check the network tab / logs for a failed ONNX download or backend creation error that left the session null","Re-run initialization and handle its error explicitly instead of proceeding"],"exampleFix":"// before\nconst rec = createRecModel(...);\nconst texts = await rec.predict(cv, crops); // throws: session not initialized\n\n// after\nconst rec = createRecModel(...);\nawait loadRecModelSession(rec);\nif (!rec.provider) throw new Error(\"rec model failed to load\");\nconst texts = await rec.predict(cv, crops);","handlingStrategy":"validation","validationCode":"if (recModel.provider === \"\") {\n  await ensureRecModelLoaded(recModel); // or your loader equivalent\n}","typeGuard":"function isRecModelReady(m: { provider: string }): boolean {\n  return m.provider !== \"\";\n}","tryCatchPattern":"try { await recModel.predict(cv, crops); } catch (e) {\n  if (e instanceof Error && /session is not initialized/.test(e.message)) {\n    await ensureRecModelLoaded(recModel); // then retry once\n  } else throw e;\n}","preventionTips":["Await model loading before wiring predict into UI events","Check the network tab for failed .onnx/.wasm fetches when the session stays null","Expose and check provider as a readiness flag in application state"],"tags":["paddleocr","onnxruntime","lifecycle","async-init","recognition"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}