Skip to content

Commit 517e52b

Browse files
committed
Fixes google#482 - further refinement on exception handling and threading
1 parent 59e859f commit 517e52b

5 files changed

Lines changed: 511 additions & 47 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.adk.models.springai;
17+
18+
/**
19+
* Exception thrown when message conversion between ADK and Spring AI formats fails.
20+
*
21+
* <p>This exception is thrown when there are issues converting between ADK's Content/Part format
22+
* and Spring AI's Message/ChatResponse format, such as JSON parsing errors, invalid message
23+
* structures, or unsupported content types.
24+
*/
25+
public class MessageConversionException extends RuntimeException {
26+
27+
/**
28+
* Constructs a new MessageConversionException with the specified detail message.
29+
*
30+
* @param message the detail message
31+
*/
32+
public MessageConversionException(String message) {
33+
super(message);
34+
}
35+
36+
/**
37+
* Constructs a new MessageConversionException with the specified detail message and cause.
38+
*
39+
* @param message the detail message
40+
* @param cause the cause of the exception
41+
*/
42+
public MessageConversionException(String message, Throwable cause) {
43+
super(message, cause);
44+
}
45+
46+
/**
47+
* Constructs a new MessageConversionException with the specified cause.
48+
*
49+
* @param cause the cause of the exception
50+
*/
51+
public MessageConversionException(Throwable cause) {
52+
super(cause);
53+
}
54+
55+
/**
56+
* Creates a MessageConversionException for JSON parsing failures.
57+
*
58+
* @param context the context where the parsing failed (e.g., "tool call arguments")
59+
* @param cause the underlying JSON processing exception
60+
* @return a new MessageConversionException with appropriate message
61+
*/
62+
public static MessageConversionException jsonParsingFailed(String context, Throwable cause) {
63+
return new MessageConversionException(
64+
String.format("Failed to parse JSON for %s", context), cause);
65+
}
66+
67+
/**
68+
* Creates a MessageConversionException for invalid message structure.
69+
*
70+
* @param message description of the invalid structure
71+
* @return a new MessageConversionException
72+
*/
73+
public static MessageConversionException invalidMessageStructure(String message) {
74+
return new MessageConversionException("Invalid message structure: " + message);
75+
}
76+
77+
/**
78+
* Creates a MessageConversionException for unsupported content type.
79+
*
80+
* @param contentType the unsupported content type
81+
* @return a new MessageConversionException
82+
*/
83+
public static MessageConversionException unsupportedContentType(String contentType) {
84+
return new MessageConversionException("Unsupported content type: " + contentType);
85+
}
86+
}

‎contrib/spring-ai/src/main/java/com/google/adk/models/springai/MessageConverter.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private Content convertAssistantMessageToContent(AssistantMessage assistantMessa
311311
objectMapper.readValue(toolCall.arguments(), MAP_TYPE_REFERENCE);
312312
parts.add(Part.fromFunctionCall(toolCall.name(), args));
313313
} catch (JsonProcessingException e) {
314-
throw new RuntimeException("Failed to parse tool call arguments", e);
314+
throw MessageConversionException.jsonParsingFailed("tool call arguments", e);
315315
}
316316
}
317317
}
@@ -323,7 +323,7 @@ private String toJson(Object object) {
323323
try {
324324
return objectMapper.writeValueAsString(object);
325325
} catch (JsonProcessingException e) {
326-
throw new RuntimeException("Failed to convert object to JSON", e);
326+
throw MessageConversionException.jsonParsingFailed("object serialization", e);
327327
}
328328
}
329329
}

‎contrib/spring-ai/src/main/java/com/google/adk/models/springai/StreamingResponseAggregator.java‎

Lines changed: 81 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,26 @@
2020
import com.google.genai.types.Part;
2121
import java.util.ArrayList;
2222
import java.util.List;
23+
import java.util.concurrent.CopyOnWriteArrayList;
24+
import java.util.concurrent.locks.ReadWriteLock;
25+
import java.util.concurrent.locks.ReentrantReadWriteLock;
2326

