spring-projects/spring-ai · error · IllegalStateException

Neither template nor resource is set

Error message

Neither template nor resource is set

What it means

SystemPromptTemplate.Builder.build() throws this IllegalStateException when neither a template string nor a Resource was provided, since a system prompt template requires content from one of those sources.

Source

Thrown at spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/SystemPromptTemplate.java:108

		public Builder renderer(TemplateRenderer renderer) {
			Assert.notNull(renderer, "renderer cannot be null");
			this.renderer = renderer;
			return this;
		}

		@Override
		public SystemPromptTemplate build() {
			if (this.template != null && this.resource != null) {
				throw new IllegalArgumentException("Only one of template or resource can be set");
			}
			else if (this.resource != null) {
				return new SystemPromptTemplate(this.resource, this.variables, this.renderer);
			}
			else if (this.template != null) {
				return new SystemPromptTemplate(this.template, this.variables, this.renderer);
			}
			else {
				throw new IllegalStateException("Neither template nor resource is set");
			}
		}

	}

}

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Call .template("...") or .resource(...) before build().
  2. Validate that a system-prompt source exists in configuration before building.
  3. Add an early guard that fails with a descriptive config error.

Example fix

// before
SystemPromptTemplate.builder().build();
// after
SystemPromptTemplate.builder().template("You are a helpful assistant").build();
Defensive patterns

Strategy: validation

Validate before calling

if (templateText == null && resource == null) throw new IllegalStateException("Provide a template or resource before building SystemPromptTemplate");

Try / catch

try { return builder.build(); } catch (IllegalStateException e) { logger.error("no system prompt source configured", e); throw e; }

Prevention

When it happens

Trigger: Calling SystemPromptTemplate.builder().build() without calling template(...) or resource(...).

Common situations: Config-driven prompt loading where the template file path was null/empty and the setter was skipped; builder reuse across branches where neither branch fired.

Understand the failure class

Background: "Must pass :limit option" / "Missing required option" — required option errors explained — this error's family across 41 libraries.

Related errors


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