invoke-ai/InvokeAI · error · Exception

You should call create_session before running model

Error message

You should call create_session before running model

What it means

OnnxRuntimeModel.__call__ requires an ONNX InferenceSession to have been created before inference. If self.session is None (create_session() was never called or the model was constructed without loading), the generic Exception is raised.

Source

Thrown at invokeai/backend/onnx/onnx_runtime.py:180

            if "TensorrtExecutionProvider" in providers:
                providers.remove("TensorrtExecutionProvider")
            try:
                self.session = InferenceSession(self.proto.SerializeToString(), providers=providers, sess_options=sess)
            except Exception as e:
                raise e
            # self.session = InferenceSession("tmp.onnx", providers=[self.provider], sess_options=self.sess_options)
            # self.io_binding = self.session.io_binding()

    def release_session(self):
        self.session = None
        import gc

        gc.collect()
        return

    def __call__(self, **kwargs):
        if self.session is None:
            raise Exception("You should call create_session before running model")

        inputs = {k: np.array(v) for k, v in kwargs.items()}
        # output_names = self.session.get_outputs()
        # for k in inputs:
        #     self.io_binding.bind_cpu_input(k, inputs[k])
        # for name in output_names:
        #     self.io_binding.bind_output(name.name)
        # self.session.run_with_iobinding(self.io_binding, None)
        # return self.io_binding.copy_outputs_to_cpu()
        return self.session.run(None, inputs)

    # compatability with RawModel ABC
    def to(self, device: Optional[torch.device] = None, dtype: Optional[torch.dtype] = None) -> None:
        pass

    # compatability with diffusers load code
    @classmethod
    def from_pretrained(

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Call model.create_session() once before the first inference call.
  2. Use the standard diffusers from_pretrained() path, which sets up the session for you.
  3. If the session was dropped after an error, recreate it before reusing the model.

Example fix

// before
model = OnnxRuntimeModel(...)
outputs = model(**inputs)  # raises
// after
model = OnnxRuntimeModel(...)
model.create_session()
outputs = model(**inputs)
Defensive patterns

Strategy: validation

Validate before calling

if getattr(model, 'session', None) is None:
    model.create_session()
outputs = model(**inputs)

Type guard

def session_ready(model) -> bool:
    return getattr(model, 'session', None) is not None

Try / catch

try:
    outputs = model(**inputs)
except Exception as e:
    if 'create_session before running model' in str(e):
        model.create_session()
        outputs = model(**inputs)
    else:
        raise

Prevention

When it happens

Trigger: Calling the model (i.e. onnx_model(**inputs)) directly after instantiation without first calling onnx_model.create_session(), or after a session that failed to initialize silently.

Common situations: Custom pipeline code instantiating OnnxRuntimeModel manually; reusing a model object across processes/threads where session creation never ran; failures during model load swallowed upstream leaving session None.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/b1a56314984a6e55. Report an issue: GitHub.