2427
/**
2528
* Aggregates streaming responses from Spring AI models.
2629
*
2730
* <p>This class helps manage the accumulation of partial responses in streaming mode, ensuring that
2831
* text content is properly concatenated and tool calls are correctly handled.
32+
*
33+
* <p><b>Thread Safety:</b> This class is thread-safe. All public methods are synchronized to ensure
34+
* safe concurrent access. The internal state is protected using a combination of thread-safe data
35+
* structures and synchronization locks.
2936
*/
3037
public class StreamingResponseAggregator {
3138

32-
private final StringBuilder textAccumulator = new StringBuilder();
33-
private final List<Part> toolCallParts = new ArrayList<>();
34-
private boolean isFirstResponse = true;
39+
private final StringBuffer textAccumulator = new StringBuffer();
40+
private final List<Part> toolCallParts = new CopyOnWriteArrayList<>();
41+
private final ReadWriteLock lock = new ReentrantReadWriteLock();
42+
private volatile boolean isFirstResponse = true;
3543

3644
/**
3745
* Processes a streaming LlmResponse and returns the current aggregated state.
@@ -49,38 +57,43 @@ public LlmResponse processStreamingResponse(LlmResponse response) {
4957
return response;
5058
}
5159

52-
// Process each part in the response
53-
for (Part part : content.parts().get()) {
54-
if (part.text().isPresent()) {
55-
textAccumulator.append(part.text().get());
56-
} else if (part.functionCall().isPresent()) {
57-
// Tool calls are typically complete in each response
58-
toolCallParts.add(part);
60+
lock.writeLock().lock();
61+
try {
62+
// Process each part in the response
63+
for (Part part : content.parts().get()) {
64+
if (part.text().isPresent()) {
65+
textAccumulator.append(part.text().get());
66+
} else if (part.functionCall().isPresent()) {
67+
// Tool calls are typically complete in each response
68+
toolCallParts.add(part);
69+
}
5970
}
60-
}
6171

62-
// Create aggregated content
63-
List<Part> aggregatedParts = new ArrayList<>();
64-
if (textAccumulator.length() > 0) {
65-
aggregatedParts.add(Part.fromText(textAccumulator.toString()));
66-
}
67-
aggregatedParts.addAll(toolCallParts);
72+
// Create aggregated content
73+
List<Part> aggregatedParts = new ArrayList<>();
74+
if (textAccumulator.length() > 0) {
75+
aggregatedParts.add(Part.fromText(textAccumulator.toString()));
76+
}
77+
aggregatedParts.addAll(toolCallParts);
6878

69-
Content aggregatedContent = Content.builder().role("model").parts(aggregatedParts).build();
79+
Content aggregatedContent = Content.builder().role("model").parts(aggregatedParts).build();
7080

71-
// Determine if this is still partial
72-
boolean isPartial = response.partial().orElse(false);
73-
boolean isTurnComplete = response.turnComplete().orElse(true);
81+
// Determine if this is still partial
82+
boolean isPartial = response.partial().orElse(false);
83+
boolean isTurnComplete = response.turnComplete().orElse(true);
7484

75-
LlmResponse aggregatedResponse =
76-
LlmResponse.builder()
77-
.content(aggregatedContent)
78-
.partial(isPartial)
79-
.turnComplete(isTurnComplete)
80-
.build();
85+
LlmResponse aggregatedResponse =
86+
LlmResponse.builder()
87+
.content(aggregatedContent)
88+
.partial(isPartial)
89+
.turnComplete(isTurnComplete)
90+
.build();
8191

82-
isFirstResponse = false;
83-
return aggregatedResponse;
92+
isFirstResponse = false;
93+
return aggregatedResponse;
94+
} finally {
95+
lock.writeLock().unlock();
96+
}
8497
}
8598

8699
/**
@@ -89,36 +102,59 @@ public LlmResponse processStreamingResponse(LlmResponse response) {
89102
* @return The final complete response
90103
*/
91104
public LlmResponse getFinalResponse() {
92-
List<Part> finalParts = new ArrayList<>();
93-
if (textAccumulator.length() > 0) {
94-
finalParts.add(Part.fromText(textAccumulator.toString()));
95-
}
96-
finalParts.addAll(toolCallParts);
105+
lock.writeLock().lock();
106+
try {
107+
List<Part> finalParts = new ArrayList<>();
108+
if (textAccumulator.length() > 0) {
109+
finalParts.add(Part.fromText(textAccumulator.toString()));
110+
}
111+
finalParts.addAll(toolCallParts);
112+
113+
Content finalContent = Content.builder().role("model").parts(finalParts).build();
97114

98-
Content finalContent = Content.builder().role("model").parts(finalParts).build();
115+
LlmResponse finalResponse =
116+
LlmResponse.builder().content(finalContent).partial(false).turnComplete(true).build();
99117

100-
LlmResponse finalResponse =
101-
LlmResponse.builder().content(finalContent).partial(false).turnComplete(true).build();
118+
// Reset internal state without calling reset() to avoid nested locking
119+
textAccumulator.setLength(0);
120+
toolCallParts.clear();
121+
isFirstResponse = true;
102122

103-
// Reset for next use
104-
reset();
105-
return finalResponse;
123+
return finalResponse;
124+
} finally {
125+
lock.writeLock().unlock();
126+
}
106127
}
107128

108129
/** Resets the aggregator for reuse. */
109130
public void reset() {
110-
textAccumulator.setLength(0);
111-
toolCallParts.clear();
112-
isFirstResponse = true;
131+
lock.writeLock().lock();
132+
try {
133+
textAccumulator.setLength(0);
134+
toolCallParts.clear();
135+
isFirstResponse = true;
136+
} finally {
137+
lock.writeLock().unlock();
138+
}
113139
}
114140

115141
/** Returns true if no content has been processed yet. */
116142
public boolean isEmpty() {
117-
return textAccumulator.length() == 0 && toolCallParts.isEmpty();
143+
lock.readLock().lock();
144+
try {
145+
return textAccumulator.length() == 0 && toolCallParts.isEmpty();
146+
} finally {
147+
lock.readLock().unlock();
148+
}
118149
}
119150

120151
/** Returns the current accumulated text length. */
121152
public int getAccumulatedTextLength() {
122-
return textAccumulator.length();
153+
lock.readLock().lock();
154+
try {
155+
return textAccumulator.length();
156+
} finally {
157+
lock.readLock().unlock();
158+
}
123159
}
124160
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.adk.models.springai;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
20+
21+
import com.fasterxml.jackson.core.JsonProcessingException;
22+
import com.fasterxml.jackson.databind.ObjectMapper;
23+
import org.junit.jupiter.api.Test;
24+
import org.springframework.ai.chat.messages.AssistantMessage;
25+
26+
class MessageConversionExceptionTest {
27+
28+
@Test
29+
void testBasicConstructors() {
30+
// Test message-only constructor
31+
MessageConversionException ex1 = new MessageConversionException("Test message");
32+
assertThat(ex1.getMessage()).isEqualTo("Test message");
33+
assertThat(ex1.getCause()).isNull();
34+
35+
// Test message and cause constructor
36+
Throwable cause = new RuntimeException("Original cause");
37+
MessageConversionException ex2 = new MessageConversionException("Test with cause", cause);
38+
assertThat(ex2.getMessage()).isEqualTo("Test with cause");
39+
assertThat(ex2.getCause()).isEqualTo(cause);
40+
41+
// Test cause-only constructor
42+
MessageConversionException ex3 = new MessageConversionException(cause);
43+
assertThat(ex3.getCause()).isEqualTo(cause);
44+
}
45+
46+
@Test
47+
void testJsonParsingFailedFactory() {
48+
JsonProcessingException jsonException = new JsonProcessingException("JSON error") {};
49+
50+
MessageConversionException ex =
51+
MessageConversionException.jsonParsingFailed("tool call arguments", jsonException);
52+
53+
assertThat(ex.getMessage()).isEqualTo("Failed to parse JSON for tool call arguments");
54+
assertThat(ex.getCause()).isEqualTo(jsonException);
55+
}
56+
57+
@Test
58+
void testInvalidMessageStructureFactory() {
59+
MessageConversionException ex =
60+
MessageConversionException.invalidMessageStructure("missing required field");
61+
62+
assertThat(ex.getMessage()).isEqualTo("Invalid message structure: missing required field");
63+
assertThat(ex.getCause()).isNull();
64+
}
65+
66+
@Test
67+
void testUnsupportedContentTypeFactory() {
68+
MessageConversionException ex = MessageConversionException.unsupportedContentType("video/mp4");
69+
70+
assertThat(ex.getMessage()).isEqualTo("Unsupported content type: video/mp4");
71+
assertThat(ex.getCause()).isNull();
72+
}
73+
74+
@Test
75+
void testExceptionInMessageConverter() {
76+
// This test verifies that MessageConverter throws the custom exception
77+
MessageConverter converter = new MessageConverter(new ObjectMapper());
78+
79+
// Create an AssistantMessage with invalid JSON in tool call arguments
80+
AssistantMessage.ToolCall invalidToolCall =
81+
new AssistantMessage.ToolCall("id123", "function", "test_function", "invalid json{");
82+
AssistantMessage assistantMessage =
83+
new AssistantMessage("Test", java.util.Map.of(), java.util.List.of(invalidToolCall));
84+
85+
// This should throw MessageConversionException due to invalid JSON
86+
Exception exception =
87+
assertThrows(
88+
Exception.class,
89+
() -> {
90+
// Use reflection to access private method for testing
91+
java.lang.reflect.Method method =
92+
MessageConverter.class.getDeclaredMethod(
93+
"convertAssistantMessageToContent", AssistantMessage.class);
94+
method.setAccessible(true);
95+
method.invoke(converter, assistantMessage);
96+
});
97+
98+
// When using reflection, the exception is wrapped in InvocationTargetException
99+
assertThat(exception).isInstanceOf(java.lang.reflect.InvocationTargetException.class);
100+
Throwable cause = exception.getCause();
101+
assertThat(cause).isInstanceOf(MessageConversionException.class);
102+
assertThat(cause.getMessage()).contains("Failed to parse JSON for tool call arguments");
103+
assertThat(cause.getCause()).isInstanceOf(JsonProcessingException.class);
104+
}
105+
}

0 commit comments

Comments
 (0)