SpyBara
Go Premium

Documentation 2026-08-03 18:01 UTC to 2026-08-04 22:59 UTC

36 files changed +3,547 −3,535. View all changes and history on the product overview
2026
Sat 15 01:01 Fri 14 20:01 Thu 13 22:00 Wed 12 02:57 Tue 11 19:59 Mon 10 19:00 Fri 7 00:58 Thu 6 21:58 Wed 5 18:01 Tue 4 22:59 Mon 3 18:01
Details

23 23 

24For example, to create an Assistant that can create data visualization based on a `.csv` file, first upload a file.24For example, to create an Assistant that can create data visualization based on a `.csv` file, first upload a file.

25 25 

26```python

27file = client.files.create(

28 file=open("revenue-forecast.csv", "rb"), purpose="assistants"

29)

30```

31 

32```javascript26```javascript

33const file = await openai.files.create({27const file = await openai.files.create({

34 file: fs.createReadStream("revenue-forecast.csv"),28 file: fs.createReadStream("revenue-forecast.csv"),


36});30});

37```31```

38 32 

33```python

34file = client.files.create(

35 file=open("revenue-forecast.csv", "rb"), purpose="assistants"

36)

37```

38 

39```bash39```bash

40curl https://api.openai.com/v1/files \40curl https://api.openai.com/v1/files \

41 -H "Authorization: Bearer $OPENAI_API_KEY" \41 -H "Authorization: Bearer $OPENAI_API_KEY" \


46 46 

47Then, create the Assistant with the `code_interpreter` tool enabled and provide the file as a resource to the tool.47Then, create the Assistant with the `code_interpreter` tool enabled and provide the file as a resource to the tool.

48 48 

49```python

50assistant = client.beta.assistants.create(

51 name="Data visualizer",

52 description="You are great at creating beautiful data visualizations. You analyze data present in .csv files, understand trends, and come up with data visualizations relevant to those trends. You also share a brief text summary of the trends observed.",

53 model="gpt-4o",

54 tools=[{"type": "code_interpreter"}],

55 tool_resources={"code_interpreter": {"file_ids": [file.id]}},

56)

57```

58 

59```javascript49```javascript

60const assistant = await openai.beta.assistants.create({50const assistant = await openai.beta.assistants.create({

61 name: "Data visualizer",51 name: "Data visualizer",


71});61});

72```62```

73 63 

64```python

65assistant = client.beta.assistants.create(

66 name="Data visualizer",

67 description="You are great at creating beautiful data visualizations. You analyze data present in .csv files, understand trends, and come up with data visualizations relevant to those trends. You also share a brief text summary of the trends observed.",

68 model="gpt-4o",

69 tools=[{"type": "code_interpreter"}],

70 tool_resources={"code_interpreter": {"file_ids": [file.id]}},

71)

72```

73 

74```bash74```bash

75curl https://api.openai.com/v1/assistants \75curl https://api.openai.com/v1/assistants \

76 -H "Authorization: Bearer $OPENAI_API_KEY" \76 -H "Authorization: Bearer $OPENAI_API_KEY" \


100 100 

101You can create a Thread with an initial list of Messages like this:101You can create a Thread with an initial list of Messages like this:

102 102 

103```python

104thread = client.beta.threads.create(

105 messages=[

106 {

107 "role": "user",

108 "content": "Create 3 data visualizations based on the trends in this file.",

109 "attachments": [

110 {"file_id": file.id, "tools": [{"type": "code_interpreter"}]}

111 ],

112 }

113 ]

114)

115```

116 

117```javascript103```javascript

118const thread = await openai.beta.threads.create({104const thread = await openai.beta.threads.create({

119 messages: [105 messages: [


131});117});

132```118```

133 119 

120```python

121thread = client.beta.threads.create(

122 messages=[

123 {

124 "role": "user",

125 "content": "Create 3 data visualizations based on the trends in this file.",

126 "attachments": [

127 {"file_id": file.id, "tools": [{"type": "code_interpreter"}]}

128 ],

129 }

130 ]

131)

132```

133 

134```bash134```bash

135curl https://api.openai.com/v1/threads \135curl https://api.openai.com/v1/threads \

136 -H "Authorization: Bearer $OPENAI_API_KEY" \136 -H "Authorization: Bearer $OPENAI_API_KEY" \


161 161 

162Tools cannot access image content unless specified. To pass image files to Code Interpreter, add the file ID in the message `attachments` list to allow the tool to read and analyze the input. Image URLs cannot be downloaded in Code Interpreter today.162Tools cannot access image content unless specified. To pass image files to Code Interpreter, add the file ID in the message `attachments` list to allow the tool to read and analyze the input. Image URLs cannot be downloaded in Code Interpreter today.

163 163 

164```python

165file = client.files.create(file=open("myimage.png", "rb"), purpose="vision")

166thread = client.beta.threads.create(

167 messages=[

168 {

169 "role": "user",

170 "content": [

171 {

172 "type": "text",

173 "text": "What is the difference between these images?",

174 },

175 {

176 "type": "image_url",

177 "image_url": {

178 "url": "https://openai-documentation.vercel.app/images/cat_and_otter.png"

179 },

180 },

181 {"type": "image_file", "image_file": {"file_id": file.id}},

182 ],

183 }

184 ]

185)

186```

187 

188```javascript164```javascript

189import fs from "fs";165import fs from "fs";

190 166 


217});193});

218```194```

219 195 

196```python

197file = client.files.create(file=open("myimage.png", "rb"), purpose="vision")

198thread = client.beta.threads.create(

199 messages=[

200 {

201 "role": "user",

202 "content": [

203 {

204 "type": "text",

205 "text": "What is the difference between these images?",

206 },

207 {

208 "type": "image_url",

209 "image_url": {

210 "url": "https://openai-documentation.vercel.app/images/cat_and_otter.png"

211 },

212 },

213 {"type": "image_file", "image_file": {"file_id": file.id}},

214 ],

215 }

216 ]

217)

218```

219 

220```bash220```bash

221# Upload a file with an "vision" purpose221# Upload a file with an "vision" purpose

222curl https://api.openai.com/v1/files \222curl https://api.openai.com/v1/files \


261- `low` will enable the "low res" mode. The model will receive a low-res 512px x 512px version of the image, and represent the image with a budget of 85 tokens. This allows the API to return faster responses and consume fewer input tokens for use cases that do not require high detail.261- `low` will enable the "low res" mode. The model will receive a low-res 512px x 512px version of the image, and represent the image with a budget of 85 tokens. This allows the API to return faster responses and consume fewer input tokens for use cases that do not require high detail.

262- `high` will enable "high res" mode, which first allows the model to see the low res image and then creates detailed crops of input images based on the input image size. Use the [pricing calculator](https://openai.com/api/pricing/) to see token counts for various image sizes.262- `high` will enable "high res" mode, which first allows the model to see the low res image and then creates detailed crops of input images based on the input image size. Use the [pricing calculator](https://openai.com/api/pricing/) to see token counts for various image sizes.

263 263 

264```python

265thread = client.beta.threads.create(

266 messages=[

267 {

268 "role": "user",

269 "content": [

270 {"type": "text", "text": "What is this an image of?"},

271 {

272 "type": "image_url",

273 "image_url": {

274 "url": "https://openai-documentation.vercel.app/images/cat_and_otter.png",

275 "detail": "high",

276 },

277 },

278 ],

279 }

280 ]

281)

282```

283 

284```javascript264```javascript

285const thread = await openai.beta.threads.create({265const thread = await openai.beta.threads.create({

286 messages: [266 messages: [


304});284});

305```285```

306 286 

287```python

288thread = client.beta.threads.create(

289 messages=[

290 {

291 "role": "user",

292 "content": [

293 {"type": "text", "text": "What is this an image of?"},

294 {

295 "type": "image_url",

296 "image_url": {

297 "url": "https://openai-documentation.vercel.app/images/cat_and_otter.png",

298 "detail": "high",

299 },

300 },

301 ],

302 }

303 ]

304)

305```

306 

307```bash307```bash

308curl https://api.openai.com/v1/threads \308curl https://api.openai.com/v1/threads \

309 -H "Authorization: Bearer $OPENAI_API_KEY" \309 -H "Authorization: Bearer $OPENAI_API_KEY" \


415 415 

416When you have all the context you need from your user in the Thread, you can run the Thread with an Assistant of your choice.416When you have all the context you need from your user in the Thread, you can run the Thread with an Assistant of your choice.

417 417 

418```javascript

419const run = await openai.beta.threads.runs.create(thread.id, {

420 assistant_id: assistant.id,

421});

422```

423 

418```python424```python

419run = client.beta.threads.runs.create(425run = client.beta.threads.runs.create(

420 thread_id=thread.id,426 thread_id=thread.id,


422)428)

423```429```

424 430 

425```javascript

426const run = await openai.beta.threads.runs.create(thread.id, {

427 assistant_id: assistant.id,

428});

429```

430 

431```bash431```bash

432curl https://api.openai.com/v1/threads/THREAD_ID/runs \432curl https://api.openai.com/v1/threads/THREAD_ID/runs \

433 -H "Authorization: Bearer $OPENAI_API_KEY" \433 -H "Authorization: Bearer $OPENAI_API_KEY" \


441 441 

442By default, a Run will use the `model` and `tools` configuration specified in Assistant object, but you can override most of these when creating the Run for added flexibility:442By default, a Run will use the `model` and `tools` configuration specified in Assistant object, but you can override most of these when creating the Run for added flexibility:

443 443 

444```javascript

445const run = await openai.beta.threads.runs.create(thread.id, {

446 assistant_id: assistant.id,

447 model: "gpt-4o",

448 instructions: "New instructions that override the Assistant instructions",

449 tools: [{ type: "code_interpreter" }, { type: "file_search" }],

450});

451```

452 

444```python453```python

445run = client.beta.threads.runs.create(454run = client.beta.threads.runs.create(

446 thread_id=thread.id,455 thread_id=thread.id,


451)460)

452```461```

453 462 

454```javascript

455const run = await openai.beta.threads.runs.create(thread.id, {

456 assistant_id: assistant.id,

457 model: "gpt-4o",

458 instructions: "New instructions that override the Assistant instructions",

459 tools: [{ type: "code_interpreter" }, { type: "file_search" }],

460});

461```

462 

463```bash463```bash

464curl https://api.openai.com/v1/threads/THREAD_ID/runs \464curl https://api.openai.com/v1/threads/THREAD_ID/runs \

465 -H "Authorization: Bearer $OPENAI_API_KEY" \465 -H "Authorization: Bearer $OPENAI_API_KEY" \

Details

18 18 

19Pass `code_interpreter` in the `tools` parameter of the Assistant object to enable Code Interpreter:19Pass `code_interpreter` in the `tools` parameter of the Assistant object to enable Code Interpreter:

20 20 

21```python

22assistant = client.beta.assistants.create(

23 instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.",

24 model="gpt-4o",

25 tools=[{"type": "code_interpreter"}],

26)

27```

28 

29```javascript21```javascript

30const assistant = await openai.beta.assistants.create({22const assistant = await openai.beta.assistants.create({

31 instructions:23 instructions:


35});27});

36```28```

37 29 

30```python

31assistant = client.beta.assistants.create(

32 instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.",

33 model="gpt-4o",

34 tools=[{"type": "code_interpreter"}],

35)

36```

37 

38```bash38```bash

39curl https://api.openai.com/v1/assistants \39curl https://api.openai.com/v1/assistants \

40 -u :$OPENAI_API_KEY \40 -u :$OPENAI_API_KEY \


56 56 

57Files that are passed at the Assistant level are accessible by all Runs with this Assistant:57Files that are passed at the Assistant level are accessible by all Runs with this Assistant:

58 58 

59```python

60# Upload a file with an "assistants" purpose

61file = client.files.create(file=open("mydata.csv", "rb"), purpose="assistants")

62 

63# Create an assistant using the file ID

64assistant = client.beta.assistants.create(

65 instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.",

66 model="gpt-4o",

67 tools=[{"type": "code_interpreter"}],

68 tool_resources={"code_interpreter": {"file_ids": [file.id]}},

69)

70```

71 

72```javascript59```javascript

73// Upload a file with an "assistants" purpose60// Upload a file with an "assistants" purpose

74const file = await openai.files.create({61const file = await openai.files.create({


90});77});

91```78```

92 79 

80```python

81# Upload a file with an "assistants" purpose

82file = client.files.create(file=open("mydata.csv", "rb"), purpose="assistants")

83 

84# Create an assistant using the file ID

85assistant = client.beta.assistants.create(

86 instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.",

87 model="gpt-4o",

88 tools=[{"type": "code_interpreter"}],

89 tool_resources={"code_interpreter": {"file_ids": [file.id]}},

90)

91```

92 

93```bash93```bash

94# Upload a file with an "assistants" purpose94# Upload a file with an "assistants" purpose

95curl https://api.openai.com/v1/files \95curl https://api.openai.com/v1/files \


117 117 

118Files can also be passed at the Thread level. These files are only accessible in the specific Thread. Upload the File using the [File upload](https://developers.openai.com/api/reference/resources/files/methods/create) endpoint and then pass the File ID as part of the Message creation request:118Files can also be passed at the Thread level. These files are only accessible in the specific Thread. Upload the File using the [File upload](https://developers.openai.com/api/reference/resources/files/methods/create) endpoint and then pass the File ID as part of the Message creation request:

119 119 

120```python

121thread = client.beta.threads.create(

122 messages=[

123 {

124 "role": "user",

125 "content": "I need to solve the equation `3x + 11 = 14`. Can you help me?",

126 "attachments": [

127 {"file_id": file.id, "tools": [{"type": "code_interpreter"}]}

128 ],

129 }

130 ]

131)

132```

133 

134```javascript120```javascript

135const thread = await openai.beta.threads.create({121const thread = await openai.beta.threads.create({

136 messages: [122 messages: [


148});134});

149```135```

150 136 

137```python

138thread = client.beta.threads.create(

139 messages=[

140 {

141 "role": "user",

142 "content": "I need to solve the equation `3x + 11 = 14`. Can you help me?",

143 "attachments": [

144 {"file_id": file.id, "tools": [{"type": "code_interpreter"}]}

145 ],

146 }

147 ]

148)

149```

150 

151```bash151```bash

152curl https://api.openai.com/v1/threads/thread_abc123/messages \152curl https://api.openai.com/v1/threads/thread_abc123/messages \

153 -u :$OPENAI_API_KEY \153 -u :$OPENAI_API_KEY \


198 198 

199The file content can then be downloaded by passing the file ID to the Files API:199The file content can then be downloaded by passing the file ID to the Files API:

200 200 

201```python

202import os

203 

204from openai import OpenAI

205 

206file_id = os.environ["OPENAI_FILE_ID"]

207client = OpenAI()

208 

209image_data = client.files.content(file_id)

210image_data_bytes = image_data.read()

211 

212with open("./my-image.png", "wb") as file:

213 file.write(image_data_bytes)

214```

215 

216```javascript201```javascript

217import fs from "fs";202import fs from "fs";

218import OpenAI from "openai";203import OpenAI from "openai";


235main();220main();

236```221```

237 222 

223```python

224import os

225 

226from openai import OpenAI

227 

228file_id = os.environ["OPENAI_FILE_ID"]

229client = OpenAI()

230 

231image_data = client.files.content(file_id)

232image_data_bytes = image_data.read()

233 

234with open("./my-image.png", "wb") as file:

235 file.write(image_data_bytes)

236```

237 

238```bash238```bash

239curl https://api.openai.com/v1/files/file-abc123/content \239curl https://api.openai.com/v1/files/file-abc123/content \

240 -H "Authorization: Bearer $OPENAI_API_KEY" \240 -H "Authorization: Bearer $OPENAI_API_KEY" \


273 273 

274By listing the steps of a Run that called Code Interpreter, you can inspect the code `input` and `outputs` logs of Code Interpreter:274By listing the steps of a Run that called Code Interpreter, you can inspect the code `input` and `outputs` logs of Code Interpreter:

275 275 

276```javascript

277const runSteps = await openai.beta.threads.runs.steps.list(run.id, {

278 thread_id: thread.id,

279});

280```

281 

276```python282```python

277import os283import os

278 284 


285)291)

286```292```

287 293 

288```javascript

289const runSteps = await openai.beta.threads.runs.steps.list(run.id, {

290 thread_id: thread.id,

291});

292```

293 

294```bash294```bash

295curl https://api.openai.com/v1/threads/thread_abc123/runs/RUN_ID/steps \295curl https://api.openai.com/v1/threads/thread_abc123/runs/RUN_ID/steps \

296 -H "Authorization: Bearer $OPENAI_API_KEY" \296 -H "Authorization: Bearer $OPENAI_API_KEY" \

Details

20With the launch of Structured Outputs, you can now use the parameter `strict:20With the launch of Structured Outputs, you can now use the parameter `strict:

21 true` when using function calling with the Assistants API. For more21 true` when using function calling with the Assistants API. For more

22 information, refer to the [Function calling22 information, refer to the [Function calling

23 guide](https://developers.openai.com/api/docs/guides/function-calling#function-calling-with-structured-outputs).23 guide](https://developers.openai.com/api/docs/guides/function-calling#strict-mode). Please note that

24 Please note that Structured Outputs are not supported in the Assistants API24 Structured Outputs are not supported in the Assistants API when using vision.

25 when using vision.

26 25 

27### Step 1: Define functions26### Step 1: Define functions

28 27 

29When creating your assistant, you will first define the functions under the `tools` param of the assistant.28When creating your assistant, you will first define the functions under the `tools` param of the assistant.

30 29 

30```javascript

31const assistant = await client.beta.assistants.create({

32 model: "gpt-4o",

33 instructions:

34 "You are a weather bot. Use the provided functions to answer questions.",

35 tools: [

36 {

37 type: "function",

38 function: {

39 name: "getCurrentTemperature",

40 description: "Get the current temperature for a specific location",

41 parameters: {

42 type: "object",

43 properties: {

44 location: {

45 type: "string",

46 description: "The city and state, e.g., San Francisco, CA",

47 },

48 unit: {

49 type: "string",

50 enum: ["Celsius", "Fahrenheit"],

51 description:

52 "The temperature unit to use. Infer this from the user's location.",

53 },

54 },

55 required: ["location", "unit"],

56 },

57 },

58 },

59 {

60 type: "function",

61 function: {

62 name: "getRainProbability",

63 description: "Get the probability of rain for a specific location",

64 parameters: {

65 type: "object",

66 properties: {

67 location: {

68 type: "string",

69 description: "The city and state, e.g., San Francisco, CA",

70 },

71 },

72 required: ["location"],

73 },

74 },

75 },

76 ],

77});

78```

79 

31```python80```python

32from openai import OpenAI81from openai import OpenAI

33 82 


80)129)

81```130```

82 131 

83```javascript

84const assistant = await client.beta.assistants.create({

85 model: "gpt-4o",

86 instructions:

87 "You are a weather bot. Use the provided functions to answer questions.",

88 tools: [

89 {

90 type: "function",

91 function: {

92 name: "getCurrentTemperature",

93 description: "Get the current temperature for a specific location",

94 parameters: {

95 type: "object",

96 properties: {

97 location: {

98 type: "string",

99 description: "The city and state, e.g., San Francisco, CA",

100 },

101 unit: {

102 type: "string",

103 enum: ["Celsius", "Fahrenheit"],

104 description:

105 "The temperature unit to use. Infer this from the user's location.",

106 },

107 },

108 required: ["location", "unit"],

109 },

110 },

111 },

112 {

113 type: "function",

114 function: {

115 name: "getRainProbability",

116 description: "Get the probability of rain for a specific location",

117 parameters: {

118 type: "object",

119 properties: {

120 location: {

121 type: "string",

122 description: "The city and state, e.g., San Francisco, CA",

123 },

124 },

125 required: ["location"],

126 },

127 },

128 },

129 ],

130});

131```

132 

133 132 

134### Step 2: Create a Thread and add Messages133### Step 2: Create a Thread and add Messages

135 134 

136Create a Thread when a user starts a conversation and add Messages to the Thread as the user asks questions.135Create a Thread when a user starts a conversation and add Messages to the Thread as the user asks questions.

137 136 

138```python

139thread = client.beta.threads.create()

140message = client.beta.threads.messages.create(

141 thread_id=thread.id,

142 role="user",

143 content="What's the weather in San Francisco today and the likelihood it'll rain?",

144)

145```

146 

147```javascript137```javascript

148const thread = await client.beta.threads.create();138const thread = await client.beta.threads.create();

149const message = client.beta.threads.messages.create(thread.id, {139const message = client.beta.threads.messages.create(thread.id, {


153});143});

154```144```

155 145 

146```python

147thread = client.beta.threads.create()

148message = client.beta.threads.messages.create(

149 thread_id=thread.id,

150 role="user",

151 content="What's the weather in San Francisco today and the likelihood it'll rain?",

152)

153```

154 

156 155 

157### Step 3: Initiate a Run156### Step 3: Initiate a Run

158 157 


216 215 

217For the streaming case, we create an EventHandler class to handle events in the response stream and submit all tool outputs at once with the “submit tool outputs stream” helper in the Python and Node SDKs.216For the streaming case, we create an EventHandler class to handle events in the response stream and submit all tool outputs at once with the “submit tool outputs stream” helper in the Python and Node SDKs.

218 217 

219```python

220from typing_extensions import override

221from openai import AssistantEventHandler

222 

223class EventHandler(AssistantEventHandler):

224 @override

225 def on_event(self, event):

226 # Retrieve events that are denoted with 'requires_action'

227 # since these will have our tool_calls

228 if event.event == "thread.run.requires_action":

229 run_id = event.data.id # Retrieve the run ID from the event data

230 self.handle_requires_action(event.data, run_id)

231 

232 def handle_requires_action(self, data, run_id):

233 tool_outputs = []

234 

235 for tool in data.required_action.submit_tool_outputs.tool_calls:

236 if tool.function.name == "get_current_temperature":

237 tool_outputs.append({"tool_call_id": tool.id, "output": "57"})

238 elif tool.function.name == "get_rain_probability":

239 tool_outputs.append({"tool_call_id": tool.id, "output": "0.06"})

240 

241 # Submit all tool_outputs at the same time

242 self.submit_tool_outputs(tool_outputs, run_id)

243 

244 def submit_tool_outputs(self, tool_outputs, run_id):

245 # Use the submit_tool_outputs_stream helper

246 with client.beta.threads.runs.submit_tool_outputs_stream(

247 thread_id=self.current_run.thread_id,

248 run_id=self.current_run.id,

249 tool_outputs=tool_outputs,

250 event_handler=EventHandler(),

251 ) as stream:

252 for text in stream.text_deltas:

253 print(text, end="", flush=True)

254 print()

255 

256with client.beta.threads.runs.stream(

257 thread_id=thread.id,

258 assistant_id=assistant.id,

259 event_handler=EventHandler(),

260) as stream:

261 stream.until_done()

262```

263 

264```javascript218```javascript

265class EventHandler extends EventEmitter {219class EventHandler extends EventEmitter {

266 constructor(client) {220 constructor(client) {


336}290}

337```291```

338 292 

293```python

294from typing_extensions import override

295from openai import AssistantEventHandler

339 296 

297class EventHandler(AssistantEventHandler):

298 @override

299 def on_event(self, event):

300 # Retrieve events that are denoted with 'requires_action'

301 # since these will have our tool_calls

302 if event.event == "thread.run.requires_action":

303 run_id = event.data.id # Retrieve the run ID from the event data

304 self.handle_requires_action(event.data, run_id)

340 305 

306 def handle_requires_action(self, data, run_id):

307 tool_outputs = []

341 308 

309 for tool in data.required_action.submit_tool_outputs.tool_calls:

310 if tool.function.name == "get_current_temperature":

311 tool_outputs.append({"tool_call_id": tool.id, "output": "57"})

312 elif tool.function.name == "get_rain_probability":

313 tool_outputs.append({"tool_call_id": tool.id, "output": "0.06"})

342 314 

315 # Submit all tool_outputs at the same time

316 self.submit_tool_outputs(tool_outputs, run_id)

343 317 

318 def submit_tool_outputs(self, tool_outputs, run_id):

319 # Use the submit_tool_outputs_stream helper

320 with client.beta.threads.runs.submit_tool_outputs_stream(

321 thread_id=self.current_run.thread_id,

322 run_id=self.current_run.id,

323 tool_outputs=tool_outputs,

324 event_handler=EventHandler(),

325 ) as stream:

326 for text in stream.text_deltas:

327 print(text, end="", flush=True)

328 print()

344 329 

345Without streaming330with client.beta.threads.runs.stream(

331 thread_id=thread.id,

332 assistant_id=assistant.id,

333 event_handler=EventHandler(),

334) as stream:

335 stream.until_done()

336```

346 337 

347 338 

348 339

349Runs are asynchronous, which means you'll want to monitor their `status` by polling the Run object until a

350[terminal status](https://developers.openai.com/api/docs/assistants/deep-dive#runs-and-run-steps) is reached. For convenience, the 'create and poll' SDK helpers assist both in

351creating the run and then polling for its completion. Once the Run completes, you can list the

352Messages added to the Thread by the Assistant. Finally, you would retrieve all the `tool_outputs` from

353`required_action` and submit them at the same time to the 'submit tool outputs and poll' helper.

354 340 

355```python

356run = client.beta.threads.runs.create_and_poll(

357 thread_id=thread.id,

358 assistant_id=assistant.id,

359)

360 341

361if run.status == "completed":

362 messages = client.beta.threads.messages.list(thread_id=thread.id)

363 print(messages)

364 342 

365# Define the list to store tool outputs

366tool_outputs = []

367 343

368# Loop through each tool in the required action section344Without streaming

369if run.required_action:

370 for tool in run.required_action.submit_tool_outputs.tool_calls:

371 if tool.function.name == "get_current_temperature":

372 tool_outputs.append({"tool_call_id": tool.id, "output": "57"})

373 elif tool.function.name == "get_rain_probability":

374 tool_outputs.append({"tool_call_id": tool.id, "output": "0.06"})

375 345 

376# Submit all tool outputs at once after collecting them in a list

377if tool_outputs:

378 try:

379 run = client.beta.threads.runs.submit_tool_outputs_and_poll(

380 thread_id=thread.id,

381 run_id=run.id,

382 tool_outputs=tool_outputs,

383 )

384 print("Tool outputs submitted successfully.")

385 except Exception as e:

386 print("Failed to submit tool outputs:", e)

387else:

388 print("No tool outputs to submit.")

389 346

390if run.status == "completed":347 

391 messages = client.beta.threads.messages.list(thread_id=thread.id)348Runs are asynchronous, which means you'll want to monitor their `status` by polling the Run object until a

392 print(messages)349[terminal status](https://developers.openai.com/api/docs/assistants/deep-dive#runs-and-run-steps) is reached. For convenience, the 'create and poll' SDK helpers assist both in

393else:350creating the run and then polling for its completion. Once the Run completes, you can list the

394 print(run.status)351Messages added to the Thread by the Assistant. Finally, you would retrieve all the `tool_outputs` from

395```352`required_action` and submit them at the same time to the 'submit tool outputs and poll' helper.

396 353 

397```javascript354```javascript

398const handleRequiresAction = async (run) => {355const handleRequiresAction = async (run) => {


457handleRunStatus(run);414handleRunStatus(run);

458```415```

459 416 

417```python

418run = client.beta.threads.runs.create_and_poll(

419 thread_id=thread.id,

420 assistant_id=assistant.id,

421)

422 

423if run.status == "completed":

424 messages = client.beta.threads.messages.list(thread_id=thread.id)

425 print(messages)

426 

427# Define the list to store tool outputs

428tool_outputs = []

429 

430# Loop through each tool in the required action section

431if run.required_action:

432 for tool in run.required_action.submit_tool_outputs.tool_calls:

433 if tool.function.name == "get_current_temperature":

434 tool_outputs.append({"tool_call_id": tool.id, "output": "57"})

435 elif tool.function.name == "get_rain_probability":

436 tool_outputs.append({"tool_call_id": tool.id, "output": "0.06"})

437 

438# Submit all tool outputs at once after collecting them in a list

439if tool_outputs:

440 try:

441 run = client.beta.threads.runs.submit_tool_outputs_and_poll(

442 thread_id=thread.id,

443 run_id=run.id,

444 tool_outputs=tool_outputs,

445 )

446 print("Tool outputs submitted successfully.")

447 except Exception as e:

448 print("Failed to submit tool outputs:", e)

449else:

450 print("No tool outputs to submit.")

451 

452if run.status == "completed":

453 messages = client.beta.threads.messages.list(thread_id=thread.id)

454 print(messages)

455else:

456 print(run.status)

457```

458 

460 459 

461 460 

462### Using Structured Outputs461### Using Structured Outputs

463 462 

464When you enable [Structured Outputs](https://developers.openai.com/api/docs/guides/structured-outputs) by supplying `strict: true`, the OpenAI API will pre-process your supplied schema on your first request, and then use this artifact to constrain the model to your schema.463When you enable [Structured Outputs](https://developers.openai.com/api/docs/guides/structured-outputs) by supplying `strict: true`, the OpenAI API will pre-process your supplied schema on your first request, and then use this artifact to constrain the model to your schema.

465 464 

465```javascript

466const assistant = await client.beta.assistants.create({

467 model: "gpt-4o-2024-08-06",

468 instructions:

469 "You are a weather bot. Use the provided functions to answer questions.",

470 tools: [

471 {

472 type: "function",

473 function: {

474 name: "getCurrentTemperature",

475 description: "Get the current temperature for a specific location",

476 parameters: {

477 type: "object",

478 properties: {

479 location: {

480 type: "string",

481 description: "The city and state, e.g., San Francisco, CA",

482 },

483 unit: {

484 type: "string",

485 enum: ["Celsius", "Fahrenheit"],

486 description:

487 "The temperature unit to use. Infer this from the user's location.",

488 },

489 },

490 required: ["location", "unit"],

491 // highlight-start

492 additionalProperties: false,

493 // highlight-end

494 },

495 // highlight-start

496 strict: true,

497 // highlight-end

498 },

499 },

500 {

501 type: "function",

502 function: {

503 name: "getRainProbability",

504 description: "Get the probability of rain for a specific location",

505 parameters: {

506 type: "object",

507 properties: {

508 location: {

509 type: "string",

510 description: "The city and state, e.g., San Francisco, CA",

511 },

512 },

513 required: ["location"],

514 // highlight-start

515 additionalProperties: false,

516 // highlight-end

517 },

518 // highlight-start

519 strict: true,

520 // highlight-end

521 },

522 },

523 ],

524});

525```

526 

466```python527```python

467from openai import OpenAI528from openai import OpenAI

468 529 


526 ],587 ],

527)588)

528```589```

529 

530```javascript

531const assistant = await client.beta.assistants.create({

532 model: "gpt-4o-2024-08-06",

533 instructions:

534 "You are a weather bot. Use the provided functions to answer questions.",

535 tools: [

536 {

537 type: "function",

538 function: {

539 name: "getCurrentTemperature",

540 description: "Get the current temperature for a specific location",

541 parameters: {

542 type: "object",

543 properties: {

544 location: {

545 type: "string",

546 description: "The city and state, e.g., San Francisco, CA",

547 },

548 unit: {

549 type: "string",

550 enum: ["Celsius", "Fahrenheit"],

551 description:

552 "The temperature unit to use. Infer this from the user's location.",

553 },

554 },

555 required: ["location", "unit"],

556 // highlight-start

557 additionalProperties: false,

558 // highlight-end

559 },

560 // highlight-start

561 strict: true,

562 // highlight-end

563 },

564 },

565 {

566 type: "function",

567 function: {

568 name: "getRainProbability",

569 description: "Get the probability of rain for a specific location",

570 parameters: {

571 type: "object",

572 properties: {

573 location: {

574 type: "string",

575 description: "The city and state, e.g., San Francisco, CA",

576 },

577 },

578 required: ["location"],

579 // highlight-start

580 additionalProperties: false,

581 // highlight-end

582 },

583 // highlight-start

584 strict: true,

585 // highlight-end

586 },

587 },

588 ],

589});

590```

Details

58}58}

59```59```

60 60 

61```ruby

62require "openai"

63 

64openai = OpenAI::Client.new(

65 admin_api_key: ENV.fetch("OPENAI_ADMIN_KEY")

66)

67```

68 

69```java61```java

70import com.openai.client.OpenAIClient;62import com.openai.client.OpenAIClient;

71import com.openai.client.okhttp.OpenAIOkHttpClient;63import com.openai.client.okhttp.OpenAIOkHttpClient;


74 OpenAIOkHttpClient.builder().adminApiKey(System.getenv("OPENAI_ADMIN_KEY")).build();66 OpenAIOkHttpClient.builder().adminApiKey(System.getenv("OPENAI_ADMIN_KEY")).build();

75```67```

76 68 

69```ruby

70require "openai"

71 

72openai = OpenAI::Client.new(

73 admin_api_key: ENV.fetch("OPENAI_ADMIN_KEY")

74)

75```

76 

77 77 

78## Restrict model access for projects78## Restrict model access for projects

79 79 


119println(modelPermissions.Mode)119println(modelPermissions.Mode)

120```120```

121 121 

122```ruby

123model_permissions = openai.admin.organization.projects.model_permissions.update(

124 "proj_abc",

125 mode: :allow_list,

126 model_ids: ["gpt-4.1", "o3"]

127)

128 

129puts(model_permissions.mode)

130```

131 

132```java122```java

133import com.openai.models.admin.organization.projects.modelpermissions.ModelPermissionUpdateParams;123import com.openai.models.admin.organization.projects.modelpermissions.ModelPermissionUpdateParams;

134import com.openai.models.admin.organization.projects.modelpermissions.ProjectModelPermissions;124import com.openai.models.admin.organization.projects.modelpermissions.ProjectModelPermissions;


150System.out.println(modelPermissions.mode());140System.out.println(modelPermissions.mode());

151```141```

152 142 

143```ruby

144model_permissions = openai.admin.organization.projects.model_permissions.update(

145 "proj_abc",

146 mode: :allow_list,

147 model_ids: ["gpt-4.1", "o3"]

148)

149 

150puts(model_permissions.mode)

151```

152 

153 153 

154## Set an organization spend limit154## Set an organization spend limit

155 155 


232println(spendAlert.ID)232println(spendAlert.ID)

233```233```

234 234 

235```ruby

236spend_alert = openai.admin.organization.projects.spend_alerts.create(

237 "proj_abc",

238 currency: :USD,

239 interval: :month,

240 notification_channel: {

241 recipients: ["billing@example.com"],

242 type: :email,

243 subject_prefix: "[OpenAI spend]"

244 },

245 threshold_amount: 50_000

246)

247 

248puts(spend_alert.id)

249```

250 

251```java235```java

252import com.openai.models.admin.organization.projects.spendalerts.ProjectSpendAlert;236import com.openai.models.admin.organization.projects.spendalerts.ProjectSpendAlert;

253import com.openai.models.admin.organization.projects.spendalerts.SpendAlertCreateParams;237import com.openai.models.admin.organization.projects.spendalerts.SpendAlertCreateParams;


274System.out.println(spendAlert.id());258System.out.println(spendAlert.id());

275```259```

276 260 

261```ruby

262spend_alert = openai.admin.organization.projects.spend_alerts.create(

263 "proj_abc",

264 currency: :USD,

265 interval: :month,

266 notification_channel: {

267 recipients: ["billing@example.com"],

268 type: :email,

269 subject_prefix: "[OpenAI spend]"

270 },

271 threshold_amount: 50_000

272)

273 

274puts(spend_alert.id)

275```

276 

277 277 

278## Manage data retention278## Manage data retention

279 279 


316println(dataRetention.Type)316println(dataRetention.Type)

317```317```

318 318 

319```ruby

320data_retention = openai.admin.organization.projects.data_retention.update(

321 "proj_abc",

322 retention_type: :organization_default

323)

324 

325puts(data_retention.type)

326```

327 

328```java319```java

329import com.openai.models.admin.organization.projects.dataretention.DataRetentionUpdateParams;320import com.openai.models.admin.organization.projects.dataretention.DataRetentionUpdateParams;

330import com.openai.models.admin.organization.projects.dataretention.ProjectDataRetention;321import com.openai.models.admin.organization.projects.dataretention.ProjectDataRetention;


344System.out.println(dataRetention.type());335System.out.println(dataRetention.type());

345```336```

346 337 

338```ruby

339data_retention = openai.admin.organization.projects.data_retention.update(

340 "proj_abc",

341 retention_type: :organization_default

342)

343 

344puts(data_retention.type)

345```

346 

347 347 

348## Invite a user by email348## Invite a user by email

349 349 


383println(invite.ID)383println(invite.ID)

384```384```

385 385 

386```ruby

387invite = openai.admin.organization.invites.create(

388 email: "user@example.com",

389 role: :reader

390)

391 

392puts(invite.id)

393```

394 

395```java386```java

396import com.openai.models.admin.organization.invites.Invite;387import com.openai.models.admin.organization.invites.Invite;

397import com.openai.models.admin.organization.invites.InviteCreateParams;388import com.openai.models.admin.organization.invites.InviteCreateParams;


410System.out.println(invite.id());401System.out.println(invite.id());

411```402```

412 403 

404```ruby

405invite = openai.admin.organization.invites.create(

406 email: "user@example.com",

407 role: :reader

408)

409 

410puts(invite.id)

411```

412 

413 413 

414## Retrieve audit logs414## Retrieve audit logs

415 415 


447}447}

448```448```

449 449 

450```ruby

451audit_logs = openai.admin.organization.audit_logs.list(limit: 10)

452 

453audit_logs.data.each do |audit_log|

454 puts(audit_log.id)

455end

456```

457 

458```java450```java

459import com.openai.models.admin.organization.auditlogs.AuditLogListParams;451import com.openai.models.admin.organization.auditlogs.AuditLogListParams;

460 452 


467 459 

468page.data().forEach(auditLog -> System.out.println(auditLog.id()));460page.data().forEach(auditLog -> System.out.println(auditLog.id()));

469```461```

462 

463```ruby

464audit_logs = openai.admin.organization.audit_logs.list(limit: 10)

465 

466audit_logs.data.each do |audit_log|

467 puts(audit_log.id)

468end

469```

guides/chatkit.md +29 −29

Details

191 191 

192 Your frontend code192 Your frontend code

193 193 

194```react

195import { ChatKit, useChatKit } from '@openai/chatkit-react';

196 

197 export function MyChat({ getAppAuthToken }) {

198 const { control } = useChatKit({

199 api: {

200 async getClientSecret(existing) {

201 if (existing) {

202 // implement session refresh

203 }

204 

205 const appAuthToken = await getAppAuthToken();

206 const res = await fetch('/api/chatkit/session', {

207 method: 'POST',

208 headers: {

209 'Authorization': 'Bearer ' + appAuthToken,

210 'Content-Type': 'application/json',

211 },

212 });

213 const { client_secret } = await res.json();

214 return client_secret;

215 },

216 },

217 });

218 

219 return ;

220 }

221```

222 

223```javascript194```javascript

224const chatkit = document.getElementById("my-chat");195const chatkit = document.getElementById("my-chat");

225if (196if (


251});222});

