Skip to content

Commit c3c99d3

Browse files
authored
Fix errors caused by setting ChatResponse.last (#19)
* Fix error when setting 'last'. Message history should contain dicts. * Add test.
1 parent 37fb1be commit c3c99d3

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

google/generativeai/discuss.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ def last(self) -> Optional[str]:
399399
@last.setter
400400
def last(self, message: discuss_types.MessageOptions):
401401
message = _make_message(message)
402+
message = type(message).to_dict(message)
402403
self.messages[-1] = message
403404

404405
@set_doc(discuss_types.ChatResponse.reply.__doc__)

tests/test_discuss.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ def test_chat_citations(self):
344344
)
345345
self.assertLen(response.messages, 4)
346346

347+
def test_set_last(self):
348+
response = discuss.chat(messages="Can you overwrite `.last`?")
349+
response.last = 'yes'
350+
response = response.reply('glad to hear it!')
351+
response.last = "Me too!"
352+
self.assertEqual([msg['content'] for msg in response.messages],
353+
["Can you overwrite `.last`?", 'yes', 'glad to hear it!', 'Me too!'])
347354

348355
if __name__ == "__main__":
349356
absltest.main()

0 commit comments

Comments
 (0)