Streaming `parsed` snapshot drops every field after an empty array written as `[ ]`
Confirm this is a Node library issue and not an underlying OpenAI API issue
- [x] This is an issue with the Node library
Describe the bug
chat.completions.stream() with a JSON schema response format gives a wrong parsed value on content.delta events when the content contains an empty array with whitespace inside ([ ] or [\n], valid per RFC 8259 section 5). Everything after that array is missing from the snapshot, even when the content is already complete. content.done and finalChatCompletion() are correct, because they use JSON.parse. The cause is the vendored partial JSON parser (src/_vendor/partial-json-parser/parser.ts):
partialParse('{"tags":[ ],"title":"hello"}') // {"tags":[]}
partialParse('[[ ],1]') // [[]]
In parseArr, the while (jsonString[index] !== ']') check runs before whitespace is skipped. For [ ], parseAny throws on ], the catch returns without consuming ], and the parent array or object treats that ] as its own end. parsed_arguments on streamed tool calls uses the same function.
To Reproduce
- Save the snippet below as
repro.mjs. - Run
node repro.mjs. No network:fetchis replaced with a stub that returns an SSE body.
Code snippets
import OpenAI from 'openai';
const content = '{"tags":[ ],"title":"hello"}';
const base = { id: 'c1', object: 'chat.completion.chunk', created: 0, model: 'gpt-4o' };
const chunks = [
{ ...base, choices: [{ index: 0, delta: { role: 'assistant', content: '' }, finish_reason: null }] },
{ ...base, choices: [{ index: 0, delta: { content }, finish_reason: null }] },
{ ...base, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }] },
];
const sse = chunks.map((c) => `data: ${JSON.stringify(c)}\n\n`).join('') + 'data: [DONE]\n\n';
const client = new OpenAI({
apiKey: 'test',
fetch: async () => new Response(sse, { headers: { 'content-type': 'text/event-stream' } }),
});
const stream = client.chat.completions.stream({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'x' }],
response_format: {
type: 'json_schema',
json_schema: {
name: 'r',
strict: true,
schema: {
type: 'object',
properties: { tags: { type: 'array', items: { type: 'string' } }, title: { type: 'string' } },
required: ['tags', 'title'],
additionalProperties: false,
},
},
},
});
stream.on('content.delta', (e) => console.log('content.delta parsed:', JSON.stringify(e.parsed)));
stream.on('content.done', (e) => console.log('content.done parsed:', JSON.stringify(e.parsed)));
const fin = await stream.finalChatCompletion();
console.log('final parsed:', JSON.stringify(fin.choices[0].message.parsed));
Output:
content.delta parsed: {"tags":[]}
content.done parsed: {"tags":[],"title":"hello"}
final parsed: {"tags":[],"title":"hello"}
Expected content.delta parsed: {"tags":[],"title":"hello"}. Calling skipBlank() before the loop condition in parseArr fixes it.
OS
Linux x86_64
Node version
Node v26.13.0
Library version
openai v7.53.0 (also main at 47bade0)
Credit: https://ucsbplse.github.io/
I looked into this and can confirm the root cause and the suggested fix direction.
In src/_vendor/partial-json-parser/parser.ts, parseArr checks jsonString[index] !== ']' before whitespace is skipped. For [ ], the space enters the loop, parseAny skips the blank and throws on ], and the catch in parseArr returns the partial array without consuming ]. The enclosing object/array then reads that ] as its own terminator, so everything after the empty array is dropped from the incremental snapshot. parseObj doesn't have this problem because it calls skipBlank() before its loop condition.
I applied the fix locally (call skipBlank() right after consuming [ and again after each comma) and verified it against the repro plus extra cases:
partialParse('{"tags":[ ],"title":"hello"}')now gives{"tags":[],"title":"hello"}(was{"tags":[]})partialParse('[[ ],1]')now gives[[],1](was[[]])- partial-recovery behavior is unchanged:
'[1, '->[1],'[[ ],'->[[]], the exponent-recovery cases and the fast-check property tests all still pass
I also wrote a stream-level regression test with a stubbed SSE body confirming content.delta reports {"tags":[],"title":"hello"} and matches content.done / finalChatCompletion().
Since PRs here are limited to collaborators, I'm leaving this as analysis. Happy to share the full diff and test files if that helps whoever picks it up.