PaddlePaddle/PaddleOCR · critical
Recognition model session is not initialized.
Error message
Recognition model session is not initialized.
What it means
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.
Source
Thrown at paddleocr-js/packages/core/src/models/rec.ts:130
});
const config = parseRecModelConfigText(configText);
const defaultBatchSize = Math.max(1, batchSizeArg ?? 1);
let sessionState: SessionState | null = await createRecModelSession(
ort,
modelBytes,
backend,
webgpuState
);
return {
kind: "rec",
config,
get provider() {
return sessionState?.provider || "";
},
async predict(cv, mats, overrides) {
if (!sessionState?.session) {
throw new Error("Recognition model session is not initialized.");
}
const batchSize = resolveRuntimeBatchSize(overrides?.batchSize, defaultBatchSize);
const ctx = { cv, config };
const samples = preprocess(ctx, mats);
const charDict = config.charDict;
const ordered = samples.slice().sort((a, b) => a.width - b.width);
const decoded: Array<{ inputIndex: number; text: string; score: number }> = [];
const targetH = config.imageShape[1];
for (const batch of chunkArray(ordered, batchSize)) {
const inputTensor = packRecBatchTensor(ort, batch, targetH);
const output = await runInference(sessionState.session, inputTensor);
const batchResults = postprocess(output, charDict);
for (let index = 0; index < batchResults.length; index += 1) {
decoded.push({
inputIndex: batch[index].inputIndex,
...batchResults[index]
});View on GitHub (pinned to 2661c7c0ef)
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
Example fix
// before
const rec = createRecModel(...);
const texts = await rec.predict(cv, crops); // throws: session not initialized
// after
const rec = createRecModel(...);
await loadRecModelSession(rec);
if (!rec.provider) throw new Error("rec model failed to load");
const texts = await rec.predict(cv, crops); Defensive patterns
Strategy: validation
Validate before calling
if (recModel.provider === "") {
await ensureRecModelLoaded(recModel); // or your loader equivalent
} Type guard
function isRecModelReady(m: { provider: string }): boolean {
return m.provider !== "";
} Try / catch
try { await recModel.predict(cv, crops); } catch (e) {
if (e instanceof Error && /session is not initialized/.test(e.message)) {
await ensureRecModelLoaded(recModel); // then retry once
} else throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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).
Related errors
- Detection model session is not initialized.
- Unexpected rec output dims: [${dims.join(", ")}]
- Initialization did not complete. Call initialize() first.
- Unexpected det output dims: [${dims.join(", ")}]
- Unexpected det output dims: [${od.join(", ")}]
AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14).
Data as JSON: /api/errors/814dcddc67c6681c.
Report an issue: GitHub.