Skip to content

Commit fb2e752

Browse files
jitokimmarkpollack
authored andcommitted
Fix Anthropic issue using unsubscribed response body in exception message
Signed-off-by: jitokim <pigberger70@gmail.com>
1 parent a7a1804 commit fb2e752

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
@@ -53,6 +53,7 @@
5353
* @author Christian Tzolov
5454
* @author Mariusz Bernacki
5555
* @author Thomas Vitale
56+
* @author Jihoon Kim
5657
* @since 1.0.0
5758
*/
5859
public class AnthropicApi {
@@ -143,8 +144,9 @@ public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVers
143144
this.webClient = webClientBuilder.baseUrl(baseUrl)
144145
.defaultHeaders(jsonContentHeaders)
145146
.defaultStatusHandler(HttpStatusCode::isError,
146-
resp -> Mono.just(new RuntimeException("Response exception, Status: [" + resp.statusCode()
147-
+ "], Body:[" + resp.bodyToMono(java.lang.String.class) + "]")))
147+
resp -> resp.bodyToMono(String.class)
148+
.flatMap(it -> Mono.error(new RuntimeException(
149+
"Response exception, Status: [" + resp.statusCode() + "], Body:[" + it + "]"))))
148150
.build();
149151
}
150152

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)