252```223```

253 224 

225```tsx

226import { ChatKit, useChatKit } from '@openai/chatkit-react';

227 

228 export function MyChat({ getAppAuthToken }) {

229 const { control } = useChatKit({

230 api: {

231 async getClientSecret(existing) {

232 if (existing) {

233 // implement session refresh

234 }

235 

236 const appAuthToken = await getAppAuthToken();

237 const res = await fetch('/api/chatkit/session', {

238 method: 'POST',

239 headers: {

240 'Authorization': 'Bearer ' + appAuthToken,

241 'Content-Type': 'application/json',

242 },

243 });

244 const { client_secret } = await res.json();

245 return client_secret;

246 },

247 },

248 });

249 

250 return ;

251 }

252```

253 

254 254 

255### 3. Build and iterate255### 3. Build and iterate

256 256 

Details

197 197 

198Citation parsing helpers198Citation parsing helpers

199 199 

200```python

201import re

202from typing import Iterable, TypedDict

203 

204CITATION_START = "\ue200"

205CITATION_DELIMITER = "\ue202"

206CITATION_STOP = "\ue201"

207 

208SOURCE_ID_RE = re.compile(r"^[A-Za-z0-9_-]+$")

209LINE_LOCATOR_RE = re.compile(r"^L\d+(?:-L\d+)?$")

210 

211 

212class Citation(TypedDict):

213 raw: str

214 family: str

215 source_ids: list[str]

216 locator: str | None

217 start: int

218 end: int

219 

220 

221def extract_citations(

222 text: str,

223 *,

224 families: tuple[str, ...] = ("cite",),

225) -> list[Citation]:

226 """

227 Extract citations such as:

228 

229 {CITATION_START}cite{CITATION_DELIMITER}turn0file0{CITATION_STOP}

230 {CITATION_START}cite{CITATION_DELIMITER}turn0file0{CITATION_DELIMITER}L8-L13{CITATION_STOP}

231 {CITATION_START}cite{CITATION_DELIMITER}turn0search0{CITATION_DELIMITER}turn1news2{CITATION_STOP}

232 """

233 if not families:

234 return []

235 

236 family_pattern = "|".join(re.escape(family) for family in families)

237 token_re = re.compile(

238 rf"{re.escape(CITATION_START)}"

239 rf"(?P<family>{family_pattern})"

240 rf"{re.escape(CITATION_DELIMITER)}"

241 rf"(?P<body>.*?)"

242 rf"{re.escape(CITATION_STOP)}",

243 re.DOTALL,

244 )

245 

246 citations: list[Citation] = []

247 

248 for match in token_re.finditer(text):

249 parts = [part.strip() for part in match.group("body").split(CITATION_DELIMITER)]

250 parts = [part for part in parts if part]

251 

252 if not parts:

253 continue

254 

255 locator = None

256 if LINE_LOCATOR_RE.fullmatch(parts[-1]):

257 locator = parts.pop()

258 

259 if not parts or any(not SOURCE_ID_RE.fullmatch(part) for part in parts):

260 continue

261 

262 citations.append(

263 {

264 "raw": match.group(0),

265 "family": match.group("family"),

266 "source_ids": parts,

267 "locator": locator,

268 "start": match.start(),

269 "end": match.end(),

270 }

271 )

272 

273 return citations

274 

275 

276def strip_citations(text: str, citations: Iterable[Citation]) -> str:

277 """

278 Remove raw citation markers from text using offsets returned by

279 extract_citations().

280 """

281 clean_text = text

282 

283 for citation in sorted(citations, key=lambda item: item["start"], reverse=True):

284 clean_text = clean_text[: citation["start"]] + clean_text[citation["end"] :]

285 

286 return clean_text

287```

288 

289```javascript200```javascript

290const CITATION_START = "\uE200";201const CITATION_START = "\uE200";

291const CITATION_DELIMITER = "\uE202";202const CITATION_DELIMITER = "\uE202";


386}297}

387```298```

388 299 

300```python

301import re

302from typing import Iterable, TypedDict

303 

304CITATION_START = "\ue200"

305CITATION_DELIMITER = "\ue202"

306CITATION_STOP = "\ue201"

307 

308SOURCE_ID_RE = re.compile(r"^[A-Za-z0-9_-]+$")

309LINE_LOCATOR_RE = re.compile(r"^L\d+(?:-L\d+)?$")

310 

311 

312class Citation(TypedDict):

313 raw: str

314 family: str

315 source_ids: list[str]

316 locator: str | None

317 start: int

318 end: int

319 

320 

321def extract_citations(

322 text: str,

323 *,

324 families: tuple[str, ...] = ("cite",),

325) -> list[Citation]:

326 """

327 Extract citations such as:

328 

329 {CITATION_START}cite{CITATION_DELIMITER}turn0file0{CITATION_STOP}

330 {CITATION_START}cite{CITATION_DELIMITER}turn0file0{CITATION_DELIMITER}L8-L13{CITATION_STOP}

331 {CITATION_START}cite{CITATION_DELIMITER}turn0search0{CITATION_DELIMITER}turn1news2{CITATION_STOP}

332 """

333 if not families:

334 return []

335 

336 family_pattern = "|".join(re.escape(family) for family in families)

337 token_re = re.compile(

338 rf"{re.escape(CITATION_START)}"

339 rf"(?P<family>{family_pattern})"

340 rf"{re.escape(CITATION_DELIMITER)}"

341 rf"(?P<body>.*?)"

342 rf"{re.escape(CITATION_STOP)}",

343 re.DOTALL,

344 )

345 

346 citations: list[Citation] = []

347 

348 for match in token_re.finditer(text):

349 parts = [part.strip() for part in match.group("body").split(CITATION_DELIMITER)]

350 parts = [part for part in parts if part]

351 

352 if not parts:

353 continue

354 

355 locator = None

356 if LINE_LOCATOR_RE.fullmatch(parts[-1]):

357 locator = parts.pop()

358 

359 if not parts or any(not SOURCE_ID_RE.fullmatch(part) for part in parts):

360 continue

361 

362 citations.append(

363 {

364 "raw": match.group(0),

365 "family": match.group("family"),

366 "source_ids": parts,

367 "locator": locator,

368 "start": match.start(),

369 "end": match.end(),

370 }

371 )

372 

373 return citations

374 

375 

376def strip_citations(text: str, citations: Iterable[Citation]) -> str:

377 """

378 Remove raw citation markers from text using offsets returned by

379 extract_citations().

380 """

381 clean_text = text

382 

383 for citation in sorted(citations, key=lambda item: item["start"], reverse=True):

384 clean_text = clean_text[: citation["start"]] + clean_text[citation["end"] :]

385 

386 return clean_text

387```

388 

389 389 

390If your source IDs use a different shape, update `SOURCE_ID_RE` to match your390If your source IDs use a different shape, update `SOURCE_ID_RE` to match your

391system.391system.

Details

6 6 

7An example legacy Completions API call looks like the following:7An example legacy Completions API call looks like the following:

8 8 

9```javascript

10const completion = await openai.completions.create({

11 model: "gpt-3.5-turbo-instruct",

12 prompt: "Write a tagline for an ice cream shop.",

13});

14```

15 

9```python16```python

10from openai import OpenAI17from openai import OpenAI

11 18 


16)23)

17```24```

18 25 

19```javascript

20const completion = await openai.completions.create({

21 model: "gpt-3.5-turbo-instruct",

22 prompt: "Write a tagline for an ice cream shop.",

23});

24```

25 

26 26 

