PythiloLesson 7 of 12 · progress kept in this browser

Streaming responses

A reply that appears word by word feels faster than one that arrives whole. Foundation Models streams with streamResponse, and each element it yields is a snapshot: the whole response so far, not just the new part. Many cloud APIs send the new part instead, called a delta.

To append to a log, or to speak the reply aloud, you need deltas: the part of each snapshot that the previous one did not have.

before = "The cat"
after = "The cat sat"
print(repr(after[len(before):]))

Speech and subtitles should not show half a sentence. A sentence ends at a full stop, question mark or exclamation mark followed by a space or the end of the text, so the full stop in 3.14 does not count.

Your task

Write deltas(snapshots) returning the list of non-empty new parts, raising ValueError if a snapshot does not start with the previous one. Write complete_sentences(text) returning text up to and including the last ., ! or ? that is followed by whitespace or the end, or "" when there is none.

Next: Tool calling