{"record":{"id":"8d5bbec4c7daf1ee","repo":"spring-projects/spring-ai","slug":"ai-onnxruntime-ortexception","errorCode":null,"errorMessage":"ai.onnxruntime.OrtException","messagePattern":"ai\\.onnxruntime\\.OrtException","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-transformers/src/main/java/org/springframework/ai/transformers/TransformersEmbeddingModel.java","lineNumber":379,"sourceCode":"\t\t\t\t\t\t\t// 1 - sequence_length (128)\n\t\t\t\t\t\t\t// 2 - embedding dimensions (384)\n\t\t\t\t\t\t\tfloat[][][] tokenEmbeddings = (float[][][]) lastHiddenState.getValue();\n\n\t\t\t\t\t\t\ttry (NDManager manager = NDManager.newBaseManager()) {\n\t\t\t\t\t\t\t\tNDArray ndTokenEmbeddings = create(tokenEmbeddings, manager);\n\t\t\t\t\t\t\t\tNDArray ndAttentionMask = manager.create(attention_mask0);\n\n\t\t\t\t\t\t\t\tNDArray embedding = meanPooling(ndTokenEmbeddings, ndAttentionMask);\n\n\t\t\t\t\t\t\t\tfor (int i = 0; i < embedding.size(0); i++) {\n\t\t\t\t\t\t\t\t\tresultEmbeddings.add(embedding.get(i).toFloatArray());\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tcatch (OrtException ex) {\n\t\t\t\t\tthrow new RuntimeException(ex);\n\t\t\t\t}\n\n\t\t\t\tvar indexCounter = new AtomicInteger(0);\n\n\t\t\t\tEmbeddingResponse embeddingResponse = new EmbeddingResponse(\n\t\t\t\t\t\tresultEmbeddings.stream().map(e -> new Embedding(e, indexCounter.incrementAndGet())).toList());\n\t\t\t\tobservationContext.setResponse(embeddingResponse);\n\n\t\t\t\treturn embeddingResponse;\n\t\t\t});\n\t}\n\n\tprivate Map<String, OnnxTensor> removeUnknownModelInputs(Map<String, OnnxTensor> modelInputs) {\n\n\t\treturn modelInputs.entrySet()\n\t\t\t.stream()\n\t\t\t.filter(a -> this.onnxModelInputs.contains(a.getKey()))\n\t\t\t.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));","sourceCodeStart":361,"sourceCodeEnd":397,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-transformers/src/main/java/org/springframework/ai/transformers/TransformersEmbeddingModel.java#L361-L397","documentation":"TransformersEmbeddingModel.call() runs sentence embedding through the ONNX Runtime session. Any OrtException raised by the ONNX runtime during inference (bad input tensor shape, tokenizer/model mismatch, corrupted or incompatible model file, native runtime failure) is caught and rethrown as a RuntimeException wrapping the OrtException.","triggerScenarios":"Calling embed()/call() when the ONNX session cannot process the input: input tensor shape does not match model expectations, wrong tokenizer/model pairing, corrupted cached ONNX model, or ONNX Runtime native library problems.","commonSituations":"Manually replaced or partially downloaded cached model files; using a model whose input signature differs from what the code feeds (e.g. different max sequence length); mixing model and tokenizer resources from different models; failing ONNX Runtime native extraction in exotic environments.","solutions":["Read the wrapped OrtException cause message — it names the failing tensor/shape/operator.","Clear the spring-ai model cache directory and let the library re-download clean model files.","Ensure the tokenizer and ONNX model come from the same model repository/version.","Verify ONNX Runtime native libraries load correctly on your platform/architecture."],"exampleFix":"// before\nmodel.setMetadata(new TransformersEmbeddingModel.Metadata(\"model.onnx\", \"tokenizer.json from another model\"));\n// after\nmodel.setMetadata(new TransformersEmbeddingModel.Metadata(\n    \"https://huggingface.co/Xenova/all-MiniLM-L6-v2/onnx/model.onnx\",\n    \"https://huggingface.co/Xenova/all-MiniLM-L6-v2/tokenizer.json\"));","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    float[][] embeddings = embeddingModel.embed(docs);\n} catch (RuntimeException e) {\n    if (e.getCause() instanceof OrtException ort) {\n        throw new IllegalStateException(\"ONNX inference failed: \" + ort.getMessage(), ort);\n    }\n    throw e;\n}","preventionTips":["Keep tokenizer and ONNX model files from the same model repo/version.","Clear and re-download the model cache if files may be corrupted.","Verify ONNX Runtime native libs support your platform/arch.","Keep input text lengths within the model's sequence limit."],"tags":["onnx","embedding","inference","native-library"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}