in google/generativeai/types/generation_types.py [0:0]
def __iter__(self):
# This is not thread safe.
if self._done:
for chunk in self._chunks:
yield GenerateContentResponse.from_response(chunk)
return
# Always have the next chunk available.
if len(self._chunks) == 0:
self._chunks.append(next(self._iterator))
for n in itertools.count():
if self._error:
raise self._error
if n >= len(self._chunks) - 1:
# Look ahead for a new item, so that you know the stream is done
# when you yield the last item.
if self._done:
return
try:
item = next(self._iterator)
except StopIteration:
self._done = True
except Exception as e:
self._error = e
self._done = True
else:
self._chunks.append(item)
self._result = _join_chunks([self._result, item])
item = self._chunks[n]
item = GenerateContentResponse.from_response(item)
yield item