summary
Check if
content
isNone
finish_reason
is"stop"
This way you can determine when an OpenAI API response has finished streaming. You can use it to set up further processing (e.g. saving full response, displaying to the user, etc.).
method 1 :: check if content
is None
In each chunk, the content field will be !None
except for the last chunk. By checking when the content chunk became None
, you can identify the end of the stream.
|
|
Outputs:
|
|
method 2 :: check if finish_reason
is stop
In the last chunk, the finish_reason
value will be set to "stop"
. This can be used as a signal that the stream has ended.
|
|
Outputs:
|
|
Context
In my tldr project I am streaming the response to the user, but I also want to store it. So I needed a way to understand when the last chunk has arrived so that I can process the response further. I found two methods to determine when the streaming has completed and wanted to share them.
Feel free to share your thoughts/methods here in the comments or under the post on 𝕏.