27See the full [API reference documentation](https://platform.openai.com/docs/api-reference/completions) to learn more.27See the full [API reference documentation](https://platform.openai.com/docs/api-reference/completions) to learn more.

28 28 

Details

12 12 

13Kick off a deep research task13Kick off a deep research task

14 14 

15```javascript

16import OpenAI from "openai";

17const openai = new OpenAI({ timeout: 3600 * 1000 });

18 

19const input = `

20Research the economic impact of semaglutide on global healthcare systems.

21Do:

22- Include specific figures, trends, statistics, and measurable outcomes.

23- Prioritize reliable, up-to-date sources: peer-reviewed research, health

24 organizations (e.g., WHO, CDC), regulatory agencies, or pharmaceutical

25 earnings reports.

26- Include inline citations and return all source metadata.

27 

28Be analytical, avoid generalities, and ensure that each section supports

29data-backed reasoning that could inform healthcare policy or financial modeling.

30`;

31 

32const response = await openai.responses.create({

33 model: "o3-deep-research",

34 input,

35 background: true,

36 tools: [

37 { type: "web_search_preview" },

38 {

39 type: "file_search",

40 vector_store_ids: [

41 "vs_68870b8868b88191894165101435eef6",

42 "vs_12345abcde6789fghijk101112131415",

43 ],

44 },

45 { type: "code_interpreter", container: { type: "auto" } },

46 ],

47});

48 

49console.log(response);

50```

51 

15```python52```python

16from openai import OpenAI53from openai import OpenAI

17 54 


53print(response.output_text)90print(response.output_text)

54```91```

55 92 

56```javascript

57import OpenAI from "openai";

58const openai = new OpenAI({ timeout: 3600 * 1000 });

59 

60const input = `

61Research the economic impact of semaglutide on global healthcare systems.

62Do:

63- Include specific figures, trends, statistics, and measurable outcomes.

64- Prioritize reliable, up-to-date sources: peer-reviewed research, health

65 organizations (e.g., WHO, CDC), regulatory agencies, or pharmaceutical

66 earnings reports.

67- Include inline citations and return all source metadata.

68 

69Be analytical, avoid generalities, and ensure that each section supports

70data-backed reasoning that could inform healthcare policy or financial modeling.

71`;

72 

73const response = await openai.responses.create({

74 model: "o3-deep-research",

75 input,

76 background: true,

77 tools: [

78 { type: "web_search_preview" },

79 {

80 type: "file_search",

81 vector_store_ids: [

82 "vs_68870b8868b88191894165101435eef6",

83 "vs_12345abcde6789fghijk101112131415",

84 ],

85 },

86 { type: "code_interpreter", container: { type: "auto" } },

87 ],

88});

89 

90console.log(response);

91```

92 

93```bash93```bash

94curl https://api.openai.com/v1/responses \94curl https://api.openai.com/v1/responses \

95 -H "Authorization: Bearer $OPENAI_API_KEY" \95 -H "Authorization: Bearer $OPENAI_API_KEY" \


187 187 

188Asking clarifying questions using a faster, smaller model188Asking clarifying questions using a faster, smaller model

189 189 

190```python190```javascript

191from openai import OpenAI191import OpenAI from "openai";

192 192const openai = new OpenAI();

193client = OpenAI()

194 193 

195instructions = """194const instructions = `

196You are talking to a user who is asking for a research task to be conducted. Your job is to gather more information from the user to successfully complete the task.195You are talking to a user who is asking for a research task to be conducted. Your job is to gather more information from the user to successfully complete the task.

197 196 

198GUIDELINES:197GUIDELINES:


202- Don't ask for unnecessary information, or information that the user has already provided.201- Don't ask for unnecessary information, or information that the user has already provided.

203 202 

204IMPORTANT: Do NOT conduct any research yourself, just gather information that will be given to a researcher to conduct the research task.203IMPORTANT: Do NOT conduct any research yourself, just gather information that will be given to a researcher to conduct the research task.

205"""204`;

206 205 

207input_text = "Research surfboards for me. I'm interested in ..."206const input = "Research surfboards for me. I'm interested in ...";

208 207 

209response = client.responses.create(208const response = await openai.responses.create({

210 model="gpt-5.6",209 model: "gpt-5.6",

211 input=input_text,210 input,

212 instructions=instructions,211 instructions,

213)212});

214 213 

215print(response.output_text)214console.log(response.output_text);

216```215```

217 216 

218```javascript217```python

219import OpenAI from "openai";218from openai import OpenAI

220const openai = new OpenAI();

221 219 

222const instructions = `220client = OpenAI()

221 

222instructions = """

223You are talking to a user who is asking for a research task to be conducted. Your job is to gather more information from the user to successfully complete the task.223You are talking to a user who is asking for a research task to be conducted. Your job is to gather more information from the user to successfully complete the task.

224 224 

225GUIDELINES:225GUIDELINES:


229- Don't ask for unnecessary information, or information that the user has already provided.229- Don't ask for unnecessary information, or information that the user has already provided.

230 230 

231IMPORTANT: Do NOT conduct any research yourself, just gather information that will be given to a researcher to conduct the research task.231IMPORTANT: Do NOT conduct any research yourself, just gather information that will be given to a researcher to conduct the research task.

232`;232"""

233 233 

234const input = "Research surfboards for me. I'm interested in ...";234input_text = "Research surfboards for me. I'm interested in ..."

235 235 

236const response = await openai.responses.create({236response = client.responses.create(

237 model: "gpt-5.6",237 model="gpt-5.6",

238 input,238 input=input_text,

239 instructions,239 instructions=instructions,

240});240)

241 241 

242console.log(response.output_text);242print(response.output_text)

243```243```

244 244 

245```bash245```bash


256 256 

257Enrich a user prompt using a faster, smaller model257Enrich a user prompt using a faster, smaller model

258 258 

259```python259```javascript

260from openai import OpenAI260import OpenAI from "openai";

261 261const openai = new OpenAI();

262client = OpenAI()

263 262 

264instructions = """263const instructions = `

265You will be given a research task by a user. Your job is to produce a set of264You will be given a research task by a user. Your job is to produce a set of

266instructions for a researcher that will complete the task. Do NOT complete the265instructions for a researcher that will complete the task. Do NOT complete the

267task yourself, just provide instructions on how to complete it.266task yourself, just provide instructions on how to complete it.


325 summaries.324 summaries.

326- If the query is in a specific language, prioritize sources published in that325- If the query is in a specific language, prioritize sources published in that

327 language.326 language.

328"""327`;

329 328 

330input_text = "Research surfboards for me. I'm interested in ..."329const input = "Research surfboards for me. I'm interested in ...";

331 330 

332response = client.responses.create(331const response = await openai.responses.create({

333 model="gpt-5.6",332 model: "gpt-5.6",

334 input=input_text,333 input,

335 instructions=instructions,334 instructions,

336)335});

337 336 

338print(response.output_text)337console.log(response.output_text);

339```338```

340 339 

341```javascript340```python

342import OpenAI from "openai";341from openai import OpenAI

343const openai = new OpenAI();

344 342 

345const instructions = `343client = OpenAI()

344 

345instructions = """

346You will be given a research task by a user. Your job is to produce a set of346You will be given a research task by a user. Your job is to produce a set of

347instructions for a researcher that will complete the task. Do NOT complete the347instructions for a researcher that will complete the task. Do NOT complete the

348task yourself, just provide instructions on how to complete it.348task yourself, just provide instructions on how to complete it.


406 summaries.406 summaries.

407- If the query is in a specific language, prioritize sources published in that407- If the query is in a specific language, prioritize sources published in that

408 language.408 language.

409`;409"""

410 410 

411const input = "Research surfboards for me. I'm interested in ...";411input_text = "Research surfboards for me. I'm interested in ..."

412 412 

413const response = await openai.responses.create({413response = client.responses.create(

414 model: "gpt-5.6",414 model="gpt-5.6",

415 input,415 input=input_text,

416 instructions,416 instructions=instructions,

417});417)

418 418 

419console.log(response.output_text);419print(response.output_text)

420```420```

421 421 

422```bash422```bash

guides/evals.md +83 −83

Details

34 34 

35 Categorize IT support tickets35 Categorize IT support tickets

36 36 

37```bash

38curl https://api.openai.com/v1/responses \

39 -H "Authorization: Bearer $OPENAI_API_KEY" \

40 -H "Content-Type: application/json" \

41 -d '{

42 "model": "gpt-5.6",

43 "input": [

44 {

45 "role": "developer",

46 "content": "Categorize the following support ticket into one of Hardware, Software, or Other."

47 },

48 {

49 "role": "user",

50 "content": "My monitor wont turn on - help!"

51 }

52 ]

53 }'

54```

55 

56```javascript37```javascript

57import OpenAI from "openai";38import OpenAI from "openai";

58const client = new OpenAI();39const client = new OpenAI();


100print(response.output_text)81print(response.output_text)

101```82```

102 83 

103 

104 

105 

106 

107Let's set up an eval to test this behavior [via API](https://developers.openai.com/api/reference/resources/evals). An eval needs two key ingredients:

108 

109- `data_source_config`: A schema for the test data you will use along with the eval.

110- `testing_criteria`: The [graders](https://developers.openai.com/api/docs/guides/graders) that determine if the model output is correct.

111 

112Create an eval

113 

114```bash84```bash

115curl https://api.openai.com/v1/evals \85curl https://api.openai.com/v1/responses \

116 -H "Authorization: Bearer $OPENAI_API_KEY" \86 -H "Authorization: Bearer $OPENAI_API_KEY" \

117 -H "Content-Type: application/json" \87 -H "Content-Type: application/json" \

118 -d '{88 -d '{

119 "name": "IT Ticket Categorization",89 "model": "gpt-5.6",

120 "data_source_config": {90 "input": [

121 "type": "custom",91 {

122 "item_schema": {92 "role": "developer",

123 "type": "object",93 "content": "Categorize the following support ticket into one of Hardware, Software, or Other."

124 "properties": {

125 "ticket_text": { "type": "string" },

126 "correct_label": { "type": "string" }

127 },

128 "required": ["ticket_text", "correct_label"]

129 },

130 "include_sample_schema": true

131 },94 },

132 "testing_criteria": [

133 {95 {

134 "type": "string_check",96 "role": "user",

135 "name": "Match output to human label",97 "content": "My monitor wont turn on - help!"

136 "input": "{{ sample.output_text }}",

137 "operation": "eq",

138 "reference": "{{ item.correct_label }}"

139 }98 }

140 ]99 ]

141 }'100 }'

142```101```

143 102 

103 

104 

105 

106 

107Let's set up an eval to test this behavior [via API](https://developers.openai.com/api/reference/resources/evals). An eval needs two key ingredients:

108 

109- `data_source_config`: A schema for the test data you will use along with the eval.

110- `testing_criteria`: The [graders](https://developers.openai.com/api/docs/guides/graders) that determine if the model output is correct.

111 

112Create an eval

113 

144```javascript114```javascript

145import OpenAI from "openai";115import OpenAI from "openai";

146const openai = new OpenAI();116const openai = new OpenAI();


206print(eval_obj)176print(eval_obj)

207```177```

208 178 

179```bash

180curl https://api.openai.com/v1/evals \

181 -H "Authorization: Bearer $OPENAI_API_KEY" \

182 -H "Content-Type: application/json" \

183 -d '{

184 "name": "IT Ticket Categorization",

185 "data_source_config": {

186 "type": "custom",

187 "item_schema": {

188 "type": "object",

189 "properties": {

190 "ticket_text": { "type": "string" },

191 "correct_label": { "type": "string" }

192 },

193 "required": ["ticket_text", "correct_label"]

194 },

195 "include_sample_schema": true

196 },

197 "testing_criteria": [

198 {

199 "type": "string_check",

200 "name": "Match output to human label",

201 "input": "{{ sample.output_text }}",

202 "operation": "eq",

203 "reference": "{{ item.correct_label }}"

204 }

205 ]

206 }'

207```

208 

209 209 

210Explanation: data_source_config parameter210Explanation: data_source_config parameter

211 211 


298 298 

299Upload a test data file299Upload a test data file

300 300 

301```bash

302curl https://api.openai.com/v1/files \

303 -H "Authorization: Bearer $OPENAI_API_KEY" \

304 -F purpose="evals" \

305 -F file="@tickets.jsonl"

306```

307 

308```javascript301```javascript

309import fs from "fs";302import fs from "fs";

310import OpenAI from "openai";303import OpenAI from "openai";


329print(file)322print(file)

330```323```

331 324 

325```bash

326curl https://api.openai.com/v1/files \

327 -H "Authorization: Bearer $OPENAI_API_KEY" \

328 -F purpose="evals" \

329 -F file="@tickets.jsonl"

330```

331 

332 332 

333When you upload the file, make note of the unique `id` property in the response payload (also available in the UI if you uploaded via the browser) - we will need to reference that value later:333When you upload the file, make note of the unique `id` property in the response payload (also available in the UI if you uploaded via the browser) - we will need to reference that value later:

334 334 


355 355 

356 Create an eval run356 Create an eval run

357 357 

358```bash

359curl https://api.openai.com/v1/evals/YOUR_EVAL_ID/runs \

360 -H "Authorization: Bearer $OPENAI_API_KEY" \

361 -H "Content-Type: application/json" \

362 -d '{

363 "name": "Categorization text run",

364 "data_source": {

365 "type": "responses",

366 "model": "gpt-5.6",

367 "input_messages": {

368 "type": "template",

369 "template": [

370 {"role": "developer", "content": "You are an expert in categorizing IT support tickets. Given the support ticket below, categorize the request into one of Hardware, Software, or Other. Respond with only one of those words."},

371 {"role": "user", "content": "{{ item.ticket_text }}"}

372 ]

373 },

374 "source": { "type": "file_id", "id": "YOUR_FILE_ID" }

375 }

376 }'

377```

378 

379```javascript358```javascript

380import OpenAI from "openai";359import OpenAI from "openai";

381const openai = new OpenAI();360const openai = new OpenAI();


431print(run)410print(run)

432```411```

433 412 

413```bash

414curl https://api.openai.com/v1/evals/YOUR_EVAL_ID/runs \

415 -H "Authorization: Bearer $OPENAI_API_KEY" \

416 -H "Content-Type: application/json" \

417 -d '{

418 "name": "Categorization text run",

419 "data_source": {

420 "type": "responses",

421 "model": "gpt-5.6",

422 "input_messages": {

423 "type": "template",

424 "template": [

425 {"role": "developer", "content": "You are an expert in categorizing IT support tickets. Given the support ticket below, categorize the request into one of Hardware, Software, or Other. Respond with only one of those words."},

426 {"role": "user", "content": "{{ item.ticket_text }}"}

427 ]

428 },

429 "source": { "type": "file_id", "id": "YOUR_FILE_ID" }

430 }

431 }'

432```

433 

434 434 

435 435 

436 436 


501 501 

502Retrieve eval run status502Retrieve eval run status

503 503 

504```bash

505curl https://api.openai.com/v1/evals/YOUR_EVAL_ID/runs/YOUR_RUN_ID \

506 -H "Authorization: Bearer $OPENAI_API_KEY" \

507 -H "Content-Type: application/json"

508```

509 

510```javascript504```javascript

511import OpenAI from "openai";505import OpenAI from "openai";

512const openai = new OpenAI();506const openai = new OpenAI();


525print(run)519print(run)

526```520```

527 521 

522```bash

523curl https://api.openai.com/v1/evals/YOUR_EVAL_ID/runs/YOUR_RUN_ID \

524 -H "Authorization: Bearer $OPENAI_API_KEY" \

525 -H "Content-Type: application/json"

526```

527 

528 528 

529You'll need the UUID of both your eval and eval run to fetch its status. When you do, you'll see eval run data that looks like this:529You'll need the UUID of both your eval and eval run to fetch its status. When you do, you'll see eval run data that looks like this:

530 530 

Details

17 17 

18Create a response with Fast mode18Create a response with Fast mode

19 19 

20```bash

21curl https://api.openai.com/v1/responses \

22 -H "Authorization: Bearer $OPENAI_API_KEY" \

23 -H "Content-Type: application/json" \

24 -d '{

25 "model": "gpt-5.6-sol",

26 "input": "What does 'fit check for my napalm era' mean?",

27 "service_tier": "fast"

28 }'

29```

30 

31```javascript20```javascript

32import OpenAI from "openai";21import OpenAI from "openai";

33 22 


55print(response)44print(response)

56```45```

57 46 

47```bash

48curl https://api.openai.com/v1/responses \

49 -H "Authorization: Bearer $OPENAI_API_KEY" \

50 -H "Content-Type: application/json" \

51 -d '{

52 "model": "gpt-5.6-sol",

53 "input": "What does 'fit check for my napalm era' mean?",

54 "service_tier": "fast"

55 }'

56```

57 

58 58 

59To opt in at the project level, open **Settings**, select **General** under **Project**, and change **Project Service Tier** to **Fast**. Requests that don't specify a `service_tier` then default to Fast mode. Requests for the project transition gradually to Fast mode over time.59To opt in at the project level, open **Settings**, select **General** under **Project**, and change **Project Service Tier** to **Fast**. Requests that don't specify a `service_tier` then default to Fast mode. Requests for the project transition gradually to Fast mode over time.

60 60 

guides/file-inputs.md +184 −184

Details

92 92 

93Use an external file URL93Use an external file URL

94 94 

95```bash

96curl "https://api.openai.com/v1/responses" \

97 -H "Content-Type: application/json" \

98 -H "Authorization: Bearer $OPENAI_API_KEY" \

99 -d '{

100 "model": "gpt-5.6",

101 "input": [

102 {

103 "role": "user",

104 "content": [

105 {

106 "type": "input_text",

107 "text": "Analyze the letter and provide a summary of the key points."

108 },

109 {

110 "type": "input_file",

111 "file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf"

112 }

113 ]

114 }

115 ]

116 }'

117```

118 

119```javascript95```javascript

120import OpenAI from "openai";96import OpenAI from "openai";

121const client = new OpenAI();97const client = new OpenAI();


142console.log(response.output_text);118console.log(response.output_text);

143```119```

144 120 

121```python

122from openai import OpenAI

123 

124client = OpenAI()

125 

126response = client.responses.create(

127 model="gpt-5.6",

128 input=[

129 {

130 "role": "user",

131 "content": [

132 {

133 "type": "input_text",

134 "text": "Analyze the letter and provide a summary of the key points.",

135 },

136 {

137 "type": "input_file",

138 "file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf",

139 },

140 ],

141 },

142 ],

143)

144 

145print(response.output_text)

146```

147 

145```go148```go

146package main149package main

147 150 


186}189}

187```190```

188 191 

189```python

190from openai import OpenAI

191 

192client = OpenAI()

193 

194response = client.responses.create(

195 model="gpt-5.6",

196 input=[

197 {

198 "role": "user",

199 "content": [

200 {

201 "type": "input_text",

202 "text": "Analyze the letter and provide a summary of the key points.",

203 },

204 {

205 "type": "input_file",

206 "file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf",

207 },

208 ],

209 },

210 ],

211)

212 

213print(response.output_text)

214```

215 

216```ruby

217require "openai"

218 

219openai = OpenAI::Client.new

220 

221response = openai.responses.create(

222 model: "gpt-5.6",

223 input: [

224 {

225 role: "user",

226 content: [

227 {

228 type: "input_text",

229 text: "Analyze the letter and provide a summary of the key points."

230 },

231 {

232 type: "input_file",

233 file_url: "https://www.berkshirehathaway.com/letters/2024ltr.pdf"

234 }

235 ]

236 }

237 ]

238)

239 

240puts(response.output_text)

241```

242 

243```csharp192```csharp

244using OpenAI.Responses;193using OpenAI.Responses;

245#pragma warning disable OPENAI001194#pragma warning disable OPENAI001


268Console.WriteLine(response.GetOutputText());217Console.WriteLine(response.GetOutputText());

269```218```

270 219 

220```ruby

221require "openai"

271 222 

223openai = OpenAI::Client.new

272 224 

225response = openai.responses.create(

226 model: "gpt-5.6",

227 input: [

228 {

229 role: "user",

230 content: [

231 {

232 type: "input_text",

233 text: "Analyze the letter and provide a summary of the key points."

234 },

235 {

236 type: "input_file",

237 file_url: "https://www.berkshirehathaway.com/letters/2024ltr.pdf"

238 }

239 ]

240 }

241 ]

242)

273 243 

274 244puts(response.output_text)

275 245```

276## Uploading files

277 

278The following example uploads a file with the [Files API](https://developers.openai.com/api/reference/resources/files), then references its file ID in a request to the model.

279 

280 

281 

282Upload a file

283 246 

284```bash247```bash

285curl https://api.openai.com/v1/files \

286 -H "Authorization: Bearer $OPENAI_API_KEY" \

287 -F purpose="user_data" \

288 -F file="@draconomicon.pdf"

289 

290curl "https://api.openai.com/v1/responses" \248curl "https://api.openai.com/v1/responses" \

291 -H "Content-Type: application/json" \249 -H "Content-Type: application/json" \

292 -H "Authorization: Bearer $OPENAI_API_KEY" \250 -H "Authorization: Bearer $OPENAI_API_KEY" \


297 "role": "user",255 "role": "user",

298 "content": [256 "content": [

299 {257 {

300 "type": "input_file",258 "type": "input_text",

301 "file_id": "file-6F2ksmvXxt4VdoqmHRw6kL"259 "text": "Analyze the letter and provide a summary of the key points."

302 },260 },

303 {261 {

304 "type": "input_text",262 "type": "input_file",

305 "text": "What is the first dragon in the book?"263 "file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf"

306 }264 }

307 ]265 ]

308 }266 }


310 }'268 }'

311```269```

312 270 

271 

272 

273 

274 

275 

276## Uploading files

277 

278The following example uploads a file with the [Files API](https://developers.openai.com/api/reference/resources/files), then references its file ID in a request to the model.

279 

280 

281 

282Upload a file

283 

313```javascript284```javascript

314import fs from "fs";285import fs from "fs";

315import OpenAI from "openai";286import OpenAI from "openai";


342console.log(response.output_text);313console.log(response.output_text);

343```314```

344 315 

316```python

317from openai import OpenAI

318 

319client = OpenAI()

320 

321file = client.files.create(file=open("draconomicon.pdf", "rb"), purpose="user_data")

322 

323response = client.responses.create(

324 model="gpt-5.6",

325 input=[

326 {

327 "role": "user",

328 "content": [

329 {

330 "type": "input_file",

331 "file_id": file.id,

332 },

333 {

334 "type": "input_text",

335 "text": "What is the first dragon in the book?",

336 },

337 ],

338 }

339 ],

340)

341 

342print(response.output_text)

343```

344 

345```go345```go

346package main346package main

347 347 


399}399}

400```400```

401 401 

402```python

403from openai import OpenAI

404 

405client = OpenAI()

406 

407file = client.files.create(file=open("draconomicon.pdf", "rb"), purpose="user_data")

408 

409response = client.responses.create(

410 model="gpt-5.6",

411 input=[

412 {

413 "role": "user",

414 "content": [

415 {

416 "type": "input_file",

417 "file_id": file.id,

418 },

419 {

420 "type": "input_text",

421 "text": "What is the first dragon in the book?",

422 },

423 ],

424 }

425 ],

426)

427 

428print(response.output_text)

429```

430 

431```ruby

432require "openai"

433 

434openai = OpenAI::Client.new

435 

436file = openai.files.create(

437 file: File.open("draconomicon.pdf", "rb"),

438 purpose: "user_data"

439)

440 

441response = openai.responses.create(

442 model: "gpt-5.6",

443 input: [

444 {

445 role: "user",

446 content: [

447 {type: "input_file", file_id: file.id},

448 {type: "input_text", text: "What is the first dragon in the book?"}

449 ]

450 }

451 ]

452)

453 

454puts(response.output_text)

455```

456 

457```csharp402```csharp

458using OpenAI.Files;403using OpenAI.Files;

459using OpenAI.Responses;404using OpenAI.Responses;


486Console.WriteLine(response.GetOutputText());431Console.WriteLine(response.GetOutputText());

487```432```

488 433 

434```ruby

435require "openai"

489 436 

437openai = OpenAI::Client.new

490 438 

439file = openai.files.create(

440 file: File.open("draconomicon.pdf", "rb"),

441 purpose: "user_data"

442)

491 443 

444response = openai.responses.create(

445 model: "gpt-5.6",

446 input: [

447 {

448 role: "user",

449 content: [

450 {type: "input_file", file_id: file.id},

451 {type: "input_text", text: "What is the first dragon in the book?"}

452 ]

453 }

454 ]

455)

492 456 

493 457puts(response.output_text)

494## Base64-encoded files458```

495 

496You can also send file inputs as Base64-encoded file data.

497 

498 

499 

500Send a Base64-encoded file

501 459 

502```bash460```bash

461curl https://api.openai.com/v1/files \

462 -H "Authorization: Bearer $OPENAI_API_KEY" \

463 -F purpose="user_data" \

464 -F file="@draconomicon.pdf"

465 

503curl "https://api.openai.com/v1/responses" \466curl "https://api.openai.com/v1/responses" \

504 -H "Content-Type: application/json" \467 -H "Content-Type: application/json" \

505 -H "Authorization: Bearer $OPENAI_API_KEY" \468 -H "Authorization: Bearer $OPENAI_API_KEY" \


511 "content": [474 "content": [

512 {475 {

513 "type": "input_file",476 "type": "input_file",

514 "filename": "draconomicon.pdf",477 "file_id": "file-6F2ksmvXxt4VdoqmHRw6kL"

515 "file_data": "...base64 encoded PDF bytes here..."

516 },478 },

517 {479 {

518 "type": "input_text",480 "type": "input_text",


524 }'486 }'

525```487```

526 488 

489 

490 

491 

492 

493 

494## Base64-encoded files

495 

496You can also send file inputs as Base64-encoded file data.

497 

498 

499 

500Send a Base64-encoded file

501 

527```javascript502```javascript

528import fs from "fs";503import fs from "fs";

529import OpenAI from "openai";504import OpenAI from "openai";


555console.log(response.output_text);530console.log(response.output_text);

556```531```

557 532 

533```python

534import base64

535from openai import OpenAI

536 

537client = OpenAI()

538 

539with open("draconomicon.pdf", "rb") as f:

540 data = f.read()

541 

542base64_string = base64.b64encode(data).decode("utf-8")

543 

544response = client.responses.create(

545 model="gpt-5.6",

546 input=[

547 {

548 "role": "user",

549 "content": [

550 {

551 "type": "input_file",

552 "filename": "draconomicon.pdf",

553 "file_data": f"data:application/pdf;base64,{base64_string}",

554 },

555 {

556 "type": "input_text",

557 "text": "What is the first dragon in the book?",

558 },

559 ],

560 },

561 ],

562)

563 

564print(response.output_text)

565```

566 

558```go567```go

559package main568package main

560 569 


606}615}

607```616```

608 617 

609```python618```bash

610import base64619curl "https://api.openai.com/v1/responses" \

611from openai import OpenAI620 -H "Content-Type: application/json" \

612 621 -H "Authorization: Bearer $OPENAI_API_KEY" \

613client = OpenAI()622 -d '{

614 623 "model": "gpt-5.6",

615with open("draconomicon.pdf", "rb") as f:624 "input": [

616 data = f.read()

617 

618base64_string = base64.b64encode(data).decode("utf-8")

619 

620response = client.responses.create(

621 model="gpt-5.6",

622 input=[

623 {625 {

624 "role": "user",626 "role": "user",

625 "content": [627 "content": [

626 {628 {

627 "type": "input_file",629 "type": "input_file",

628 "filename": "draconomicon.pdf",630 "filename": "draconomicon.pdf",

629 "file_data": f"data:application/pdf;base64,{base64_string}",631 "file_data": "...base64 encoded PDF bytes here..."

630 },632 },

631 {633 {

632 "type": "input_text",634 "type": "input_text",

633 "text": "What is the first dragon in the book?",635 "text": "What is the first dragon in the book?"

634 },636 }

635 ],637 ]

636 },638 }

637 ],639 ]

638)640 }'

639 

640print(response.output_text)

641```641```

642 642 

643 643 

Details

73 73 

74 Complete tool calling example74 Complete tool calling example

75 75 

76```python

77from openai import OpenAI

78import json

79 

80client = OpenAI()

81 

82# 1. Define a list of callable tools for the model

83tools = [

84 {

85 "type": "function",

86 "name": "get_horoscope",

87 "description": "Get today's horoscope for an astrological sign.",

88 "parameters": {

89 "type": "object",

90 "properties": {

91 "sign": {

92 "type": "string",

93 "description": "An astrological sign like Taurus or Aquarius",

94 },

95 },

96 "required": ["sign"],

97 },

98 },

99]

100 

101 

102def get_horoscope(sign):

103 return f"{sign}: Next Tuesday you will befriend a baby otter."

104 

105 

106# Create a running input list we will add to over time

107input_list = [{"role": "user", "content": "What is my horoscope? I am an Aquarius."}]

108 

109# 2. Prompt the model with tools defined

110response = client.responses.create(

111 model="gpt-5.6",

112 tools=tools,

113 input=input_list,

114)

115 

116# Save function call outputs for subsequent requests

117input_list += response.output

118 

119for item in response.output:

120 if item.type == "function_call":

121 if item.name == "get_horoscope":

122 # 3. Execute the function logic for get_horoscope

123 sign = json.loads(item.arguments)["sign"]

124 horoscope = get_horoscope(sign)

125 

126 # 4. Provide function call results to the model

127 input_list.append(

128 {

129 "type": "function_call_output",

130 "call_id": item.call_id,

131 "output": horoscope,

132 }

133 )

134 

135print("Final input:")

136print(input_list)

137 

138response = client.responses.create(

139 model="gpt-5.6",

140 instructions="Respond only with a horoscope generated by a tool.",

141 tools=tools,

142 input=input_list,

143)

144 

145# 5. The model should be able to give a response!

146print("Final output:")

147print(response.model_dump_json(indent=2))

148print("\n" + response.output_text)

149```

150 

151```javascript76```javascript

152import OpenAI from "openai";77import OpenAI from "openai";

153 78 


227console.log(response.output_text);152console.log(response.output_text);

228```153```

229 154 

155```python

156from openai import OpenAI

157import json

158 

159client = OpenAI()

160 

161# 1. Define a list of callable tools for the model

162tools = [

163 {

164 "type": "function",

165 "name": "get_horoscope",

166 "description": "Get today's horoscope for an astrological sign.",

167 "parameters": {

168 "type": "object",

169 "properties": {

170 "sign": {

171 "type": "string",

172 "description": "An astrological sign like Taurus or Aquarius",

173 },

174 },

175 "required": ["sign"],

176 },

177 },

178]

179 

180 

181def get_horoscope(sign):

182 return f"{sign}: Next Tuesday you will befriend a baby otter."

183 

184 

185# Create a running input list we will add to over time

186input_list = [{"role": "user", "content": "What is my horoscope? I am an Aquarius."}]

187 

188# 2. Prompt the model with tools defined

189response = client.responses.create(

190 model="gpt-5.6",

191 tools=tools,

192 input=input_list,

193)

194 

195# Save function call outputs for subsequent requests

196input_list += response.output

197 

198for item in response.output:

199 if item.type == "function_call":

200 if item.name == "get_horoscope":

201 # 3. Execute the function logic for get_horoscope

202 sign = json.loads(item.arguments)["sign"]

203 horoscope = get_horoscope(sign)

204 

205 # 4. Provide function call results to the model

206 input_list.append(

207 {

208 "type": "function_call_output",

209 "call_id": item.call_id,

210 "output": horoscope,

211 }

212 )

213 

214print("Final input:")

215print(input_list)

216 

217response = client.responses.create(

218 model="gpt-5.6",

219 instructions="Respond only with a horoscope generated by a tool.",

220 tools=tools,

221 input=input_list,

222)

223 

224# 5. The model should be able to give a response!

225print("Final output:")

226print(response.model_dump_json(indent=2))

227print("\n" + response.output_text)

228```

229 

230 230 

231 231 

232Note that for reasoning models like GPT-5 or o4-mini, any reasoning items232Note that for reasoning models like GPT-5 or o4-mini, any reasoning items


394 394 

395Execute function calls and append results395Execute function calls and append results

396 396 

397```python

398input_messages += response.output

399 

400for tool_call in response.output:

401 if tool_call.type != "function_call":

402 continue

403 

404 name = tool_call.name

405 args = json.loads(tool_call.arguments)

406 

407 result = call_function(name, args)

408 input_messages.append(

409 {

410 "type": "function_call_output",

411 "call_id": tool_call.call_id,

412 "output": json.dumps(result),

413 }

414 )

415```

416 

417```javascript397```javascript

418input.push(...response.output);398input.push(...response.output);

419 399 


434}414}

435```415```

436 416 

417```python

418input_messages += response.output

419 

420for tool_call in response.output:

421 if tool_call.type != "function_call":

422 continue

423 

424 name = tool_call.name

425 args = json.loads(tool_call.arguments)

426 

427 result = call_function(name, args)

428 input_messages.append(

429 {

430 "type": "function_call_output",

431 "call_id": tool_call.call_id,

432 "output": json.dumps(result),

433 }

434 )

435```

436 

437 437 

438 438 

439In the example above, we have a hypothetical `call_function` to route each call. Here’s a possible implementation:439In the example above, we have a hypothetical `call_function` to route each call. Here’s a possible implementation:

440 440 

441Execute function calls and append results441Execute function calls and append results

442 442 

443```python

444def call_function(name, args):

445 if name == "get_weather":

446 return get_weather(**args)

447 if name == "send_email":

448 return send_email(**args)

449 raise ValueError(f"Unknown function: {name}")

450```

451 

452```javascript443```javascript

453const callFunction = async (name, args) => {444const callFunction = async (name, args) => {

454 if (name === "get_weather") {445 if (name === "get_weather") {


460};451};

461```452```

462 453 

454```python

455def call_function(name, args):

456 if name == "get_weather":

457 return get_weather(**args)

458 if name == "send_email":

459 return send_email(**args)

460 raise ValueError(f"Unknown function: {name}")

461```

462 

463 463 

464### Formatting results464### Formatting results

465 465 


477 477 

478Send results back to model478Send results back to model

479 479 

480```javascript

481const response = await openai.responses.create({

482 model: "gpt-5.6",

483 input,

484 tools,

485});

486```

487 

480```python488```python

481response = client.responses.create(489response = client.responses.create(

482 model="gpt-5.6",490 model="gpt-5.6",


487print(response.output_text)495print(response.output_text)

488```496```

489 497 

490```javascript

491const response = await openai.responses.create({

492 model: "gpt-5.6",

493 input,

494 tools,

495});

496```

497 

498 498 

499 499 

500Final response500Final response


669 669 

670Streaming function calls670Streaming function calls

671 671 

672```python

673from openai import OpenAI

674 

675client = OpenAI()

676 

677tools = [

678 {

679 "type": "function",

680 "name": "get_weather",

681 "description": "Get current temperature for a given location.",

682 "parameters": {

683 "type": "object",

684 "properties": {

685 "location": {

686 "type": "string",

687 "description": "City and country e.g. Bogotá, Colombia",

688 }

689 },

690 "required": ["location"],

691 "additionalProperties": False,

692 },

693 }

694]

695 

696stream = client.responses.create(

697 model="gpt-5.6",

698 input=[{"role": "user", "content": "What's the weather like in Paris today?"}],

699 tools=tools,

700 stream=True,

701)

702 

703for event in stream:

704 print(event)

705```

706 

707```javascript672```javascript

708import { OpenAI } from "openai";673import { OpenAI } from "openai";

709 674 


741}706}

742```707```

743 708 

709```python

710from openai import OpenAI

711 

712client = OpenAI()

713 

714tools = [

715 {

716 "type": "function",

717 "name": "get_weather",

718 "description": "Get current temperature for a given location.",

719 "parameters": {

720 "type": "object",

721 "properties": {

722 "location": {

723 "type": "string",

724 "description": "City and country e.g. Bogotá, Colombia",

725 }

726 },

727 "required": ["location"],

728 "additionalProperties": False,

729 },

730 }

731]

732 

733stream = client.responses.create(

734 model="gpt-5.6",

735 input=[{"role": "user", "content": "What's the weather like in Paris today?"}],

736 tools=tools,

737 stream=True,

738)

739 

740for event in stream:

741 print(event)

742```

743 

744 744 

745Output events745Output events

746 746 


781 781 

782Accumulating tool_call deltas782Accumulating tool_call deltas

783 783 

784```python

785final_tool_calls = {}

786 

787for event in stream:

788 if event.type == "response.output_item.added":

789 final_tool_calls[event.output_index] = event.item

790 elif event.type == "response.function_call_arguments.delta":

791 index = event.output_index

792 

793 if final_tool_calls[index]:

794 final_tool_calls[index].arguments += event.delta

795```

796 

797```javascript784```javascript

798const finalToolCalls = {};785const finalToolCalls = {};

799 786 


810}797}

