Skip to content
Open
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 @@ -18,6 +18,7 @@
import io.agentscope.core.message.ContentBlock;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* Represents a chat completion response from a language model.
Expand Down Expand Up @@ -176,11 +177,20 @@ public Builder finishReason(String finishReason) {

/**
* Builds a new ChatResponse instance with the set values.
* <p>
* If no id was set (or is empty/blank), a random UUID will be generated
* automatically. This ensures compatibility with LLM providers (like Ollama)
* that don't return an id field in their API responses.
*
* @return a new ChatResponse instance
*/
public ChatResponse build() {
return new ChatResponse(id, content, usage, metadata, finishReason);
// Auto-generate id if not set, empty, or blank (for providers like Ollama)
String responseId = this.id;
if (responseId == null || responseId.trim().isEmpty()) {
responseId = UUID.randomUUID().toString();
}
return new ChatResponse(responseId, content, usage, metadata, finishReason);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.agentscope.core.formatter.anthropic;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -324,7 +325,8 @@ void testParseStreamEventUnknownType() throws Exception {
ChatResponse response = invokeParseStreamEvent(event, startTime);

assertNotNull(response);
assertNull(response.getId());
assertNotNull(response.getId()); // Builder auto-generates UUID when id is null
assertFalse(response.getId().isEmpty());
assertTrue(response.getContent().isEmpty());
assertNull(response.getUsage());
}
Expand Down
Loading