Skip to content

fix: Throw NonTransientAiException on syntax errors in prompt templates #3614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.retry.NonTransientAiException;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;

Expand Down Expand Up @@ -117,7 +118,7 @@ public void testRenderWithList() {
assertEqualsWithNormalizedEOLs(expected, message.getText());

PromptTemplate unfilledPromptTemplate = new PromptTemplate(templateString);
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(unfilledPromptTemplate::render)
assertThatExceptionOfType(NonTransientAiException.class).isThrownBy(unfilledPromptTemplate::render)
.withMessage("Not all variables were replaced in the template. Missing variable names are: [items].");
}

Expand Down Expand Up @@ -202,7 +203,7 @@ public void testRenderFailure() {
PromptTemplate promptTemplate = PromptTemplate.builder().template(template).variables(model).build();

// Rendering the template with a missing key should throw an exception
assertThrows(IllegalStateException.class, promptTemplate::render);
assertThrows(NonTransientAiException.class, promptTemplate::render);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.retry.NonTransientAiException;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -43,7 +44,7 @@ void newApiPlaygroundTests() {

// Try to render with missing value for template variable, expect exception
Assertions.assertThatThrownBy(() -> pt.render(model))
.isInstanceOf(IllegalStateException.class)
.isInstanceOf(NonTransientAiException.class)
.hasMessage("Not all variables were replaced in the template. Missing variable names are: [lastName].");

pt.add("lastName", "Park"); // TODO investigate partial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.ai.retry.NonTransientAiException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -85,7 +86,7 @@ void renderWithMissingVariableShouldThrow() {
// If render() doesn't throw, fail the test
Assertions.fail("Expected IllegalStateException was not thrown.");
}
catch (IllegalStateException e) {
catch (NonTransientAiException e) {
// Assert that the message is exactly the expected string
assertThat(e.getMessage())
.isEqualTo("Not all variables were replaced in the template. Missing variable names are: [name].");
Expand Down
8 changes: 7 additions & 1 deletion spring-ai-template-st/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-retry</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.antlr</groupId>
<artifactId>ST4</artifactId>
Expand All @@ -74,4 +80,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.antlr.runtime.TokenStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.retry.NonTransientAiException;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.compiler.Compiler;
import org.stringtemplate.v4.compiler.STLexer;
Expand Down Expand Up @@ -115,7 +116,7 @@ private ST createST(String template) {
return new ST(template, this.startDelimiterToken, this.endDelimiterToken);
}
catch (Exception ex) {
throw new IllegalArgumentException("The template string is not valid.", ex);
throw new NonTransientAiException("The template string is not valid.", ex);
}
}

Expand All @@ -137,7 +138,7 @@ private Set<String> validate(ST st, Map<String, Object> templateVariables) {
logger.warn(VALIDATION_MESSAGE.formatted(missingVariables));
}
else if (this.validationMode == ValidationMode.THROW) {
throw new IllegalStateException(VALIDATION_MESSAGE.formatted(missingVariables));
throw new NonTransientAiException(VALIDATION_MESSAGE.formatted(missingVariables));
}
}
return missingVariables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.junit.jupiter.api.Test;

import org.springframework.ai.retry.NonTransientAiException;
import org.springframework.ai.template.ValidationMode;
import org.springframework.test.util.ReflectionTestUtils;

Expand Down Expand Up @@ -107,7 +108,7 @@ void shouldThrowExceptionForInvalidTemplateSyntax() {
Map<String, Object> variables = new HashMap<>();
variables.put("name", "Spring AI");

assertThatThrownBy(() -> renderer.apply("Hello {name!", variables)).isInstanceOf(IllegalArgumentException.class)
assertThatThrownBy(() -> renderer.apply("Hello {name!", variables)).isInstanceOf(NonTransientAiException.class)
.hasMessageContaining("The template string is not valid.");
}

Expand All @@ -118,7 +119,7 @@ void shouldThrowExceptionForMissingVariablesInThrowMode() {
variables.put("greeting", "Hello");

assertThatThrownBy(() -> renderer.apply("{greeting} {name}!", variables))
.isInstanceOf(IllegalStateException.class)
.isInstanceOf(NonTransientAiException.class)
.hasMessageContaining(
"Not all variables were replaced in the template. Missing variable names are: [name]");
}
Expand Down