spring-projects/spring-ai · error · IllegalStateException

Neither template nor resource is set

Error message

Neither template nor resource is set

What it means

Validation guard in PromptTemplate.Builder.build(): neither a template string nor a template resource was supplied, so there is no prompt content to render and constructing the PromptTemplate would produce a useless object. It is a generic guard over the builder's own state — the faulty input is an empty builder (both this.template and this.resource left null). Fix by calling template(...) or resource(...) before build().

Solutions

  1. Call builder.template(...) with the template string
  2. Or call builder.resource(...) with a classpath/file Resource

Example fix

// before
PromptTemplate.builder().variables(Map.of("x", 1)).build();
// after
PromptTemplate.builder().template("Hello {x}").variables(Map.of("x", 1)).build();
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: Calling PromptTemplate.builder().build() (or only setting variables/renderer) without calling template(...) or resource(...).

Common situations: Dynamically built templates where a condition skipped setting the source; builder reused after an earlier branch chose neither; configuration-driven code where the template property resolved to null and was silently skipped.

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/1e1ca11e652cb365. Report an issue: GitHub.

Appendix: source

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

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

		public PromptTemplate 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 PromptTemplate(this.resource, this.variables, this.renderer);
			}
			else if (this.template != null) {
				return new PromptTemplate(this.template, this.variables, this.renderer);
			}
			else {
				throw new IllegalStateException("Neither template nor resource is set");
			}
		}

	}

}

View on GitHub (pinned to 98a7beda4f)