Skip to content

Commit 7e71a3e

Browse files
committed
Fix issue with using unsubscribed response body in exception message
Signed-off-by: jitokim <[email protected]>
1 parent 5e1f681 commit 7e71a3e

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/api/AnthropicApi.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @author Christian Tzolov
5252
* @author Mariusz Bernacki
5353
* @author Thomas Vitale
54+
* @author Jihoon Kim
5455
* @since 1.0.0
5556
*/
5657
public class AnthropicApi {
@@ -138,8 +139,9 @@ public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVers
138139
this.webClient = webClientBuilder.baseUrl(baseUrl)
139140
.defaultHeaders(jsonContentHeaders)
140141
.defaultStatusHandler(HttpStatusCode::isError,
141-
resp -> Mono.just(new RuntimeException("Response exception, Status: [" + resp.statusCode()
142-
+ "], Body:[" + resp.bodyToMono(java.lang.String.class) + "]")))
142+
resp -> resp.bodyToMono(String.class)
143+
.flatMap(it -> Mono.error(new RuntimeException(
144+
"Response exception, Status: [" + resp.statusCode() + "], Body:[" + it + "]"))))
143145
.build();
144146
}
145147

models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/AnthropicApiIT.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlock;
2929
import org.springframework.ai.anthropic.api.AnthropicApi.Role;
3030
import org.springframework.http.ResponseEntity;
31-
31+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3232
import static org.assertj.core.api.Assertions.assertThat;
3333

3434
/**
3535
* @author Christian Tzolov
36+
* @author Jihoon Kim
3637
*/
3738
@EnabledIfEnvironmentVariable(named = "ANTHROPIC_API_KEY", matches = ".+")
3839
public class AnthropicApiIT {
@@ -70,4 +71,21 @@ void chatCompletionStream() {
7071
bla.stream().forEach(r -> System.out.println(r));
7172
}
7273

74+
@Test
75+
void chatCompletionStreamError() {
76+
AnthropicMessage chatCompletionMessage = new AnthropicMessage(List.of(new ContentBlock("Tell me a Joke?")),
77+
Role.USER);
78+
AnthropicApi api = new AnthropicApi("FAKE_KEY_FOR_ERROR_RESPONSE");
79+
80+
Flux<ChatCompletionResponse> response = api.chatCompletionStream(new ChatCompletionRequest(
81+
AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(), List.of(chatCompletionMessage), null, 100, 0.8, true));
82+
83+
assertThat(response).isNotNull();
84+
85+
assertThatThrownBy(() -> response.collectList().block()).isInstanceOf(RuntimeException.class)
86+
.hasMessageStartingWith("Response exception, Status: [")
87+
.hasMessageContaining(
88+
"{\"type\":\"error\",\"error\":{\"type\":\"authentication_error\",\"message\":\"invalid x-api-key\"}}");
89+
}
90+
7391
}

0 commit comments

Comments
 (0)