Skip to content

Commit a5537c2

Browse files
committed
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 f907196 commit a5537c2

File tree

2 files changed

+430
-3
lines changed

2 files changed

+430
-3
lines changed

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

Lines changed: 6 additions & 3 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.
@@ -57,6 +57,7 @@
5757
* @author Alexandros Pappas
5858
* @author Claudio Silva Junior
5959
* @author Soby Chacko
60+
* @author Sun Yuhan
6061
* @since 1.0.0
6162
*/
6263
public class StreamHelper {
@@ -83,7 +84,9 @@ public boolean isToolUseFinish(StreamEvent event) {
8384
*/
8485
public StreamEvent mergeToolUseEvents(StreamEvent previousEvent, StreamEvent event) {
8586

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

8891
if (event.type() == EventType.CONTENT_BLOCK_START) {
8992
ContentBlockStartEvent contentBlockStart = (ContentBlockStartEvent) event;
@@ -161,7 +164,7 @@ else if (event.type().equals(EventType.CONTENT_BLOCK_START)) {
161164
}
162165
else if (contentBlockStartEvent.contentBlock() instanceof ContentBlockThinking thinkingBlock) {
163166
ContentBlock cb = new ContentBlock(Type.THINKING, null, null, contentBlockStartEvent.index(), null,
164-
null, null, null, null, null, thinkingBlock.thinking(), null, null);
167+
null, null, null, null, thinkingBlock.signature(), thinkingBlock.thinking(), null, null);
165168
contentBlockReference.get().withType(event.type().name()).withContent(List.of(cb));
166169
}
167170
else {

0 commit comments

Comments
 (0)