Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(streaming): correctly handle trailing new lines in byte chunks #708

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
});
});