811```798```

812 799 

800```python

801final_tool_calls = {}

802 

803for event in stream:

804 if event.type == "response.output_item.added":

805 final_tool_calls[event.output_index] = event.item

806 elif event.type == "response.function_call_arguments.delta":

807 index = event.output_index

808 

809 if final_tool_calls[index]:

810 final_tool_calls[index].arguments += event.delta

811```

812 

813 813 

814Accumulated final_tool_calls[0]814Accumulated final_tool_calls[0]

815 815 


842 842 

843Custom tool calling example843Custom tool calling example

844 844 

845```python

846from openai import OpenAI

847 

848client = OpenAI()

849 

850response = client.responses.create(

851 model="gpt-5.6",

852 input="Use the code_exec tool to print hello world to the console.",

853 tools=[

854 {

855 "type": "custom",

856 "name": "code_exec",

857 "description": "Executes arbitrary Python code.",

858 }

859 ],

860)

861print(response.output)

862```

863 

864```javascript845```javascript

865import OpenAI from "openai";846import OpenAI from "openai";

866const client = new OpenAI();847const client = new OpenAI();


880console.log(response.output);861console.log(response.output);

881```862```

882 863 

864```python

865from openai import OpenAI

866 

867client = OpenAI()

868 

869response = client.responses.create(

870 model="gpt-5.6",

871 input="Use the code_exec tool to print hello world to the console.",

872 tools=[

873 {

874 "type": "custom",

875 "name": "code_exec",

876 "description": "Executes arbitrary Python code.",

877 }

878 ],

879)

880print(response.output)

881```

882 

883 883 

884Just as before, the `output` array will contain a tool call generated by the model. Except this time, the tool call input is given as plain text.884Just as before, the `output` array will contain a tool call generated by the model. Except this time, the tool call input is given as plain text.

885 885 


912 912 

913Lark context free grammar example913Lark context free grammar example

914 914 

915```python

916from openai import OpenAI

917 

918client = OpenAI()

919 

920grammar = """

921start: expr

922expr: term (SP ADD SP term)* -> add

923| term

924term: factor (SP MUL SP factor)* -> mul

925| factor

926factor: INT

927SP: " "

928ADD: "+"

929MUL: "*"

930%import common.INT

931"""

932 

933response = client.responses.create(

934 model="gpt-5.6",

935 input="Use the math_exp tool to add four plus four.",

936 tools=[

937 {

938 "type": "custom",

939 "name": "math_exp",

940 "description": "Creates valid mathematical expressions",

941 "format": {

942 "type": "grammar",

943 "syntax": "lark",

944 "definition": grammar,

945 },

946 }

947 ],

948)

949print(response.output)

950```

951 

952```javascript915```javascript

953import OpenAI from "openai";916import OpenAI from "openai";

954const client = new OpenAI();917const client = new OpenAI();


986console.log(response.output);949console.log(response.output);

987```950```

988 951 

952```python

953from openai import OpenAI

954 

955client = OpenAI()

956 

957grammar = """

958start: expr

959expr: term (SP ADD SP term)* -> add

960| term

961term: factor (SP MUL SP factor)* -> mul

962| factor

963factor: INT

964SP: " "

965ADD: "+"

966MUL: "*"

967%import common.INT

968"""

969 

970response = client.responses.create(

971 model="gpt-5.6",

972 input="Use the math_exp tool to add four plus four.",

973 tools=[

974 {

975 "type": "custom",

976 "name": "math_exp",

977 "description": "Creates valid mathematical expressions",

978 "format": {

979 "type": "grammar",

980 "syntax": "lark",

981 "definition": grammar,

982 },

983 }

984 ],

985)

986print(response.output)

987```

988 

989 989 

990The output from the tool should then conform to the Lark CFG that you defined:990The output from the tool should then conform to the Lark CFG that you defined:

991 991 


1097 1097 

1098Regex context free grammar example1098Regex context free grammar example

1099 1099 

1100```python

1101from openai import OpenAI

1102 

1103client = OpenAI()

1104 

1105grammar = r"^(?P<month>January|February|March|April|May|June|July|August|September|October|November|December)\s+(?P<day>\d{1,2})(?:st|nd|rd|th)?\s+(?P<year>\d{4})\s+at\s+(?P<hour>0?[1-9]|1[0-2])(?P<ampm>AM|PM)$"

1106 

1107response = client.responses.create(

1108 model="gpt-5.6",

1109 input="Use the timestamp tool to save a timestamp for August 7th 2025 at 10AM.",

1110 tools=[

1111 {

1112 "type": "custom",

1113 "name": "timestamp",

1114 "description": "Saves a timestamp in date + time in 24-hr format.",

1115 "format": {

1116 "type": "grammar",

1117 "syntax": "regex",

1118 "definition": grammar,

1119 },

1120 }

1121 ],

1122)

1123print(response.output)

1124```

1125 

1126```javascript1100```javascript

1127import OpenAI from "openai";1101import OpenAI from "openai";

1128const client = new OpenAI();1102const client = new OpenAI();


1151console.log(response.output);1125console.log(response.output);

1152```1126```

1153 1127 

1128```python

1129from openai import OpenAI

1130 

1131client = OpenAI()

1132 

1133grammar = r"^(?P<month>January|February|March|April|May|June|July|August|September|October|November|December)\s+(?P<day>\d{1,2})(?:st|nd|rd|th)?\s+(?P<year>\d{4})\s+at\s+(?P<hour>0?[1-9]|1[0-2])(?P<ampm>AM|PM)$"

1134 

1135response = client.responses.create(

1136 model="gpt-5.6",

1137 input="Use the timestamp tool to save a timestamp for August 7th 2025 at 10AM.",

1138 tools=[

1139 {

1140 "type": "custom",

1141 "name": "timestamp",

1142 "description": "Saves a timestamp in date + time in 24-hr format.",

1143 "format": {

1144 "type": "grammar",

1145 "syntax": "regex",

1146 "definition": grammar,

1147 },

1148 }

1149 ],

1150)

1151print(response.output)

1152```

1153 

1154 1154 

1155The output from the tool should then conform to the Regex CFG that you defined:1155The output from the tool should then conform to the Regex CFG that you defined:

1156 1156 

Details

708 708 

709Create a File709Create a File

710 710 

711```python

712from openai import OpenAI

713 

714client = OpenAI()

715 

716 

717def create_file(file_path):

718 with open(file_path, "rb") as file_content:

719 result = client.files.create(

720 file=file_content,

721 purpose="vision",

722 )

723 return result.id

724```

725 

726```javascript711```javascript

727import fs from "fs";712import fs from "fs";

728import OpenAI from "openai";713import OpenAI from "openai";


739}724}

740```725```

741 726 

727```python

728from openai import OpenAI

742 729 

743#### Create a base64 encoded image730client = OpenAI()

744 731 

745Create a base64 encoded image

746 732 

747```python733def create_file(file_path):

748import base64734 with open(file_path, "rb") as file_content:

735 result = client.files.create(

736 file=file_content,

737 purpose="vision",

738 )

739 return result.id

740```

749 741 

750 742 

751def encode_image(file_path):743#### Create a base64 encoded image

752 with open(file_path, "rb") as f:744 

753 base64_image = base64.b64encode(f.read()).decode("utf-8")745Create a base64 encoded image

754 return base64_image

755```

756 746 

757```javascript747```javascript

758import fs from "fs";748import fs from "fs";


763}753}

764```754```

765 755 

766 

767Edit an image

768 

769```python756```python

770from openai import OpenAI

771import base64757import base64

772 758 

773client = OpenAI()

774 

775 759 

776def encode_image(file_path):760def encode_image(file_path):

777 with open(file_path, "rb") as image_file:761 with open(file_path, "rb") as f:

778 return base64.b64encode(image_file.read()).decode("utf-8")762 base64_image = base64.b64encode(f.read()).decode("utf-8")

779 763 return base64_image

780 764```

781def create_file(file_path):

782 with open(file_path, "rb") as file_content:

783 result = client.files.create(file=file_content, purpose="vision")

784 return result.id

785 

786 

787prompt = """Generate a photorealistic image of a gift basket on a white background

788labeled 'Relax & Unwind' with a ribbon and handwriting-like font,

789containing all the items in the reference pictures."""

790 

791base64_image1 = encode_image("body-lotion.png")

792base64_image2 = encode_image("soap.png")

793file_id1 = create_file("bath-bomb.png")

794file_id2 = create_file("incense-kit.png")

795 

796response = client.responses.create(

797 model="gpt-5.6",

798 input=[

799 {

800 "role": "user",

801 "content": [

802 {"type": "input_text", "text": prompt},

803 {

804 "type": "input_image",

805 "image_url": f"data:image/png;base64,{base64_image1}",

806 },

807 {

808 "type": "input_image",

809 "image_url": f"data:image/png;base64,{base64_image2}",

810 },

811 {

812 "type": "input_image",

813 "file_id": file_id1,

814 },

815 {

816 "type": "input_image",

817 "file_id": file_id2,

818 },

819 ],

820 }

821 ],

822 tools=[{"type": "image_generation"}],

823)

824 

825image_generation_calls = [

826 output for output in response.output if output.type == "image_generation_call"

827]

828 765 

829image_data = [output.result for output in image_generation_calls]

830 766 

831if image_data:767Edit an image

832 image_base64 = image_data[0]

833 with open("gift-basket.png", "wb") as f:

834 f.write(base64.b64decode(image_base64))

835else:

836 print(response.output_text)

837```

838 768 

839```javascript769```javascript

840import fs from "fs";770import fs from "fs";


908}838}

909```839```

910 840 

841```python

842from openai import OpenAI

843import base64

911 844 

845client = OpenAI()

912 846 

913 847 

848def encode_image(file_path):

849 with open(file_path, "rb") as image_file:

850 return base64.b64encode(image_file.read()).decode("utf-8")

914 851 

915 852 

853def create_file(file_path):

854 with open(file_path, "rb") as file_content:

855 result = client.files.create(file=file_content, purpose="vision")

856 return result.id

916 857 

917Image API

918 

919 Edit an image

920 

921```python

922import base64

923from openai import OpenAI

924 

925client = OpenAI()

926 858 

927prompt = """859prompt = """Generate a photorealistic image of a gift basket on a white background

928Generate a photorealistic image of a gift basket on a white background

929labeled 'Relax & Unwind' with a ribbon and handwriting-like font,860labeled 'Relax & Unwind' with a ribbon and handwriting-like font,

930containing all the items in the reference pictures.861containing all the items in the reference pictures."""

931"""

932 862 

933result = client.images.edit(863base64_image1 = encode_image("body-lotion.png")

934 model="gpt-image-2",864base64_image2 = encode_image("soap.png")

935 image=[865file_id1 = create_file("bath-bomb.png")

936 open("body-lotion.png", "rb"),866file_id2 = create_file("incense-kit.png")

937 open("bath-bomb.png", "rb"),867 

938 open("incense-kit.png", "rb"),868response = client.responses.create(

939 open("soap.png", "rb"),869 model="gpt-5.6",

870 input=[

871 {

872 "role": "user",

873 "content": [

874 {"type": "input_text", "text": prompt},

875 {

876 "type": "input_image",

877 "image_url": f"data:image/png;base64,{base64_image1}",

878 },

879 {

880 "type": "input_image",

881 "image_url": f"data:image/png;base64,{base64_image2}",

882 },

883 {

884 "type": "input_image",

885 "file_id": file_id1,

886 },

887 {

888 "type": "input_image",

889 "file_id": file_id2,

890 },

940 ],891 ],

941 prompt=prompt,892 }

893 ],

894 tools=[{"type": "image_generation"}],

942)895)

943 896 

944image_base64 = result.data[0].b64_json897image_generation_calls = [

945image_bytes = base64.b64decode(image_base64)898 output for output in response.output if output.type == "image_generation_call"

899]

946 900 

947# Save the image to a file901image_data = [output.result for output in image_generation_calls]

948with open("gift-basket.png", "wb") as f:902 

949 f.write(image_bytes)903if image_data:

904 image_base64 = image_data[0]

905 with open("gift-basket.png", "wb") as f:

906 f.write(base64.b64decode(image_base64))

907else:

908 print(response.output_text)

950```909```

951 910 

911 

912

913 

914

915 

916

917Image API

918 

919 Edit an image

920 

952```javascript921```javascript

953import fs from "fs";922import fs from "fs";

954import OpenAI, { toFile } from "openai";923import OpenAI, { toFile } from "openai";


989fs.writeFileSync("basket.png", image_bytes);958fs.writeFileSync("basket.png", image_bytes);

990```959```

991 960 

961```python

962import base64

963from openai import OpenAI

964 

965client = OpenAI()

966 

967prompt = """

968Generate a photorealistic image of a gift basket on a white background

969labeled 'Relax & Unwind' with a ribbon and handwriting-like font,

970containing all the items in the reference pictures.

971"""

972 

973result = client.images.edit(

974 model="gpt-image-2",

975 image=[

976 open("body-lotion.png", "rb"),

977 open("bath-bomb.png", "rb"),

978 open("incense-kit.png", "rb"),

979 open("soap.png", "rb"),

980 ],

981 prompt=prompt,

982)

983 

984image_base64 = result.data[0].b64_json

985image_bytes = base64.b64decode(image_base64)

986 

987# Save the image to a file

988with open("gift-basket.png", "wb") as f:

989 f.write(image_bytes)

990```

991 

992```bash992```bash

993curl -s -D >(grep -i x-request-id >&2) \993curl -s -D >(grep -i x-request-id >&2) \

994 -o >(jq -r '.data[0].b64_json' | base64 --decode > gift-basket.png) \994 -o >(jq -r '.data[0].b64_json' | base64 --decode > gift-basket.png) \


1033 1033 

1034 Edit an image with a mask1034 Edit an image with a mask

1035 1035 

1036```python

1037from openai import OpenAI

1038import base64

1039 

1040client = OpenAI()

1041 

1042 

1043def create_file(file_path):

1044 with open(file_path, "rb") as file_content:

1045 result = client.files.create(file=file_content, purpose="vision")

1046 return result.id

1047 

1048 

1049fileId = create_file("sunlit_lounge.png")

1050maskId = create_file("mask.png")

1051 

1052response = client.responses.create(

1053 model="gpt-5.6",

1054 input=[

1055 {

1056 "role": "user",

1057 "content": [

1058 {

1059 "type": "input_text",

1060 "text": "generate an image of the same sunlit indoor lounge area with a pool but the pool should contain a flamingo",

1061 },

1062 {

1063 "type": "input_image",

1064 "file_id": fileId,

1065 },

1066 ],

1067 },

1068 ],

1069 tools=[

1070 {

1071 "type": "image_generation",

1072 "quality": "high",

1073 "input_image_mask": {

1074 "file_id": maskId,

1075 },

1076 },

1077 ],

1078)

1079 

1080image_data = [

1081 output.result

1082 for output in response.output

1083 if output.type == "image_generation_call"

1084]

1085 

1086if image_data:

1087 image_base64 = image_data[0]

1088 with open("lounge.png", "wb") as f:

1089 f.write(base64.b64decode(image_base64))

1090```

1091 

1092```javascript1036```javascript

1093import fs from "fs";1037import fs from "fs";

1094import OpenAI from "openai";1038import OpenAI from "openai";


1145}1089}

1146```1090```

1147 1091 

1092```python

1093from openai import OpenAI

1094import base64

1148 1095 

1096client = OpenAI()

1149 1097 

1150 1098 

1099def create_file(file_path):

1100 with open(file_path, "rb") as file_content:

1101 result = client.files.create(file=file_content, purpose="vision")

1102 return result.id

1151 1103 

1152 1104 

1153Image API1105fileId = create_file("sunlit_lounge.png")

1106maskId = create_file("mask.png")

1154 1107 

1155 Edit an image with a mask1108response = client.responses.create(

1109 model="gpt-5.6",

1110 input=[

1111 {

1112 "role": "user",

1113 "content": [

1114 {

1115 "type": "input_text",

1116 "text": "generate an image of the same sunlit indoor lounge area with a pool but the pool should contain a flamingo",

1117 },

1118 {

1119 "type": "input_image",

1120 "file_id": fileId,

1121 },

1122 ],

1123 },

1124 ],

1125 tools=[

1126 {

1127 "type": "image_generation",

1128 "quality": "high",

1129 "input_image_mask": {

1130 "file_id": maskId,

1131 },

1132 },

1133 ],

1134)

1156 1135 

1157```python1136image_data = [

1158from openai import OpenAI1137 output.result

1159import base641138 for output in response.output

1139 if output.type == "image_generation_call"

1140]

1160 1141 

1161client = OpenAI()1142if image_data:

1143 image_base64 = image_data[0]

1144 with open("lounge.png", "wb") as f:

1145 f.write(base64.b64decode(image_base64))

1146```

1162 1147 

1163result = client.images.edit(

1164 model="gpt-image-2",

1165 image=open("sunlit_lounge.png", "rb"),

1166 mask=open("mask.png", "rb"),

1167 prompt="A sunlit indoor lounge area with a pool containing a flamingo",

1168)

1169 1148

1170image_base64 = result.data[0].b64_json

1171image_bytes = base64.b64decode(image_base64)

1172 1149 

1173# Save the image to a file1150

1174with open("composition.png", "wb") as f:1151 

1175 f.write(image_bytes)1152

1176```1153Image API

1154 

1155 Edit an image with a mask

1177 1156 

1178```javascript1157```javascript

1179import fs from "fs";1158import fs from "fs";


1198fs.writeFileSync("lounge.png", image_bytes);1177fs.writeFileSync("lounge.png", image_bytes);

1199```1178```

1200 1179 

1180```python

1181from openai import OpenAI

1182import base64

1183 

1184client = OpenAI()

1185 

1186result = client.images.edit(

1187 model="gpt-image-2",

1188 image=open("sunlit_lounge.png", "rb"),

1189 mask=open("mask.png", "rb"),

1190 prompt="A sunlit indoor lounge area with a pool containing a flamingo",

1191)

1192 

1193image_base64 = result.data[0].b64_json

1194image_bytes = base64.b64decode(image_base64)

1195 

1196# Save the image to a file

1197with open("composition.png", "wb") as f:

1198 f.write(image_bytes)

1199```

1200 

1201```bash1201```bash

1202curl -s -D >(grep -i x-request-id >&2) \1202curl -s -D >(grep -i x-request-id >&2) \

1203 -o >(jq -r '.data[0].b64_json' | base64 --decode > lounge.png) \1203 -o >(jq -r '.data[0].b64_json' | base64 --decode > lounge.png) \

Details

56 56 

57Reasoning effort set to none57Reasoning effort set to none

58 58 

59```bash

60curl --request POST \

61 --url https://api.openai.com/v1/responses \

62 --header "Authorization: Bearer $OPENAI_API_KEY" \

63 --header 'Content-type: application/json' \

64 --data '{

65 "model": "gpt-5.2",

66 "input": "Think carefully and outline your steps before answering. How much gold would it take to coat the Statue of Liberty in a 1mm layer?",

67 "reasoning": {

68 "effort": "none"

69 }

70}'

71```

72 

73```javascript59```javascript

74import OpenAI from "openai";60import OpenAI from "openai";

75const openai = new OpenAI();61const openai = new OpenAI();


100print(response)86print(response)

101```87```

102 88 

89```bash

90curl --request POST \

91 --url https://api.openai.com/v1/responses \

92 --header "Authorization: Bearer $OPENAI_API_KEY" \

93 --header 'Content-type: application/json' \

94 --data '{

95 "model": "gpt-5.2",

96 "input": "Think carefully and outline your steps before answering. How much gold would it take to coat the Statue of Liberty in a 1mm layer?",

97 "reasoning": {

98 "effort": "none"

99 }

100}'

101```

102 

103 103 

104### Verbosity104### Verbosity

105 105 


114 114 

115Control verbosity115Control verbosity

116 116 

117```bash

118curl --request POST \

119 --url https://api.openai.com/v1/responses \

120 --header "Authorization: Bearer $OPENAI_API_KEY" \

121 --header 'Content-type: application/json' \

122 --data '{

123 "model": "gpt-5.2",

124 "input": "What is the answer to the ultimate question of life, the universe, and everything?",

125 "text": {

126 "verbosity": "low"

127 }

128}'

129```

130 

131```javascript117```javascript

132import OpenAI from "openai";118import OpenAI from "openai";

133const openai = new OpenAI();119const openai = new OpenAI();


158print(response)144print(response)

159```145```

160 146 

147```bash

148curl --request POST \

149 --url https://api.openai.com/v1/responses \

150 --header "Authorization: Bearer $OPENAI_API_KEY" \

151 --header 'Content-type: application/json' \

152 --data '{

153 "model": "gpt-5.2",

154 "input": "What is the answer to the ultimate question of life, the universe, and everything?",

155 "text": {

156 "verbosity": "low"

157 }

158}'

159```

160 

161 161 

162You can still steer verbosity through prompting after setting it to `low` in the API. The verbosity parameter defines a general token range at the system prompt level, but the actual output is flexible to both developer and user prompts within that range.162You can still steer verbosity through prompting after setting it to `low` in the API. The verbosity parameter defines a general token range at the system prompt level, but the actual output is flexible to both developer and user prompts within that range.

163 163 

Details

59 59 

60Reasoning effort set to none60Reasoning effort set to none

61 61 

62```bash

63curl --request POST \

64 --url https://api.openai.com/v1/responses \

65 --header "Authorization: Bearer $OPENAI_API_KEY" \

66 --header 'Content-type: application/json' \

67 --data '{

68 "model": "gpt-5.4",

69 "input": "Think carefully and outline your steps before answering. How much gold would it take to coat the Statue of Liberty in a 1mm layer?",

70 "reasoning": {

71 "effort": "none"

72 }

73}'

74```

75 

76```javascript62```javascript

77import OpenAI from "openai";63import OpenAI from "openai";

78const openai = new OpenAI();64const openai = new OpenAI();


103print(response)89print(response)

104```90```

105 91 

92```bash

93curl --request POST \

94 --url https://api.openai.com/v1/responses \

95 --header "Authorization: Bearer $OPENAI_API_KEY" \

96 --header 'Content-type: application/json' \

97 --data '{

98 "model": "gpt-5.4",

99 "input": "Think carefully and outline your steps before answering. How much gold would it take to coat the Statue of Liberty in a 1mm layer?",

100 "reasoning": {

101 "effort": "none"

102 }

103}'

104```

105 

106 106 

107### Verbosity107### Verbosity

108 108 


117 117 

118Control verbosity118Control verbosity

119 119 

120```bash

121curl --request POST \

122 --url https://api.openai.com/v1/responses \

123 --header "Authorization: Bearer $OPENAI_API_KEY" \

124 --header 'Content-type: application/json' \

125 --data '{

126 "model": "gpt-5.4",

127 "input": "What is the answer to the ultimate question of life, the universe, and everything?",

128 "text": {

129 "verbosity": "low"

130 }

131}'

132```

133 

134```javascript120```javascript

135import OpenAI from "openai";121import OpenAI from "openai";

136const openai = new OpenAI();122const openai = new OpenAI();


161print(response)147print(response)

162```148```

163 149 

150```bash

151curl --request POST \

152 --url https://api.openai.com/v1/responses \

153 --header "Authorization: Bearer $OPENAI_API_KEY" \

154 --header 'Content-type: application/json' \

155 --data '{

156 "model": "gpt-5.4",

157 "input": "What is the answer to the ultimate question of life, the universe, and everything?",

158 "text": {

159 "verbosity": "low"

160 }

161}'

162```

163 

164 164 

165You can still steer verbosity through prompting after setting it to `low` in the API. The verbosity parameter defines a general token range at the system prompt level, but the actual output is flexible to both developer and user prompts within that range.165You can still steer verbosity through prompting after setting it to `low` in the API. The verbosity parameter defines a general token range at the system prompt level, but the actual output is flexible to both developer and user prompts within that range.

166 166 

Details

151 151 

152Reuse simple message input152Reuse simple message input

153 153 

154```bash

155INPUT='[

156 { "role": "system", "content": "You are a helpful assistant." },

157 { "role": "user", "content": "Hello!" }

158]'

159 

160curl -s https://api.openai.com/v1/chat/completions \

161 -H "Content-Type: application/json" \

162 -H "Authorization: Bearer $OPENAI_API_KEY" \

163 -d "{

164 \"model\": \"gpt-5.6\",

165 \"messages\": $INPUT

166 }"

167 

168curl -s https://api.openai.com/v1/responses \

169 -H "Content-Type: application/json" \

170 -H "Authorization: Bearer $OPENAI_API_KEY" \

171 -d "{

172 \"model\": \"gpt-5.6\",

173 \"input\": $INPUT

174 }"

175```

176 

177```javascript154```javascript

178/** @type {OpenAI.ChatCompletionMessageParam[] & OpenAI.Responses.ResponseInput} */155/** @type {OpenAI.ChatCompletionMessageParam[] & OpenAI.Responses.ResponseInput} */

179const context = [156const context = [


203response = client.responses.create(model="gpt-5.6", input=context)180response = client.responses.create(model="gpt-5.6", input=context)

204```181```

205 182 

183```bash

184INPUT='[

185 { "role": "system", "content": "You are a helpful assistant." },

186 { "role": "user", "content": "Hello!" }

187]'

188 

189curl -s https://api.openai.com/v1/chat/completions \

190 -H "Content-Type: application/json" \

191 -H "Authorization: Bearer $OPENAI_API_KEY" \

192 -d "{

193 \"model\": \"gpt-5.6\",

194 \"messages\": $INPUT

195 }"

196 

197curl -s https://api.openai.com/v1/responses \

198 -H "Content-Type: application/json" \

199 -H "Authorization: Bearer $OPENAI_API_KEY" \

200 -d "{

201 \"model\": \"gpt-5.6\",

202 \"input\": $INPUT

203 }"

204```

205 

206 206 

207 207 

208 208 


380 input of another.380 input of another.

381 Multi-turn conversation381 Multi-turn conversation

382 382 

383```python

384context = [{"role": "user", "content": "What is the capital of France?"}]

385res1 = client.responses.create(

386 model="gpt-5.6",

387 input=context,

388)

389 

390# Append the first response's output to context

391context += res1.output

392 

393# Add the next user message

394context += [{"role": "user", "content": "And its population?"}]

395 

396res2 = client.responses.create(

397 model="gpt-5.6",

398 input=context,

399)

400```

401 

402```javascript383```javascript

403/** @type {OpenAI.Responses.ResponseInput} */384/** @type {OpenAI.Responses.ResponseInput} */

404let context = [{ role: "user", content: "What is the capital of France?" }];385let context = [{ role: "user", content: "What is the capital of France?" }];


418 model: "gpt-5.6",399 model: "gpt-5.6",

419 input: context,400 input: context,

420});401});

402```

403 

404```python

405context = [{"role": "user", "content": "What is the capital of France?"}]

406res1 = client.responses.create(

407 model="gpt-5.6",

408 input=context,

409)

