Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* @author Christian Tzolov
* @author Mariusz Bernacki
* @author Thomas Vitale
* @author Jihoon Kim
* @since 1.0.0
*/
public class AnthropicApi {
Expand Down Expand Up @@ -138,8 +139,9 @@ public AnthropicApi(String baseUrl, String anthropicApiKey, String anthropicVers
this.webClient = webClientBuilder.baseUrl(baseUrl)
.defaultHeaders(jsonContentHeaders)
.defaultStatusHandler(HttpStatusCode::isError,
resp -> Mono.just(new RuntimeException("Response exception, Status: [" + resp.statusCode()
+ "], Body:[" + resp.bodyToMono(java.lang.String.class) + "]")))
resp -> resp.bodyToMono(String.class)
.flatMap(it -> Mono.error(new RuntimeException(
"Response exception, Status: [" + resp.statusCode() + "], Body:[" + it + "]"))))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlock;
import org.springframework.ai.anthropic.api.AnthropicApi.Role;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Christian Tzolov
* @author Jihoon Kim
*/
@EnabledIfEnvironmentVariable(named = "ANTHROPIC_API_KEY", matches = ".+")
public class AnthropicApiIT {
Expand Down Expand Up @@ -70,4 +71,21 @@ void chatCompletionStream() {
bla.stream().forEach(r -> System.out.println(r));
}

@Test
void chatCompletionStreamError() {
AnthropicMessage chatCompletionMessage = new AnthropicMessage(List.of(new ContentBlock("Tell me a Joke?")),
Role.USER);
AnthropicApi api = new AnthropicApi("FAKE_KEY_FOR_ERROR_RESPONSE");

Flux<ChatCompletionResponse> response = api.chatCompletionStream(new ChatCompletionRequest(
AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(), List.of(chatCompletionMessage), null, 100, 0.8, true));

assertThat(response).isNotNull();

assertThatThrownBy(() -> response.collectList().block()).isInstanceOf(RuntimeException.class)
.hasMessageStartingWith("Response exception, Status: [")
.hasMessageContaining(
"{\"type\":\"error\",\"error\":{\"type\":\"authentication_error\",\"message\":\"invalid x-api-key\"}}");
}

}