Commit 6d7856c
fix(model): auto-generate ChatResponse.id when LLM doesn't provide it (#721)
AgentScope-Java Version
1.0.9-SNAPSHOT
Description
This PR fixes Issue #708 where OllamaChatModel fails with the AGUI
protocol
due to missing id field in Ollama API responses.
Problem
Ollama API doesn't return an id field in its chat completion responses,
unlike other LLM providers (OpenAI, Anthropic, DashScope). This causes
the
AGUI protocol and other components that depend on ChatResponse.getId()
to
fail with NullPointerException: "messageId cannot be null".
Root Cause Chain:
Ollama API → no id field
↓
OllamaResponseParser → ChatResponse.id = null
↓
Msg.id = null
↓
Event.getMessage().getId() = null
↓
AguiEvent.TextMessageStart → NullPointerException
Solution
Modified ChatResponse.Builder.build() to automatically generate a UUID
when
id is null or empty. This provides a unified solution that:
1. Fixes the Ollama issue - Ollama responses now get auto-generated IDs
2. Benefits all parsers - Any future provider without an id field is
handled
automatically
3. Maintains backward compatibility - Providers that return an id keep
their
original value
4. Follows single responsibility principle - The Builder ensures valid
object
state
Changes
Modified Files:
- ChatResponse.java - Modified build() method to auto-generate UUID when
id
is null or empty
- AnthropicResponseParserTest.java - Updated test assertion to verify
auto-generated IDs
Code Change:
public ChatResponse build() {
// Auto-generate id if not set or empty (for providers like Ollama)
String responseId = this.id;
if (responseId == null || responseId.isEmpty()) {
responseId = UUID.randomUUID().toString();
}
return new ChatResponse(responseId, content, usage, metadata,
finishReason);
}
Testing
Manual Testing:
- Created reproduction test using OllamaChatModel with AGUI protocol
- Verified that all events now contain valid messageId values
- Tested with Ollama model qwen2.5:0.5b running locally
Automated Testing:
- All existing tests pass (3289 tests, 0 failures, 118 skipped)
- Updated AnthropicResponseParserTest to reflect new behavior
Checklist
- Code has been formatted with mvn spotless:apply
- All tests are passing (mvn test)
- Javadoc comments are complete and follow project conventions
- Related documentation has been updated (e.g. links, examples, etc.)
- Code is ready for review
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>1 parent f83ce3c commit 6d7856c
File tree
2 files changed
+14
-2
lines changed- agentscope-core/src
- main/java/io/agentscope/core/model
- test/java/io/agentscope/core/formatter/anthropic
2 files changed
+14
-2
lines changedLines changed: 11 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
179 | 184 | | |
180 | 185 | | |
181 | 186 | | |
182 | 187 | | |
183 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
184 | 194 | | |
185 | 195 | | |
186 | 196 | | |
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
324 | 325 | | |
325 | 326 | | |
326 | 327 | | |
327 | | - | |
| 328 | + | |
| 329 | + | |
328 | 330 | | |
329 | 331 | | |
330 | 332 | | |
| |||
0 commit comments