TransformersEmbeddingModel serves stale ONNX model from temp cache when running from fat jar
Bug description
I am using TransformersEmbeddingModel to power a RAG pipeline backed by Redis VectorStore. I tried switching from one ONNX model to another with a different embedding dimension.
I exported the model using optimum-cli and saved model.onnx and tokenizer.json into src/main/resources/models/. The bean is wired in a @Configuration class:
@Bean
public EmbeddingModel embeddingModel() throws Exception {
TransformersEmbeddingModel embeddingModel = new TransformersEmbeddingModel();
embeddingModel.setTokenizerResource("classpath:/models/tokenizer.json");
embeddingModel.setModelResource("classpath:/models/model.onnx");
embeddingModel.afterPropertiesSet();
return embeddingModel;
}
Everything works fine the first time: the VectorStore index and Spring AI both use the same model. I am using Redis.
I then switched to a different model with a lower embedding dimension (768 → 384):
- Re-indexed Redis with vectors from the new model (384-dim).
- Exported the new ONNX model and overwrote
src/main/resources/models/model.onnxandtokenizer.json. - Rebuilt the fat jar.
- Restarted the application.
After the restart every query to the RAG produces:
Error invoking method: searchRAG
Error parsing vector similarity query: query vector blob size (3072) does not match index's expected size (1536).
3072 bytes ÷ 4 = 768 floats — Spring AI is still querying with the old 768-dim model despite the jar containing the new 384-dim one. Redis correctly expects 384-dim (1536 bytes).
This error only occurs when running from the fat jar (e.g. stdio MCP mode, java -jar). When running from an exploded classpath (e.g. mvn spring-boot:run or HTTP mode via IDE), the model file is read directly from target/classes/models/ without any caching intermediate, so the new model is picked up immediately and it works fine. The difference is fat-jar vs exploded classpath, not the transport protocol itself.
Root cause:
When loading a classpath: resource from inside a fat jar, Spring AI extracts the ONNX model to a temp directory:
C:\Users\<user>\AppData\Local\Temp\spring-ai-onnx-generative\<deterministic-uuid>\
The UUID appears to be derived from the resource path (classpath:/models/model.onnx), so the same path always resolves to the same temp directory across restarts. When the jar is rebuilt with a new model, the old extracted files are still present in that temp directory and are reused — the new jar contents are never re-extracted.
Workaround: manually delete the temp directory. On the next startup Spring AI re-extracts the model from the jar and the correct dimensions are used.
Environment
- Spring AI: 2.0.0-M4, 2.0.0-M8
- Java: Amazon Corretto 21 (JVM)
- OS: Windows 11
- Vector store: Redis (RedisJSON + HNSW index)
- Transport: Spring AI MCP Server (stdio and Streamable HTTP)
Steps to reproduce
- Export a first ONNX model (e.g.
msmarco-distilbert-base-v4, 768-dim) tosrc/main/resources/models/. - Create a Redis index and ingest documents embedded with this model (768-dim vectors).
- Wire
TransformersEmbeddingModelas shown above and build a fat jar. - Run
java -jar app.jar— queries work correctly. - Export a second ONNX model with a different dimension (e.g.
thenlper/gte-small, 384-dim) to the samesrc/main/resources/models/directory, overwriting the previous files. - Re-index Redis with the new model (384-dim vectors).
- Rebuild the fat jar (
mvn clean package) — note thatcleanonly wipestarget/, it does not touch the temp directory. - Run
java -jar app.jaragain without deleting the temp directory from step 4. - Issue a vector similarity query.
- Observe the dimension mismatch error — the old 768-dim model is loaded from the temp cache instead of the new 384-dim model from the jar.
Expected behavior
When the fat jar is rebuilt with a new model.onnx, the next startup of the application should use the model bundled in the new jar. The embedding dimension reported at query time should match the model that was packaged into the jar, not a model that was extracted from a previous build.
Minimal Complete Reproducible Example
package org.example.springaiissues;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.transformers.TransformersEmbeddingModel;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringaiIssuesApplication {
public static void main(String[] args) {
SpringApplication.run(SpringaiIssuesApplication.class, args);
}
@Bean
public EmbeddingModel embeddingModel() throws Exception {
TransformersEmbeddingModel m = new TransformersEmbeddingModel();
m.setTokenizerResource("classpath:/models/tokenizer.json");
m.setModelResource("classpath:/models/model.onnx");
m.afterPropertiesSet();
return m;
}
@Bean
CommandLineRunner run(EmbeddingModel model) {
return args -> {
float[] embedding = model.embed("test sentence");
System.out.println("Embedding dimension: " + embedding.length);
// First run with 768-dim model → prints 768
// Second run after swapping to 384-dim model + rebuild → still prints 768
// Expected: 384
};
}
}
Place the ONNX model and tokenizer under src/main/resources/models/. Run once with a 768-dim model, then swap for a 384-dim model and rebuild the jar (mvn clean package). mvn clean only wipes target/ — it does not affect the system temp directory. Run again without manually deleting %TEMP%\spring-ai-onnx-generative\. The printed dimension will still be 768.
A ready-to-use reproduction repository is available at: https://github.com/olegbilovus/springai-issues
Hi team, I opened a PR for this in #6240.
Root cause seems to be that ResourceCacheService reused an existing cached file based only on the resolved cache path. For resources packaged inside an executable jar, the classpath URI can stay the same across application upgrades, so the cache could keep serving an old extracted model.onnx / tokenizer.json.
The PR adds lightweight metadata for cached local resources and refreshes the cached file when the source resource metadata changes. Remote http / https cache behavior is left unchanged.