1. SpringAI 多轮对话400错误问题解析
最近在使用SpringAI进行多轮对话开发时,遇到了一个棘手的400 Bad Request错误。这个问题看似简单,但背后涉及到SpringAI框架与OpenAI API规范的微妙差异,值得深入探讨。
1.1 问题现象重现
当使用SpringAI进行多轮对话时,第一次请求可以正常执行,但第二次及后续请求会抛出以下异常:
java复制2026-01-12 20:17:02.116 [http-nio-8438-exec-5] ERROR -RuntimeException
org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest: 400 Bad Request from POST https://xxxx.com/v1/chat/completions
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:321)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Assembly trace from producer [reactor.core.publisher.MonoIgnoreThen] :
reactor.core.publisher.Mono.then(Mono.java:4879)
这个错误最令人困惑的是:同样的代码,第一次请求能成功,后续请求却失败。这显然与对话状态管理有关。
1.2 环境配置说明
项目使用的是Spring Boot 3.5.4和Spring AI 1.1.2版本,依赖配置如下:
xml复制<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.5.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.1.2</version>
<type>pom</type>
<scope>import
