Submit tool ouputs to run

When a run has the `status: "requires_action"` and `required_action.type` is `submit_tool_outputs`, this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request.

Script openai Verified

by hugo697 ยท 12/1/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 372 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
/**
6
 * Submit tool ouputs to run
7
 * When a run has the `status: "requires_action"` and `required_action.type` is `submit_tool_outputs`, this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request.
8

9
 */
10
export async function main(
11
  auth: Openai,
12
  thread_id: string,
13
  run_id: string,
14
  body: {
15
    tool_outputs: {
16
      tool_call_id?: string;
17
      output?: string;
18
      [k: string]: unknown;
19
    }[];
20
  }
21
) {
22
  const url = new URL(
23
    `https://api.openai.com/v1/threads/${thread_id}/runs/${run_id}/submit_tool_outputs`
24
  );
25

26
  const response = await fetch(url, {
27
    method: "POST",
28
    headers: {
29
      "OpenAI-Organization": auth.organization_id,
30
      "Content-Type": "application/json",
31
      Authorization: "Bearer " + auth.api_key,
32
    },
33
    body: JSON.stringify(body),
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41