410 

411# Append the first response's output to context

412context += res1.output

413 

414# Add the next user message

415context += [{"role": "user", "content": "And its population?"}]

416 

417res2 = client.responses.create(

418 model="gpt-5.6",

419 input=context,

420)

421```421```

422 422 

423 You can also use `previous_response_id` to reference the previous response423 You can also use `previous_response_id` to reference the previous response


545 545 

546 Structured Outputs546 Structured Outputs

547 547 

548```javascript

549const completion = await openai.chat.completions.create({

550 model: "gpt-5.6",

551 messages: [

552 {

553 role: "user",

554 content: "Jane, 54 years old",

555 },

556 ],

557 response_format: {

558 type: "json_schema",

559 json_schema: {

560 name: "person",

561 strict: true,

562 schema: {

563 type: "object",

564 properties: {

565 name: {

566 type: "string",

567 minLength: 1,

568 },

569 age: {

570 type: "number",

571 minimum: 0,

572 maximum: 130,

573 },

574 },

575 required: ["name", "age"],

576 additionalProperties: false,

577 },

578 },

579 },

580 reasoning_effort: "medium",

581});

582```

583 

584```python

585from openai import OpenAI

586 

587client = OpenAI()

588 

589response = client.chat.completions.create(

590 model="gpt-5.6",

591 messages=[

592 {

593 "role": "user",

594 "content": "Jane, 54 years old",

595 }

596 ],

597 response_format={

598 "type": "json_schema",

599 "json_schema": {

600 "name": "person",

601 "strict": True,

602 "schema": {

603 "type": "object",

604 "properties": {

605 "name": {"type": "string", "minLength": 1},

606 "age": {"type": "number", "minimum": 0, "maximum": 130},

607 },

608 "required": ["name", "age"],

609 "additionalProperties": False,

610 },

611 },

612 },

613 reasoning_effort="medium",

614)

615```

616 

548```bash617```bash

549curl https://api.openai.com/v1/chat/completions \618curl https://api.openai.com/v1/chat/completions \

550 -H "Content-Type: application/json" \619 -H "Content-Type: application/json" \


587}'656}'

588```657```

589 658 

590```python

591from openai import OpenAI

592 659

593client = OpenAI()

594 660 

595response = client.chat.completions.create(661

596 model="gpt-5.6",662 

597 messages=[663

598 {664Responses

599 "role": "user",665 

600 "content": "Jane, 54 years old",666 Structured Outputs

601 }

602 ],

603 response_format={

604 "type": "json_schema",

605 "json_schema": {

606 "name": "person",

607 "strict": True,

608 "schema": {

609 "type": "object",

610 "properties": {

611 "name": {"type": "string", "minLength": 1},

612 "age": {"type": "number", "minimum": 0, "maximum": 130},

613 },

614 "required": ["name", "age"],

615 "additionalProperties": False,

616 },

617 },

618 },

619 reasoning_effort="medium",

620)

621```

622 667 

623```javascript668```javascript

624const completion = await openai.chat.completions.create({669const response = await openai.responses.create({

625 model: "gpt-5.6",670 model: "gpt-5.6",

626 messages: [671 input: "Jane, 54 years old",

627 {672 text: {

628 role: "user",673 format: {

629 content: "Jane, 54 years old",

630 },

631 ],

632 response_format: {

633 type: "json_schema",674 type: "json_schema",

634 json_schema: {

635 name: "person",675 name: "person",

636 strict: true,676 strict: true,

637 schema: {677 schema: {


652 },692 },

653 },693 },

654 },694 },

655 reasoning_effort: "medium",

656});695});

657```696```

658 697 

659 698```python

660 699response = client.responses.create(

661 700 model="gpt-5.6",

662 701 input="Jane, 54 years old",

663 702 text={

664Responses703 "format": {

665 704 "type": "json_schema",

666 Structured Outputs705 "name": "person",

706 "strict": True,

707 "schema": {

708 "type": "object",

709 "properties": {

710 "name": {"type": "string", "minLength": 1},

711 "age": {"type": "number", "minimum": 0, "maximum": 130},

712 },

713 "required": ["name", "age"],

714 "additionalProperties": False,

715 },

716 }

717 },

718)

719```

667 720 

668```bash721```bash

669curl https://api.openai.com/v1/responses \722curl https://api.openai.com/v1/responses \


701}'754}'

702```755```

703 756 

704```python

705response = client.responses.create(

706 model="gpt-5.6",

707 input="Jane, 54 years old",

708 text={

709 "format": {

710 "type": "json_schema",

711 "name": "person",

712 "strict": True,

713 "schema": {

714 "type": "object",

715 "properties": {

716 "name": {"type": "string", "minLength": 1},

717 "age": {"type": "number", "minimum": 0, "maximum": 130},

718 },

719 "required": ["name", "age"],

720 "additionalProperties": False,

721 },

722 }

723 },

724)

725```

726 

727```javascript

728const response = await openai.responses.create({

729 model: "gpt-5.6",

730 input: "Jane, 54 years old",

731 text: {

732 format: {

733 type: "json_schema",

734 name: "person",

735 strict: true,

736 schema: {

737 type: "object",

738 properties: {

739 name: {

740 type: "string",

741 minLength: 1,

742 },

743 age: {

744 type: "number",

745 minimum: 0,

746 maximum: 130,

747 },

748 },

749 required: ["name", "age"],

750 additionalProperties: false,

751 },

752 },

753 },

754});

755```

756 

757 757 

758 758 

759### 7. Update streaming consumers759### 7. Update streaming consumers

Details

27 27 

28Generate a response with moderation scores28Generate a response with moderation scores

29 29 

30```python

31from openai import OpenAI

32 

33client = OpenAI()

34 

35response = client.responses.create(

36 model="gpt-5.6",

37 input=[

38 {

39 "role": "user",

40 "content": (

41 "A user asks for instructions to make a harmful weapon. "

42 "Draft a brief refusal and offer a safer alternative."

43 ),

44 }

45 ],

46 moderation={"model": "omni-moderation-latest"},

47)

48 

49input_moderation = response.moderation.input

50output_moderation = response.moderation.output

51if input_moderation.type == "error":

52 raise RuntimeError(input_moderation.message)

53if output_moderation.type == "error":

54 raise RuntimeError(output_moderation.message)

55 

56print(input_moderation.flagged)

57print(output_moderation.flagged)

58```

59 

60```javascript30```javascript

61import OpenAI from "openai";31import OpenAI from "openai";

62 32 


87console.log(outputModeration.flagged);57console.log(outputModeration.flagged);

88```58```

89 59 

60```python

61from openai import OpenAI

62 

63client = OpenAI()

64 

65response = client.responses.create(

66 model="gpt-5.6",

67 input=[

68 {

69 "role": "user",

70 "content": (

71 "A user asks for instructions to make a harmful weapon. "

72 "Draft a brief refusal and offer a safer alternative."

73 ),

74 }

75 ],

76 moderation={"model": "omni-moderation-latest"},

77)

78 

79input_moderation = response.moderation.input

80output_moderation = response.moderation.output

81if input_moderation.type == "error":

82 raise RuntimeError(input_moderation.message)

83if output_moderation.type == "error":

84 raise RuntimeError(output_moderation.message)

85 

86print(input_moderation.flagged)

87print(output_moderation.flagged)

88```

89 

90 90 

91The Responses API returns an input `moderation_result` object at `response.moderation.input` and an output `moderation_result` object at `response.moderation.output`.91The Responses API returns an input `moderation_result` object at `response.moderation.input` and an output `moderation_result` object at `response.moderation.output`.

92 92 


114 114 

115Get classification information for a text input115Get classification information for a text input

116 116 

117```javascript

118import OpenAI from "openai";

119const openai = new OpenAI();

120 

121const moderation = await openai.moderations.create({

122 model: "omni-moderation-latest",

123 input: "...text to classify goes here...",

124});

125 

126console.log(moderation);

127```

128 

117```python129```python

118from openai import OpenAI130from openai import OpenAI

119 131 


127print(response)139print(response)

128```140```

129 141 

130```javascript

131import OpenAI from "openai";

132const openai = new OpenAI();

133 

134const moderation = await openai.moderations.create({

135 model: "omni-moderation-latest",

136 input: "...text to classify goes here...",

137});

138 

139console.log(moderation);

140```

141 

142```bash142```bash

143curl https://api.openai.com/v1/moderations \143curl https://api.openai.com/v1/moderations \

144 -X POST \144 -X POST \


162 162 

163Get classification information for image and text input163Get classification information for image and text input

164 164 

165```javascript

166import OpenAI from "openai";

167const openai = new OpenAI();

168 

169const moderation = await openai.moderations.create({

170 model: "omni-moderation-latest",

171 input: [

172 { type: "text", text: "...text to classify goes here..." },

173 {

174 type: "image_url",

175 image_url: {

176 url: "https://example.com/image.png",

177 // You can also use a Base64 encoded image URL.

178 // url: "data:image/jpeg;base64,abcdefg...",

179 },

180 },

181 ],

182});

183 

184console.log(moderation);

185```

186 

165```python187```python

166from openai import OpenAI188from openai import OpenAI

167 189 


185print(response)207print(response)

186```208```

187 209 

188```javascript

189import OpenAI from "openai";

190const openai = new OpenAI();

191 

192const moderation = await openai.moderations.create({

193 model: "omni-moderation-latest",

194 input: [

195 { type: "text", text: "...text to classify goes here..." },

196 {

197 type: "image_url",

198 image_url: {

199 url: "https://example.com/image.png",

200 // You can also use a Base64 encoded image URL.

201 // url: "data:image/jpeg;base64,abcdefg...",

202 },

203 },

204 ],

205});

206 

207console.log(moderation);

208```

209 

210```bash210```bash

211curl https://api.openai.com/v1/moderations \211curl https://api.openai.com/v1/moderations \

212 -X POST \212 -X POST \

Details

35print(response.output_text)35print(response.output_text)

36```36```

37 37 

38```bash38```go

39openai responses create \39package main

40 --model "gpt-5.6" \

41 --input "Write a one-sentence bedtime story about a unicorn." \

42 --raw-output \

43 --transform 'output.#(type=="message").content.0.text'

44```

45 40 

46```csharp41import (

47using OpenAI.Responses;42 "context"

48#pragma warning disable OPENAI00143 "fmt"

49 44 

50string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;45 "github.com/openai/openai-go/v3"

51ResponsesClient client = new(key);46 "github.com/openai/openai-go/v3/option"

47 "github.com/openai/openai-go/v3/responses"

48)

52 49 

53ResponseResult response = await client.CreateResponseAsync(50func main() {

54 "gpt-5.6",51 client := openai.NewClient(

55 "Say 'this is a test.'"52 option.WithAPIKey("My API Key"), // or set OPENAI_API_KEY in your env

56);53 )

57 54 

58Console.WriteLine($"[ASSISTANT]: {response.GetOutputText()}");55 resp, err := client.Responses.New(context.TODO(), responses.ResponseNewParams{

56 Model: "gpt-5.6",

57 Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Say this is a test")},

58 })

59 if err != nil {

60 panic(err.Error())

61 }

62 

63 fmt.Println(resp.OutputText())

64}

59```65```

60 66 

61```java67```java


81}87}

82```88```

83 89 

84```go90```csharp

85package main91using OpenAI.Responses;

86 92#pragma warning disable OPENAI001

87import (

88 "context"

89 "fmt"

90 

91 "github.com/openai/openai-go/v3"

92 "github.com/openai/openai-go/v3/option"

93 "github.com/openai/openai-go/v3/responses"

94)

95 93 

96func main() {94string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;

97 client := openai.NewClient(95ResponsesClient client = new(key);

98 option.WithAPIKey("My API Key"), // or set OPENAI_API_KEY in your env

99 )

100 96 

101 resp, err := client.Responses.New(context.TODO(), responses.ResponseNewParams{97ResponseResult response = await client.CreateResponseAsync(

102 Model: "gpt-5.6",98 "gpt-5.6",

103 Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Say this is a test")},99 "Say 'this is a test.'"

104 })100);

105 if err != nil {

106 panic(err.Error())

107 }

108 101 

109 fmt.Println(resp.OutputText())102Console.WriteLine($"[ASSISTANT]: {response.GetOutputText()}");

110}

111```103```

112 104 

113```ruby105```ruby


123puts(response.output_text)115puts(response.output_text)

124```116```

125 117 

118```bash

119openai responses create \

120 --model "gpt-5.6" \

121 --input "Write a one-sentence bedtime story about a unicorn." \

122 --raw-output \

123 --transform 'output.#(type=="message").content.0.text'

124```

125 

126```bash126```bash

127curl "https://api.openai.com/v1/responses" \127curl "https://api.openai.com/v1/responses" \

128 -H "Content-Type: application/json" \128 -H "Content-Type: application/json" \


210console.log(response.output_text);210console.log(response.output_text);

211```211```

212 212 

213```python

214from openai import OpenAI

215 

216client = OpenAI()

217 

218response = client.responses.create(

219 model="gpt-5.6",

220 reasoning={"effort": "low"},

221 instructions="Talk like a pirate.",

222 input="Are semicolons optional in JavaScript?",

223)

224 

225print(response.output_text)

226```

227 

213```go228```go

214package main229package main

215 230 


242}257}

243```258```

244 259 

245```python

246from openai import OpenAI

247 

248client = OpenAI()

249 

250response = client.responses.create(

251 model="gpt-5.6",

252 reasoning={"effort": "low"},

253 instructions="Talk like a pirate.",

254 input="Are semicolons optional in JavaScript?",

255)

256 

257print(response.output_text)

258```

259 

260```bash260```bash

261curl "https://api.openai.com/v1/responses" \261curl "https://api.openai.com/v1/responses" \

262 -H "Content-Type: application/json" \262 -H "Content-Type: application/json" \


296console.log(response.output_text);296console.log(response.output_text);

297```297```

298 298 

299```python

300from openai import OpenAI

301 

302client = OpenAI()

303 

304response = client.responses.create(

305 model="gpt-5.6",

306 reasoning={"effort": "low"},

307 input=[

308 {"role": "developer", "content": "Talk like a pirate."},

309 {"role": "user", "content": "Are semicolons optional in JavaScript?"},

310 ],

311)

312 

313print(response.output_text)

314```

315 

299```go316```go

300package main317package main

301 318 


336}353}

337```354```

338 355 

339```python

340from openai import OpenAI

341 

342client = OpenAI()

343 

344response = client.responses.create(

345 model="gpt-5.6",

346 reasoning={"effort": "low"},

347 input=[

348 {"role": "developer", "content": "Talk like a pirate."},

349 {"role": "user", "content": "Are semicolons optional in JavaScript?"},

350 ],

351)

352 

353print(response.output_text)

354```

355 

356```bash356```bash

357curl "https://api.openai.com/v1/responses" \357curl "https://api.openai.com/v1/responses" \

358 -H "Content-Type: application/json" \358 -H "Content-Type: application/json" \


486console.log(response.output_text);486console.log(response.output_text);

487```487```

488 488 

489```python

490from openai import OpenAI

491 

492client = OpenAI()

493 

494with open("prompt.txt", "r", encoding="utf-8") as f:

495 instructions = f.read()

496 

497response = client.responses.create(

498 model="gpt-5.6",

499 instructions=instructions,

500 input="How would I declare a variable for a last name?",

501)

502 

503print(response.output_text)

504```

505 

489```go506```go

490package main507package main

491 508 


521}538}

522```539```

523 540 

524```python

525from openai import OpenAI

526 

527client = OpenAI()

528 

529with open("prompt.txt", "r", encoding="utf-8") as f:

530 instructions = f.read()

531 

532response = client.responses.create(

533 model="gpt-5.6",

534 instructions=instructions,

535 input="How would I declare a variable for a last name?",

536)

537 

538print(response.output_text)

539```

540 

541```bash541```bash

542curl https://api.openai.com/v1/responses \542curl https://api.openai.com/v1/responses \

543 -H "Authorization: Bearer $OPENAI_API_KEY" \543 -H "Authorization: Bearer $OPENAI_API_KEY" \

Details

400 400 

4011. **Set `additionalProperties` to `false`** for all objects.4011. **Set `additionalProperties` to `false`** for all objects.

4021. **Mark all properties as required**.4021. **Mark all properties as required**.

4031. **For structured output schemas**, wrap them in [`json_schema`](https://developers.openai.com/api/docs/guides/structured-outputs#how-to-use?context=without_parse) object.4031. **For structured output schemas**, wrap them in [`json_schema`](https://developers.openai.com/api/docs/guides/structured-outputs?context=without_parse#how-to-use) object.

4041. **For functions**, wrap them in a [`function`](https://developers.openai.com/api/docs/guides/function-calling#step-3-pass-your-function-definitions-as-available-tools-to-the-model-along-with-the-messages) object.4041. **For functions**, wrap them in a [`function`](https://developers.openai.com/api/docs/guides/function-calling#defining-functions) object.

405 405 

406The Realtime API406The Realtime API

407 [function](https://developers.openai.com/api/docs/guides/realtime-conversations#function-calling) object407 [function](https://developers.openai.com/api/docs/guides/realtime-conversations#function-calling) object

Details

16Start by creating a [webhook](https://developers.openai.com/api/docs/guides/webhooks) for incoming calls, through your **platform.openai.com** [settings](https://platform.openai.com/settings) > Project > **Webhooks**.16Start by creating a [webhook](https://developers.openai.com/api/docs/guides/webhooks) for incoming calls, through your **platform.openai.com** [settings](https://platform.openai.com/settings) > Project > **Webhooks**.

17Then, point your SIP trunk at the OpenAI SIP endpoint, using the project ID17Then, point your SIP trunk at the OpenAI SIP endpoint, using the project ID

18for which you configured the webhook, e.g., `sip:$PROJECT_ID@sip.api.openai.com;transport=tls`.18for which you configured the webhook, e.g., `sip:$PROJECT_ID@sip.api.openai.com;transport=tls`.

19For European data residency, use `sip:$PROJECT_ID@sip-eu.api.openai.com;transport=tls` instead.

19To find your `$PROJECT_ID`, visit [settings](https://platform.openai.com/settings) > Project > **General**. That page will display the project ID, which20To find your `$PROJECT_ID`, visit [settings](https://platform.openai.com/settings) > Project > **General**. That page will display the project ID, which

20will have a `proj_` prefix.21will have a `proj_` prefix.

21 22 


180 181 

181The API responds with `200 OK` when it starts tearing down the call.182The API responds with `200 OK` when it starts tearing down the call.

182 183 

183## Dedicated SIP IP ranges184<a id="dedicated-sip-ip-ranges"></a>

184 185 

185If you need to allowlist OpenAI SIP traffic. `sip.api.openai.com` does GeoIP routing, you186## SIP signaling and media IP ranges

186will be connected to the closest region.

187 187 

188- `13.79.45.80/28` for `northeurope`188Realtime SIP calls use separate network paths for signaling and media. To ensure proper operation,

189- `23.98.140.64/28` for `southcentralus`189configure your network to allow signaling and media traffic as described below.

190- `40.67.149.176/28` for `eastus2`190 

191- `40.83.204.240/28` for `westus`191### SIP signaling

192 

193`sip.api.openai.com` and `sip-eu.api.openai.com` are GeoIP-routed endpoints. Your network must allow

194outbound TCP/TLS traffic to the addresses returned by DNS on port `5061`.

195 

196### SRTP media

197 

198The API specifies a separate media IP address and UDP port in the negotiated SDP. Your network must

199allow bidirectional SRTP traffic over UDP to and from the following CIDRs:

200 

201- `13.79.45.80/28`

202- `23.98.140.64/28`

203- `40.67.149.176/28`

204- `40.83.204.240/28`

192 205 

193## Python example206## Python example

194 207 

Details

46 46 

47Review a pull request with subagents47Review a pull request with subagents

48 48 

49```typescript

50import OpenAI from "openai";

51 

52const client = new OpenAI();

53 

54async function reviewPullRequest(diff: string): Promise<string> {

55 const response = await client.beta.responses.create({

56 model: "gpt-5.6-sol",

57 input:

58 "Review the pull-request diff below with three agents: one for " +

59 "correctness, one for security, and one for missing tests. " +

60 "Reconcile duplicate or conflicting findings, then return a " +

61 "prioritized review with file and line references.\n\n" +

62 `<diff>\n${diff}\n</diff>`,

63 multi_agent: {

64 enabled: true,

65 max_concurrent_subagents: 3,

66 },

67 betas: ["responses_multi_agent=v1"],

68 });

69 

70 return response.output

71 .flatMap((item) =>

72 item.type === "message" &&

73 item.agent?.agent_name === "/root" &&

74 item.phase === "final_answer"

75 ? item.content

76 : []

77 )

78 .filter((part) => part.type === "output_text")

79 .map((part) => part.text)

80 .join("");

81}

82```

83 

49```python84```python

50from openai import OpenAI85from openai import OpenAI

51 86 


83 )118 )

84```119```

85 120 

86```typescript

87import OpenAI from "openai";

88 

89const client = new OpenAI();

90 

91async function reviewPullRequest(diff: string): Promise<string> {

92 const response = await client.beta.responses.create({

93 model: "gpt-5.6-sol",

94 input:

95 "Review the pull-request diff below with three agents: one for " +

96 "correctness, one for security, and one for missing tests. " +

97 "Reconcile duplicate or conflicting findings, then return a " +

98 "prioritized review with file and line references.\n\n" +

99 `<diff>\n${diff}\n</diff>`,

100 multi_agent: {

101 enabled: true,

102 max_concurrent_subagents: 3,

103 },

104 betas: ["responses_multi_agent=v1"],

105 });

106 

107 return response.output

108 .flatMap((item) =>

109 item.type === "message" &&

110 item.agent?.agent_name === "/root" &&

111 item.phase === "final_answer"

112 ? item.content

113 : []

114 )

115 .filter((part) => part.type === "output_text")

116 .map((part) => part.text)

117 .join("");

118}

119```

120 

121 121 

122`max_concurrent_subagents` sets the maximum number of subagents that can be active simultaneously across the entire agent tree. It includes all descendants—children, grandchildren, and deeper subagents—but excludes the root agent.122`max_concurrent_subagents` sets the maximum number of subagents that can be active simultaneously across the entire agent tree. It includes all descendants—children, grandchildren, and deeper subagents—but excludes the root agent.

123 123 


186 186 

187Handle HTTP streaming tool calls187Handle HTTP streaming tool calls

188 188 

189```python

190from __future__ import annotations

191 

192import json

193import sys

194 

195from openai import OpenAI

196from openai.types.beta import BetaResponseOutputItem

197 

198client = OpenAI()

199ROOT = "/root"

200PROPOSALS = {

201 "alpha": {"estimated_weeks": 6, "risk": "medium"},

202 "beta": {"estimated_weeks": 8, "risk": "low"},

203}

204tools = [

205 {

206 "type": "function",

207 "name": "get_proposal",

208 "description": "Return details for a proposal that the agents should compare.",

209 "parameters": {

210 "type": "object",

211 "properties": {

212 "proposal": {

213 "type": "string",

214 "enum": ["alpha", "beta"],

215 }

216 },

217 "required": ["proposal"],

218 "additionalProperties": False,

219 },

220 "strict": True,

221 }

222]

223history = [

224 {

225 "role": "user",

226 "content": "Compare proposal alpha and proposal beta.",

227 }

228]

229 

230 

231def agent_name(item: BetaResponseOutputItem) -> str:

232 return item.agent.agent_name if item.agent else ROOT

233 

234 

235def render_to_user(delta: str) -> None:

236 print(delta, end="", flush=True)

237 

238 

239def log_subagent_text(agent: str, delta: str) -> None:

240 print(f"[{agent}] {delta}", end="", file=sys.stderr, flush=True)

241 

242 

243def process_tool_call(name: str, arguments: str) -> str:

244 if name != "get_proposal":

245 raise ValueError(f"Unknown tool: {name}")

246 parsed_arguments = json.loads(arguments)

247 return json.dumps(PROPOSALS[parsed_arguments["proposal"]])

248 

249 

250while True:

251 output_items = []

252 pending_calls = []

253 item_agents: dict[int, str] = {}

254 

255 stream = client.beta.responses.create(

256 model="gpt-5.6-sol",

257 input=history,

258 tools=tools,

259 store=False,

260 multi_agent={

261 "enabled": True,

262 "max_concurrent_subagents": 3,

263 },

264 stream=True,

265 betas=["responses_multi_agent=v1"],

266 )

267 for event in stream:

268 if event.type == "response.output_item.added":

269 item_agents[event.output_index] = agent_name(event.item)

270 elif event.type == "response.output_text.delta":

271 agent = item_agents.get(event.output_index, ROOT)

272 if agent == ROOT:

273 render_to_user(event.delta)

274 else:

275 log_subagent_text(agent, event.delta)

276 elif event.type == "response.output_item.done":

277 output_items.append(event.item)

278 if event.item.type == "function_call":

279 # Handle function calls from both the root agent and subagents.

280 pending_calls.append(event.item)

281 elif event.type == "response.completed":

282 print(f"\nUsage: {event.response.usage}", file=sys.stderr)

283 break

284 elif event.type in {

285 "error",

286 "response.failed",

287 "response.incomplete",

288 }:

289 raise RuntimeError(event)

290 

291 history.extend(output_items)

292 

293 for call in pending_calls:

294 history.append(

295 {

296 "type": "function_call_output",

297 "call_id": call.call_id,

298 "output": process_tool_call(call.name, call.arguments),

299 }

300 )

301 

302 if not pending_calls:

303 break

304```

305 

306```typescript189```typescript

307import OpenAI from "openai";190import OpenAI from "openai";

308import type {191import type {


420}303}

421```304```

422 305 

423 

424If one or more agents call developer-defined functions, execute every pending call and create a continuation request containing their outputs.

425 

426### WebSocket

427 

428In WebSocket mode, when an agent calls a developer-defined function, execute the function in your application and send its result to the active response with a `response.inject` event. The waiting agent can then resume without waiting for the entire Multi-agent response to complete.

429 

430```json

431{

432 "type": "response.inject",

433 "response_id": "resp_123",

434 "input": [

435 {

436 "type": "function_call_output",

437 "call_id": "call_123",

438 "output": "{\"temperature\":72}"

439 }

440 ]

441}

442```

443 

444For a valid `response.inject` request, the server replies with one of two events:

445 

446- `response.inject.created`: the input was validated and accepted for injection

447- `response.inject.failed`: the input was not injected; inspect `error.code`

448 

449```json

450{

451 "type": "response.inject.created",

452 "sequence_number": 42,

453 "response_id": "resp_123"

454}

455```

456 

457```json

458{

459 "type": "response.inject.failed",

460 "sequence_number": 43,

461 "response_id": "resp_123",

462 "input": [

463 {

464 "type": "function_call_output",

465 "call_id": "call_123",

466 "output": "{\"temperature\":72}"

467 }

468 ],

469 "error": {

470 "code": "response_already_completed",

471 "message": "Response 'resp_123' has already completed."

472 }

473}

474```

475 

476If a request doesn't conform to the `response.inject` schema, the server sends a generic error with status `400` and closes the WebSocket connection. Fix the request and open a new WebSocket connection before sending another event.

477 

478The Python beta SDK exposes WebSocket mode through `client.beta.responses.connect`. The TypeScript beta SDK exposes it through `ResponsesWS`. Pass `OpenAI-Beta: responses_multi_agent=v1` in the connection headers; unlike HTTP streaming, the WebSocket connectors do not yet accept the `betas` argument.

479 

480Save the response ID from the `response.created` event and include it in every `response.inject` event you send for that response. After sending an injection item, continue reading from the WebSocket until the response has completed and every injection has produced either a `response.inject.created` or `response.inject.failed` event.

481 

482Inject tool outputs over WebSocket

483 

484```python306```python

485from __future__ import annotations307from __future__ import annotations

486 308 

487import json309import json

310import sys

488 311 

489from openai import OpenAI312from openai import OpenAI

313from openai.types.beta import BetaResponseOutputItem

490 314 

491client = OpenAI()315client = OpenAI()

316ROOT = "/root"

492PROPOSALS = {317PROPOSALS = {

493 "alpha": {"estimated_weeks": 6, "risk": "medium"},318 "alpha": {"estimated_weeks": 6, "risk": "medium"},

494 "beta": {"estimated_weeks": 8, "risk": "low"},319 "beta": {"estimated_weeks": 8, "risk": "low"},


512 "strict": True,337 "strict": True,

513 }338 }

514]339]

340history = [

341 {

342 "role": "user",

343 "content": "Compare proposal alpha and proposal beta.",

344 }

345]

515 346 

516 347 

517def process_tool_call(name: str, arguments: str) -> str:348def agent_name(item: BetaResponseOutputItem) -> str:

518 if name != "get_proposal":349 return item.agent.agent_name if item.agent else ROOT

519 raise ValueError(f"Unknown tool: {name}")

520 parsed_arguments = json.loads(arguments)

521 return json.dumps(PROPOSALS[parsed_arguments["proposal"]])

522 

523 

524def run_multi_agent(connection):

525 previous_response_id: str | None = None

526 pending_input: list[dict[str, object]] = [{"role": "user", "content": input()}]

527 350 

528 while pending_input:

529 request = {

530 "type": "response.create",

531 "model": "gpt-5.6-sol",

532 "store": True,

533 "multi_agent": {"enabled": True},

534 "tools": tools,

535 "input": pending_input,

536 }

537 if previous_response_id is not None:

538 request["previous_response_id"] = previous_response_id

539 351 

540 connection.send(request)352def render_to_user(delta: str) -> None:

353 print(delta, end="", flush=True)

541 354 

542 next_input: list[dict[str, object]] = []

543 completed_response = None

544 response_id: str | None = None

545 pending_injections = 0

546 355 

547 for event in connection:356def log_subagent_text(agent: str, delta: str) -> None:

548 event_type = event.type357 print(f"[{agent}] {delta}", end="", file=sys.stderr, flush=True)

549 358 

550 if event_type == "response.created":

551 response_id = event.response.id

552 359 

553 elif event_type == "response.output_item.done":360def process_tool_call(name: str, arguments: str) -> str:

554 item = event.item361 if name != "get_proposal":

362 raise ValueError(f"Unknown tool: {name}")

363 parsed_arguments = json.loads(arguments)

364 return json.dumps(PROPOSALS[parsed_arguments["proposal"]])

555 365 

556 if item.type == "function_call":366 

557 if response_id is None:367while True:

558 raise RuntimeError(368 output_items = []

559 "Received a function call before response.created"369 pending_calls = []

370 item_agents: dict[int, str] = {}

371 

372 stream = client.beta.responses.create(

373 model="gpt-5.6-sol",

374 input=history,

375 tools=tools,

376 store=False,

377 multi_agent={

378 "enabled": True,

379 "max_concurrent_subagents": 3,

380 },

381 stream=True,

382 betas=["responses_multi_agent=v1"],

560 )383 )

384 for event in stream:

385 if event.type == "response.output_item.added":

386 item_agents[event.output_index] = agent_name(event.item)

387 elif event.type == "response.output_text.delta":

388 agent = item_agents.get(event.output_index, ROOT)

389 if agent == ROOT:

390 render_to_user(event.delta)

391 else:

392 log_subagent_text(agent, event.delta)

393 elif event.type == "response.output_item.done":

394 output_items.append(event.item)

395 if event.item.type == "function_call":

396 # Handle function calls from both the root agent and subagents.

397 pending_calls.append(event.item)

398 elif event.type == "response.completed":

399 print(f"\nUsage: {event.response.usage}", file=sys.stderr)

400 break

401 elif event.type in {

402 "error",

403 "response.failed",

404 "response.incomplete",

405 }:

406 raise RuntimeError(event)

561 407 

562 output = {408 history.extend(output_items)

563 "type": "function_call_output",

564 "call_id": item.call_id,

565 "output": process_tool_call(item.name, item.arguments),

566 }

567 pending_injections += 1

568 409 

569 connection.send(410 for call in pending_calls:

411 history.append(

570 {412 {

571 "type": "response.inject",413 "type": "function_call_output",

572 "response_id": response_id,414 "call_id": call.call_id,

573 "input": [output],415 "output": process_tool_call(call.name, call.arguments),

574 }416 }

575 )417 )

576 418 

577 elif event_type == "response.inject.created":419 if not pending_calls:

578 pending_injections -= 1420 break

421```

579 422 

580 elif event_type == "response.inject.failed":

581 pending_injections -= 1

582 423 

583 if event.error.code != "response_already_completed":424If one or more agents call developer-defined functions, execute every pending call and create a continuation request containing their outputs.

584 raise RuntimeError(event.error)

585 425 

586 next_input.extend(item.model_dump(mode="json") for item in event.input)426### WebSocket

587 427 

588 elif event_type == "response.completed":428In WebSocket mode, when an agent calls a developer-defined function, execute the function in your application and send its result to the active response with a `response.inject` event. The waiting agent can then resume without waiting for the entire Multi-agent response to complete.

589 completed_response = event.response

590 429 

591 elif event_type in {430```json

592 "error",431{

593 "response.failed",432 "type": "response.inject",

594 "response.incomplete",433 "response_id": "resp_123",

595 }:434 "input": [

596 raise RuntimeError(event)435 {

436 "type": "function_call_output",

437 "call_id": "call_123",

438 "output": "{\"temperature\":72}"

439 }

440 ]

441}

