Skip to content

Commit

Permalink
fix(streaming): correctly handle trailing new lines in byte chunks (#708
Browse files Browse the repository at this point in the history
)
  • Loading branch information
stainless-bot authored Mar 6, 2024
1 parent d144789 commit 4753be2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class SSEDecoder {
*
* https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
*/
class LineDecoder {
export class LineDecoder {
// prettier-ignore
static NEWLINE_CHARS = new Set(['\n', '\r', '\x0b', '\x0c', '\x1c', '\x1d', '\x1e', '\x85', '\u2028', '\u2029']);
static NEWLINE_REGEXP = /\r\n|[\n\r\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029]/g;
Expand Down Expand Up @@ -300,6 +300,12 @@ class LineDecoder {
const trailingNewline = LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || '');
let lines = text.split(LineDecoder.NEWLINE_REGEXP);

// if there is a trailing new line then the last entry will be an empty
// string which we don't care about
if (trailingNewline) {
lines.pop();
}

if (lines.length === 1 && !trailingNewline) {
this.buffer.push(lines[0]!);
return [];
Expand Down
42 changes: 42 additions & 0 deletions tests/streaming.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { LineDecoder } from 'openai/streaming';

function decodeChunks(chunks: string[], decoder?: LineDecoder): string[] {
if (!decoder) {
decoder = new LineDecoder();
}

const lines = [];
for (const chunk of chunks) {
lines.push(...decoder.decode(chunk));
}

return lines;
}

describe('line decoder', () => {
test('basic', () => {
// baz is not included because the line hasn't ended yet
expect(decodeChunks(['foo', ' bar\nbaz'])).toEqual(['foo bar']);
});

test('basic with \\r', () => {
// baz is not included because the line hasn't ended yet
expect(decodeChunks(['foo', ' bar\r\nbaz'])).toEqual(['foo bar']);
});

test('trailing new lines', () => {
expect(decodeChunks(['foo', ' bar', 'baz\n', 'thing\n'])).toEqual(['foo barbaz', 'thing']);
});

test('trailing new lines with \\r', () => {
expect(decodeChunks(['foo', ' bar', 'baz\r\n', 'thing\r\n'])).toEqual(['foo barbaz', 'thing']);
});

test('escaped new lines', () => {
expect(decodeChunks(['foo', ' bar\\nbaz\n'])).toEqual(['foo bar\\nbaz']);
});

test('escaped new lines with \\r', () => {
expect(decodeChunks(['foo', ' bar\\r\\nbaz\n'])).toEqual(['foo bar\\r\\nbaz']);
});
});

0 comments on commit 4753be2

Please sign in to comment.