Skip to content

Commit 6082f2f

Browse files
authored
Default the role to 'model' for chat responses (#205)
Fixes #197 A content requires a role when it is sent to the model in the history. If the backend happens to respond with a message that has no role, default it to 'model'.
1 parent 9ea128f commit 6082f2f

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

pkgs/google_generative_ai/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- Add support for model side Code Execution. Enable code execution by
44
configuring `Tools` with a `codeExecution` argument.
5+
- Use a default role `'model'` when a chat response comes back with no role.
56

67
## 0.4.4
78

pkgs/google_generative_ai/lib/src/chat.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ final class ChatSession {
7070
safetySettings: _safetySettings, generationConfig: _generationConfig);
7171
if (response.candidates case [final candidate, ...]) {
7272
_history.add(message);
73-
// TODO: Append role?
74-
_history.add(candidate.content);
73+
final normalizedContent = candidate.content.role == null
74+
? Content('model', candidate.content.parts)
75+
: candidate.content;
76+
_history.add(normalizedContent);
7577
}
7678
return response;
7779
} finally {

0 commit comments

Comments
 (0)