442```

597 443 

598 if completed_response is not None and pending_injections == 0:444For a valid `response.inject` request, the server replies with one of two events:

599 break

600 445 

601 if completed_response is None:446- `response.inject.created`: the input was validated and accepted for injection

602 raise RuntimeError("Connection ended before response.completed")447- `response.inject.failed`: the input was not injected; inspect `error.code`

603 448 

604 if not next_input:449```json

605 return completed_response450{

451 "type": "response.inject.created",

452 "sequence_number": 42,

453 "response_id": "resp_123"

454}

455```

606 456 

607 previous_response_id = completed_response.id457```json

608 pending_input = next_input458{

459 "type": "response.inject.failed",

460 "sequence_number": 43,

461 "response_id": "resp_123",

462 "input": [

463 {

464 "type": "function_call_output",

465 "call_id": "call_123",

466 "output": "{\"temperature\":72}"

467 }

468 ],

469 "error": {

470 "code": "response_already_completed",

471 "message": "Response 'resp_123' has already completed."

472 }

473}

474```

609 475 

476If a request doesn't conform to the `response.inject` schema, the server sends a generic error with status `400` and closes the WebSocket connection. Fix the request and open a new WebSocket connection before sending another event.

610 477 

611with client.beta.responses.connect(478The Python beta SDK exposes WebSocket mode through `client.beta.responses.connect`. The TypeScript beta SDK exposes it through `ResponsesWS`. Pass `OpenAI-Beta: responses_multi_agent=v1` in the connection headers; unlike HTTP streaming, the WebSocket connectors do not yet accept the `betas` argument.

612 extra_headers={"OpenAI-Beta": "responses_multi_agent=v1"},479 

613) as connection:480Save the response ID from the `response.created` event and include it in every `response.inject` event you send for that response. After sending an injection item, continue reading from the WebSocket until the response has completed and every injection has produced either a `response.inject.created` or `response.inject.failed` event.

614 run_multi_agent(connection)481 

615```482Inject tool outputs over WebSocket

616 483 

617```typescript484```typescript

618import OpenAI from "openai";485import OpenAI from "openai";


748}615}

749```616```

750 617 

618```python

619from __future__ import annotations

620 

621import json

622 

623from openai import OpenAI

624 

625client = OpenAI()

626PROPOSALS = {

627 "alpha": {"estimated_weeks": 6, "risk": "medium"},

628 "beta": {"estimated_weeks": 8, "risk": "low"},

629}

630tools = [

631 {

632 "type": "function",

633 "name": "get_proposal",

634 "description": "Return details for a proposal that the agents should compare.",

635 "parameters": {

636 "type": "object",

637 "properties": {

638 "proposal": {

639 "type": "string",

640 "enum": ["alpha", "beta"],

641 }

642 },

643 "required": ["proposal"],

644 "additionalProperties": False,

645 },

646 "strict": True,

647 }

648]

649 

650 

651def process_tool_call(name: str, arguments: str) -> str:

652 if name != "get_proposal":

653 raise ValueError(f"Unknown tool: {name}")

654 parsed_arguments = json.loads(arguments)

655 return json.dumps(PROPOSALS[parsed_arguments["proposal"]])

656 

657 

658def run_multi_agent(connection):

659 previous_response_id: str | None = None

660 pending_input: list[dict[str, object]] = [{"role": "user", "content": input()}]

661 

662 while pending_input:

663 request = {

664 "type": "response.create",

665 "model": "gpt-5.6-sol",

666 "store": True,

667 "multi_agent": {"enabled": True},

668 "tools": tools,

669 "input": pending_input,

670 }

671 if previous_response_id is not None:

672 request["previous_response_id"] = previous_response_id

673 

674 connection.send(request)

675 

676 next_input: list[dict[str, object]] = []

677 completed_response = None

678 response_id: str | None = None

679 pending_injections = 0

680 

681 for event in connection:

682 event_type = event.type

683 

684 if event_type == "response.created":

685 response_id = event.response.id

686 

687 elif event_type == "response.output_item.done":

688 item = event.item

689 

690 if item.type == "function_call":

691 if response_id is None:

692 raise RuntimeError(

693 "Received a function call before response.created"

694 )

695 

696 output = {

697 "type": "function_call_output",

698 "call_id": item.call_id,

699 "output": process_tool_call(item.name, item.arguments),

700 }

701 pending_injections += 1

702 

703 connection.send(

704 {

705 "type": "response.inject",

706 "response_id": response_id,

707 "input": [output],

708 }

709 )

710 

711 elif event_type == "response.inject.created":

712 pending_injections -= 1

713 

714 elif event_type == "response.inject.failed":

715 pending_injections -= 1

716 

717 if event.error.code != "response_already_completed":

718 raise RuntimeError(event.error)

719 

720 next_input.extend(item.model_dump(mode="json") for item in event.input)

721 

722 elif event_type == "response.completed":

723 completed_response = event.response

724 

725 elif event_type in {

726 "error",

727 "response.failed",

728 "response.incomplete",

729 }:

730 raise RuntimeError(event)

731 

732 if completed_response is not None and pending_injections == 0:

733 break

734 

735 if completed_response is None:

736 raise RuntimeError("Connection ended before response.completed")

737 

738 if not next_input:

739 return completed_response

740 

741 previous_response_id = completed_response.id

742 pending_input = next_input

743 

744 

745with client.beta.responses.connect(

746 extra_headers={"OpenAI-Beta": "responses_multi_agent=v1"},

747) as connection:

748 run_multi_agent(connection)

749```

750 

751 751 

752After sending a `response.inject` event, keep reading from the WebSocket and handle the acknowledgement:752After sending a `response.inject` event, keep reading from the WebSocket and handle the acknowledgement:

753 753 

guides/retrieval.md +193 −193

Details

16 16 

17Create vector store with files17Create vector store with files

18 18 

19```python

20from openai import OpenAI

21 

22client = OpenAI()

23 

24vector_store = client.vector_stores.create( # Create vector store

25 name="Support FAQ",

26)

27 

28client.vector_stores.files.upload_and_poll( # Upload file

29 vector_store_id=vector_store.id,

30 file=open("customer_policies.txt", "rb")

31)

32```

33 

34```javascript19```javascript

35import OpenAI from "openai";20import OpenAI from "openai";

36const client = new OpenAI();21const client = new OpenAI();


47);32);

48```33```

49 34 

35```python

36from openai import OpenAI

37 

38client = OpenAI()

39 

40vector_store = client.vector_stores.create( # Create vector store

41 name="Support FAQ",

42)

43 

44client.vector_stores.files.upload_and_poll( # Upload file

45 vector_store_id=vector_store.id,

46 file=open("customer_policies.txt", "rb")

47)

48```

49 

50 50 

51<li className={s.StandaloneLi} data-number={2}>51<li className={s.StandaloneLi} data-number={2}>

52 **Send search query** to get relevant results.52 **Send search query** to get relevant results.


54 54 

55Search query55Search query

56 56 

57```javascript

58const userQuery = "What is the return policy?";

59 

60const results = await client.vectorStores.search(vector_store.id, {

61 query: userQuery,

62});

63```

64 

57```python65```python

58user_query = "What is the return policy?"66user_query = "What is the return policy?"

59 67 


63)71)

64```72```

65 73 

66```javascript

67const userQuery = "What is the return policy?";

68 

69const results = await client.vectorStores.search(vector_store.id, {

70 query: userQuery,

71});

72```

73 

74 74 

75To learn how to use the results with our models, check out the [synthesizing75To learn how to use the results with our models, check out the [synthesizing

76 responses](#synthesizing-responses) section.76 responses](#synthesizing-responses) section.


99 99 

100Search query100Search query

101 101 

102```javascript

103const results = await client.vectorStores.search(vector_store.id, {

104 query: "How many woodchucks are allowed per passenger?",

105});

106```

107 

102```python108```python

103results = client.vector_stores.search(109results = client.vector_stores.search(

104 vector_store_id=vector_store.id,110 vector_store_id=vector_store.id,


106)112)

107```113```

108 114 

109```javascript

110const results = await client.vectorStores.search(vector_store.id, {

111 query: "How many woodchucks are allowed per passenger?",

112});

113```

114 

115 115 

116Results116Results

117 117 


363 363 

364 Create vector store364 Create vector store

365 365 

366```python

367client.vector_stores.create(

368 name="Support FAQ",

369 file_ids=["file_123"]

370)

371```

372 

373```javascript366```javascript

374await client.vectorStores.create({367await client.vectorStores.create({

375 name: "Support FAQ",368 name: "Support FAQ",


377});370});

378```371```

379 372 

373```python

374client.vector_stores.create(

375 name="Support FAQ",

376 file_ids=["file_123"]

377)

378```

379 

380 380

381 381 

382 382


386 386 

387 Retrieve vector store387 Retrieve vector store

388 388 

389```javascript

390await client.vectorStores.retrieve("vs_123");

391```

392 

389```python393```python

390client.vector_stores.retrieve(394client.vector_stores.retrieve(

391 vector_store_id="vs_123"395 vector_store_id="vs_123"

392)396)

393```397```

394 398 

395```javascript

396await client.vectorStores.retrieve("vs_123");

397```

398 

399 399

400 400 

401 401


405 405 

406 Update vector store406 Update vector store

407 407 

408```javascript

409await client.vectorStores.update("vs_123", {

410 name: "Support FAQ Updated",

411});

412```

413 

408```python414```python

409client.vector_stores.update(415client.vector_stores.update(

410 vector_store_id="vs_123",416 vector_store_id="vs_123",


412)418)

413```419```

414 420 

415```javascript

416await client.vectorStores.update("vs_123", {

417 name: "Support FAQ Updated",

418});

419```

420 

421 421

422 422 

423 423


427 427 

428 Delete vector store428 Delete vector store

429 429 

430```javascript

431await client.vectorStores.delete("vs_123");

432```

433 

430```python434```python

431client.vector_stores.delete(435client.vector_stores.delete(

432 vector_store_id="vs_123"436 vector_store_id="vs_123"

433)437)

434```438```

435 439 

436```javascript

437await client.vectorStores.delete("vs_123");

438```

439 

440 440

441 441 

442 442


446 446 

447 List vector stores447 List vector stores

448 448 

449```python

450client.vector_stores.list()

451```

452 

453```javascript449```javascript

454await client.vectorStores.list();450await client.vectorStores.list();

455```451```

456 452 

453```python

454client.vector_stores.list()

455```

456 

457 457 

458 458 

459### Vector store file operations459### Vector store file operations


468 468 

469 Create vector store file469 Create vector store file

470 470 

471```javascript

472await client.vectorStores.files.createAndPoll("vs_123", {

473 file_id: "file_123",

474});

475```

476 

471```python477```python

472client.vector_stores.files.create_and_poll(478client.vector_stores.files.create_and_poll(

473 vector_store_id="vs_123",479 vector_store_id="vs_123",


475)481)

476```482```

477 483 

478```javascript

479await client.vectorStores.files.createAndPoll("vs_123", {

480 file_id: "file_123",

481});

482```

483 

484 484

485 485 

486 486


490 490 

491 Upload vector store file491 Upload vector store file

492 492 

493```python

494client.vector_stores.files.upload_and_poll(

495 vector_store_id="vs_123",

496 file=open("customer_policies.txt", "rb")

497)

498```

499 

500```javascript493```javascript

501await client.vectorStores.files.uploadAndPoll(494await client.vectorStores.files.uploadAndPoll(

502 "vs_123",495 "vs_123",


504);497);

505```498```

506 499 

500```python

501client.vector_stores.files.upload_and_poll(

502 vector_store_id="vs_123",

503 file=open("customer_policies.txt", "rb")

504)

505```

506 

507 507

508 508 

509 509


513 513 

514 Retrieve vector store file514 Retrieve vector store file

515 515 

516```javascript

517await client.vectorStores.files.retrieve("file_123", {

518 vector_store_id: "vs_123",

519});

520```

521 

516```python522```python

517client.vector_stores.files.retrieve(523client.vector_stores.files.retrieve(

518 vector_store_id="vs_123",524 vector_store_id="vs_123",


520)526)

521```527```

522 528 

523```javascript

524await client.vectorStores.files.retrieve("file_123", {

525 vector_store_id: "vs_123",

526});

527```

528 

529 529

530 530 

531 531


535 535 

536 Update vector store file536 Update vector store file

537 537 

538```javascript

539await client.vectorStores.files.update("file_123", {

540 vector_store_id: "vs_123",

541 attributes: { key: "value" },

542});

543```

544 

538```python545```python

539client.vector_stores.files.update(546client.vector_stores.files.update(

540 vector_store_id="vs_123",547 vector_store_id="vs_123",


543)550)

544```551```

545 552 

546```javascript

547await client.vectorStores.files.update("file_123", {

548 vector_store_id: "vs_123",

549 attributes: { key: "value" },

550});

551```

552 

553 553

554 554 

555 555


559 559 

560 Delete vector store file560 Delete vector store file

561 561 

562```javascript

563await client.vectorStores.files.delete("file_123", {

564 vector_store_id: "vs_123",

565});

566```

567 

562```python568```python

563client.vector_stores.files.delete(569client.vector_stores.files.delete(

564 vector_store_id="vs_123",570 vector_store_id="vs_123",


566)572)

567```573```

568 574 

569```javascript

570await client.vectorStores.files.delete("file_123", {

571 vector_store_id: "vs_123",

572});

573```

574 

575 575

576 576 

577 577


581 581 

582 List vector store files582 List vector store files

583 583 

584```javascript

585await client.vectorStores.files.list("vs_123");

586```

587 

584```python588```python

585client.vector_stores.files.list(589client.vector_stores.files.list(

586 vector_store_id="vs_123"590 vector_store_id="vs_123"

587)591)

588```592```

589 593 

590```javascript

591await client.vectorStores.files.list("vs_123");

592```

593 

594 594 

595 595 

596### Batch operations596### Batch operations


601 601 

602 Batch create operation602 Batch create operation

603 603 

604```python

605client.vector_stores.file_batches.create_and_poll(

606 vector_store_id="vs_123",

607 files=[

608 {

609 "file_id": "file_123",

610 "attributes": {"department": "finance"}

611 },

612 {

613 "file_id": "file_456",

614 "chunking_strategy": {

615 "type": "static",

616 "max_chunk_size_tokens": 1200,

617 "chunk_overlap_tokens": 200

618 }

619 }

620 ]

621)

622```

623 

624```javascript604```javascript

625await client.vectorStores.fileBatches.createAndPoll("vs_123", {605await client.vectorStores.fileBatches.createAndPoll("vs_123", {

626 files: [606 files: [


642});622});

643```623```

644 624 

625```python

626client.vector_stores.file_batches.create_and_poll(

627 vector_store_id="vs_123",

628 files=[

629 {

630 "file_id": "file_123",

631 "attributes": {"department": "finance"}

632 },

633 {

634 "file_id": "file_456",

635 "chunking_strategy": {

636 "type": "static",

637 "max_chunk_size_tokens": 1200,

638 "chunk_overlap_tokens": 200

639 }

640 }

641 ]

642)

643```

644 

645 645

646 646 

647 647


651 651 

652 Batch retrieve operation652 Batch retrieve operation

653 653 

654```javascript

655await client.vectorStores.fileBatches.retrieve("vsfb_123", {

656 vector_store_id: "vs_123",

657});

658```

659 

654```python660```python

655client.vector_stores.file_batches.retrieve(661client.vector_stores.file_batches.retrieve(

656 vector_store_id="vs_123",662 vector_store_id="vs_123",


658)664)

659```665```

660 666 

661```javascript

662await client.vectorStores.fileBatches.retrieve("vsfb_123", {

663 vector_store_id: "vs_123",

664});

665```

666 

667 667

668 668 

669 669


673 673 

674 Batch cancel operation674 Batch cancel operation

675 675 

676```javascript

677await client.vectorStores.fileBatches.cancel("vsfb_123", {

678 vector_store_id: "vs_123",

679});

680```

681 

676```python682```python

677client.vector_stores.file_batches.cancel(683client.vector_stores.file_batches.cancel(

678 vector_store_id="vs_123",684 vector_store_id="vs_123",


680)686)

681```687```

682 688 

683```javascript

684await client.vectorStores.fileBatches.cancel("vsfb_123", {

685 vector_store_id: "vs_123",

686});

687```

688 

689 689

690 690 

691 691


695 695 

696 List files in a batch696 List files in a batch

697 697 

698```javascript

699await client.vectorStores.fileBatches.listFiles("vsfb_123", {

700 vector_store_id: "vs_123",

701});

702```

703 

698```python704```python

699client.vector_stores.file_batches.list_files(705client.vector_stores.file_batches.list_files(

700 "vsfb_123",706 "vsfb_123",


702)708)

703```709```

704 710 

705```javascript

706await client.vectorStores.fileBatches.listFiles("vsfb_123", {

707 vector_store_id: "vs_123",

708});

709```

710 

711 711 

712 712 

713When creating a batch you can either provide `file_ids` with optional `attributes` and/or `chunking_strategy`, or use the `files` array to pass objects that include a `file_id` plus optional `attributes` and `chunking_strategy` for each file. The two options are mutually exclusive so that you can cleanly control whether every file shares the same settings or you need per-file overrides.713When creating a batch you can either provide `file_ids` with optional `attributes` and/or `chunking_strategy`, or use the `files` array to pass objects that include a `file_id` plus optional `attributes` and `chunking_strategy` for each file. The two options are mutually exclusive so that you can cleanly control whether every file shares the same settings or you need per-file overrides.


720 720 

721Create vector store file with attributes721Create vector store file with attributes

722 722 

723```javascript

724await client.vectorStores.files.create("<vector_store_id>", {

725 file_id: "file_123",

726 attributes: {

727 region: "US",

728 category: "Marketing",

729 date: 1672531200, // Jan 1, 2023

730 },

731});

732```

733 

723```python734```python

724client.vector_stores.files.create(735client.vector_stores.files.create(

725 vector_store_id="<vector_store_id>",736 vector_store_id="<vector_store_id>",


732)743)

733```744```

734 745 

735```javascript

736await client.vectorStores.files.create("<vector_store_id>", {

737 file_id: "file_123",

738 attributes: {

739 region: "US",

740 category: "Marketing",

741 date: 1672531200, // Jan 1, 2023

742 },

743});

744```

745 

746 746 

747### Expiration policies747### Expiration policies

748 748 


750 750 

751Set expiration policy for vector store751Set expiration policy for vector store

752 752 

753```javascript

754await client.vectorStores.update("vs_123", {

755 expires_after: {

756 anchor: "last_active_at",

757 days: 7,

758 },

759});

760```

761 

753```python762```python

754client.vector_stores.update(763client.vector_stores.update(

755 vector_store_id="vs_123",764 vector_store_id="vs_123",


760)769)

761```770```

762 771 

763```javascript

764await client.vectorStores.update("vs_123", {

765 expires_after: {

766 anchor: "last_active_at",

767 days: 7,

768 },

769});

770```

771 

772 772 

773### Limits773### Limits

774 774 


820 820 

821Perform search query to get results821Perform search query to get results

822 822 

823```python

824from openai import OpenAI

825 

826client = OpenAI()

827 

828user_query = "What is the return policy?"

829 

830results = client.vector_stores.search(

831 vector_store_id=vector_store.id,

832 query=user_query,

833)

834```

835 

836```javascript823```javascript

837import OpenAI from "openai";824import OpenAI from "openai";

838 825 


845});832});

846```833```

847 834 

848 

849Synthesize a response based on results

850 

851```python835```python

852formatted_results = format_results(results.data)836from openai import OpenAI

853 837 

854"\n".join("\n".join(c.text for c in result.content) for result in results.data)838client = OpenAI()

855 839 

856completion = client.chat.completions.create(840user_query = "What is the return policy?"

857 model="gpt-5.6",

858 messages=[

859 {

860 "role": "developer",

861 "content": "Produce a concise answer to the query based on the provided sources.",

862 },

863 {

864 "role": "user",

865 "content": f"Sources: {formatted_results}\n\nQuery: '{user_query}'",

866 },

867 ],

868)

869 841 

870print(completion.choices[0].message.content)842results = client.vector_stores.search(

843 vector_store_id=vector_store.id,

844 query=user_query,

845)

871```846```

872 847 

848 

849Synthesize a response based on results

850 

873```javascript851```javascript

874const formattedResults = formatResults(results.data);852const formattedResults = formatResults(results.data);

875// Join the text content of all results853// Join the text content of all results


895console.log(completion.choices[0].message.content);873console.log(completion.choices[0].message.content);

896```874```

897 875 

876```python

877formatted_results = format_results(results.data)

878 

879"\n".join("\n".join(c.text for c in result.content) for result in results.data)

880 

881completion = client.chat.completions.create(

882 model="gpt-5.6",

883 messages=[

884 {

885 "role": "developer",

886 "content": "Produce a concise answer to the query based on the provided sources.",

887 },

888 {

889 "role": "user",

890 "content": f"Sources: {formatted_results}\n\nQuery: '{user_query}'",

891 },

892 ],

893)

894 

895print(completion.choices[0].message.content)

896```

897 

898 898 

899```json899```json

900"Our return policy allows returns within 30 days of purchase."900"Our return policy allows returns within 30 days of purchase."


905 905 

906Sample result formatting function906Sample result formatting function

907 907 

908```python

909def format_results(results):

910 formatted_results = ""

911 for result in results.data:

912 formatted_result = (

913 f"<result file_id='{result.file_id}' file_name='{result.file_name}'>"

914 )

915 for part in result.content:

916 formatted_result += f"<content>{part.text}</content>"

917 formatted_results += formatted_result + "</result>"

918 return f"<sources>{formatted_results}</sources>"

919```

920 

921```javascript908```javascript

922function formatResults(results) {909function formatResults(results) {

923 let formattedResults = "";910 let formattedResults = "";


931 return `<sources>${formattedResults}</sources>`;918 return `<sources>${formattedResults}</sources>`;

932}919}

933```920```

921 

