spring-projects/spring-ai · error · IllegalStateException

Failed to create index:

Error message

Failed to create index: 

What it means

RedisChatMemoryRepository.initializeSchema() creates a RediSearch index via jedisClient.ftCreate(). If the server response is not "OK" the repository throws IllegalStateException including the raw response. This usually means the index could not be created (name conflict with different parameters, unsupported schema, or a Redis module issue).

Source

Thrown at memory-repositories/spring-ai-model-chat-memory-repository-redis/src/main/java/org/springframework/ai/chat/memory/repository/redis/RedisChatMemoryRepository.java:443

					// When specific metadata fields are defined, we don't add a wildcard
					// metadata field to avoid indexing errors with non-string values
				}

				else {
					// No schema provided - fallback to indexing all metadata as text
					schemaFields.add(new TextField("$.metadata.*").as("metadata"));
				}

				// Create the index with the defined schema
				FTCreateParams indexParams = FTCreateParams.createParams()
					.on(IndexDataType.JSON)
					.prefix(this.config.getKeyPrefix());

				String response = this.jedisClient.ftCreate(this.config.getIndexName(), indexParams,
						schemaFields.toArray(new SchemaField[0]));

				if (!response.equals("OK")) {
					throw new IllegalStateException("Failed to create index: " + response);
				}

				if (logger.isDebugEnabled()) {
					logger.debug("Created Redis search index '" + this.config.getIndexName() + "' with "
							+ schemaFields.size() + " schema fields");
				}
			}

			else if (logger.isDebugEnabled()) {
				logger.debug("Redis search index '" + this.config.getIndexName() + "' already exists");
			}
		}

		catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Failed to initialize Redis schema: " + e.getMessage());
			}
			logger.debug("Error details", e);

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Check the response string in the message for the RediSearch error detail
  2. Drop the stale index (FT.DROPINDEX <indexName>) and restart so it is recreated with the current schema
  3. Verify the Redis server has the RediSearch module loaded (FT.INFO <indexName>)
  4. Ensure the configured indexName doesn't collide with an index of a different schema

Example fix

// before
redis-cli> FT.INFO chat-memory-index  // schema mismatch
// after
redis-cli> FT.DROPINDEX chat-memory-index
// then restart the app to recreate the index
Defensive patterns

Strategy: try-catch

Validate before calling

// before constructing, confirm the index name and RediSearch availability
String info = jedis.ftInfo(indexName); // catch unknown-index and check module support

Try / catch

try { repo = RedisChatMemoryRepository.builder()...build(); }
catch (IllegalStateException e) {
    if (e.getMessage().startsWith("Failed to create index")) { /* drop and recreate index or alert */ }
    throw e;
}

Prevention

When it happens

Trigger: Repository construction triggering schema init against a Redis instance where ftCreate fails — e.g. an existing index with the same name but a different schema, or a misbehaving RediSearch module.

Common situations: Index already exists from an earlier run with a different schema; connecting to plain Redis without the RediSearch module; restricted/managed Redis disallowing FT.CREATE.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/9b775d1bd6849f6c. Report an issue: GitHub.