Skip to content

Commit 8031df3

Browse files
committed
feat(api): run polling helpers (#749)
refactor: rename createAndStream to stream
1 parent bc202fc commit 8031df3

File tree

12 files changed

+389
-20
lines changed

12 files changed

+389
-20
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,30 @@ Documentation for each method, request param, and response field are available i
100100
> [!IMPORTANT]
101101
> Previous versions of this SDK used a `Configuration` class. See the [v3 to v4 migration guide](https://github.com/openai/openai-node/discussions/217).
102102
103+
### Polling Helpers
104+
105+
When interacting with the API some actions such as starting a Run may take time to complete. The SDK includes
106+
helper functions which will poll the status until it reaches a terminal state and then return the resulting object.
107+
If an API method results in an action which could benefit from polling there will be a corresponding version of the
108+
method ending in 'AndPoll'.
109+
110+
For instance to create a Run and poll until it reaches a terminal state you can run:
111+
112+
```ts
113+
const run = await openai.beta.threads.runs.createAndPoll(thread.id, {
114+
assistant_id: assistantId,
115+
});
116+
```
117+
118+
More information on the lifecycle of a Run can be found in the [Run Lifecycle Documentation](https://platform.openai.com/docs/assistants/how-it-works/run-lifecycle)
119+
103120
### Streaming Helpers
104121

105122
The SDK also includes helpers to process streams and handle the incoming events.
106123

107124
```ts
108125
const run = openai.beta.threads.runs
109-
.createAndStream(thread.id, {
126+
.stream(thread.id, {
110127
assistant_id: assistant.id,
111128
})
112129
.on('textCreated', (text) => process.stdout.write('\nassistant > '))

api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ Methods:
224224
- <code title="post /threads/{thread_id}">client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">update</a>(threadId, { ...params }) -> Thread</code>
225225
- <code title="delete /threads/{thread_id}">client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">del</a>(threadId) -> ThreadDeleted</code>
226226
- <code title="post /threads/runs">client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">createAndRun</a>({ ...params }) -> Run</code>
227+
- <code>client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">createAndRunPoll</a>(body, options?) -> Promise&lt;Threads.Run&gt;</code>
227228
- <code>client.beta.threads.<a href="./src/resources/beta/threads/threads.ts">createAndRunStream</a>(body, options?) -> AssistantStream</code>
228229

229230
### Runs
@@ -242,7 +243,11 @@ Methods:
242243
- <code title="get /threads/{thread_id}/runs">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">list</a>(threadId, { ...params }) -> RunsPage</code>
243244
- <code title="post /threads/{thread_id}/runs/{run_id}/cancel">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">cancel</a>(threadId, runId) -> Run</code>
244245
- <code title="post /threads/{thread_id}/runs/{run_id}/submit_tool_outputs">client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">submitToolOutputs</a>(threadId, runId, { ...params }) -> Run</code>
246+
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">createAndPoll</a>(threadId, body, options?) -> Promise&lt;Run&gt;</code>
245247
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">createAndStream</a>(threadId, body, options?) -> AssistantStream</code>
248+
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">poll</a>(threadId, runId, options?) -> Promise&lt;Run&gt;</code>
249+
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">stream</a>(threadId, body, options?) -> AssistantStream</code>
250+
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">submitToolOutputsAndPoll</a>(threadId, runId, body, options?) -> Promise&lt;Run&gt;</code>
246251
- <code>client.beta.threads.runs.<a href="./src/resources/beta/threads/runs/runs.ts">submitToolOutputsStream</a>(threadId, runId, body, options?) -> AssistantStream</code>
247252

248253
#### Steps

examples/assistant-stream-raw.ts

100644100755
File mode changed.

examples/assistant-stream.ts

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async function main() {
3131
console.log('Created thread with Id: ' + threadId);
3232

3333
const run = openai.beta.threads.runs
34-
.createAndStream(threadId, {
34+
.stream(threadId, {
3535
assistant_id: assistantId,
3636
})
3737
//Subscribe to streaming events and log them

examples/assistants.ts

100644100755
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env -S npm run tsn -T
22

33
import OpenAI from 'openai';
4-
import { sleep } from 'openai/core';
54

65
/**
76
* Example of polling for a complete response from an assistant
@@ -32,24 +31,17 @@ async function main() {
3231
let threadId = thread.id;
3332
console.log('Created thread with Id: ' + threadId);
3433

35-
const run = await openai.beta.threads.runs.create(thread.id, {
34+
const run = await openai.beta.threads.runs.createAndPoll(thread.id, {
3635
assistant_id: assistantId,
3736
additional_instructions: 'Please address the user as Jane Doe. The user has a premium account.',
3837
});
3938

40-
console.log('Created run with Id: ' + run.id);
41-
42-
while (true) {
43-
const result = await openai.beta.threads.runs.retrieve(thread.id, run.id);
44-
if (result.status == 'completed') {
45-
const messages = await openai.beta.threads.messages.list(thread.id);
46-
for (const message of messages.getPaginatedItems()) {
47-
console.log(message);
48-
}
49-
break;
50-
} else {
51-
console.log('Waiting for completion. Current status: ' + result.status);
52-
await sleep(5000);
39+
console.log('Run finished with status: ' + run.status);
40+
41+
if (run.status == 'completed') {
42+
const messages = await openai.beta.threads.messages.list(thread.id);
43+
for (const message of messages.getPaginatedItems()) {
44+
console.log(message);
5345
}
5446
}
5547
}

helpers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ More information can be found in the documentation: [Assistant Streaming](https:
1313

1414
```ts
1515
const run = openai.beta.threads.runs
16-
.createAndStream(thread.id, {
16+
.stream(thread.id, {
1717
assistant_id: assistant.id,
1818
})
1919
.on('textCreated', (text) => process.stdout.write('\nassistant > '))
@@ -41,7 +41,7 @@ const run = openai.beta.threads.runs
4141
There are three helper methods for creating streams:
4242

4343
```ts
44-
openai.beta.threads.runs.createAndStream();
44+
openai.beta.threads.runs.stream();
4545
```
4646

4747
This method can be used to start and stream the response to an existing run with an associated thread

src/resources/beta/beta.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ export namespace Beta {
3737
export import ThreadCreateAndRunParams = ThreadsAPI.ThreadCreateAndRunParams;
3838
export import ThreadCreateAndRunParamsNonStreaming = ThreadsAPI.ThreadCreateAndRunParamsNonStreaming;
3939
export import ThreadCreateAndRunParamsStreaming = ThreadsAPI.ThreadCreateAndRunParamsStreaming;
40+
export import ThreadCreateAndRunPollParams = ThreadsAPI.ThreadCreateAndRunPollParams;
4041
export import ThreadCreateAndRunStreamParams = ThreadsAPI.ThreadCreateAndRunStreamParams;
4142
}

src/resources/beta/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export {
2828
ThreadCreateAndRunParams,
2929
ThreadCreateAndRunParamsNonStreaming,
3030
ThreadCreateAndRunParamsStreaming,
31+
ThreadCreateAndRunPollParams,
3132
ThreadCreateAndRunStreamParams,
3233
Threads,
3334
} from './threads/index';

src/resources/beta/threads/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ export {
3636
RunCreateParamsStreaming,
3737
RunUpdateParams,
3838
RunListParams,
39+
RunCreateAndPollParams,
3940
RunCreateAndStreamParams,
41+
RunStreamParams,
4042
RunSubmitToolOutputsParams,
4143
RunSubmitToolOutputsParamsNonStreaming,
4244
RunSubmitToolOutputsParamsStreaming,
45+
RunSubmitToolOutputsAndPollParams,
4346
RunSubmitToolOutputsStreamParams,
4447
RunsPage,
4548
Runs,
@@ -52,6 +55,7 @@ export {
5255
ThreadCreateAndRunParams,
5356
ThreadCreateAndRunParamsNonStreaming,
5457
ThreadCreateAndRunParamsStreaming,
58+
ThreadCreateAndRunPollParams,
5559
ThreadCreateAndRunStreamParams,
5660
Threads,
5761
} from './threads';

src/resources/beta/threads/runs/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ export {
3131
RunCreateParamsStreaming,
3232
RunUpdateParams,
3333
RunListParams,
34+
RunCreateAndPollParams,
3435
RunCreateAndStreamParams,
36+
RunStreamParams,
3537
RunSubmitToolOutputsParams,
3638
RunSubmitToolOutputsParamsNonStreaming,
3739
RunSubmitToolOutputsParamsStreaming,
40+
RunSubmitToolOutputsAndPollParams,
3841
RunSubmitToolOutputsStreamParams,
3942
RunsPage,
4043
Runs,

0 commit comments

Comments
 (0)