922```python

923def format_results(results):

924 formatted_results = ""

925 for result in results.data:

926 formatted_result = (

927 f"<result file_id='{result.file_id}' file_name='{result.file_name}'>"

928 )

929 for part in result.content:

930 formatted_result += f"<content>{part.text}</content>"

931 formatted_results += formatted_result + "</result>"

932 return f"<sources>{formatted_results}</sources>"

933```

Details

124 The remainder of this guide will focus on non-function calling use cases in124 The remainder of this guide will focus on non-function calling use cases in

125 the Responses API. To learn more about how to use Structured Outputs with125 the Responses API. To learn more about how to use Structured Outputs with

126 function calling, check out the 126 function calling, check out the

127 [Function Calling](https://developers.openai.com/api/docs/guides/function-calling#function-calling-with-structured-outputs) 127 [Function Calling](https://developers.openai.com/api/docs/guides/function-calling#strict-mode)

128 guide.128 guide.

129 129 

130 130 


913 913 

914 914 

915 915 

916```python

917response = client.responses.create(

918 model="gpt-5.6",

919 input=[

920 {

921 "role": "system",

922 "content": "You are a helpful math tutor. Guide the user through the solution step by step.",

923 },

924 {"role": "user", "content": "how can I solve 8x + 7 = -23"},

925 ],

926 text={

927 "format": {

928 "type": "json_schema",

929 "name": "math_response",

930 "schema": {

931 "type": "object",

932 "properties": {

933 "steps": {

934 "type": "array",

935 "items": {

936 "type": "object",

937 "properties": {

938 "explanation": {"type": "string"},

939 "output": {"type": "string"},

940 },

941 "required": ["explanation", "output"],

942 "additionalProperties": False,

943 },

944 },

945 "final_answer": {"type": "string"},

946 },

947 "required": ["steps", "final_answer"],

948 "additionalProperties": False,

949 },

950 "strict": True,

951 },

952 },

953)

954 

955print(response.output_text)

956```

957 

958```javascript916```javascript

959const response = await openai.responses.create({917const response = await openai.responses.create({

960 model: "gpt-5.6",918 model: "gpt-5.6",


998console.log(response.output_text);956console.log(response.output_text);

999```957```

1000 958 

959```python

960response = client.responses.create(

961 model="gpt-5.6",

962 input=[

963 {

964 "role": "system",

965 "content": "You are a helpful math tutor. Guide the user through the solution step by step.",

966 },

967 {"role": "user", "content": "how can I solve 8x + 7 = -23"},

968 ],

969 text={

970 "format": {

971 "type": "json_schema",

972 "name": "math_response",

973 "schema": {

974 "type": "object",

975 "properties": {

976 "steps": {

977 "type": "array",

978 "items": {

979 "type": "object",

980 "properties": {

981 "explanation": {"type": "string"},

982 "output": {"type": "string"},

983 },

984 "required": ["explanation", "output"],

985 "additionalProperties": False,

986 },

987 },

988 "final_answer": {"type": "string"},

989 },

990 "required": ["steps", "final_answer"],

991 "additionalProperties": False,

992 },

993 "strict": True,

994 },

995 },

996)

997 

998print(response.output_text)

999```

1000 

1001```bash1001```bash

1002curl https://api.openai.com/v1/responses \1002curl https://api.openai.com/v1/responses \

1003 -H "Authorization: Bearer $OPENAI_API_KEY" \1003 -H "Authorization: Bearer $OPENAI_API_KEY" \


1218 1218 

1219 1219 

1220 1220 

1221```python

1222class Step(BaseModel):

1223 explanation: str

1224 output: str

1225 

1226 

1227class MathReasoning(BaseModel):

1228 steps: list[Step]

1229 final_answer: str

1230 

1231 

1232response = client.responses.parse(

1233 model="gpt-5.6",

1234 input=[

1235 {

1236 "role": "system",

1237 "content": "You are a helpful math tutor. Guide the user through the solution step by step.",

1238 },

1239 {"role": "user", "content": "how can I solve 8x + 7 = -23"},

1240 ],

1241 text_format=MathReasoning,

1242)

1243 

1244for output in response.output:

1245 if output.type != "message":

1246 continue

1247 

1248 for item in output.content:

1249 if item.type == "refusal":

1250 # If the model refuses to respond, you will get a refusal message

1251 print(item.refusal)

1252 continue

1253 

1254 if not item.parsed:

1255 raise Exception("Could not parse response")

1256 

1257 print(item.parsed)

1258```

1259 

1260```javascript1221```javascript

1261const Step = z.object({1222const Step = z.object({

1262 explanation: z.string(),1223 explanation: z.string(),


1304}1265}

1305```1266```

1306 1267 

1268```python

1269class Step(BaseModel):

1270 explanation: str

1271 output: str

1272 

1273 

1274class MathReasoning(BaseModel):

1275 steps: list[Step]

1276 final_answer: str

1277 

1278 

1279response = client.responses.parse(

1280 model="gpt-5.6",

1281 input=[

1282 {

1283 "role": "system",

1284 "content": "You are a helpful math tutor. Guide the user through the solution step by step.",

1285 },

1286 {"role": "user", "content": "how can I solve 8x + 7 = -23"},

1287 ],

1288 text_format=MathReasoning,

1289)

1290 

1291for output in response.output:

1292 if output.type != "message":

1293 continue

1294 

1295 for item in output.content:

1296 if item.type == "refusal":

1297 # If the model refuses to respond, you will get a refusal message

1298 print(item.refusal)

1299 continue

1300 

1301 if not item.parsed:

1302 raise Exception("Could not parse response")

1303 

1304 print(item.parsed)

1305```

1306 

1307 1307 

1308 1308 

1309The API response from a refusal will look something like this:1309The API response from a refusal will look something like this:


1387 1387 

1388 1388 

1389 1389 

1390```python

1391from typing import List

1392 

1393from openai import OpenAI

1394from pydantic import BaseModel

1395 

1396 

1397class EntitiesModel(BaseModel):

1398 attributes: List[str]

1399 colors: List[str]

1400 animals: List[str]

1401 

1402 

1403client = OpenAI()

1404 

1405with client.responses.stream(

1406 model="gpt-5.6",

1407 input=[

1408 {"role": "system", "content": "Extract entities from the input text"},

1409 {

1410 "role": "user",

1411 "content": "The quick brown fox jumps over the lazy dog with piercing blue eyes",

1412 },

1413 ],

1414 text_format=EntitiesModel,

1415) as stream:

1416 for event in stream:

1417 if event.type == "response.refusal.delta":

1418 print(event.delta, end="")

1419 elif event.type == "response.output_text.delta":

1420 print(event.delta, end="")

1421 elif event.type == "response.error":

1422 print(event.error, end="")

1423 elif event.type == "response.completed":

1424 print("Completed") # print(event.response.output)

1425 

1426 final_response = stream.get_final_response()

1427 print(final_response)

1428```

1429 

1430```javascript1390```javascript

1431import { OpenAI } from "openai";1391import { OpenAI } from "openai";

1432import { zodTextFormat } from "openai/helpers/zod";1392import { zodTextFormat } from "openai/helpers/zod";


1467console.log(result);1427console.log(result);

1468```1428```

1469 1429 

1430```python

1431from typing import List

1432 

1433from openai import OpenAI

1434from pydantic import BaseModel

1435 

1436 

1437class EntitiesModel(BaseModel):

1438 attributes: List[str]

1439 colors: List[str]

1440 animals: List[str]

1441 

1442 

1443client = OpenAI()

1444 

1445with client.responses.stream(

1446 model="gpt-5.6",

1447 input=[

1448 {"role": "system", "content": "Extract entities from the input text"},

1449 {

1450 "role": "user",

1451 "content": "The quick brown fox jumps over the lazy dog with piercing blue eyes",

1452 },

1453 ],

1454 text_format=EntitiesModel,

1455) as stream:

1456 for event in stream:

1457 if event.type == "response.refusal.delta":

1458 print(event.delta, end="")

1459 elif event.type == "response.output_text.delta":

1460 print(event.delta, end="")

1461 elif event.type == "response.error":

1462 print(event.error, end="")

1463 elif event.type == "response.completed":

1464 print("Completed") # print(event.response.output)

1465 

1466 final_response = stream.get_final_response()

1467 print(final_response)

1468```

1469 

1470 1470 

1471 1471 

1472## Supported schemas1472## Supported schemas

guides/text.md +73 −73

Details

33print(response.output_text)33print(response.output_text)

34```34```

35 35 

36```bash36```go

37openai responses create \37package main

38 --model "gpt-5.6" \

39 --input "Write a one-sentence bedtime story about a unicorn." \

40 --raw-output \

41 --transform 'output.#(type=="message").content.0.text'

42```

43 38 

44```csharp39import (

45using OpenAI.Responses;40 "context"

46#pragma warning disable OPENAI00141 "fmt"

47 42 

48string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;43 "github.com/openai/openai-go/v3"

49ResponsesClient client = new(key);44 "github.com/openai/openai-go/v3/option"

45 "github.com/openai/openai-go/v3/responses"

46)

50 47 

51ResponseResult response = await client.CreateResponseAsync(48func main() {

52 "gpt-5.6",49 client := openai.NewClient(

53 "Say 'this is a test.'"50 option.WithAPIKey("My API Key"), // or set OPENAI_API_KEY in your env

54);51 )

55 52 

56Console.WriteLine($"[ASSISTANT]: {response.GetOutputText()}");53 resp, err := client.Responses.New(context.TODO(), responses.ResponseNewParams{

54 Model: "gpt-5.6",

55 Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Say this is a test")},

56 })

57 if err != nil {

58 panic(err.Error())

59 }

60 

61 fmt.Println(resp.OutputText())

62}

57```63```

58 64 

59```java65```java


79}85}

80```86```

81 87 

82```go88```csharp

83package main89using OpenAI.Responses;

84 90#pragma warning disable OPENAI001

85import (

86 "context"

87 "fmt"

88 

89 "github.com/openai/openai-go/v3"

90 "github.com/openai/openai-go/v3/option"

91 "github.com/openai/openai-go/v3/responses"

92)

93 91 

94func main() {92string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;

95 client := openai.NewClient(93ResponsesClient client = new(key);

96 option.WithAPIKey("My API Key"), // or set OPENAI_API_KEY in your env

97 )

98 94 

99 resp, err := client.Responses.New(context.TODO(), responses.ResponseNewParams{95ResponseResult response = await client.CreateResponseAsync(

100 Model: "gpt-5.6",96 "gpt-5.6",

101 Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Say this is a test")},97 "Say 'this is a test.'"

102 })98);

103 if err != nil {

104 panic(err.Error())

105 }

106 99 

107 fmt.Println(resp.OutputText())100Console.WriteLine($"[ASSISTANT]: {response.GetOutputText()}");

108}

109```101```

110 102 

111```ruby103```ruby


121puts(response.output_text)113puts(response.output_text)

122```114```

123 115 

116```bash

117openai responses create \

118 --model "gpt-5.6" \

119 --input "Write a one-sentence bedtime story about a unicorn." \

120 --raw-output \

121 --transform 'output.#(type=="message").content.0.text'

122```

123 

124```bash124```bash

125curl "https://api.openai.com/v1/responses" \125curl "https://api.openai.com/v1/responses" \

126 -H "Content-Type: application/json" \126 -H "Content-Type: application/json" \


198console.log(response.output_text);198console.log(response.output_text);

199```199```

200 200 

201```python

202from openai import OpenAI

203 

204client = OpenAI()

205 

206response = client.responses.create(

207 model="gpt-5.6",

208 reasoning={"effort": "low"},

209 instructions="Talk like a pirate.",

210 input="Are semicolons optional in JavaScript?",

211)

212 

213print(response.output_text)

214```

215 

201```go216```go

202package main217package main

203 218 


230}245}

231```246```

232 247 

233```python

234from openai import OpenAI

235 

236client = OpenAI()

237 

238response = client.responses.create(

239 model="gpt-5.6",

240 reasoning={"effort": "low"},

241 instructions="Talk like a pirate.",

242 input="Are semicolons optional in JavaScript?",

243)

244 

245print(response.output_text)

246```

247 

248```bash248```bash

249curl "https://api.openai.com/v1/responses" \249curl "https://api.openai.com/v1/responses" \

250 -H "Content-Type: application/json" \250 -H "Content-Type: application/json" \


284console.log(response.output_text);284console.log(response.output_text);

285```285```

286 286 

287```python

288from openai import OpenAI

289 

290client = OpenAI()

291 

292response = client.responses.create(

293 model="gpt-5.6",

294 reasoning={"effort": "low"},

295 input=[

296 {"role": "developer", "content": "Talk like a pirate."},

297 {"role": "user", "content": "Are semicolons optional in JavaScript?"},

298 ],

299)

300 

301print(response.output_text)

302```

303 

287```go304```go

288package main305package main

289 306 


324}341}

325```342```

326 343 

327```python

328from openai import OpenAI

329 

330client = OpenAI()

331 

332response = client.responses.create(

333 model="gpt-5.6",

334 reasoning={"effort": "low"},

335 input=[

336 {"role": "developer", "content": "Talk like a pirate."},

337 {"role": "user", "content": "Are semicolons optional in JavaScript?"},

338 ],

339)

340 

341print(response.output_text)

342```

343 

344```bash344```bash

345curl "https://api.openai.com/v1/responses" \345curl "https://api.openai.com/v1/responses" \

346 -H "Content-Type: application/json" \346 -H "Content-Type: application/json" \

Details

27 27 

28Simple text input28Simple text input

29 29 

30```python

31from openai import OpenAI

32 

33client = OpenAI()

34 

35response = client.responses.input_tokens.count(

36 model="gpt-5.6", input="Tell me a joke."

37)

38print(response.input_tokens)

39```

40 

41```javascript30```javascript

42import OpenAI from "openai";31import OpenAI from "openai";

43 32 


51console.log(response.input_tokens);40console.log(response.input_tokens);

52```41```

53 42 

43```python

44from openai import OpenAI

45 

46client = OpenAI()

47 

48response = client.responses.input_tokens.count(

49 model="gpt-5.6", input="Tell me a joke."

50)

51print(response.input_tokens)

52```

53 

54```bash54```bash

55curl https://api.openai.com/v1/responses/input_tokens \55curl https://api.openai.com/v1/responses/input_tokens \

56 -H "Authorization: Bearer $OPENAI_API_KEY" \56 -H "Authorization: Bearer $OPENAI_API_KEY" \


74 74 

75Multi-turn conversation75Multi-turn conversation

76 76 

77```python

78from openai import OpenAI

79 

80client = OpenAI()

81 

82response = client.responses.input_tokens.count(

83 model="gpt-5.6",

84 input=[

85 {"role": "user", "content": "What is 2 + 2?"},

86 {"role": "assistant", "content": "2 + 2 equals 4."},

87 {"role": "user", "content": "What about 3 + 3?"},

88 ],

89)

90print(response.input_tokens)

91```

92 

93```javascript77```javascript

94import OpenAI from "openai";78import OpenAI from "openai";

95 79 


107console.log(response.input_tokens);91console.log(response.input_tokens);

108```92```

109 93 

94```python

95from openai import OpenAI

96 

97client = OpenAI()

98 

99response = client.responses.input_tokens.count(

100 model="gpt-5.6",

101 input=[

102 {"role": "user", "content": "What is 2 + 2?"},

103 {"role": "assistant", "content": "2 + 2 equals 4."},

104 {"role": "user", "content": "What about 3 + 3?"},

105 ],

106)

107print(response.input_tokens)

108```

109 

110```bash110```bash

111curl https://api.openai.com/v1/responses/input_tokens \111curl https://api.openai.com/v1/responses/input_tokens \

112 -H "Authorization: Bearer $OPENAI_API_KEY" \112 -H "Authorization: Bearer $OPENAI_API_KEY" \


141 141 

142Input with system instructions142Input with system instructions

143 143 

144```python

145from openai import OpenAI

146 

147client = OpenAI()

148 

149response = client.responses.input_tokens.count(

150 model="gpt-5.6",

151 instructions="You are a helpful assistant that explains concepts simply.",

152 input="Explain quantum computing in one sentence.",

153)

154print(response.input_tokens)

155```

156 

157```javascript144```javascript

158import OpenAI from "openai";145import OpenAI from "openai";

159 146 


168console.log(response.input_tokens);155console.log(response.input_tokens);

169```156```

170 157 

158```python

159from openai import OpenAI

160 

161client = OpenAI()

162 

163response = client.responses.input_tokens.count(

164 model="gpt-5.6",

165 instructions="You are a helpful assistant that explains concepts simply.",

166 input="Explain quantum computing in one sentence.",

167)

168print(response.input_tokens)

169```

170 

171```bash171```bash

172curl https://api.openai.com/v1/responses/input_tokens \172curl https://api.openai.com/v1/responses/input_tokens \

173 -H "Authorization: Bearer $OPENAI_API_KEY" \173 -H "Authorization: Bearer $OPENAI_API_KEY" \


196 196 

197Input with an image197Input with an image

198 198 

199```python

200from openai import OpenAI

201 

202client = OpenAI()

203 

204# Use file_id from uploaded file, or image_url for a URL

205response = client.responses.input_tokens.count(

206 model="gpt-5.6",

207 input=[

208 {

209 "role": "user",

210 "content": [

211 {

212 "type": "input_image",

213 "image_url": "https://example.com/chart.png",

214 },

215 {"type": "input_text", "text": "Summarize this chart."},

216 ],

217 }

218 ],

219)

220print(response.input_tokens)

221```

222 

223```javascript199```javascript

224import OpenAI from "openai";200import OpenAI from "openai";

225 201 


245console.log(response.input_tokens);221console.log(response.input_tokens);

246```222```

247 223 

224```python

225from openai import OpenAI

226 

227client = OpenAI()

228 

229# Use file_id from uploaded file, or image_url for a URL

230response = client.responses.input_tokens.count(

231 model="gpt-5.6",

232 input=[

233 {

234 "role": "user",

235 "content": [

236 {

237 "type": "input_image",

238 "image_url": "https://example.com/chart.png",

239 },

240 {"type": "input_text", "text": "Summarize this chart."},

241 ],

242 }

243 ],

244)

245print(response.input_tokens)

246```

247 

248```bash248```bash

249curl https://api.openai.com/v1/responses/input_tokens \249curl https://api.openai.com/v1/responses/input_tokens \

250 -H "Authorization: Bearer $OPENAI_API_KEY" \250 -H "Authorization: Bearer $OPENAI_API_KEY" \


285 285 

286Input with function tools286Input with function tools

287 287 

288```python

289from openai import OpenAI

290 

291client = OpenAI()

292 

293response = client.responses.input_tokens.count(

294 model="gpt-5.6",

295 tools=[

296 {

297 "type": "function",

298 "name": "get_weather",

299 "description": "Get the current weather in a location",

300 "parameters": {

301 "type": "object",

302 "properties": {"location": {"type": "string"}},

303 "required": ["location"],

304 },

305 }

306 ],

307 input="What is the weather in San Francisco?",

308)

309print(response.input_tokens)

310```

311 

312```javascript288```javascript

313import OpenAI from "openai";289import OpenAI from "openai";

314 290 


336console.log(response.input_tokens);312console.log(response.input_tokens);

337```313```

338 314 

315```python

316from openai import OpenAI

317 

318client = OpenAI()

319 

320response = client.responses.input_tokens.count(

321 model="gpt-5.6",

322 tools=[

323 {

324 "type": "function",

325 "name": "get_weather",

326 "description": "Get the current weather in a location",

327 "parameters": {

328 "type": "object",

329 "properties": {"location": {"type": "string"}},

330 "required": ["location"],

331 },

332 }

333 ],

334 input="What is the weather in San Francisco?",

335)

336print(response.input_tokens)

337```

338 

339```bash339```bash

340curl https://api.openai.com/v1/responses/input_tokens \340curl https://api.openai.com/v1/responses/input_tokens \

341 -H "Authorization: Bearer $OPENAI_API_KEY" \341 -H "Authorization: Bearer $OPENAI_API_KEY" \

guides/tools.md +121 −121

Details

37print(response.output_text)37print(response.output_text)

38```38```

39 39 

40```bash

41curl "https://api.openai.com/v1/responses" \

42 -H "Content-Type: application/json" \

43 -H "Authorization: Bearer $OPENAI_API_KEY" \

44 -d '{

45 "model": "gpt-5.6",

46 "tools": [{"type": "web_search"}],

47 "input": "what was a positive news story from today?"

48}'

49```

50 

51```bash

52openai responses create \

53 --model gpt-5.6 \

54 --raw-output \

55 --transform 'output.#(type=="message").content.0.text' <<'YAML'

56tools:

57 - type: web_search

58input: What was a positive news story from today?

59YAML

60```

61 

62```csharp40```csharp

63using OpenAI.Responses;41using OpenAI.Responses;

64#pragma warning disable OPENAI00142#pragma warning disable OPENAI001


91puts(response.output_text)69puts(response.output_text)

92```70```

93 71 

72```bash

73curl "https://api.openai.com/v1/responses" \

74 -H "Content-Type: application/json" \

75 -H "Authorization: Bearer $OPENAI_API_KEY" \

76 -d '{

77 "model": "gpt-5.6",

78 "tools": [{"type": "web_search"}],

79 "input": "what was a positive news story from today?"

80}'

81```

94 82 

83```bash

84openai responses create \

85 --model gpt-5.6 \

86 --raw-output \

87 --transform 'output.#(type=="message").content.0.text' <<'YAML'

88tools:

89 - type: web_search

90input: What was a positive news story from today?

91YAML

92```

95 93 

96 94

97 95 

98 96

99File search

100 

101 Search your files in a response

102 97 

103```python

104from openai import OpenAI

105 98

106client = OpenAI()99File search

107 100 

108response = client.responses.create(101 Search your files in a response

109 model="gpt-5.6",

110 input="What is deep research by OpenAI?",

111 tools=[{"type": "file_search", "vector_store_ids": ["<vector_store_id>"]}],

112)

113print(response)

114```

115 102 

116```javascript103```javascript

117import OpenAI from "openai";104import OpenAI from "openai";


130console.log(response);117console.log(response);

131```118```

132 119 

120```python

121from openai import OpenAI

122 

123client = OpenAI()

124 

125response = client.responses.create(

126 model="gpt-5.6",

127 input="What is deep research by OpenAI?",

128 tools=[{"type": "file_search", "vector_store_ids": ["<vector_store_id>"]}],

129)

130print(response)

131```

132 

133```csharp133```csharp

134using OpenAI.Responses;134using OpenAI.Responses;

135#pragma warning disable OPENAI001135#pragma warning disable OPENAI001


178 178 

179 Load deferred tools at runtime179 Load deferred tools at runtime

180 180 

181```javascript

182import OpenAI from "openai";

183 

184const client = new OpenAI();

185 

186/** @type {OpenAI.Responses.NamespaceTool} */

187const crmNamespace = {

188 type: "namespace",

189 name: "crm",

190 description: "CRM tools for customer lookup and order management.",

191 tools: [

192 {

193 type: "function",

194 name: "get_customer_profile",

195 description: "Fetch a customer profile by customer ID.",

196 parameters: {

197 type: "object",

198 properties: {

199 customer_id: { type: "string" },

200 },

201 required: ["customer_id"],

202 additionalProperties: false,

203 },

204 },

205 {

206 type: "function",

207 name: "list_open_orders",

208 description: "List open orders for a customer ID.",

209 // highlight-start:subtle

210 defer_loading: true,

211 // highlight-end

212 parameters: {

213 type: "object",

214 properties: {

215 customer_id: { type: "string" },

216 },

217 required: ["customer_id"],

218 additionalProperties: false,

219 },

220 },

221 ],

222};

223 

224const response = await client.responses.create({

225 model: "gpt-5.6",

226 input: "List open orders for customer CUST-12345.",

227 // highlight-start:subtle

228 tools: [crmNamespace, { type: "tool_search" }],

229 // highlight-end

230 parallel_tool_calls: false,

231});

232 

233console.log(response.output);

234```

235 

181```python236```python

182from openai import OpenAI237from openai import OpenAI

183 238 


235print(response.output)290print(response.output)

236```291```

237 292 

238```javascript

239import OpenAI from "openai";

240 

241const client = new OpenAI();

242 

243/** @type {OpenAI.Responses.NamespaceTool} */

244const crmNamespace = {

245 type: "namespace",

246 name: "crm",

247 description: "CRM tools for customer lookup and order management.",

248 tools: [

249 {

250 type: "function",

251 name: "get_customer_profile",

252 description: "Fetch a customer profile by customer ID.",

253 parameters: {

254 type: "object",

255 properties: {

256 customer_id: { type: "string" },

257 },

258 required: ["customer_id"],

259 additionalProperties: false,

260 },

261 },

262 {

263 type: "function",

264 name: "list_open_orders",

265 description: "List open orders for a customer ID.",

266 // highlight-start:subtle

267 defer_loading: true,

268 // highlight-end

269 parameters: {

270 type: "object",

271 properties: {

272 customer_id: { type: "string" },

273 },

274 required: ["customer_id"],

275 additionalProperties: false,

276 },

277 },

278 ],

279};

280 

281const response = await client.responses.create({

282 model: "gpt-5.6",

283 input: "List open orders for customer CUST-12345.",

284 // highlight-start:subtle

285 tools: [crmNamespace, { type: "tool_search" }],

286 // highlight-end

287 parallel_tool_calls: false,

288});

289 

290console.log(response.output);

291```

292 

293 293

294 294 

295 295


420);420);

421```421```

422 422 

423```bash

424curl -X POST https://api.openai.com/v1/responses \

425 -H "Authorization: Bearer $OPENAI_API_KEY" \

426 -H "Content-Type: application/json" \

427 -d '{

428 "model": "gpt-5.6",

429 "input": [

430 {"role": "user", "content": "What is the weather like in Paris today?"}

431 ],

432 "tools": [

433 {

434 "type": "function",

435 "name": "get_weather",

436 "description": "Get current temperature for a given location.",

437 "parameters": {

438 "type": "object",

439 "properties": {

440 "location": {

441 "type": "string",

442 "description": "City and country e.g. Bogotá, Colombia"

443 }

444 },

445 "required": ["location"],

446 "additionalProperties": false

447 },

448 "strict": true

449 }

450 ]

451 }'

452```

453 

454```ruby423```ruby

455require "openai"424require "openai"

456 425 


487puts(response.output.first.to_json)456puts(response.output.first.to_json)

488```457```

489 458 

459```bash

460curl -X POST https://api.openai.com/v1/responses \

461 -H "Authorization: Bearer $OPENAI_API_KEY" \

462 -H "Content-Type: application/json" \

463 -d '{

464 "model": "gpt-5.6",

465 "input": [

466 {"role": "user", "content": "What is the weather like in Paris today?"}

467 ],

468 "tools": [

469 {

470 "type": "function",

471 "name": "get_weather",

472 "description": "Get current temperature for a given location.",

473 "parameters": {

474 "type": "object",

475 "properties": {

476 "location": {

477 "type": "string",

478 "description": "City and country e.g. Bogotá, Colombia"

479 }

480 },

481 "required": ["location"],

482 "additionalProperties": false

483 },

484 "strict": true

485 }

486 ]

487 }'

488```

489 

490 490

491 491 

492 492

Details

118 }'118 }'

119```119```

120 120 

121```python

122from openai import OpenAI

123 

124client = OpenAI()

125 

126container = client.containers.create(name="test-container", memory_limit="4g")

127 

128response = client.responses.create(

129 model="gpt-5.6",

130 tools=[{"type": "code_interpreter", "container": container.id}],

131 tool_choice="required",

132 input="use the python tool to calculate what is 4 * 3.82. and then find its square root and then find the square root of that result",

133)

134 

135print(response.output_text)

136```

