Skip to content
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
5 changes: 4 additions & 1 deletion examples/responses/stream_background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ async function main() {
});

let id: string | null = null;
let lastSequenceNumber = -1;
let completed = false;

for await (const event of runner) {
lastSequenceNumber = event.sequence_number;

if (event.type === 'response.created') {
id = event.response.id;
}
Expand All @@ -41,7 +44,7 @@ async function main() {

const runner2 = openai.responses.stream({
response_id: id!,
starting_after: 10,
starting_after: lastSequenceNumber,
});

for await (const event of runner2) {
Expand Down
9 changes: 8 additions & 1 deletion tests/lib/background-streaming-example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ test.each(cases)('$source: $status $name', async ({ chunks, resumed, partial, st
const events = responseEvents(chunks, status, background);
const nonterminal = status === 'queued' || status === 'in_progress';
const body = `${events.map((event) => `data: ${JSON.stringify(event)}`).join('\n\n')}\n\n${nonterminal ? '' : 'data: [DONE]\n\n'}`;
const partialEvents = nonterminal ? events : events.slice(0, -1);
// Disconnect before the final delta as well as the terminal event.
const partialEvents = nonterminal ? events : events.slice(0, -2);
const partialBody = `${partialEvents.map((event) => `data: ${JSON.stringify(event)}`).join('\n\n')}\n\n`;
const requests: {
method: string | undefined;
Expand Down Expand Up @@ -220,6 +221,8 @@ main();`,
OPENAI_BASE_URL: `http://127.0.0.1:${address.port}/v1`,
OPENAI_CUSTOM_HEADERS: undefined,
OPENAI_LOG: undefined,
// Keep console-inspected event numbers plain for the sequence assertions.
FORCE_COLOR: undefined,
TS_NODE_PROJECT: path.join(root, 'tsconfig.json'),
DISABLE_V8_COMPILE_CACHE: '1',
},
Expand Down Expand Up @@ -270,6 +273,10 @@ main();`,
}
if (source === 'example') {
expect(stdout.includes('Interrupted. Continuing...')).toBe(resumed);
const seenSequences = [...stdout.matchAll(/sequence_number:\s*(?<sequence>\d+)/gu)].map((match) =>
Number(match.groups?.['sequence']),
);
expect(seenSequences).toEqual(events.map((event) => event.sequence_number));
} else if (source === 'guide') {
expect(stdout + stderr).not.toContain(privateErrorDetail);
}
Expand Down
Loading