PaddlePaddle/PaddleOCR · critical
Detection model session is not initialized.
Error message
Detection model session is not initialized.
What it means
Thrown by the detection model's predict() when the ONNX Runtime session was never created. createDetModel() returns a model object whose session is populated asynchronously (or during an initialize step); calling predict() before that completes — or after session creation failed — hits this guard. The getter provider returning "" is the observable symptom of the same uninitialized state.
Source
Thrown at paddleocr-js/packages/core/src/models/det.ts:221
boxThresh: config.postprocess.boxThresh,
unclipRatio: config.postprocess.unclipRatio
};
let sessionState: SessionState | null = await createDetModelSession(
ort,
modelBytes,
backend,
webgpuState
);
return {
kind: "det",
config,
get provider() {
return sessionState?.provider || "";
},
async predict(cv, mats, overrides) {
if (!sessionState?.session) {
throw new Error("Detection model session is not initialized.");
}
const params = resolveDetParams(defaultParams, overrides);
const batchSize = resolveRuntimeBatchSize(overrides?.batchSize, defaultBatchSize);
const results: DetResult[] = [];
const runCtx: DetRunContext = {
cv,
ort,
config,
session: sessionState.session
};
for (const chunk of chunkArray(mats, batchSize)) {
const preps = preprocess({ cv, ort, config }, chunk, params);
const inputTensor = packDetBatchTensor(ort, preps);
const fullOutput = await runInference(sessionState.session, inputTensor);
const internals = postprocess(runCtx, fullOutput, preps, params);
for (const internal of internals) {
results.push({
boxes: internal.boxes,View on GitHub (pinned to 2661c7c0ef)
Solutions
- Await the model loading/initialization step (whatever populates the session) before any predict() call
- Check model.provider !== "" as a cheap readiness signal before predicting
- Inspect initialization logs/network tab — a failed model download or backend error usually explains the null session
- If initialization failed, re-run it and surface its error instead of proceeding to predict
Example fix
// before
const model = createDetModel(...); // session loads async
const boxes = await model.predict(cv, mats); // throws: session not initialized
// after
const model = createDetModel(...);
await loadDetModelSession(model); // whatever call assigns sessionState.session
if (!model.provider) throw new Error("det model failed to load");
const boxes = await model.predict(cv, mats); Defensive patterns
Strategy: validation
Validate before calling
const ready = detModel.provider !== "";
if (!ready) {
await ensureDetModelLoaded(detModel); // or your loader equivalent
} Type guard
function isDetModelReady(m: { provider: string }): boolean {
return m.provider !== "";
} Try / catch
try { await detModel.predict(cv, mats); } catch (e) {
if (e instanceof Error && /session is not initialized/.test(e.message)) {
await ensureDetModelLoaded(detModel); // then retry once
} else throw e;
} Prevention
- Always await the model load call and keep the promise in a variable you can await again before predict
- Gate prediction UI on model.provider becoming non-empty
- Surface initialization errors instead of swallowing them; a null session is usually a swallowed load failure
When it happens
Trigger: Calling detModel.predict(...) immediately after createDetModel() without awaiting the load/initialize routine that assigns sessionState.session; session creation threw earlier and the error was swallowed; model disposed/reset then reused.
Common situations: Missing await on an async loadModel/create call; fire-and-forget initialization with .then() while a UI event triggers prediction first; WASM/WebGPU backend failing to build the session (e.g. model fetch 404) leaving sessionState null.
Related errors
- Recognition model session is not initialized.
- Unexpected det output dims: [${dims.join(", ")}]
- Unexpected det output dims: [${od.join(", ")}]
- Detection batch output N=${String(nOut)} does not match inpu
- Initialization did not complete. Call initialize() first.
AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14).
Data as JSON: /api/errors/c3a41b4ef0223f82.
Report an issue: GitHub.