Skip to content

Commit 42a13a3

Browse files
authored
fix(message): message deserialization errror (#156)
1 parent fd4f753 commit 42a13a3

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

src/main/java/com/alibaba/dashscope/common/MessageAdapter.java

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,69 @@ private void writeCallFunction(JsonWriter out, ToolCallFunction toolCall) throws
119119
out.endObject();
120120
}
121121

122+
// Parse array of content objects
123+
@SuppressWarnings({"unchecked", "rawtypes"})
124+
private List<MessageContentBase> parseContentList(List<LinkedTreeMap> contentList) {
125+
List<MessageContentBase> contents = new ArrayList<>();
126+
for (LinkedTreeMap<String, Object> contentItem : contentList) {
127+
String type = (String) contentItem.get("type");
128+
if (ApiKeywords.CONTENT_TYPE_TEXT.equals(type)) {
129+
contents.add(parseTextContent(contentItem));
130+
} else if (ApiKeywords.CONTENT_TYPE_IMAGE_URL.equals(type)) {
131+
contents.add(parseImageContent(contentItem));
132+
}
133+
}
134+
return contents;
135+
}
136+
137+
// Parse text content with optional cache_control
138+
@SuppressWarnings({"unchecked", "rawtypes"})
139+
private MessageContentText parseTextContent(LinkedTreeMap<String, Object> contentItem) {
140+
MessageContentText.MessageContentTextBuilder textBuilder =
141+
MessageContentText.builder()
142+
.type((String) contentItem.get("type"))
143+
.text((String) contentItem.get(ApiKeywords.CONTENT_TYPE_TEXT));
144+
145+
// Parse cache_control if present
146+
if (contentItem.containsKey(ApiKeywords.CONTENT_TYPE_CACHE_CONTROL)) {
147+
LinkedTreeMap<String, Object> cacheControlMap =
148+
(LinkedTreeMap<String, Object>) contentItem.get(
149+
ApiKeywords.CONTENT_TYPE_CACHE_CONTROL);
150+
MessageContentText.CacheControl.CacheControlBuilder cacheBuilder =
151+
MessageContentText.CacheControl.builder()
152+
.type((String) cacheControlMap.get("type"));
153+
154+
// Handle ttl field - convert to String regardless of input type
155+
if (cacheControlMap.containsKey("ttl")) {
156+
Object ttlObj = cacheControlMap.get("ttl");
157+
cacheBuilder.ttl(ttlObj instanceof Number
158+
? String.valueOf(((Number) ttlObj).intValue())
159+
: String.valueOf(ttlObj));
160+
}
161+
textBuilder.cacheControl(cacheBuilder.build());
162+
}
163+
return textBuilder.build();
164+
}
165+
166+
// Parse image_url content
167+
@SuppressWarnings({"unchecked", "rawtypes"})
168+
private MessageContentImageURL parseImageContent(
169+
LinkedTreeMap<String, Object> contentItem) {
170+
LinkedTreeMap<String, Object> imageUrlMap =
171+
(LinkedTreeMap<String, Object>) contentItem.get(
172+
ApiKeywords.CONTENT_TYPE_IMAGE_URL);
173+
ImageURL.ImageURLBuilder imageBuilder =
174+
ImageURL.builder().url((String) imageUrlMap.get("url"));
175+
if (imageUrlMap.containsKey("detail")) {
176+
imageBuilder.detail((String) imageUrlMap.get("detail"));
177+
}
178+
return MessageContentImageURL.builder()
179+
.type((String) contentItem.get("type"))
180+
.imageURL(imageBuilder.build())
181+
.build();
182+
}
183+
184+
@SuppressWarnings("unchecked")
122185
private ToolCallFunction convertToCallFunction(LinkedTreeMap<String, Object> toolCall) {
123186
ToolCallFunction functionCall = new ToolCallFunction();
124187
if (toolCall.containsKey("function")) {
@@ -147,6 +210,7 @@ private ToolCallFunction convertToCallFunction(LinkedTreeMap<String, Object> too
147210
}
148211

149212
@Override
213+
@SuppressWarnings({"unchecked", "rawtypes"})
150214
public Message read(JsonReader in) throws IOException {
151215
Map<String, Object> objectMap = JsonUtils.gson.fromJson(in, Map.class);
152216
Message msg = new Message();
@@ -157,7 +221,13 @@ public Message read(JsonReader in) throws IOException {
157221
}
158222

159223
if (objectMap.containsKey(ApiKeywords.CONTENT)) {
160-
msg.setContent((String) objectMap.get(ApiKeywords.CONTENT));
224+
Object contentObj = objectMap.get(ApiKeywords.CONTENT);
225+
// Handle both string and array content types
226+
if (contentObj instanceof String) {
227+
msg.setContent((String) contentObj);
228+
} else if (contentObj instanceof List) {
229+
msg.setContents(parseContentList((List<LinkedTreeMap>) contentObj));
230+
}
161231
objectMap.remove(ApiKeywords.CONTENT);
162232
}
163233

0 commit comments

Comments
 (0)