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

  1. Await the model loading/initialization step (whatever populates the session) before any predict() call
  2. Check model.provider !== "" as a cheap readiness signal before predicting
  3. Inspect initialization logs/network tab — a failed model download or backend error usually explains the null session
  4. 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

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


AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14). Data as JSON: /api/errors/c3a41b4ef0223f82. Report an issue: GitHub.