Skip to content

PromptTemplate validation prevents the use of StringTemplate built-in functions #2456

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
asw12 opened this issue Mar 13, 2025 · 1 comment

Comments

@asw12
Copy link

asw12 commented Mar 13, 2025

Bug description
PromptTemplate has a validate method that compares input variables used in the template (pulled straight from the underlying Antlr TokenStream) to the parameters provided in an attempt to verify that there are no unbound variables. However, StringTemplate has some built-in functions, and this validation is preventing their use.

Environment
Spring AI 1.0.0-M6
Java 17

Steps to reproduce
I was attempting to make it so that the prompt added by VectorStoreChatMemoryAdvisor would be added conditionally on whether there was actually data pulled from the chat history. Something like:

   String chatMemoryPrompt = """
                {if(strlen(long_term_memory))}

                Use the long term conversation memory from the LONG_TERM_MEMORY section to provide accurate answers.

                ---------------------
                LONG_TERM_MEMORY:
                {long_term_memory}
                ---------------------
                {endif}
                """;

    VectorStoreChatMemoryAdvisor.builder(chatHistoryStore)
                    .systemTextAdvise(chatMemoryPrompt)
                    .build();

But the validation considers strlen to be a variable that is unbound, and fails.

Expected behavior
I think the same conclusion from this can be reached as #1428 - make the validation optional.
But if we wanted to continue the validation "whack-a-mole", there exists a org.stringtemplate.v4.compiler.Compiler#funcs static map whose keys could be used to find the names of these built-in functions, and omit these in validation.
https://github.com/antlr/stringtemplate4/blob/master/src/org/stringtemplate/v4/compiler/Compiler.java#L81

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants