spring-projects / spring-projects/spring-ai

There is a bug in the rendering of Prompt templates in StringTemplate mode

Open
#6,412 2 comments 0 reactions 1 assignee View on GitHub

@sdeleuze is already working on this.

Since Jun 16, 2026.

status: feedback-provided status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 10h
Merged PRs (30d)
5

Description

我的pom如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>4.1.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-openai</artifactId>
            <version>2.0.0-RC2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


配置文件如下:

spring:
  application:
    name: demo
  ai:
    openai:
      base-url: http://192.168.10.233:3000/v1
      api-key: sk-NV44FDGiuspxz27Z1eZqaR2l4YIXI1X1pHgGR5w12i2SI3vS

代码如下:

@Test
    void contextLoads() {
        Map<String, Object> map = new HashMap<>();
        map.put("myMap", Map.of("a", 1, "b", 2, "c", 3));
//        map.put("k", null);
        System.out.println(PromptTemplate.builder().template("""
				{myMap:{k | key={k}, value={myMap.(k)}\n}}
				""").build().render(map));
    }

错误信息如下:
java.lang.IllegalStateException: Not all variables were replaced in the template. Missing variable names are: [k].

at org.springframework.ai.template.st.StTemplateRenderer.validate(StTemplateRenderer.java:145)
at org.springframework.ai.template.st.StTemplateRenderer.apply(StTemplateRenderer.java:111)
at org.springframework.ai.chat.prompt.PromptTemplate.render(PromptTemplate.java:143)
at com.example.demo.DemoApplicationTests.contextLoads(DemoApplicationTests.java:20)

只要我取消注释的那行代码就可以正常运行,正常运行结果为
key=c, value=3
key=b, value=2
key=a, value=1
prompt模板中k是模板内变量不应该要求外面传一个k的变量
依赖分析如下

Image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.