137 

138```javascript121```javascript

139import OpenAI from "openai";122import OpenAI from "openai";

140const client = new OpenAI();123const client = new OpenAI();


160console.log(resp.output_text);143console.log(resp.output_text);

161```144```

162 145 

146```python

147from openai import OpenAI

148 

149client = OpenAI()

150 

151container = client.containers.create(name="test-container", memory_limit="4g")

152 

153response = client.responses.create(

154 model="gpt-5.6",

155 tools=[{"type": "code_interpreter", "container": container.id}],

156 tool_choice="required",

157 input="use the python tool to calculate what is 4 * 3.82. and then find its square root and then find the square root of that result",

158)

159 

160print(response.output_text)

161```

162 

163 163 

164You can choose from `1g` (default), `4g`, `16g`, or `64g`. Higher tiers offer more RAM for the session and are billed at the [built-in tools rates](https://developers.openai.com/api/docs/pricing#built-in-tools) for Code Interpreter. The selected `memory_limit` applies for the entire life of that container, whether it was created automatically or via the containers API.164You can choose from `1g` (default), `4g`, `16g`, or `64g`. Higher tiers offer more RAM for the session and are billed at the [built-in tools rates](https://developers.openai.com/api/docs/pricing#built-in-tools) for Code Interpreter. The selected `memory_limit` applies for the entire life of that container, whether it was created automatically or via the containers API.

165 165 

Details

125 125 

126Execute commands on the container126Execute commands on the container

127 127 

128```python

129import subprocess

130 

131 

132def docker_exec(cmd: str, container_name: str, decode: bool = True):

133 safe_cmd = cmd.replace('"', '\\"')

134 docker_cmd = f'docker exec {container_name} sh -c "{safe_cmd}"'

135 output = subprocess.check_output(docker_cmd, shell=True)

136 if decode:

137 return output.decode("utf-8", errors="ignore")

138 return output

139 

140 

141class VM:

142 def __init__(self, display: str, container_name: str):

143 self.display = display

144 self.container_name = container_name

145 

146 

147vm = VM(display=":99", container_name="cua-image")

148```

149 

150```javascript128```javascript

151import { execFile } from "node:child_process";129import { execFile } from "node:child_process";

152import { promisify } from "node:util";130import { promisify } from "node:util";


186};164};

187```165```

188 166 

167```python

168import subprocess

169 

170 

171def docker_exec(cmd: str, container_name: str, decode: bool = True):

172 safe_cmd = cmd.replace('"', '\\"')

173 docker_cmd = f'docker exec {container_name} sh -c "{safe_cmd}"'

174 output = subprocess.check_output(docker_cmd, shell=True)

175 if decode:

176 return output.decode("utf-8", errors="ignore")

177 return output

178 

179 

180class VM:

181 def __init__(self, display: str, container_name: str):

182 self.display = display

183 self.container_name = container_name

184 

185 

186vm = VM(display=":99", container_name="cua-image")

187```

188 

189 189 

190Whether you use a browser or VM, treat screenshots, page text, tool outputs, PDFs, emails, chats, and other third-party content as untrusted input. Only direct instructions from the user count as permission.190Whether you use a browser or VM, treat screenshots, page text, tool outputs, PDFs, emails, chats, and other third-party content as untrusted input. Only direct instructions from the user count as permission.

191 191 

Details

898 }'898 }'

899```899```

900 900 

901```python

902from openai import OpenAI

903 

904client = OpenAI()

905 

906response = client.responses.create(

907 model="gpt-5.6",

908 instructions="The local bash shell environment is on Mac.",

909 input="find me the largest pdf file in ~/Documents",

910 tools=[{"type": "shell", "environment": {"type": "local"}}],

911)

912 

913print(response)

914```

915 

916```javascript901```javascript

917import OpenAI from "openai";902import OpenAI from "openai";

918 903 


928console.log(response);913console.log(response);

929```914```

930 915 

916```python

917from openai import OpenAI

918 

919client = OpenAI()

920 

921response = client.responses.create(

922 model="gpt-5.6",

923 instructions="The local bash shell environment is on Mac.",

924 input="find me the largest pdf file in ~/Documents",

925 tools=[{"type": "shell", "environment": {"type": "local"}}],

926)

927 

928print(response)

929```

930 

931 931 

932When you receive `shell_call` output items:932When you receive `shell_call` output items:

933 933 


937 937 

938Local shell executor example938Local shell executor example

939 939 

940```python

941@dataclass

942class CmdResult:

943 stdout: str

944 stderr: str

945 exit_code: int | None

946 timed_out: bool

947 

948 

949class ShellExecutor:

950 def __init__(self, default_timeout: float = 60):

951 self.default_timeout = default_timeout

952 

953 def run(self, cmd: str, timeout: float | None = None) -> CmdResult:

954 t = timeout or self.default_timeout

955 p = subprocess.Popen(

956 cmd,

957 shell=True,

958 stdout=subprocess.PIPE,

959 stderr=subprocess.PIPE,

960 text=True,

961 )

962 try:

963 out, err = p.communicate(timeout=t)

964 return CmdResult(out, err, p.returncode, False)

965 except subprocess.TimeoutExpired:

966 p.kill()

967 out, err = p.communicate()

968 return CmdResult(out, err, p.returncode, True)

969```

970 

971```javascript940```javascript

972import { exec as execCallback } from "node:child_process";941import { exec as execCallback } from "node:child_process";

973import { promisify } from "node:util";942import { promisify } from "node:util";


999}968}

1000```969```

1001 970 

971```python

972@dataclass

973class CmdResult:

974 stdout: str

975 stderr: str

976 exit_code: int | None

977 timed_out: bool

978 

979 

980class ShellExecutor:

981 def __init__(self, default_timeout: float = 60):

982 self.default_timeout = default_timeout

983 

984 def run(self, cmd: str, timeout: float | None = None) -> CmdResult:

985 t = timeout or self.default_timeout

986 p = subprocess.Popen(

987 cmd,

988 shell=True,

989 stdout=subprocess.PIPE,

990 stderr=subprocess.PIPE,

991 text=True,

992 )

993 try:

994 out, err = p.communicate(timeout=t)

995 return CmdResult(out, err, p.returncode, False)

996 except subprocess.TimeoutExpired:

997 p.kill()

998 out, err = p.communicate()

999 return CmdResult(out, err, p.returncode, True)

1000```

1001 

1002 1002 

1003Example shell_call_output payload1003Example shell_call_output payload

1004 1004 

Details

297console.log("Wrote video.mp4");297console.log("Wrote video.mp4");

298```298```

299 299 

300```bash

301curl -L "https://api.openai.com/v1/videos/video_abc123/content" \

302 -H "Authorization: Bearer $OPENAI_API_KEY" \

303 --output video.mp4

304```

305 

306```python300```python

307from openai import OpenAI301from openai import OpenAI

308import sys302import sys


352print("Wrote video.mp4")346print("Wrote video.mp4")

353```347```

354 348 

349```bash

350curl -L "https://api.openai.com/v1/videos/video_abc123/content" \

351 -H "Authorization: Bearer $OPENAI_API_KEY" \

352 --output video.mp4

353```

354 

355 355 

356You now have the final video file ready for playback, editing, or distribution. Download URLs are valid for a maximum of 1 hour after generation. If you need long-term storage, copy the file to your own storage system promptly.356You now have the final video file ready for playback, editing, or distribution. Download URLs are valid for a maximum of 1 hour after generation. If you need long-term storage, copy the file to your own storage system promptly.

357 357 

guides/webhooks.md +43 −43

Details

14 14 

15Webhooks server15Webhooks server

16 16 

17```python

18import os

19from openai import OpenAI, InvalidWebhookSignatureError

20from flask import Flask, request, Response

21 

22app = Flask(__name__)

23client = OpenAI(webhook_secret=os.environ["OPENAI_WEBHOOK_SECRET"])

24 

25 

26@app.route("/webhook", methods=["POST"])

27def webhook():

28 try:

29 # with webhook_secret set above, unwrap will raise an error if the signature is invalid

30 event = client.webhooks.unwrap(request.data, request.headers)

31 

32 if event.type == "response.completed":

33 response_id = event.data.id

34 response = client.responses.retrieve(response_id)

35 print("Response output:", response.output_text)

36 

37 return Response(status=200)

38 except InvalidWebhookSignatureError as e:

39 print("Invalid signature", e)

40 return Response("Invalid signature", status=400)

41 

42 

43if __name__ == "__main__":

44 app.run(port=8000)

45```

46 

47```javascript17```javascript

48import OpenAI from "openai";18import OpenAI from "openai";

49import express from "express";19import express from "express";


86});56});

87```57```

88 58 

59```python

60import os

61from openai import OpenAI, InvalidWebhookSignatureError

62from flask import Flask, request, Response

63 

64app = Flask(__name__)

65client = OpenAI(webhook_secret=os.environ["OPENAI_WEBHOOK_SECRET"])

66 

67 

68@app.route("/webhook", methods=["POST"])

69def webhook():

70 try:

71 # with webhook_secret set above, unwrap will raise an error if the signature is invalid

72 event = client.webhooks.unwrap(request.data, request.headers)

73 

74 if event.type == "response.completed":

75 response_id = event.data.id

76 response = client.responses.retrieve(response_id)

77 print("Response output:", response.output_text)

78 

79 return Response(status=200)

80 except InvalidWebhookSignatureError as e:

81 print("Invalid signature", e)

82 return Response("Invalid signature", status=400)

83 

84 

85if __name__ == "__main__":

86 app.run(port=8000)

87```

88 

89 89 

90To see a webhook like this one in action, you can set up a webhook endpoint in the OpenAI dashboard subscribed to `response.completed`, and then make an API request to [generate a response in background mode](https://developers.openai.com/api/docs/guides/background).90To see a webhook like this one in action, you can set up a webhook endpoint in the OpenAI dashboard subscribed to `response.completed`, and then make an API request to [generate a response in background mode](https://developers.openai.com/api/docs/guides/background).

91 91 


200 200 

201Signature verification with the OpenAI SDK201Signature verification with the OpenAI SDK

202 202 

203```javascript

204const client = new OpenAI();

205const webhook_secret = process.env.OPENAI_WEBHOOK_SECRET;

206if (!webhook_secret) throw new Error("Set OPENAI_WEBHOOK_SECRET.");

207 

208// will throw if the signature is invalid

209const event = await client.webhooks.unwrap(

210 req.body,

211 req.headers,

212 webhook_secret

213);

214```

215 

203```python216```python

204import os217import os

205 218 


217)230)

218```231```

219 232 

220```javascript

221const client = new OpenAI();

222const webhook_secret = process.env.OPENAI_WEBHOOK_SECRET;

223if (!webhook_secret) throw new Error("Set OPENAI_WEBHOOK_SECRET.");

224 

225// will throw if the signature is invalid

226const event = await client.webhooks.unwrap(

227 req.body,

228 req.headers,

229 webhook_secret

230);

231```

232 

233 233 

234Signatures can also be verified with the [Standard Webhooks libraries](https://github.com/standard-webhooks/standard-webhooks/tree/main?tab=readme-ov-file#reference-implementations):234Signatures can also be verified with the [Standard Webhooks libraries](https://github.com/standard-webhooks/standard-webhooks/tree/main?tab=readme-ov-file#reference-implementations):

235 235 

quickstart.md +253 −253

Details

425console.log(response.output_text);425console.log(response.output_text);

426```426```

427 427 

428```bash

429curl "https://api.openai.com/v1/responses" \

430 -H "Content-Type: application/json" \

431 -H "Authorization: Bearer $OPENAI_API_KEY" \

432 -d '{

433 "model": "gpt-5.6",

434 "input": [

435 {

436 "role": "user",

437 "content": [

438 {

439 "type": "input_text",

440 "text": "What is in this image?"

441 },

442 {

443 "type": "input_image",

444 "image_url": "https://openai-documentation.vercel.app/images/cat_and_otter.png"

445 }

446 ]

447 }

448 ]

449}'

450```

451 

452```bash

453openai responses create \

454 --model gpt-5.6 \

455 --raw-output \

456 --transform 'output.#(type=="message").content.0.text' <<'YAML'

457input:

458 - role: user

459 content:

460 - type: input_text

461 text: What is in this image?

462 - type: input_image

463 image_url: https://openai-documentation.vercel.app/images/cat_and_otter.png

464YAML

465```

466 

467```python428```python

468from openai import OpenAI429from openai import OpenAI

469 430 


544puts(response.output_text)505puts(response.output_text)

545```506```

546 507 

547

548 

549

550 

551

552File URL

553 

554 Use a file URL as input

555 

556```bash508```bash

557curl "https://api.openai.com/v1/responses" \509curl "https://api.openai.com/v1/responses" \

558 -H "Content-Type: application/json" \510 -H "Content-Type: application/json" \


565 "content": [517 "content": [

566 {518 {

567 "type": "input_text",519 "type": "input_text",

568 "text": "Analyze the letter and provide a summary of the key points."520 "text": "What is in this image?"

569 },521 },

570 {522 {

571 "type": "input_file",523 "type": "input_image",

572 "file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf"524 "image_url": "https://openai-documentation.vercel.app/images/cat_and_otter.png"

573 }525 }

574 ]526 ]

575 }527 }

576 ]528 ]

577 }'529}'

578```530```

579 531 

532```bash

533openai responses create \

534 --model gpt-5.6 \

535 --raw-output \

536 --transform 'output.#(type=="message").content.0.text' <<'YAML'

537input:

538 - role: user

539 content:

540 - type: input_text

541 text: What is in this image?

542 - type: input_image

543 image_url: https://openai-documentation.vercel.app/images/cat_and_otter.png

544YAML

545```

546 

547

548 

549

550 

551

552File URL

553 

554 Use a file URL as input

555 

580```javascript556```javascript

581import OpenAI from "openai";557import OpenAI from "openai";

582const client = new OpenAI();558const client = new OpenAI();


603console.log(response.output_text);579console.log(response.output_text);

604```580```

605 581 

582```python

583from openai import OpenAI

584 

585client = OpenAI()

586 

587response = client.responses.create(

588 model="gpt-5.6",

589 input=[

590 {

591 "role": "user",

592 "content": [

593 {

594 "type": "input_text",

595 "text": "Analyze the letter and provide a summary of the key points.",

596 },

597 {

598 "type": "input_file",

599 "file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf",

600 },

601 ],

602 },

603 ],

604)

605 

606print(response.output_text)

607```

608 

606```go609```go

607package main610package main

608 611 


647}650}

648```651```

649 652 

650```python

651from openai import OpenAI

652 

653client = OpenAI()

654 

655response = client.responses.create(

656 model="gpt-5.6",

657 input=[

658 {

659 "role": "user",

660 "content": [

661 {

662 "type": "input_text",

663 "text": "Analyze the letter and provide a summary of the key points.",

664 },

665 {

666 "type": "input_file",

667 "file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf",

668 },

669 ],

670 },

671 ],

672)

673 

674print(response.output_text)

675```

676 

677```ruby

678require "openai"

679 

680openai = OpenAI::Client.new

681 

682response = openai.responses.create(

683 model: "gpt-5.6",

684 input: [

685 {

686 role: "user",

687 content: [

688 {

689 type: "input_text",

690 text: "Analyze the letter and provide a summary of the key points."

691 },

692 {

693 type: "input_file",

694 file_url: "https://www.berkshirehathaway.com/letters/2024ltr.pdf"

695 }

696 ]

697 }

698 ]

699)

700 

701puts(response.output_text)

702```

703 

704```csharp653```csharp

705using OpenAI.Responses;654using OpenAI.Responses;

706#pragma warning disable OPENAI001655#pragma warning disable OPENAI001


729Console.WriteLine(response.GetOutputText());678Console.WriteLine(response.GetOutputText());

730```679```

731 680 

681```ruby

682require "openai"

732 683 

684openai = OpenAI::Client.new

733 685 

686response = openai.responses.create(

687 model: "gpt-5.6",

688 input: [

689 {

690 role: "user",

691 content: [

692 {

693 type: "input_text",

694 text: "Analyze the letter and provide a summary of the key points."

695 },

696 {

697 type: "input_file",

698 file_url: "https://www.berkshirehathaway.com/letters/2024ltr.pdf"

699 }

700 ]

701 }

702 ]

703)

734 704 

735 705puts(response.output_text)

736 706```

737Upload file

738 

739 Upload a file and use it as input

740 707 

741```bash708```bash

742curl https://api.openai.com/v1/files \

743 -H "Authorization: Bearer $OPENAI_API_KEY" \

744 -F purpose="user_data" \

745 -F file="@draconomicon.pdf"

746 

747curl "https://api.openai.com/v1/responses" \709curl "https://api.openai.com/v1/responses" \

748 -H "Content-Type: application/json" \710 -H "Content-Type: application/json" \

749 -H "Authorization: Bearer $OPENAI_API_KEY" \711 -H "Authorization: Bearer $OPENAI_API_KEY" \


754 "role": "user",716 "role": "user",

755 "content": [717 "content": [

756 {718 {

757 "type": "input_file",719 "type": "input_text",

758 "file_id": "file-6F2ksmvXxt4VdoqmHRw6kL"720 "text": "Analyze the letter and provide a summary of the key points."

759 },721 },

760 {722 {

761 "type": "input_text",723 "type": "input_file",

762 "text": "What is the first dragon in the book?"724 "file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf"

763 }725 }

764 ]726 ]

765 }727 }


767 }'729 }'

768```730```

769 731 

732

733 

734

735 

736

737Upload file

738 

739 Upload a file and use it as input

740 

770```javascript741```javascript

771import fs from "fs";742import fs from "fs";

772import OpenAI from "openai";743import OpenAI from "openai";


799console.log(response.output_text);770console.log(response.output_text);

800```771```

801 772 

773```python

774from openai import OpenAI

775 

776client = OpenAI()

777 

778file = client.files.create(file=open("draconomicon.pdf", "rb"), purpose="user_data")

779 

780response = client.responses.create(

781 model="gpt-5.6",

782 input=[

783 {

784 "role": "user",

785 "content": [

786 {

787 "type": "input_file",

788 "file_id": file.id,

789 },

790 {

791 "type": "input_text",

792 "text": "What is the first dragon in the book?",

793 },

794 ],

795 }

796 ],

797)

798 

799print(response.output_text)

800```

801 

802```go802```go

803package main803package main

804 804 


856}856}

857```857```

858 858 

859```python859```csharp

860from openai import OpenAI860using OpenAI.Files;

861using OpenAI.Responses;

862#pragma warning disable OPENAI001

861 863 

862client = OpenAI()864string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;

865ResponsesClient client = new(key);

863 866 

864file = client.files.create(file=open("draconomicon.pdf", "rb"), purpose="user_data")867OpenAIFileClient files = new(key);

865 868 

866response = client.responses.create(869OpenAIFile file = await files.UploadFileAsync(

867 model="gpt-5.6",870 "draconomicon.pdf",

868 input=[871 FileUploadPurpose.UserData

869 {872);

870 "role": "user",

871 "content": [

872 {

873 "type": "input_file",

874 "file_id": file.id,

875 },

876 {

877 "type": "input_text",

878 "text": "What is the first dragon in the book?",

879 },

880 ],

881 }

882 ],

883)

884 873 

885print(response.output_text)874ResponseResult response = await client.CreateResponseAsync(

875 "gpt-5.6",

876 [

877 ResponseItem.CreateUserMessageItem(

878 [

879 ResponseContentPart.CreateInputFilePart(file.Id),

880 ResponseContentPart.CreateInputTextPart(

881 "What is the first dragon in the book?"

882 ),

883 ]

884 ),

885 ]

886);

887 

888Console.WriteLine(response.GetOutputText());

886```889```

887 890 

888```ruby891```ruby


911puts(response.output_text)914puts(response.output_text)

912```915```

913 916 

914```csharp917```bash

915using OpenAI.Files;918curl https://api.openai.com/v1/files \

916using OpenAI.Responses;919 -H "Authorization: Bearer $OPENAI_API_KEY" \

917#pragma warning disable OPENAI001920 -F purpose="user_data" \

918 921 -F file="@draconomicon.pdf"

919string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;

920ResponsesClient client = new(key);

921 

922OpenAIFileClient files = new(key);

923 

924OpenAIFile file = await files.UploadFileAsync(

925 "draconomicon.pdf",

926 FileUploadPurpose.UserData

927);

928 922 

929ResponseResult response = await client.CreateResponseAsync(923curl "https://api.openai.com/v1/responses" \

930 "gpt-5.6",924 -H "Content-Type: application/json" \

931 [925 -H "Authorization: Bearer $OPENAI_API_KEY" \

932 ResponseItem.CreateUserMessageItem(926 -d '{

933 [927 "model": "gpt-5.6",

934 ResponseContentPart.CreateInputFilePart(file.Id),928 "input": [

935 ResponseContentPart.CreateInputTextPart(929 {

936 "What is the first dragon in the book?"930 "role": "user",

937 ),931 "content": [

932 {

933 "type": "input_file",

934 "file_id": "file-6F2ksmvXxt4VdoqmHRw6kL"

935 },

936 {

937 "type": "input_text",

938 "text": "What is the first dragon in the book?"

939 }

938 ]940 ]

939 ),941 }

940 ]942 ]

941);943 }'

942 

943Console.WriteLine(response.GetOutputText());

944```944```

945 945 

946 946 


994print(response.output_text)994print(response.output_text)

995```995```

996 996 

997```bash

998curl "https://api.openai.com/v1/responses" \

999 -H "Content-Type: application/json" \

1000 -H "Authorization: Bearer $OPENAI_API_KEY" \

1001 -d '{

1002 "model": "gpt-5.6",

1003 "tools": [{"type": "web_search"}],

1004 "input": "what was a positive news story from today?"

1005}'

1006```

1007 

1008```bash

1009openai responses create \

1010 --model gpt-5.6 \

1011 --raw-output \

1012 --transform 'output.#(type=="message").content.0.text' <<'YAML'

1013tools:

1014 - type: web_search

1015input: What was a positive news story from today?

1016YAML

1017```

1018 

1019```csharp997```csharp

1020using OpenAI.Responses;998using OpenAI.Responses;

1021#pragma warning disable OPENAI001999#pragma warning disable OPENAI001


1048puts(response.output_text)1026puts(response.output_text)

1049```1027```

1050 1028 

1029```bash

1030curl "https://api.openai.com/v1/responses" \

1031 -H "Content-Type: application/json" \

1032 -H "Authorization: Bearer $OPENAI_API_KEY" \

1033 -d '{

1034 "model": "gpt-5.6",

1035 "tools": [{"type": "web_search"}],

1036 "input": "what was a positive news story from today?"

1037}'

1038```

1051 1039 

1040```bash

1041openai responses create \

1042 --model gpt-5.6 \

1043 --raw-output \

1044 --transform 'output.#(type=="message").content.0.text' <<'YAML'

1045tools:

1046 - type: web_search

1047input: What was a positive news story from today?

1048YAML

1049```

1052 1050 

1053 1051

1054 1052 

1055 1053

1056File search

1057 

1058 Search your files in a response

1059 1054 

1060```python

1061from openai import OpenAI

1062 1055

1063client = OpenAI()1056File search

1064 1057 

1065response = client.responses.create(1058 Search your files in a response

1066 model="gpt-5.6",

1067 input="What is deep research by OpenAI?",

1068 tools=[{"type": "file_search", "vector_store_ids": ["<vector_store_id>"]}],

1069)

1070print(response)

1071```

1072 1059 

1073```javascript1060```javascript

1074import OpenAI from "openai";1061import OpenAI from "openai";


1087console.log(response);1074console.log(response);

1088```1075```

1089 1076 

1077```python

1078from openai import OpenAI

1079 

1080client = OpenAI()

1081 

1082response = client.responses.create(

1083 model="gpt-5.6",

1084 input="What is deep research by OpenAI?",

1085 tools=[{"type": "file_search", "vector_store_ids": ["<vector_store_id>"]}],

1086)

1087print(response)

1088```

1089 

1090```csharp1090```csharp

1091using OpenAI.Responses;1091using OpenAI.Responses;

1092#pragma warning disable OPENAI0011092#pragma warning disable OPENAI001


1170print(response.output_text)1170print(response.output_text)

1171```1171```

1172 1172 

1173```bash

1174curl https://api.openai.com/v1/responses \

1175 -H "Content-Type: application/json" \

1176 -H "Authorization: Bearer $OPENAI_API_KEY" \

1177 -d '{

1178 "model": "gpt-5.6",

1179 "instructions": "You are a personal math tutor. When asked a math question, write and run code to answer the question.",

1180 "tools": [

1181 {

1182 "type": "code_interpreter",

1183 "container": { "type": "auto" }

1184 }

1185 ],

1186 "input": "I need to solve the equation 3x + 11 = 14. Can you help me?"

1187 }'

1188```

1189 

1190```ruby1173```ruby

1191require "openai"1174require "openai"

1192 1175 


1207puts(response.output_text)1190puts(response.output_text)

1208```1191```

1209 1192 

1193```bash

1194curl https://api.openai.com/v1/responses \

1195 -H "Content-Type: application/json" \

1196 -H "Authorization: Bearer $OPENAI_API_KEY" \

1197 -d '{

1198 "model": "gpt-5.6",

1199 "instructions": "You are a personal math tutor. When asked a math question, write and run code to answer the question.",

1200 "tools": [

1201 {

1202 "type": "code_interpreter",

1203 "container": { "type": "auto" }

1204 }

1205 ],

1206 "input": "I need to solve the equation 3x + 11 = 14. Can you help me?"

1207 }'

1208```

1209 

1210 1210

1211 1211 

1212 1212


1337);1337);

1338```1338```

1339 1339 

1340```bash

1341curl -X POST https://api.openai.com/v1/responses \

1342 -H "Authorization: Bearer $OPENAI_API_KEY" \

1343 -H "Content-Type: application/json" \

1344 -d '{

1345 "model": "gpt-5.6",

1346 "input": [

1347 {"role": "user", "content": "What is the weather like in Paris today?"}

1348 ],

1349 "tools": [

1350 {

1351 "type": "function",

1352 "name": "get_weather",

1353 "description": "Get current temperature for a given location.",

1354 "parameters": {

1355 "type": "object",

1356 "properties": {

1357 "location": {

1358 "type": "string",

1359 "description": "City and country e.g. Bogotá, Colombia"

1360 }

1361 },

1362 "required": ["location"],

1363 "additionalProperties": false

1364 },

1365 "strict": true

1366 }

1367 ]

1368 }'

1369```

1370 

1371```ruby1340```ruby

1372require "openai"1341require "openai"

1373 1342 


1404puts(response.output.first.to_json)1373puts(response.output.first.to_json)

1405```1374```

1406 1375 

1376```bash

1377curl -X POST https://api.openai.com/v1/responses \

1378 -H "Authorization: Bearer $OPENAI_API_KEY" \

1379 -H "Content-Type: application/json" \

1380 -d '{

1381 "model": "gpt-5.6",

1382 "input": [

1383 {"role": "user", "content": "What is the weather like in Paris today?"}

1384 ],

1385 "tools": [

1386 {

1387 "type": "function",

1388 "name": "get_weather",

1389 "description": "Get current temperature for a given location.",

1390 "parameters": {

1391 "type": "object",

1392 "properties": {

1393 "location": {

1394 "type": "string",

1395 "description": "City and country e.g. Bogotá, Colombia"

1396 }

1397 },

1398 "required": ["location"],

1399 "additionalProperties": false

1400 },

1401 "strict": true

1402 }

1403 ]

1404 }'

1405```

1406 

1407 1407

1408 1408 

1409 1409