Skip to content

Commit 2045500

Browse files
committed
fix: Throw NonTransientAiException on syntax errors in prompt templates
IAE/ISE are too generic, it's hard to use libraries that throw them. Using NonTransientAiException clearly marks the exception as coming from Spring AI, plus will integrate nicely with code that already avoids re-sending requests when faced with this exception. Signed-off-by: Piotr Kubowicz <[email protected]>
1 parent e23bf1a commit 2045500

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

spring-ai-template-st/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
<version>${project.parent.version}</version>
4949
</dependency>
5050

51+
<dependency>
52+
<groupId>org.springframework.ai</groupId>
53+
<artifactId>spring-ai-retry</artifactId>
54+
<version>${project.parent.version}</version>
55+
</dependency>
56+
5157
<dependency>
5258
<groupId>org.antlr</groupId>
5359
<artifactId>ST4</artifactId>
@@ -74,4 +80,4 @@
7480
<scope>test</scope>
7581
</dependency>
7682
</dependencies>
77-
</project>
83+
</project>

spring-ai-template-st/src/main/java/org/springframework/ai/template/st/StTemplateRenderer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.antlr.runtime.TokenStream;
2525
import org.slf4j.Logger;
2626
import org.slf4j.LoggerFactory;
27+
import org.springframework.ai.retry.NonTransientAiException;
2728
import org.stringtemplate.v4.ST;
2829
import org.stringtemplate.v4.compiler.Compiler;
2930
import org.stringtemplate.v4.compiler.STLexer;
@@ -115,7 +116,7 @@ private ST createST(String template) {
115116
return new ST(template, this.startDelimiterToken, this.endDelimiterToken);
116117
}
117118
catch (Exception ex) {
118-
throw new IllegalArgumentException("The template string is not valid.", ex);
119+
throw new NonTransientAiException("The template string is not valid.", ex);
119120
}
120121
}
121122

@@ -137,7 +138,7 @@ private Set<String> validate(ST st, Map<String, Object> templateVariables) {
137138
logger.warn(VALIDATION_MESSAGE.formatted(missingVariables));
138139
}
139140
else if (this.validationMode == ValidationMode.THROW) {
140-
throw new IllegalStateException(VALIDATION_MESSAGE.formatted(missingVariables));
141+
throw new NonTransientAiException(VALIDATION_MESSAGE.formatted(missingVariables));
141142
}
142143
}
143144
return missingVariables;

0 commit comments

Comments
 (0)