Skip to content

PromptTemplate validation prevents using arrays in templates. #631

Closed
@eschnou

Description

@eschnou

Bug description
Attempting to iterate on a list of item in a PromptTemplate leads to the following error. This is due to the validation being too restrictive and interpreting the token used for iterating on the array as a required input.

java.lang.IllegalStateException: All template variables were not replaced. Missing variable names are [item]

Environment
Spring AI 0.8.1

Steps to reproduce

The following unit test fails:

    @Test
    public void testPromptRendering() {
        String templateString = "The items are:\n{items:{item | - {item}\n}}";
        List<String> itemList = Arrays.asList("apple", "banana", "cherry");
        PromptTemplate promptTemplate = new PromptTemplate(templateString);
        Message message = promptTemplate.createMessage(Map.of("items", itemList));

        String expected = "The items are:\n" +
                "- apple\n" +
                "- banana\n" +
                "- cherry\n";

        assertEquals(expected, message.getContent());
    }

Expected behavior

The same pattern used directly within a template does work. I would expect the same to be used within a PromptTemplate.

    @Test
    public void testTemplateRendering() {
        String templateString = "The items are:\n{items:{item | - {item}\n}}";
        List<String> itemList = Arrays.asList("apple", "banana", "cherry");

        ST template = new ST(templateString, '{' , '}');
        template.add("items", itemList);

        String expected = "The items are:\n" +
                "- apple\n" +
                "- banana\n" +
                "- cherry\n";
        String result = template.render();

        assertEquals(expected, result);
    }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions