Skip to content

Commit 6ced885

Browse files
sunyuhan1998ilayaperumalg
authored andcommitted
fix: GH-4472 1. Optimized the mergeToolUseEvents method in StreamHelper to enhance robustness when previousEvent is not a ToolUseAggregationEvent;
2. Fixed an issue in the `eventToChatCompletionResponse` method of `StreamHelper` where `thinkingBlock.signature()` was not being handled; 3. Improved `StreamHelperTests` to increase test coverage Signed-off-by: Sun Yuhan <[email protected]>
1 parent 36516e0 commit 6ced885

File tree

2 files changed

+483
-6
lines changed

2 files changed

+483
-6
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,13 +28,13 @@
2828
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlock.Type;
2929
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlockDeltaEvent;
3030
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlockDeltaEvent.ContentBlockDeltaJson;
31+
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlockDeltaEvent.ContentBlockDeltaSignature;
3132
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlockDeltaEvent.ContentBlockDeltaText;
3233
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlockDeltaEvent.ContentBlockDeltaThinking;
33-
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlockDeltaEvent.ContentBlockDeltaSignature;
3434
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlockStartEvent;
3535
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlockStartEvent.ContentBlockText;
36-
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlockStartEvent.ContentBlockToolUse;
3736
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlockStartEvent.ContentBlockThinking;
37+
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlockStartEvent.ContentBlockToolUse;
3838
import org.springframework.ai.anthropic.api.AnthropicApi.EventType;
3939
import org.springframework.ai.anthropic.api.AnthropicApi.MessageDeltaEvent;
4040
import org.springframework.ai.anthropic.api.AnthropicApi.MessageStartEvent;
@@ -55,6 +55,9 @@
5555
* @author Christian Tzolov
5656
* @author Jihoon Kim
5757
* @author Alexandros Pappas
58+
* @author Claudio Silva Junior
59+
* @author Soby Chacko
60+
* @author Sun Yuhan
5861
* @since 1.0.0
5962
*/
6063
public class StreamHelper {
@@ -81,7 +84,9 @@ public boolean isToolUseFinish(StreamEvent event) {
8184
*/
8285
public StreamEvent mergeToolUseEvents(StreamEvent previousEvent, StreamEvent event) {
8386

84-
ToolUseAggregationEvent eventAggregator = (ToolUseAggregationEvent) previousEvent;
87+
if (!(previousEvent instanceof ToolUseAggregationEvent eventAggregator)) {
88+
return event;
89+
}
8590

8691
if (event.type() == EventType.CONTENT_BLOCK_START) {
8792
ContentBlockStartEvent contentBlockStart = (ContentBlockStartEvent) event;
@@ -159,7 +164,7 @@ else if (event.type().equals(EventType.CONTENT_BLOCK_START)) {
159164
}
160165
else if (contentBlockStartEvent.contentBlock() instanceof ContentBlockThinking thinkingBlock) {
161166
ContentBlock cb = new ContentBlock(Type.THINKING, null, null, contentBlockStartEvent.index(), null,
162-
null, null, null, null, null, thinkingBlock.thinking(), null);
167+
null, null, null, null, thinkingBlock.signature(), thinkingBlock.thinking(), null);
163168
contentBlockReference.get().withType(event.type().name()).withContent(List.of(cb));
164169
}
165170
else {

0 commit comments

Comments
 (0)