Skip to content

Commit ca644f6

Browse files
ThomasVitaleleijendary
authored andcommitted
Make ChatClient APIs null-safe and predictable
* Introduced null-safety for all ChatClient APIs and verified via extensive auto-tests. * Made the final message list computed by ChatClient consistent and predictable across the different options for providing messages (prompt(), messages(), user(), prompt()) and verified via extensive auto-tests. * Added even more auto-tests to cover as many scenarios and edge cases as possible. Signed-off-by: Thomas Vitale <[email protected]> Signed-off-by: leijendary <[email protected]>
1 parent 56cefc9 commit ca644f6

File tree

16 files changed

+2687
-138
lines changed

16 files changed

+2687
-138
lines changed

spring-ai-core/src/main/java/org/springframework/ai/chat/client/ChatClient.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.springframework.ai.model.function.FunctionCallback;
3939
import org.springframework.core.ParameterizedTypeReference;
4040
import org.springframework.core.io.Resource;
41+
import org.springframework.lang.Nullable;
42+
import org.springframework.util.Assert;
4143
import org.springframework.util.MimeType;
4244

4345
/**
@@ -49,6 +51,7 @@
4951
* @author Christian Tzolov
5052
* @author Josh Long
5153
* @author Arjen Poutsma
54+
* @author Thomas Vitale
5255
* @since 1.0.0
5356
*/
5457
public interface ChatClient {
@@ -62,7 +65,9 @@ static ChatClient create(ChatModel chatModel, ObservationRegistry observationReg
6265
}
6366

6467
static ChatClient create(ChatModel chatModel, ObservationRegistry observationRegistry,
65-
ChatClientObservationConvention observationConvention) {
68+
@Nullable ChatClientObservationConvention observationConvention) {
69+
Assert.notNull(chatModel, "chatModel cannot be null");
70+
Assert.notNull(observationRegistry, "observationRegistry cannot be null");
6671
return builder(chatModel, observationRegistry, observationConvention).build();
6772
}
6873

@@ -71,7 +76,9 @@ static Builder builder(ChatModel chatModel) {
7176
}
7277

7378
static Builder builder(ChatModel chatModel, ObservationRegistry observationRegistry,
74-
ChatClientObservationConvention customObservationConvention) {
79+
@Nullable ChatClientObservationConvention customObservationConvention) {
80+
Assert.notNull(chatModel, "chatModel cannot be null");
81+
Assert.notNull(observationRegistry, "observationRegistry cannot be null");
7582
return new DefaultChatClientBuilder(chatModel, observationRegistry, customObservationConvention);
7683
}
7784

@@ -136,14 +143,19 @@ interface AdvisorSpec {
136143

137144
interface CallResponseSpec {
138145

146+
@Nullable
139147
<T> T entity(ParameterizedTypeReference<T> type);
140148

149+
@Nullable
141150
<T> T entity(StructuredOutputConverter<T> structuredOutputConverter);
142151

152+
@Nullable
143153
<T> T entity(Class<T> type);
144154

155+
@Nullable
145156
ChatResponse chatResponse();
146157

158+
@Nullable
147159
String content();
148160

149161
<T> ResponseEntity<ChatResponse, T> responseEntity(Class<T> type);

0 commit comments

Comments
 (0)