Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TextDecoderStream is producing partial chunk

I am constructing a reader using response.body.pipeThrough(new TextDecoderStream()).getReader().

I then loop through the read output and parse what is supposed to be valid JSON.

The complete implementation can be seen here:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

https://github.com/lucgagan/completions/blob/fe34b3fdd9b22f630b8b48f8a07ac87d759ff7db/src/createCompletions.ts

It is supposed to produce output similar to:

data: {"id":"chatcmpl-7YkvP6XEACyebJa9awrHeEENrAQNc","object":"chat.completion.chunk","created":1688517887,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"role":"assistant","content":null,"function_call":{"name":"print_tags","arguments":""}},"finish_reason":null}]}

data: {"id":"chatcmpl-7YkvP6XEACyebJa9awrHeEENrAQNc","object":"chat.completion.chunk","created":1688517887,"model":"gpt-3.5-turbo-0613","choices":[{"index":0,"delta":{"function_call":{"arguments":"{\n"}},"finish_reason":null}]}

data: {"id":"chatcmpl-7YkvP6XEACyebJa9awrHeEENrAQNc","object":"chat.completion.chunk","created":168851788

The above is what I get from reader.read() here.

However, notice that the last line got truncated.

I assume this is happening because of some sort of misconfiguration of TextDecoderStream or the reader.

What needs to be done to ensure that I am receiving the complete chunk?

Adding tag because this might be a bug in their/specific to their API.

>Solution :

Each chunk you receive from a TextDecoderStream is supposed to be valid UTF-8 text not valid JSON.

To be 100% sure you have all the data, you need to wait for it all to be downloaded, at which point you may just use the Response#json() method instead.

Another possible solution would be to use a streaming JSON parser and feed it with each chunk. You can check this Q/A.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading