main
1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3from __future__ import annotations
4
5import typing_extensions
6from typing import List, Union, Iterable, Optional
7from functools import partial
8from typing_extensions import Literal, overload
9
10import httpx
11
12from ..... import _legacy_response
13from .steps import (
14 Steps,
15 AsyncSteps,
16 StepsWithRawResponse,
17 AsyncStepsWithRawResponse,
18 StepsWithStreamingResponse,
19 AsyncStepsWithStreamingResponse,
20)
21from ....._types import NOT_GIVEN, Body, Omit, Query, Headers, NotGiven, omit, not_given
22from ....._utils import (
23 is_given,
24 required_args,
25 maybe_transform,
26 async_maybe_transform,
27)
28from ....._compat import cached_property
29from ....._resource import SyncAPIResource, AsyncAPIResource
30from ....._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
31from ....._streaming import Stream, AsyncStream
32from .....pagination import SyncCursorPage, AsyncCursorPage
33from ....._base_client import AsyncPaginator, make_request_options
34from .....lib.streaming import (
35 AssistantEventHandler,
36 AssistantEventHandlerT,
37 AssistantStreamManager,
38 AsyncAssistantEventHandler,
39 AsyncAssistantEventHandlerT,
40 AsyncAssistantStreamManager,
41)
42from .....types.beta.threads import (
43 run_list_params,
44 run_create_params,
45 run_update_params,
46 run_submit_tool_outputs_params,
47)
48from .....types.beta.threads.run import Run
49from .....types.shared.chat_model import ChatModel
50from .....types.shared_params.metadata import Metadata
51from .....types.shared.reasoning_effort import ReasoningEffort
52from .....types.beta.assistant_tool_param import AssistantToolParam
53from .....types.beta.assistant_stream_event import AssistantStreamEvent
54from .....types.beta.threads.runs.run_step_include import RunStepInclude
55from .....types.beta.assistant_tool_choice_option_param import AssistantToolChoiceOptionParam
56from .....types.beta.assistant_response_format_option_param import AssistantResponseFormatOptionParam
57
58__all__ = ["Runs", "AsyncRuns"]
59
60
61class Runs(SyncAPIResource):
62 @cached_property
63 def steps(self) -> Steps:
64 return Steps(self._client)
65
66 @cached_property
67 def with_raw_response(self) -> RunsWithRawResponse:
68 """
69 This property can be used as a prefix for any HTTP method call to return
70 the raw response object instead of the parsed content.
71
72 For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
73 """
74 return RunsWithRawResponse(self)
75
76 @cached_property
77 def with_streaming_response(self) -> RunsWithStreamingResponse:
78 """
79 An alternative to `.with_raw_response` that doesn't eagerly read the response body.
80
81 For more information, see https://www.github.com/openai/openai-python#with_streaming_response
82 """
83 return RunsWithStreamingResponse(self)
84
85 @overload
86 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
87 def create(
88 self,
89 thread_id: str,
90 *,
91 assistant_id: str,
92 include: List[RunStepInclude] | Omit = omit,
93 additional_instructions: Optional[str] | Omit = omit,
94 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
95 instructions: Optional[str] | Omit = omit,
96 max_completion_tokens: Optional[int] | Omit = omit,
97 max_prompt_tokens: Optional[int] | Omit = omit,
98 metadata: Optional[Metadata] | Omit = omit,
99 model: Union[str, ChatModel, None] | Omit = omit,
100 parallel_tool_calls: bool | Omit = omit,
101 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
102 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
103 stream: Optional[Literal[False]] | Omit = omit,
104 temperature: Optional[float] | Omit = omit,
105 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
106 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
107 top_p: Optional[float] | Omit = omit,
108 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
109 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
110 # The extra values given here take precedence over values defined on the client or passed to this method.
111 extra_headers: Headers | None = None,
112 extra_query: Query | None = None,
113 extra_body: Body | None = None,
114 timeout: float | httpx.Timeout | None | NotGiven = not_given,
115 ) -> Run:
116 """
117 Create a run.
118
119 Args:
120 assistant_id: The ID of the
121 [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to
122 execute this run.
123
124 include: A list of additional fields to include in the response. Currently the only
125 supported value is `step_details.tool_calls[*].file_search.results[*].content`
126 to fetch the file search result content.
127
128 See the
129 [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
130 for more information.
131
132 additional_instructions: Appends additional instructions at the end of the instructions for the run. This
133 is useful for modifying the behavior on a per-run basis without overriding other
134 instructions.
135
136 additional_messages: Adds additional messages to the thread before creating the run.
137
138 instructions: Overrides the
139 [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant)
140 of the assistant. This is useful for modifying the behavior on a per-run basis.
141
142 max_completion_tokens: The maximum number of completion tokens that may be used over the course of the
143 run. The run will make a best effort to use only the number of completion tokens
144 specified, across multiple turns of the run. If the run exceeds the number of
145 completion tokens specified, the run will end with status `incomplete`. See
146 `incomplete_details` for more info.
147
148 max_prompt_tokens: The maximum number of prompt tokens that may be used over the course of the run.
149 The run will make a best effort to use only the number of prompt tokens
150 specified, across multiple turns of the run. If the run exceeds the number of
151 prompt tokens specified, the run will end with status `incomplete`. See
152 `incomplete_details` for more info.
153
154 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
155 for storing additional information about the object in a structured format, and
156 querying for objects via API or the dashboard.
157
158 Keys are strings with a maximum length of 64 characters. Values are strings with
159 a maximum length of 512 characters.
160
161 model: The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to
162 be used to execute this run. If a value is provided here, it will override the
163 model associated with the assistant. If not, the model associated with the
164 assistant will be used.
165
166 parallel_tool_calls: Whether to enable
167 [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
168 during tool use.
169
170 reasoning_effort: Constrains effort on reasoning for
171 [reasoning models](https://platform.openai.com/docs/guides/reasoning). Currently
172 supported values are `none`, `minimal`, `low`, `medium`, `high`, and `xhigh`.
173 Reducing reasoning effort can result in faster responses and fewer tokens used
174 on reasoning in a response.
175
176 - `gpt-5.1` defaults to `none`, which does not perform reasoning. The supported
177 reasoning values for `gpt-5.1` are `none`, `low`, `medium`, and `high`. Tool
178 calls are supported for all reasoning values in gpt-5.1.
179 - All models before `gpt-5.1` default to `medium` reasoning effort, and do not
180 support `none`.
181 - The `gpt-5-pro` model defaults to (and only supports) `high` reasoning effort.
182 - `xhigh` is currently only supported for `gpt-5.1-codex-max`.
183
184 response_format: Specifies the format that the model must output. Compatible with
185 [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
186 [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4),
187 and all GPT-3.5 Turbo models since `gpt-3.5-turbo-1106`.
188
189 Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured
190 Outputs which ensures the model will match your supplied JSON schema. Learn more
191 in the
192 [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs).
193
194 Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the
195 message the model generates is valid JSON.
196
197 **Important:** when using JSON mode, you **must** also instruct the model to
198 produce JSON yourself via a system or user message. Without this, the model may
199 generate an unending stream of whitespace until the generation reaches the token
200 limit, resulting in a long-running and seemingly "stuck" request. Also note that
201 the message content may be partially cut off if `finish_reason="length"`, which
202 indicates the generation exceeded `max_tokens` or the conversation exceeded the
203 max context length.
204
205 stream: If `true`, returns a stream of events that happen during the Run as server-sent
206 events, terminating when the Run enters a terminal state with a `data: [DONE]`
207 message.
208
209 temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
210 make the output more random, while lower values like 0.2 will make it more
211 focused and deterministic.
212
213 tool_choice: Controls which (if any) tool is called by the model. `none` means the model will
214 not call any tools and instead generates a message. `auto` is the default value
215 and means the model can pick between generating a message or calling one or more
216 tools. `required` means the model must call one or more tools before responding
217 to the user. Specifying a particular tool like `{"type": "file_search"}` or
218 `{"type": "function", "function": {"name": "my_function"}}` forces the model to
219 call that tool.
220
221 tools: Override the tools the assistant can use for this run. This is useful for
222 modifying the behavior on a per-run basis.
223
224 top_p: An alternative to sampling with temperature, called nucleus sampling, where the
225 model considers the results of the tokens with top_p probability mass. So 0.1
226 means only the tokens comprising the top 10% probability mass are considered.
227
228 We generally recommend altering this or temperature but not both.
229
230 truncation_strategy: Controls for how a thread will be truncated prior to the run. Use this to
231 control the initial context window of the run.
232
233 extra_headers: Send extra headers
234
235 extra_query: Add additional query parameters to the request
236
237 extra_body: Add additional JSON properties to the request
238
239 timeout: Override the client-level default timeout for this request, in seconds
240 """
241 ...
242
243 @overload
244 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
245 def create(
246 self,
247 thread_id: str,
248 *,
249 assistant_id: str,
250 stream: Literal[True],
251 include: List[RunStepInclude] | Omit = omit,
252 additional_instructions: Optional[str] | Omit = omit,
253 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
254 instructions: Optional[str] | Omit = omit,
255 max_completion_tokens: Optional[int] | Omit = omit,
256 max_prompt_tokens: Optional[int] | Omit = omit,
257 metadata: Optional[Metadata] | Omit = omit,
258 model: Union[str, ChatModel, None] | Omit = omit,
259 parallel_tool_calls: bool | Omit = omit,
260 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
261 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
262 temperature: Optional[float] | Omit = omit,
263 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
264 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
265 top_p: Optional[float] | Omit = omit,
266 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
267 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
268 # The extra values given here take precedence over values defined on the client or passed to this method.
269 extra_headers: Headers | None = None,
270 extra_query: Query | None = None,
271 extra_body: Body | None = None,
272 timeout: float | httpx.Timeout | None | NotGiven = not_given,
273 ) -> Stream[AssistantStreamEvent]:
274 """
275 Create a run.
276
277 Args:
278 assistant_id: The ID of the
279 [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to
280 execute this run.
281
282 stream: If `true`, returns a stream of events that happen during the Run as server-sent
283 events, terminating when the Run enters a terminal state with a `data: [DONE]`
284 message.
285
286 include: A list of additional fields to include in the response. Currently the only
287 supported value is `step_details.tool_calls[*].file_search.results[*].content`
288 to fetch the file search result content.
289
290 See the
291 [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
292 for more information.
293
294 additional_instructions: Appends additional instructions at the end of the instructions for the run. This
295 is useful for modifying the behavior on a per-run basis without overriding other
296 instructions.
297
298 additional_messages: Adds additional messages to the thread before creating the run.
299
300 instructions: Overrides the
301 [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant)
302 of the assistant. This is useful for modifying the behavior on a per-run basis.
303
304 max_completion_tokens: The maximum number of completion tokens that may be used over the course of the
305 run. The run will make a best effort to use only the number of completion tokens
306 specified, across multiple turns of the run. If the run exceeds the number of
307 completion tokens specified, the run will end with status `incomplete`. See
308 `incomplete_details` for more info.
309
310 max_prompt_tokens: The maximum number of prompt tokens that may be used over the course of the run.
311 The run will make a best effort to use only the number of prompt tokens
312 specified, across multiple turns of the run. If the run exceeds the number of
313 prompt tokens specified, the run will end with status `incomplete`. See
314 `incomplete_details` for more info.
315
316 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
317 for storing additional information about the object in a structured format, and
318 querying for objects via API or the dashboard.
319
320 Keys are strings with a maximum length of 64 characters. Values are strings with
321 a maximum length of 512 characters.
322
323 model: The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to
324 be used to execute this run. If a value is provided here, it will override the
325 model associated with the assistant. If not, the model associated with the
326 assistant will be used.
327
328 parallel_tool_calls: Whether to enable
329 [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
330 during tool use.
331
332 reasoning_effort: Constrains effort on reasoning for
333 [reasoning models](https://platform.openai.com/docs/guides/reasoning). Currently
334 supported values are `none`, `minimal`, `low`, `medium`, `high`, and `xhigh`.
335 Reducing reasoning effort can result in faster responses and fewer tokens used
336 on reasoning in a response.
337
338 - `gpt-5.1` defaults to `none`, which does not perform reasoning. The supported
339 reasoning values for `gpt-5.1` are `none`, `low`, `medium`, and `high`. Tool
340 calls are supported for all reasoning values in gpt-5.1.
341 - All models before `gpt-5.1` default to `medium` reasoning effort, and do not
342 support `none`.
343 - The `gpt-5-pro` model defaults to (and only supports) `high` reasoning effort.
344 - `xhigh` is currently only supported for `gpt-5.1-codex-max`.
345
346 response_format: Specifies the format that the model must output. Compatible with
347 [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
348 [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4),
349 and all GPT-3.5 Turbo models since `gpt-3.5-turbo-1106`.
350
351 Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured
352 Outputs which ensures the model will match your supplied JSON schema. Learn more
353 in the
354 [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs).
355
356 Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the
357 message the model generates is valid JSON.
358
359 **Important:** when using JSON mode, you **must** also instruct the model to
360 produce JSON yourself via a system or user message. Without this, the model may
361 generate an unending stream of whitespace until the generation reaches the token
362 limit, resulting in a long-running and seemingly "stuck" request. Also note that
363 the message content may be partially cut off if `finish_reason="length"`, which
364 indicates the generation exceeded `max_tokens` or the conversation exceeded the
365 max context length.
366
367 temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
368 make the output more random, while lower values like 0.2 will make it more
369 focused and deterministic.
370
371 tool_choice: Controls which (if any) tool is called by the model. `none` means the model will
372 not call any tools and instead generates a message. `auto` is the default value
373 and means the model can pick between generating a message or calling one or more
374 tools. `required` means the model must call one or more tools before responding
375 to the user. Specifying a particular tool like `{"type": "file_search"}` or
376 `{"type": "function", "function": {"name": "my_function"}}` forces the model to
377 call that tool.
378
379 tools: Override the tools the assistant can use for this run. This is useful for
380 modifying the behavior on a per-run basis.
381
382 top_p: An alternative to sampling with temperature, called nucleus sampling, where the
383 model considers the results of the tokens with top_p probability mass. So 0.1
384 means only the tokens comprising the top 10% probability mass are considered.
385
386 We generally recommend altering this or temperature but not both.
387
388 truncation_strategy: Controls for how a thread will be truncated prior to the run. Use this to
389 control the initial context window of the run.
390
391 extra_headers: Send extra headers
392
393 extra_query: Add additional query parameters to the request
394
395 extra_body: Add additional JSON properties to the request
396
397 timeout: Override the client-level default timeout for this request, in seconds
398 """
399 ...
400
401 @overload
402 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
403 def create(
404 self,
405 thread_id: str,
406 *,
407 assistant_id: str,
408 stream: bool,
409 include: List[RunStepInclude] | Omit = omit,
410 additional_instructions: Optional[str] | Omit = omit,
411 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
412 instructions: Optional[str] | Omit = omit,
413 max_completion_tokens: Optional[int] | Omit = omit,
414 max_prompt_tokens: Optional[int] | Omit = omit,
415 metadata: Optional[Metadata] | Omit = omit,
416 model: Union[str, ChatModel, None] | Omit = omit,
417 parallel_tool_calls: bool | Omit = omit,
418 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
419 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
420 temperature: Optional[float] | Omit = omit,
421 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
422 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
423 top_p: Optional[float] | Omit = omit,
424 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
425 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
426 # The extra values given here take precedence over values defined on the client or passed to this method.
427 extra_headers: Headers | None = None,
428 extra_query: Query | None = None,
429 extra_body: Body | None = None,
430 timeout: float | httpx.Timeout | None | NotGiven = not_given,
431 ) -> Run | Stream[AssistantStreamEvent]:
432 """
433 Create a run.
434
435 Args:
436 assistant_id: The ID of the
437 [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to
438 execute this run.
439
440 stream: If `true`, returns a stream of events that happen during the Run as server-sent
441 events, terminating when the Run enters a terminal state with a `data: [DONE]`
442 message.
443
444 include: A list of additional fields to include in the response. Currently the only
445 supported value is `step_details.tool_calls[*].file_search.results[*].content`
446 to fetch the file search result content.
447
448 See the
449 [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
450 for more information.
451
452 additional_instructions: Appends additional instructions at the end of the instructions for the run. This
453 is useful for modifying the behavior on a per-run basis without overriding other
454 instructions.
455
456 additional_messages: Adds additional messages to the thread before creating the run.
457
458 instructions: Overrides the
459 [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant)
460 of the assistant. This is useful for modifying the behavior on a per-run basis.
461
462 max_completion_tokens: The maximum number of completion tokens that may be used over the course of the
463 run. The run will make a best effort to use only the number of completion tokens
464 specified, across multiple turns of the run. If the run exceeds the number of
465 completion tokens specified, the run will end with status `incomplete`. See
466 `incomplete_details` for more info.
467
468 max_prompt_tokens: The maximum number of prompt tokens that may be used over the course of the run.
469 The run will make a best effort to use only the number of prompt tokens
470 specified, across multiple turns of the run. If the run exceeds the number of
471 prompt tokens specified, the run will end with status `incomplete`. See
472 `incomplete_details` for more info.
473
474 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
475 for storing additional information about the object in a structured format, and
476 querying for objects via API or the dashboard.
477
478 Keys are strings with a maximum length of 64 characters. Values are strings with
479 a maximum length of 512 characters.
480
481 model: The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to
482 be used to execute this run. If a value is provided here, it will override the
483 model associated with the assistant. If not, the model associated with the
484 assistant will be used.
485
486 parallel_tool_calls: Whether to enable
487 [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
488 during tool use.
489
490 reasoning_effort: Constrains effort on reasoning for
491 [reasoning models](https://platform.openai.com/docs/guides/reasoning). Currently
492 supported values are `none`, `minimal`, `low`, `medium`, `high`, and `xhigh`.
493 Reducing reasoning effort can result in faster responses and fewer tokens used
494 on reasoning in a response.
495
496 - `gpt-5.1` defaults to `none`, which does not perform reasoning. The supported
497 reasoning values for `gpt-5.1` are `none`, `low`, `medium`, and `high`. Tool
498 calls are supported for all reasoning values in gpt-5.1.
499 - All models before `gpt-5.1` default to `medium` reasoning effort, and do not
500 support `none`.
501 - The `gpt-5-pro` model defaults to (and only supports) `high` reasoning effort.
502 - `xhigh` is currently only supported for `gpt-5.1-codex-max`.
503
504 response_format: Specifies the format that the model must output. Compatible with
505 [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
506 [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4),
507 and all GPT-3.5 Turbo models since `gpt-3.5-turbo-1106`.
508
509 Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured
510 Outputs which ensures the model will match your supplied JSON schema. Learn more
511 in the
512 [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs).
513
514 Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the
515 message the model generates is valid JSON.
516
517 **Important:** when using JSON mode, you **must** also instruct the model to
518 produce JSON yourself via a system or user message. Without this, the model may
519 generate an unending stream of whitespace until the generation reaches the token
520 limit, resulting in a long-running and seemingly "stuck" request. Also note that
521 the message content may be partially cut off if `finish_reason="length"`, which
522 indicates the generation exceeded `max_tokens` or the conversation exceeded the
523 max context length.
524
525 temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
526 make the output more random, while lower values like 0.2 will make it more
527 focused and deterministic.
528
529 tool_choice: Controls which (if any) tool is called by the model. `none` means the model will
530 not call any tools and instead generates a message. `auto` is the default value
531 and means the model can pick between generating a message or calling one or more
532 tools. `required` means the model must call one or more tools before responding
533 to the user. Specifying a particular tool like `{"type": "file_search"}` or
534 `{"type": "function", "function": {"name": "my_function"}}` forces the model to
535 call that tool.
536
537 tools: Override the tools the assistant can use for this run. This is useful for
538 modifying the behavior on a per-run basis.
539
540 top_p: An alternative to sampling with temperature, called nucleus sampling, where the
541 model considers the results of the tokens with top_p probability mass. So 0.1
542 means only the tokens comprising the top 10% probability mass are considered.
543
544 We generally recommend altering this or temperature but not both.
545
546 truncation_strategy: Controls for how a thread will be truncated prior to the run. Use this to
547 control the initial context window of the run.
548
549 extra_headers: Send extra headers
550
551 extra_query: Add additional query parameters to the request
552
553 extra_body: Add additional JSON properties to the request
554
555 timeout: Override the client-level default timeout for this request, in seconds
556 """
557 ...
558
559 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
560 @required_args(["assistant_id"], ["assistant_id", "stream"])
561 def create(
562 self,
563 thread_id: str,
564 *,
565 assistant_id: str,
566 include: List[RunStepInclude] | Omit = omit,
567 additional_instructions: Optional[str] | Omit = omit,
568 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
569 instructions: Optional[str] | Omit = omit,
570 max_completion_tokens: Optional[int] | Omit = omit,
571 max_prompt_tokens: Optional[int] | Omit = omit,
572 metadata: Optional[Metadata] | Omit = omit,
573 model: Union[str, ChatModel, None] | Omit = omit,
574 parallel_tool_calls: bool | Omit = omit,
575 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
576 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
577 stream: Optional[Literal[False]] | Literal[True] | Omit = omit,
578 temperature: Optional[float] | Omit = omit,
579 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
580 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
581 top_p: Optional[float] | Omit = omit,
582 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
583 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
584 # The extra values given here take precedence over values defined on the client or passed to this method.
585 extra_headers: Headers | None = None,
586 extra_query: Query | None = None,
587 extra_body: Body | None = None,
588 timeout: float | httpx.Timeout | None | NotGiven = not_given,
589 ) -> Run | Stream[AssistantStreamEvent]:
590 if not thread_id:
591 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
592 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
593 return self._post(
594 f"/threads/{thread_id}/runs",
595 body=maybe_transform(
596 {
597 "assistant_id": assistant_id,
598 "additional_instructions": additional_instructions,
599 "additional_messages": additional_messages,
600 "instructions": instructions,
601 "max_completion_tokens": max_completion_tokens,
602 "max_prompt_tokens": max_prompt_tokens,
603 "metadata": metadata,
604 "model": model,
605 "parallel_tool_calls": parallel_tool_calls,
606 "reasoning_effort": reasoning_effort,
607 "response_format": response_format,
608 "stream": stream,
609 "temperature": temperature,
610 "tool_choice": tool_choice,
611 "tools": tools,
612 "top_p": top_p,
613 "truncation_strategy": truncation_strategy,
614 },
615 run_create_params.RunCreateParamsStreaming if stream else run_create_params.RunCreateParamsNonStreaming,
616 ),
617 options=make_request_options(
618 extra_headers=extra_headers,
619 extra_query=extra_query,
620 extra_body=extra_body,
621 timeout=timeout,
622 query=maybe_transform({"include": include}, run_create_params.RunCreateParams),
623 ),
624 cast_to=Run,
625 stream=stream or False,
626 stream_cls=Stream[AssistantStreamEvent],
627 )
628
629 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
630 def retrieve(
631 self,
632 run_id: str,
633 *,
634 thread_id: str,
635 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
636 # The extra values given here take precedence over values defined on the client or passed to this method.
637 extra_headers: Headers | None = None,
638 extra_query: Query | None = None,
639 extra_body: Body | None = None,
640 timeout: float | httpx.Timeout | None | NotGiven = not_given,
641 ) -> Run:
642 """
643 Retrieves a run.
644
645 Args:
646 extra_headers: Send extra headers
647
648 extra_query: Add additional query parameters to the request
649
650 extra_body: Add additional JSON properties to the request
651
652 timeout: Override the client-level default timeout for this request, in seconds
653 """
654 if not thread_id:
655 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
656 if not run_id:
657 raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
658 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
659 return self._get(
660 f"/threads/{thread_id}/runs/{run_id}",
661 options=make_request_options(
662 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
663 ),
664 cast_to=Run,
665 )
666
667 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
668 def update(
669 self,
670 run_id: str,
671 *,
672 thread_id: str,
673 metadata: Optional[Metadata] | Omit = omit,
674 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
675 # The extra values given here take precedence over values defined on the client or passed to this method.
676 extra_headers: Headers | None = None,
677 extra_query: Query | None = None,
678 extra_body: Body | None = None,
679 timeout: float | httpx.Timeout | None | NotGiven = not_given,
680 ) -> Run:
681 """
682 Modifies a run.
683
684 Args:
685 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
686 for storing additional information about the object in a structured format, and
687 querying for objects via API or the dashboard.
688
689 Keys are strings with a maximum length of 64 characters. Values are strings with
690 a maximum length of 512 characters.
691
692 extra_headers: Send extra headers
693
694 extra_query: Add additional query parameters to the request
695
696 extra_body: Add additional JSON properties to the request
697
698 timeout: Override the client-level default timeout for this request, in seconds
699 """
700 if not thread_id:
701 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
702 if not run_id:
703 raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
704 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
705 return self._post(
706 f"/threads/{thread_id}/runs/{run_id}",
707 body=maybe_transform({"metadata": metadata}, run_update_params.RunUpdateParams),
708 options=make_request_options(
709 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
710 ),
711 cast_to=Run,
712 )
713
714 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
715 def list(
716 self,
717 thread_id: str,
718 *,
719 after: str | Omit = omit,
720 before: str | Omit = omit,
721 limit: int | Omit = omit,
722 order: Literal["asc", "desc"] | Omit = omit,
723 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
724 # The extra values given here take precedence over values defined on the client or passed to this method.
725 extra_headers: Headers | None = None,
726 extra_query: Query | None = None,
727 extra_body: Body | None = None,
728 timeout: float | httpx.Timeout | None | NotGiven = not_given,
729 ) -> SyncCursorPage[Run]:
730 """
731 Returns a list of runs belonging to a thread.
732
733 Args:
734 after: A cursor for use in pagination. `after` is an object ID that defines your place
735 in the list. For instance, if you make a list request and receive 100 objects,
736 ending with obj_foo, your subsequent call can include after=obj_foo in order to
737 fetch the next page of the list.
738
739 before: A cursor for use in pagination. `before` is an object ID that defines your place
740 in the list. For instance, if you make a list request and receive 100 objects,
741 starting with obj_foo, your subsequent call can include before=obj_foo in order
742 to fetch the previous page of the list.
743
744 limit: A limit on the number of objects to be returned. Limit can range between 1 and
745 100, and the default is 20.
746
747 order: Sort order by the `created_at` timestamp of the objects. `asc` for ascending
748 order and `desc` for descending order.
749
750 extra_headers: Send extra headers
751
752 extra_query: Add additional query parameters to the request
753
754 extra_body: Add additional JSON properties to the request
755
756 timeout: Override the client-level default timeout for this request, in seconds
757 """
758 if not thread_id:
759 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
760 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
761 return self._get_api_list(
762 f"/threads/{thread_id}/runs",
763 page=SyncCursorPage[Run],
764 options=make_request_options(
765 extra_headers=extra_headers,
766 extra_query=extra_query,
767 extra_body=extra_body,
768 timeout=timeout,
769 query=maybe_transform(
770 {
771 "after": after,
772 "before": before,
773 "limit": limit,
774 "order": order,
775 },
776 run_list_params.RunListParams,
777 ),
778 ),
779 model=Run,
780 )
781
782 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
783 def cancel(
784 self,
785 run_id: str,
786 *,
787 thread_id: str,
788 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
789 # The extra values given here take precedence over values defined on the client or passed to this method.
790 extra_headers: Headers | None = None,
791 extra_query: Query | None = None,
792 extra_body: Body | None = None,
793 timeout: float | httpx.Timeout | None | NotGiven = not_given,
794 ) -> Run:
795 """
796 Cancels a run that is `in_progress`.
797
798 Args:
799 extra_headers: Send extra headers
800
801 extra_query: Add additional query parameters to the request
802
803 extra_body: Add additional JSON properties to the request
804
805 timeout: Override the client-level default timeout for this request, in seconds
806 """
807 if not thread_id:
808 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
809 if not run_id:
810 raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
811 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
812 return self._post(
813 f"/threads/{thread_id}/runs/{run_id}/cancel",
814 options=make_request_options(
815 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
816 ),
817 cast_to=Run,
818 )
819
820 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
821 def create_and_poll(
822 self,
823 *,
824 assistant_id: str,
825 include: List[RunStepInclude] | Omit = omit,
826 additional_instructions: Optional[str] | Omit = omit,
827 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
828 instructions: Optional[str] | Omit = omit,
829 max_completion_tokens: Optional[int] | Omit = omit,
830 max_prompt_tokens: Optional[int] | Omit = omit,
831 metadata: Optional[Metadata] | Omit = omit,
832 model: Union[str, ChatModel, None] | Omit = omit,
833 parallel_tool_calls: bool | Omit = omit,
834 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
835 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
836 temperature: Optional[float] | Omit = omit,
837 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
838 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
839 top_p: Optional[float] | Omit = omit,
840 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
841 poll_interval_ms: int | Omit = omit,
842 thread_id: str,
843 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
844 # The extra values given here take precedence over values defined on the client or passed to this method.
845 extra_headers: Headers | None = None,
846 extra_query: Query | None = None,
847 extra_body: Body | None = None,
848 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
849 ) -> Run:
850 """
851 A helper to create a run an poll for a terminal state. More information on Run
852 lifecycles can be found here:
853 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
854 """
855 run = self.create( # pyright: ignore[reportDeprecated]
856 thread_id=thread_id,
857 assistant_id=assistant_id,
858 include=include,
859 additional_instructions=additional_instructions,
860 additional_messages=additional_messages,
861 instructions=instructions,
862 max_completion_tokens=max_completion_tokens,
863 max_prompt_tokens=max_prompt_tokens,
864 metadata=metadata,
865 model=model,
866 response_format=response_format,
867 temperature=temperature,
868 tool_choice=tool_choice,
869 parallel_tool_calls=parallel_tool_calls,
870 reasoning_effort=reasoning_effort,
871 # We assume we are not streaming when polling
872 stream=False,
873 tools=tools,
874 truncation_strategy=truncation_strategy,
875 top_p=top_p,
876 extra_headers=extra_headers,
877 extra_query=extra_query,
878 extra_body=extra_body,
879 timeout=timeout,
880 )
881 return self.poll( # pyright: ignore[reportDeprecated]
882 run.id,
883 thread_id=thread_id,
884 extra_headers=extra_headers,
885 extra_query=extra_query,
886 extra_body=extra_body,
887 poll_interval_ms=poll_interval_ms,
888 timeout=timeout,
889 )
890
891 @overload
892 @typing_extensions.deprecated("use `stream` instead")
893 def create_and_stream(
894 self,
895 *,
896 assistant_id: str,
897 additional_instructions: Optional[str] | Omit = omit,
898 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
899 instructions: Optional[str] | Omit = omit,
900 max_completion_tokens: Optional[int] | Omit = omit,
901 max_prompt_tokens: Optional[int] | Omit = omit,
902 metadata: Optional[Metadata] | Omit = omit,
903 model: Union[str, ChatModel, None] | Omit = omit,
904 parallel_tool_calls: bool | Omit = omit,
905 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
906 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
907 temperature: Optional[float] | Omit = omit,
908 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
909 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
910 top_p: Optional[float] | Omit = omit,
911 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
912 thread_id: str,
913 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
914 # The extra values given here take precedence over values defined on the client or passed to this method.
915 extra_headers: Headers | None = None,
916 extra_query: Query | None = None,
917 extra_body: Body | None = None,
918 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
919 ) -> AssistantStreamManager[AssistantEventHandler]:
920 """Create a Run stream"""
921 ...
922
923 @overload
924 @typing_extensions.deprecated("use `stream` instead")
925 def create_and_stream(
926 self,
927 *,
928 assistant_id: str,
929 additional_instructions: Optional[str] | Omit = omit,
930 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
931 instructions: Optional[str] | Omit = omit,
932 max_completion_tokens: Optional[int] | Omit = omit,
933 max_prompt_tokens: Optional[int] | Omit = omit,
934 metadata: Optional[Metadata] | Omit = omit,
935 model: Union[str, ChatModel, None] | Omit = omit,
936 parallel_tool_calls: bool | Omit = omit,
937 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
938 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
939 temperature: Optional[float] | Omit = omit,
940 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
941 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
942 top_p: Optional[float] | Omit = omit,
943 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
944 thread_id: str,
945 event_handler: AssistantEventHandlerT,
946 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
947 # The extra values given here take precedence over values defined on the client or passed to this method.
948 extra_headers: Headers | None = None,
949 extra_query: Query | None = None,
950 extra_body: Body | None = None,
951 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
952 ) -> AssistantStreamManager[AssistantEventHandlerT]:
953 """Create a Run stream"""
954 ...
955
956 @typing_extensions.deprecated("use `stream` instead")
957 def create_and_stream(
958 self,
959 *,
960 assistant_id: str,
961 additional_instructions: Optional[str] | Omit = omit,
962 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
963 instructions: Optional[str] | Omit = omit,
964 max_completion_tokens: Optional[int] | Omit = omit,
965 max_prompt_tokens: Optional[int] | Omit = omit,
966 metadata: Optional[Metadata] | Omit = omit,
967 model: Union[str, ChatModel, None] | Omit = omit,
968 parallel_tool_calls: bool | Omit = omit,
969 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
970 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
971 temperature: Optional[float] | Omit = omit,
972 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
973 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
974 top_p: Optional[float] | Omit = omit,
975 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
976 thread_id: str,
977 event_handler: AssistantEventHandlerT | None = None,
978 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
979 # The extra values given here take precedence over values defined on the client or passed to this method.
980 extra_headers: Headers | None = None,
981 extra_query: Query | None = None,
982 extra_body: Body | None = None,
983 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
984 ) -> AssistantStreamManager[AssistantEventHandler] | AssistantStreamManager[AssistantEventHandlerT]:
985 """Create a Run stream"""
986 if not thread_id:
987 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
988
989 extra_headers = {
990 "OpenAI-Beta": "assistants=v2",
991 "X-Stainless-Stream-Helper": "threads.runs.create_and_stream",
992 "X-Stainless-Custom-Event-Handler": "true" if event_handler else "false",
993 **(extra_headers or {}),
994 }
995 make_request = partial(
996 self._post,
997 f"/threads/{thread_id}/runs",
998 body=maybe_transform(
999 {
1000 "assistant_id": assistant_id,
1001 "additional_instructions": additional_instructions,
1002 "additional_messages": additional_messages,
1003 "instructions": instructions,
1004 "max_completion_tokens": max_completion_tokens,
1005 "max_prompt_tokens": max_prompt_tokens,
1006 "metadata": metadata,
1007 "model": model,
1008 "response_format": response_format,
1009 "temperature": temperature,
1010 "tool_choice": tool_choice,
1011 "stream": True,
1012 "tools": tools,
1013 "truncation_strategy": truncation_strategy,
1014 "parallel_tool_calls": parallel_tool_calls,
1015 "reasoning_effort": reasoning_effort,
1016 "top_p": top_p,
1017 },
1018 run_create_params.RunCreateParams,
1019 ),
1020 options=make_request_options(
1021 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
1022 ),
1023 cast_to=Run,
1024 stream=True,
1025 stream_cls=Stream[AssistantStreamEvent],
1026 )
1027 return AssistantStreamManager(make_request, event_handler=event_handler or AssistantEventHandler())
1028
1029 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1030 def poll(
1031 self,
1032 run_id: str,
1033 thread_id: str,
1034 extra_headers: Headers | None = None,
1035 extra_query: Query | None = None,
1036 extra_body: Body | None = None,
1037 timeout: float | httpx.Timeout | None | NotGiven = not_given,
1038 poll_interval_ms: int | Omit = omit,
1039 ) -> Run:
1040 """
1041 A helper to poll a run status until it reaches a terminal state. More
1042 information on Run lifecycles can be found here:
1043 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
1044 """
1045 extra_headers = {"X-Stainless-Poll-Helper": "true", **(extra_headers or {})}
1046
1047 if is_given(poll_interval_ms):
1048 extra_headers["X-Stainless-Custom-Poll-Interval"] = str(poll_interval_ms)
1049
1050 terminal_states = {"requires_action", "cancelled", "completed", "failed", "expired", "incomplete"}
1051 while True:
1052 response = self.with_raw_response.retrieve( # pyright: ignore[reportDeprecated]
1053 thread_id=thread_id,
1054 run_id=run_id,
1055 extra_headers=extra_headers,
1056 extra_body=extra_body,
1057 extra_query=extra_query,
1058 timeout=timeout,
1059 )
1060
1061 run = response.parse()
1062 # Return if we reached a terminal state
1063 if run.status in terminal_states:
1064 return run
1065
1066 if not is_given(poll_interval_ms):
1067 from_header = response.headers.get("openai-poll-after-ms")
1068 if from_header is not None:
1069 poll_interval_ms = int(from_header)
1070 else:
1071 poll_interval_ms = 1000
1072
1073 self._sleep(poll_interval_ms / 1000)
1074
1075 @overload
1076 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1077 def stream(
1078 self,
1079 *,
1080 assistant_id: str,
1081 include: List[RunStepInclude] | Omit = omit,
1082 additional_instructions: Optional[str] | Omit = omit,
1083 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
1084 instructions: Optional[str] | Omit = omit,
1085 max_completion_tokens: Optional[int] | Omit = omit,
1086 max_prompt_tokens: Optional[int] | Omit = omit,
1087 metadata: Optional[Metadata] | Omit = omit,
1088 model: Union[str, ChatModel, None] | Omit = omit,
1089 parallel_tool_calls: bool | Omit = omit,
1090 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
1091 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
1092 temperature: Optional[float] | Omit = omit,
1093 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
1094 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
1095 top_p: Optional[float] | Omit = omit,
1096 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
1097 thread_id: str,
1098 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1099 # The extra values given here take precedence over values defined on the client or passed to this method.
1100 extra_headers: Headers | None = None,
1101 extra_query: Query | None = None,
1102 extra_body: Body | None = None,
1103 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
1104 ) -> AssistantStreamManager[AssistantEventHandler]:
1105 """Create a Run stream"""
1106 ...
1107
1108 @overload
1109 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1110 def stream(
1111 self,
1112 *,
1113 assistant_id: str,
1114 include: List[RunStepInclude] | Omit = omit,
1115 additional_instructions: Optional[str] | Omit = omit,
1116 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
1117 instructions: Optional[str] | Omit = omit,
1118 max_completion_tokens: Optional[int] | Omit = omit,
1119 max_prompt_tokens: Optional[int] | Omit = omit,
1120 metadata: Optional[Metadata] | Omit = omit,
1121 model: Union[str, ChatModel, None] | Omit = omit,
1122 parallel_tool_calls: bool | Omit = omit,
1123 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
1124 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
1125 temperature: Optional[float] | Omit = omit,
1126 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
1127 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
1128 top_p: Optional[float] | Omit = omit,
1129 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
1130 thread_id: str,
1131 event_handler: AssistantEventHandlerT,
1132 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1133 # The extra values given here take precedence over values defined on the client or passed to this method.
1134 extra_headers: Headers | None = None,
1135 extra_query: Query | None = None,
1136 extra_body: Body | None = None,
1137 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
1138 ) -> AssistantStreamManager[AssistantEventHandlerT]:
1139 """Create a Run stream"""
1140 ...
1141
1142 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1143 def stream(
1144 self,
1145 *,
1146 assistant_id: str,
1147 include: List[RunStepInclude] | Omit = omit,
1148 additional_instructions: Optional[str] | Omit = omit,
1149 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
1150 instructions: Optional[str] | Omit = omit,
1151 max_completion_tokens: Optional[int] | Omit = omit,
1152 max_prompt_tokens: Optional[int] | Omit = omit,
1153 metadata: Optional[Metadata] | Omit = omit,
1154 model: Union[str, ChatModel, None] | Omit = omit,
1155 parallel_tool_calls: bool | Omit = omit,
1156 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
1157 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
1158 temperature: Optional[float] | Omit = omit,
1159 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
1160 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
1161 top_p: Optional[float] | Omit = omit,
1162 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
1163 thread_id: str,
1164 event_handler: AssistantEventHandlerT | None = None,
1165 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1166 # The extra values given here take precedence over values defined on the client or passed to this method.
1167 extra_headers: Headers | None = None,
1168 extra_query: Query | None = None,
1169 extra_body: Body | None = None,
1170 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
1171 ) -> AssistantStreamManager[AssistantEventHandler] | AssistantStreamManager[AssistantEventHandlerT]:
1172 """Create a Run stream"""
1173 if not thread_id:
1174 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
1175
1176 extra_headers = {
1177 "OpenAI-Beta": "assistants=v2",
1178 "X-Stainless-Stream-Helper": "threads.runs.create_and_stream",
1179 "X-Stainless-Custom-Event-Handler": "true" if event_handler else "false",
1180 **(extra_headers or {}),
1181 }
1182 make_request = partial(
1183 self._post,
1184 f"/threads/{thread_id}/runs",
1185 body=maybe_transform(
1186 {
1187 "assistant_id": assistant_id,
1188 "additional_instructions": additional_instructions,
1189 "additional_messages": additional_messages,
1190 "instructions": instructions,
1191 "max_completion_tokens": max_completion_tokens,
1192 "max_prompt_tokens": max_prompt_tokens,
1193 "metadata": metadata,
1194 "model": model,
1195 "response_format": response_format,
1196 "temperature": temperature,
1197 "tool_choice": tool_choice,
1198 "stream": True,
1199 "tools": tools,
1200 "parallel_tool_calls": parallel_tool_calls,
1201 "reasoning_effort": reasoning_effort,
1202 "truncation_strategy": truncation_strategy,
1203 "top_p": top_p,
1204 },
1205 run_create_params.RunCreateParams,
1206 ),
1207 options=make_request_options(
1208 extra_headers=extra_headers,
1209 extra_query=extra_query,
1210 extra_body=extra_body,
1211 timeout=timeout,
1212 query=maybe_transform({"include": include}, run_create_params.RunCreateParams),
1213 ),
1214 cast_to=Run,
1215 stream=True,
1216 stream_cls=Stream[AssistantStreamEvent],
1217 )
1218 return AssistantStreamManager(make_request, event_handler=event_handler or AssistantEventHandler())
1219
1220 @overload
1221 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1222 def submit_tool_outputs(
1223 self,
1224 run_id: str,
1225 *,
1226 thread_id: str,
1227 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
1228 stream: Optional[Literal[False]] | Omit = omit,
1229 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1230 # The extra values given here take precedence over values defined on the client or passed to this method.
1231 extra_headers: Headers | None = None,
1232 extra_query: Query | None = None,
1233 extra_body: Body | None = None,
1234 timeout: float | httpx.Timeout | None | NotGiven = not_given,
1235 ) -> Run:
1236 """
1237 When a run has the `status: "requires_action"` and `required_action.type` is
1238 `submit_tool_outputs`, this endpoint can be used to submit the outputs from the
1239 tool calls once they're all completed. All outputs must be submitted in a single
1240 request.
1241
1242 Args:
1243 tool_outputs: A list of tools for which the outputs are being submitted.
1244
1245 stream: If `true`, returns a stream of events that happen during the Run as server-sent
1246 events, terminating when the Run enters a terminal state with a `data: [DONE]`
1247 message.
1248
1249 extra_headers: Send extra headers
1250
1251 extra_query: Add additional query parameters to the request
1252
1253 extra_body: Add additional JSON properties to the request
1254
1255 timeout: Override the client-level default timeout for this request, in seconds
1256 """
1257 ...
1258
1259 @overload
1260 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1261 def submit_tool_outputs(
1262 self,
1263 run_id: str,
1264 *,
1265 thread_id: str,
1266 stream: Literal[True],
1267 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
1268 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1269 # The extra values given here take precedence over values defined on the client or passed to this method.
1270 extra_headers: Headers | None = None,
1271 extra_query: Query | None = None,
1272 extra_body: Body | None = None,
1273 timeout: float | httpx.Timeout | None | NotGiven = not_given,
1274 ) -> Stream[AssistantStreamEvent]:
1275 """
1276 When a run has the `status: "requires_action"` and `required_action.type` is
1277 `submit_tool_outputs`, this endpoint can be used to submit the outputs from the
1278 tool calls once they're all completed. All outputs must be submitted in a single
1279 request.
1280
1281 Args:
1282 stream: If `true`, returns a stream of events that happen during the Run as server-sent
1283 events, terminating when the Run enters a terminal state with a `data: [DONE]`
1284 message.
1285
1286 tool_outputs: A list of tools for which the outputs are being submitted.
1287
1288 extra_headers: Send extra headers
1289
1290 extra_query: Add additional query parameters to the request
1291
1292 extra_body: Add additional JSON properties to the request
1293
1294 timeout: Override the client-level default timeout for this request, in seconds
1295 """
1296 ...
1297
1298 @overload
1299 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1300 def submit_tool_outputs(
1301 self,
1302 run_id: str,
1303 *,
1304 thread_id: str,
1305 stream: bool,
1306 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
1307 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1308 # The extra values given here take precedence over values defined on the client or passed to this method.
1309 extra_headers: Headers | None = None,
1310 extra_query: Query | None = None,
1311 extra_body: Body | None = None,
1312 timeout: float | httpx.Timeout | None | NotGiven = not_given,
1313 ) -> Run | Stream[AssistantStreamEvent]:
1314 """
1315 When a run has the `status: "requires_action"` and `required_action.type` is
1316 `submit_tool_outputs`, this endpoint can be used to submit the outputs from the
1317 tool calls once they're all completed. All outputs must be submitted in a single
1318 request.
1319
1320 Args:
1321 stream: If `true`, returns a stream of events that happen during the Run as server-sent
1322 events, terminating when the Run enters a terminal state with a `data: [DONE]`
1323 message.
1324
1325 tool_outputs: A list of tools for which the outputs are being submitted.
1326
1327 extra_headers: Send extra headers
1328
1329 extra_query: Add additional query parameters to the request
1330
1331 extra_body: Add additional JSON properties to the request
1332
1333 timeout: Override the client-level default timeout for this request, in seconds
1334 """
1335 ...
1336
1337 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1338 @required_args(["thread_id", "tool_outputs"], ["thread_id", "stream", "tool_outputs"])
1339 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1340 def submit_tool_outputs(
1341 self,
1342 run_id: str,
1343 *,
1344 thread_id: str,
1345 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
1346 stream: Optional[Literal[False]] | Literal[True] | Omit = omit,
1347 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1348 # The extra values given here take precedence over values defined on the client or passed to this method.
1349 extra_headers: Headers | None = None,
1350 extra_query: Query | None = None,
1351 extra_body: Body | None = None,
1352 timeout: float | httpx.Timeout | None | NotGiven = not_given,
1353 ) -> Run | Stream[AssistantStreamEvent]:
1354 if not thread_id:
1355 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
1356 if not run_id:
1357 raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
1358 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
1359 return self._post(
1360 f"/threads/{thread_id}/runs/{run_id}/submit_tool_outputs",
1361 body=maybe_transform(
1362 {
1363 "tool_outputs": tool_outputs,
1364 "stream": stream,
1365 },
1366 run_submit_tool_outputs_params.RunSubmitToolOutputsParamsStreaming
1367 if stream
1368 else run_submit_tool_outputs_params.RunSubmitToolOutputsParamsNonStreaming,
1369 ),
1370 options=make_request_options(
1371 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
1372 ),
1373 cast_to=Run,
1374 stream=stream or False,
1375 stream_cls=Stream[AssistantStreamEvent],
1376 )
1377
1378 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1379 def submit_tool_outputs_and_poll(
1380 self,
1381 *,
1382 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
1383 run_id: str,
1384 thread_id: str,
1385 poll_interval_ms: int | Omit = omit,
1386 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1387 # The extra values given here take precedence over values defined on the client or passed to this method.
1388 extra_headers: Headers | None = None,
1389 extra_query: Query | None = None,
1390 extra_body: Body | None = None,
1391 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
1392 ) -> Run:
1393 """
1394 A helper to submit a tool output to a run and poll for a terminal run state.
1395 More information on Run lifecycles can be found here:
1396 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
1397 """
1398 run = self.submit_tool_outputs( # pyright: ignore[reportDeprecated]
1399 run_id=run_id,
1400 thread_id=thread_id,
1401 tool_outputs=tool_outputs,
1402 stream=False,
1403 extra_headers=extra_headers,
1404 extra_query=extra_query,
1405 extra_body=extra_body,
1406 timeout=timeout,
1407 )
1408 return self.poll( # pyright: ignore[reportDeprecated]
1409 run_id=run.id,
1410 thread_id=thread_id,
1411 extra_headers=extra_headers,
1412 extra_query=extra_query,
1413 extra_body=extra_body,
1414 timeout=timeout,
1415 poll_interval_ms=poll_interval_ms,
1416 )
1417
1418 @overload
1419 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1420 def submit_tool_outputs_stream(
1421 self,
1422 *,
1423 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
1424 run_id: str,
1425 thread_id: str,
1426 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1427 # The extra values given here take precedence over values defined on the client or passed to this method.
1428 extra_headers: Headers | None = None,
1429 extra_query: Query | None = None,
1430 extra_body: Body | None = None,
1431 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
1432 ) -> AssistantStreamManager[AssistantEventHandler]:
1433 """
1434 Submit the tool outputs from a previous run and stream the run to a terminal
1435 state. More information on Run lifecycles can be found here:
1436 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
1437 """
1438 ...
1439
1440 @overload
1441 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1442 def submit_tool_outputs_stream(
1443 self,
1444 *,
1445 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
1446 run_id: str,
1447 thread_id: str,
1448 event_handler: AssistantEventHandlerT,
1449 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1450 # The extra values given here take precedence over values defined on the client or passed to this method.
1451 extra_headers: Headers | None = None,
1452 extra_query: Query | None = None,
1453 extra_body: Body | None = None,
1454 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
1455 ) -> AssistantStreamManager[AssistantEventHandlerT]:
1456 """
1457 Submit the tool outputs from a previous run and stream the run to a terminal
1458 state. More information on Run lifecycles can be found here:
1459 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
1460 """
1461 ...
1462
1463 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1464 def submit_tool_outputs_stream(
1465 self,
1466 *,
1467 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
1468 run_id: str,
1469 thread_id: str,
1470 event_handler: AssistantEventHandlerT | None = None,
1471 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1472 # The extra values given here take precedence over values defined on the client or passed to this method.
1473 extra_headers: Headers | None = None,
1474 extra_query: Query | None = None,
1475 extra_body: Body | None = None,
1476 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
1477 ) -> AssistantStreamManager[AssistantEventHandler] | AssistantStreamManager[AssistantEventHandlerT]:
1478 """
1479 Submit the tool outputs from a previous run and stream the run to a terminal
1480 state. More information on Run lifecycles can be found here:
1481 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
1482 """
1483 if not run_id:
1484 raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
1485
1486 if not thread_id:
1487 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
1488
1489 extra_headers = {
1490 "OpenAI-Beta": "assistants=v2",
1491 "X-Stainless-Stream-Helper": "threads.runs.submit_tool_outputs_stream",
1492 "X-Stainless-Custom-Event-Handler": "true" if event_handler else "false",
1493 **(extra_headers or {}),
1494 }
1495 request = partial(
1496 self._post,
1497 f"/threads/{thread_id}/runs/{run_id}/submit_tool_outputs",
1498 body=maybe_transform(
1499 {
1500 "tool_outputs": tool_outputs,
1501 "stream": True,
1502 },
1503 run_submit_tool_outputs_params.RunSubmitToolOutputsParams,
1504 ),
1505 options=make_request_options(
1506 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
1507 ),
1508 cast_to=Run,
1509 stream=True,
1510 stream_cls=Stream[AssistantStreamEvent],
1511 )
1512 return AssistantStreamManager(request, event_handler=event_handler or AssistantEventHandler())
1513
1514
1515class AsyncRuns(AsyncAPIResource):
1516 @cached_property
1517 def steps(self) -> AsyncSteps:
1518 return AsyncSteps(self._client)
1519
1520 @cached_property
1521 def with_raw_response(self) -> AsyncRunsWithRawResponse:
1522 """
1523 This property can be used as a prefix for any HTTP method call to return
1524 the raw response object instead of the parsed content.
1525
1526 For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
1527 """
1528 return AsyncRunsWithRawResponse(self)
1529
1530 @cached_property
1531 def with_streaming_response(self) -> AsyncRunsWithStreamingResponse:
1532 """
1533 An alternative to `.with_raw_response` that doesn't eagerly read the response body.
1534
1535 For more information, see https://www.github.com/openai/openai-python#with_streaming_response
1536 """
1537 return AsyncRunsWithStreamingResponse(self)
1538
1539 @overload
1540 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1541 async def create(
1542 self,
1543 thread_id: str,
1544 *,
1545 assistant_id: str,
1546 include: List[RunStepInclude] | Omit = omit,
1547 additional_instructions: Optional[str] | Omit = omit,
1548 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
1549 instructions: Optional[str] | Omit = omit,
1550 max_completion_tokens: Optional[int] | Omit = omit,
1551 max_prompt_tokens: Optional[int] | Omit = omit,
1552 metadata: Optional[Metadata] | Omit = omit,
1553 model: Union[str, ChatModel, None] | Omit = omit,
1554 parallel_tool_calls: bool | Omit = omit,
1555 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
1556 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
1557 stream: Optional[Literal[False]] | Omit = omit,
1558 temperature: Optional[float] | Omit = omit,
1559 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
1560 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
1561 top_p: Optional[float] | Omit = omit,
1562 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
1563 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1564 # The extra values given here take precedence over values defined on the client or passed to this method.
1565 extra_headers: Headers | None = None,
1566 extra_query: Query | None = None,
1567 extra_body: Body | None = None,
1568 timeout: float | httpx.Timeout | None | NotGiven = not_given,
1569 ) -> Run:
1570 """
1571 Create a run.
1572
1573 Args:
1574 assistant_id: The ID of the
1575 [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to
1576 execute this run.
1577
1578 include: A list of additional fields to include in the response. Currently the only
1579 supported value is `step_details.tool_calls[*].file_search.results[*].content`
1580 to fetch the file search result content.
1581
1582 See the
1583 [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
1584 for more information.
1585
1586 additional_instructions: Appends additional instructions at the end of the instructions for the run. This
1587 is useful for modifying the behavior on a per-run basis without overriding other
1588 instructions.
1589
1590 additional_messages: Adds additional messages to the thread before creating the run.
1591
1592 instructions: Overrides the
1593 [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant)
1594 of the assistant. This is useful for modifying the behavior on a per-run basis.
1595
1596 max_completion_tokens: The maximum number of completion tokens that may be used over the course of the
1597 run. The run will make a best effort to use only the number of completion tokens
1598 specified, across multiple turns of the run. If the run exceeds the number of
1599 completion tokens specified, the run will end with status `incomplete`. See
1600 `incomplete_details` for more info.
1601
1602 max_prompt_tokens: The maximum number of prompt tokens that may be used over the course of the run.
1603 The run will make a best effort to use only the number of prompt tokens
1604 specified, across multiple turns of the run. If the run exceeds the number of
1605 prompt tokens specified, the run will end with status `incomplete`. See
1606 `incomplete_details` for more info.
1607
1608 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
1609 for storing additional information about the object in a structured format, and
1610 querying for objects via API or the dashboard.
1611
1612 Keys are strings with a maximum length of 64 characters. Values are strings with
1613 a maximum length of 512 characters.
1614
1615 model: The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to
1616 be used to execute this run. If a value is provided here, it will override the
1617 model associated with the assistant. If not, the model associated with the
1618 assistant will be used.
1619
1620 parallel_tool_calls: Whether to enable
1621 [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
1622 during tool use.
1623
1624 reasoning_effort: Constrains effort on reasoning for
1625 [reasoning models](https://platform.openai.com/docs/guides/reasoning). Currently
1626 supported values are `none`, `minimal`, `low`, `medium`, `high`, and `xhigh`.
1627 Reducing reasoning effort can result in faster responses and fewer tokens used
1628 on reasoning in a response.
1629
1630 - `gpt-5.1` defaults to `none`, which does not perform reasoning. The supported
1631 reasoning values for `gpt-5.1` are `none`, `low`, `medium`, and `high`. Tool
1632 calls are supported for all reasoning values in gpt-5.1.
1633 - All models before `gpt-5.1` default to `medium` reasoning effort, and do not
1634 support `none`.
1635 - The `gpt-5-pro` model defaults to (and only supports) `high` reasoning effort.
1636 - `xhigh` is currently only supported for `gpt-5.1-codex-max`.
1637
1638 response_format: Specifies the format that the model must output. Compatible with
1639 [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
1640 [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4),
1641 and all GPT-3.5 Turbo models since `gpt-3.5-turbo-1106`.
1642
1643 Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured
1644 Outputs which ensures the model will match your supplied JSON schema. Learn more
1645 in the
1646 [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs).
1647
1648 Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the
1649 message the model generates is valid JSON.
1650
1651 **Important:** when using JSON mode, you **must** also instruct the model to
1652 produce JSON yourself via a system or user message. Without this, the model may
1653 generate an unending stream of whitespace until the generation reaches the token
1654 limit, resulting in a long-running and seemingly "stuck" request. Also note that
1655 the message content may be partially cut off if `finish_reason="length"`, which
1656 indicates the generation exceeded `max_tokens` or the conversation exceeded the
1657 max context length.
1658
1659 stream: If `true`, returns a stream of events that happen during the Run as server-sent
1660 events, terminating when the Run enters a terminal state with a `data: [DONE]`
1661 message.
1662
1663 temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
1664 make the output more random, while lower values like 0.2 will make it more
1665 focused and deterministic.
1666
1667 tool_choice: Controls which (if any) tool is called by the model. `none` means the model will
1668 not call any tools and instead generates a message. `auto` is the default value
1669 and means the model can pick between generating a message or calling one or more
1670 tools. `required` means the model must call one or more tools before responding
1671 to the user. Specifying a particular tool like `{"type": "file_search"}` or
1672 `{"type": "function", "function": {"name": "my_function"}}` forces the model to
1673 call that tool.
1674
1675 tools: Override the tools the assistant can use for this run. This is useful for
1676 modifying the behavior on a per-run basis.
1677
1678 top_p: An alternative to sampling with temperature, called nucleus sampling, where the
1679 model considers the results of the tokens with top_p probability mass. So 0.1
1680 means only the tokens comprising the top 10% probability mass are considered.
1681
1682 We generally recommend altering this or temperature but not both.
1683
1684 truncation_strategy: Controls for how a thread will be truncated prior to the run. Use this to
1685 control the initial context window of the run.
1686
1687 extra_headers: Send extra headers
1688
1689 extra_query: Add additional query parameters to the request
1690
1691 extra_body: Add additional JSON properties to the request
1692
1693 timeout: Override the client-level default timeout for this request, in seconds
1694 """
1695 ...
1696
1697 @overload
1698 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1699 async def create(
1700 self,
1701 thread_id: str,
1702 *,
1703 assistant_id: str,
1704 stream: Literal[True],
1705 include: List[RunStepInclude] | Omit = omit,
1706 additional_instructions: Optional[str] | Omit = omit,
1707 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
1708 instructions: Optional[str] | Omit = omit,
1709 max_completion_tokens: Optional[int] | Omit = omit,
1710 max_prompt_tokens: Optional[int] | Omit = omit,
1711 metadata: Optional[Metadata] | Omit = omit,
1712 model: Union[str, ChatModel, None] | Omit = omit,
1713 parallel_tool_calls: bool | Omit = omit,
1714 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
1715 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
1716 temperature: Optional[float] | Omit = omit,
1717 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
1718 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
1719 top_p: Optional[float] | Omit = omit,
1720 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
1721 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1722 # The extra values given here take precedence over values defined on the client or passed to this method.
1723 extra_headers: Headers | None = None,
1724 extra_query: Query | None = None,
1725 extra_body: Body | None = None,
1726 timeout: float | httpx.Timeout | None | NotGiven = not_given,
1727 ) -> AsyncStream[AssistantStreamEvent]:
1728 """
1729 Create a run.
1730
1731 Args:
1732 assistant_id: The ID of the
1733 [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to
1734 execute this run.
1735
1736 stream: If `true`, returns a stream of events that happen during the Run as server-sent
1737 events, terminating when the Run enters a terminal state with a `data: [DONE]`
1738 message.
1739
1740 include: A list of additional fields to include in the response. Currently the only
1741 supported value is `step_details.tool_calls[*].file_search.results[*].content`
1742 to fetch the file search result content.
1743
1744 See the
1745 [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
1746 for more information.
1747
1748 additional_instructions: Appends additional instructions at the end of the instructions for the run. This
1749 is useful for modifying the behavior on a per-run basis without overriding other
1750 instructions.
1751
1752 additional_messages: Adds additional messages to the thread before creating the run.
1753
1754 instructions: Overrides the
1755 [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant)
1756 of the assistant. This is useful for modifying the behavior on a per-run basis.
1757
1758 max_completion_tokens: The maximum number of completion tokens that may be used over the course of the
1759 run. The run will make a best effort to use only the number of completion tokens
1760 specified, across multiple turns of the run. If the run exceeds the number of
1761 completion tokens specified, the run will end with status `incomplete`. See
1762 `incomplete_details` for more info.
1763
1764 max_prompt_tokens: The maximum number of prompt tokens that may be used over the course of the run.
1765 The run will make a best effort to use only the number of prompt tokens
1766 specified, across multiple turns of the run. If the run exceeds the number of
1767 prompt tokens specified, the run will end with status `incomplete`. See
1768 `incomplete_details` for more info.
1769
1770 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
1771 for storing additional information about the object in a structured format, and
1772 querying for objects via API or the dashboard.
1773
1774 Keys are strings with a maximum length of 64 characters. Values are strings with
1775 a maximum length of 512 characters.
1776
1777 model: The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to
1778 be used to execute this run. If a value is provided here, it will override the
1779 model associated with the assistant. If not, the model associated with the
1780 assistant will be used.
1781
1782 parallel_tool_calls: Whether to enable
1783 [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
1784 during tool use.
1785
1786 reasoning_effort: Constrains effort on reasoning for
1787 [reasoning models](https://platform.openai.com/docs/guides/reasoning). Currently
1788 supported values are `none`, `minimal`, `low`, `medium`, `high`, and `xhigh`.
1789 Reducing reasoning effort can result in faster responses and fewer tokens used
1790 on reasoning in a response.
1791
1792 - `gpt-5.1` defaults to `none`, which does not perform reasoning. The supported
1793 reasoning values for `gpt-5.1` are `none`, `low`, `medium`, and `high`. Tool
1794 calls are supported for all reasoning values in gpt-5.1.
1795 - All models before `gpt-5.1` default to `medium` reasoning effort, and do not
1796 support `none`.
1797 - The `gpt-5-pro` model defaults to (and only supports) `high` reasoning effort.
1798 - `xhigh` is currently only supported for `gpt-5.1-codex-max`.
1799
1800 response_format: Specifies the format that the model must output. Compatible with
1801 [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
1802 [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4),
1803 and all GPT-3.5 Turbo models since `gpt-3.5-turbo-1106`.
1804
1805 Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured
1806 Outputs which ensures the model will match your supplied JSON schema. Learn more
1807 in the
1808 [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs).
1809
1810 Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the
1811 message the model generates is valid JSON.
1812
1813 **Important:** when using JSON mode, you **must** also instruct the model to
1814 produce JSON yourself via a system or user message. Without this, the model may
1815 generate an unending stream of whitespace until the generation reaches the token
1816 limit, resulting in a long-running and seemingly "stuck" request. Also note that
1817 the message content may be partially cut off if `finish_reason="length"`, which
1818 indicates the generation exceeded `max_tokens` or the conversation exceeded the
1819 max context length.
1820
1821 temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
1822 make the output more random, while lower values like 0.2 will make it more
1823 focused and deterministic.
1824
1825 tool_choice: Controls which (if any) tool is called by the model. `none` means the model will
1826 not call any tools and instead generates a message. `auto` is the default value
1827 and means the model can pick between generating a message or calling one or more
1828 tools. `required` means the model must call one or more tools before responding
1829 to the user. Specifying a particular tool like `{"type": "file_search"}` or
1830 `{"type": "function", "function": {"name": "my_function"}}` forces the model to
1831 call that tool.
1832
1833 tools: Override the tools the assistant can use for this run. This is useful for
1834 modifying the behavior on a per-run basis.
1835
1836 top_p: An alternative to sampling with temperature, called nucleus sampling, where the
1837 model considers the results of the tokens with top_p probability mass. So 0.1
1838 means only the tokens comprising the top 10% probability mass are considered.
1839
1840 We generally recommend altering this or temperature but not both.
1841
1842 truncation_strategy: Controls for how a thread will be truncated prior to the run. Use this to
1843 control the initial context window of the run.
1844
1845 extra_headers: Send extra headers
1846
1847 extra_query: Add additional query parameters to the request
1848
1849 extra_body: Add additional JSON properties to the request
1850
1851 timeout: Override the client-level default timeout for this request, in seconds
1852 """
1853 ...
1854
1855 @overload
1856 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
1857 async def create(
1858 self,
1859 thread_id: str,
1860 *,
1861 assistant_id: str,
1862 stream: bool,
1863 include: List[RunStepInclude] | Omit = omit,
1864 additional_instructions: Optional[str] | Omit = omit,
1865 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
1866 instructions: Optional[str] | Omit = omit,
1867 max_completion_tokens: Optional[int] | Omit = omit,
1868 max_prompt_tokens: Optional[int] | Omit = omit,
1869 metadata: Optional[Metadata] | Omit = omit,
1870 model: Union[str, ChatModel, None] | Omit = omit,
1871 parallel_tool_calls: bool | Omit = omit,
1872 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
1873 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
1874 temperature: Optional[float] | Omit = omit,
1875 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
1876 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
1877 top_p: Optional[float] | Omit = omit,
1878 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
1879 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
1880 # The extra values given here take precedence over values defined on the client or passed to this method.
1881 extra_headers: Headers | None = None,
1882 extra_query: Query | None = None,
1883 extra_body: Body | None = None,
1884 timeout: float | httpx.Timeout | None | NotGiven = not_given,
1885 ) -> Run | AsyncStream[AssistantStreamEvent]:
1886 """
1887 Create a run.
1888
1889 Args:
1890 assistant_id: The ID of the
1891 [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to
1892 execute this run.
1893
1894 stream: If `true`, returns a stream of events that happen during the Run as server-sent
1895 events, terminating when the Run enters a terminal state with a `data: [DONE]`
1896 message.
1897
1898 include: A list of additional fields to include in the response. Currently the only
1899 supported value is `step_details.tool_calls[*].file_search.results[*].content`
1900 to fetch the file search result content.
1901
1902 See the
1903 [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
1904 for more information.
1905
1906 additional_instructions: Appends additional instructions at the end of the instructions for the run. This
1907 is useful for modifying the behavior on a per-run basis without overriding other
1908 instructions.
1909
1910 additional_messages: Adds additional messages to the thread before creating the run.
1911
1912 instructions: Overrides the
1913 [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant)
1914 of the assistant. This is useful for modifying the behavior on a per-run basis.
1915
1916 max_completion_tokens: The maximum number of completion tokens that may be used over the course of the
1917 run. The run will make a best effort to use only the number of completion tokens
1918 specified, across multiple turns of the run. If the run exceeds the number of
1919 completion tokens specified, the run will end with status `incomplete`. See
1920 `incomplete_details` for more info.
1921
1922 max_prompt_tokens: The maximum number of prompt tokens that may be used over the course of the run.
1923 The run will make a best effort to use only the number of prompt tokens
1924 specified, across multiple turns of the run. If the run exceeds the number of
1925 prompt tokens specified, the run will end with status `incomplete`. See
1926 `incomplete_details` for more info.
1927
1928 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
1929 for storing additional information about the object in a structured format, and
1930 querying for objects via API or the dashboard.
1931
1932 Keys are strings with a maximum length of 64 characters. Values are strings with
1933 a maximum length of 512 characters.
1934
1935 model: The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to
1936 be used to execute this run. If a value is provided here, it will override the
1937 model associated with the assistant. If not, the model associated with the
1938 assistant will be used.
1939
1940 parallel_tool_calls: Whether to enable
1941 [parallel function calling](https://platform.openai.com/docs/guides/function-calling#configuring-parallel-function-calling)
1942 during tool use.
1943
1944 reasoning_effort: Constrains effort on reasoning for
1945 [reasoning models](https://platform.openai.com/docs/guides/reasoning). Currently
1946 supported values are `none`, `minimal`, `low`, `medium`, `high`, and `xhigh`.
1947 Reducing reasoning effort can result in faster responses and fewer tokens used
1948 on reasoning in a response.
1949
1950 - `gpt-5.1` defaults to `none`, which does not perform reasoning. The supported
1951 reasoning values for `gpt-5.1` are `none`, `low`, `medium`, and `high`. Tool
1952 calls are supported for all reasoning values in gpt-5.1.
1953 - All models before `gpt-5.1` default to `medium` reasoning effort, and do not
1954 support `none`.
1955 - The `gpt-5-pro` model defaults to (and only supports) `high` reasoning effort.
1956 - `xhigh` is currently only supported for `gpt-5.1-codex-max`.
1957
1958 response_format: Specifies the format that the model must output. Compatible with
1959 [GPT-4o](https://platform.openai.com/docs/models#gpt-4o),
1960 [GPT-4 Turbo](https://platform.openai.com/docs/models#gpt-4-turbo-and-gpt-4),
1961 and all GPT-3.5 Turbo models since `gpt-3.5-turbo-1106`.
1962
1963 Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured
1964 Outputs which ensures the model will match your supplied JSON schema. Learn more
1965 in the
1966 [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs).
1967
1968 Setting to `{ "type": "json_object" }` enables JSON mode, which ensures the
1969 message the model generates is valid JSON.
1970
1971 **Important:** when using JSON mode, you **must** also instruct the model to
1972 produce JSON yourself via a system or user message. Without this, the model may
1973 generate an unending stream of whitespace until the generation reaches the token
1974 limit, resulting in a long-running and seemingly "stuck" request. Also note that
1975 the message content may be partially cut off if `finish_reason="length"`, which
1976 indicates the generation exceeded `max_tokens` or the conversation exceeded the
1977 max context length.
1978
1979 temperature: What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
1980 make the output more random, while lower values like 0.2 will make it more
1981 focused and deterministic.
1982
1983 tool_choice: Controls which (if any) tool is called by the model. `none` means the model will
1984 not call any tools and instead generates a message. `auto` is the default value
1985 and means the model can pick between generating a message or calling one or more
1986 tools. `required` means the model must call one or more tools before responding
1987 to the user. Specifying a particular tool like `{"type": "file_search"}` or
1988 `{"type": "function", "function": {"name": "my_function"}}` forces the model to
1989 call that tool.
1990
1991 tools: Override the tools the assistant can use for this run. This is useful for
1992 modifying the behavior on a per-run basis.
1993
1994 top_p: An alternative to sampling with temperature, called nucleus sampling, where the
1995 model considers the results of the tokens with top_p probability mass. So 0.1
1996 means only the tokens comprising the top 10% probability mass are considered.
1997
1998 We generally recommend altering this or temperature but not both.
1999
2000 truncation_strategy: Controls for how a thread will be truncated prior to the run. Use this to
2001 control the initial context window of the run.
2002
2003 extra_headers: Send extra headers
2004
2005 extra_query: Add additional query parameters to the request
2006
2007 extra_body: Add additional JSON properties to the request
2008
2009 timeout: Override the client-level default timeout for this request, in seconds
2010 """
2011 ...
2012
2013 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2014 @required_args(["assistant_id"], ["assistant_id", "stream"])
2015 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2016 async def create(
2017 self,
2018 thread_id: str,
2019 *,
2020 assistant_id: str,
2021 include: List[RunStepInclude] | Omit = omit,
2022 additional_instructions: Optional[str] | Omit = omit,
2023 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
2024 instructions: Optional[str] | Omit = omit,
2025 max_completion_tokens: Optional[int] | Omit = omit,
2026 max_prompt_tokens: Optional[int] | Omit = omit,
2027 metadata: Optional[Metadata] | Omit = omit,
2028 model: Union[str, ChatModel, None] | Omit = omit,
2029 parallel_tool_calls: bool | Omit = omit,
2030 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
2031 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
2032 stream: Optional[Literal[False]] | Literal[True] | Omit = omit,
2033 temperature: Optional[float] | Omit = omit,
2034 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
2035 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
2036 top_p: Optional[float] | Omit = omit,
2037 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
2038 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2039 # The extra values given here take precedence over values defined on the client or passed to this method.
2040 extra_headers: Headers | None = None,
2041 extra_query: Query | None = None,
2042 extra_body: Body | None = None,
2043 timeout: float | httpx.Timeout | None | NotGiven = not_given,
2044 ) -> Run | AsyncStream[AssistantStreamEvent]:
2045 if not thread_id:
2046 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
2047 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
2048 return await self._post(
2049 f"/threads/{thread_id}/runs",
2050 body=await async_maybe_transform(
2051 {
2052 "assistant_id": assistant_id,
2053 "additional_instructions": additional_instructions,
2054 "additional_messages": additional_messages,
2055 "instructions": instructions,
2056 "max_completion_tokens": max_completion_tokens,
2057 "max_prompt_tokens": max_prompt_tokens,
2058 "metadata": metadata,
2059 "model": model,
2060 "parallel_tool_calls": parallel_tool_calls,
2061 "reasoning_effort": reasoning_effort,
2062 "response_format": response_format,
2063 "stream": stream,
2064 "temperature": temperature,
2065 "tool_choice": tool_choice,
2066 "tools": tools,
2067 "top_p": top_p,
2068 "truncation_strategy": truncation_strategy,
2069 },
2070 run_create_params.RunCreateParamsStreaming if stream else run_create_params.RunCreateParamsNonStreaming,
2071 ),
2072 options=make_request_options(
2073 extra_headers=extra_headers,
2074 extra_query=extra_query,
2075 extra_body=extra_body,
2076 timeout=timeout,
2077 query=await async_maybe_transform({"include": include}, run_create_params.RunCreateParams),
2078 ),
2079 cast_to=Run,
2080 stream=stream or False,
2081 stream_cls=AsyncStream[AssistantStreamEvent],
2082 )
2083
2084 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2085 async def retrieve(
2086 self,
2087 run_id: str,
2088 *,
2089 thread_id: str,
2090 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2091 # The extra values given here take precedence over values defined on the client or passed to this method.
2092 extra_headers: Headers | None = None,
2093 extra_query: Query | None = None,
2094 extra_body: Body | None = None,
2095 timeout: float | httpx.Timeout | None | NotGiven = not_given,
2096 ) -> Run:
2097 """
2098 Retrieves a run.
2099
2100 Args:
2101 extra_headers: Send extra headers
2102
2103 extra_query: Add additional query parameters to the request
2104
2105 extra_body: Add additional JSON properties to the request
2106
2107 timeout: Override the client-level default timeout for this request, in seconds
2108 """
2109 if not thread_id:
2110 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
2111 if not run_id:
2112 raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
2113 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
2114 return await self._get(
2115 f"/threads/{thread_id}/runs/{run_id}",
2116 options=make_request_options(
2117 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
2118 ),
2119 cast_to=Run,
2120 )
2121
2122 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2123 async def update(
2124 self,
2125 run_id: str,
2126 *,
2127 thread_id: str,
2128 metadata: Optional[Metadata] | Omit = omit,
2129 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2130 # The extra values given here take precedence over values defined on the client or passed to this method.
2131 extra_headers: Headers | None = None,
2132 extra_query: Query | None = None,
2133 extra_body: Body | None = None,
2134 timeout: float | httpx.Timeout | None | NotGiven = not_given,
2135 ) -> Run:
2136 """
2137 Modifies a run.
2138
2139 Args:
2140 metadata: Set of 16 key-value pairs that can be attached to an object. This can be useful
2141 for storing additional information about the object in a structured format, and
2142 querying for objects via API or the dashboard.
2143
2144 Keys are strings with a maximum length of 64 characters. Values are strings with
2145 a maximum length of 512 characters.
2146
2147 extra_headers: Send extra headers
2148
2149 extra_query: Add additional query parameters to the request
2150
2151 extra_body: Add additional JSON properties to the request
2152
2153 timeout: Override the client-level default timeout for this request, in seconds
2154 """
2155 if not thread_id:
2156 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
2157 if not run_id:
2158 raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
2159 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
2160 return await self._post(
2161 f"/threads/{thread_id}/runs/{run_id}",
2162 body=await async_maybe_transform({"metadata": metadata}, run_update_params.RunUpdateParams),
2163 options=make_request_options(
2164 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
2165 ),
2166 cast_to=Run,
2167 )
2168
2169 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2170 def list(
2171 self,
2172 thread_id: str,
2173 *,
2174 after: str | Omit = omit,
2175 before: str | Omit = omit,
2176 limit: int | Omit = omit,
2177 order: Literal["asc", "desc"] | Omit = omit,
2178 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2179 # The extra values given here take precedence over values defined on the client or passed to this method.
2180 extra_headers: Headers | None = None,
2181 extra_query: Query | None = None,
2182 extra_body: Body | None = None,
2183 timeout: float | httpx.Timeout | None | NotGiven = not_given,
2184 ) -> AsyncPaginator[Run, AsyncCursorPage[Run]]:
2185 """
2186 Returns a list of runs belonging to a thread.
2187
2188 Args:
2189 after: A cursor for use in pagination. `after` is an object ID that defines your place
2190 in the list. For instance, if you make a list request and receive 100 objects,
2191 ending with obj_foo, your subsequent call can include after=obj_foo in order to
2192 fetch the next page of the list.
2193
2194 before: A cursor for use in pagination. `before` is an object ID that defines your place
2195 in the list. For instance, if you make a list request and receive 100 objects,
2196 starting with obj_foo, your subsequent call can include before=obj_foo in order
2197 to fetch the previous page of the list.
2198
2199 limit: A limit on the number of objects to be returned. Limit can range between 1 and
2200 100, and the default is 20.
2201
2202 order: Sort order by the `created_at` timestamp of the objects. `asc` for ascending
2203 order and `desc` for descending order.
2204
2205 extra_headers: Send extra headers
2206
2207 extra_query: Add additional query parameters to the request
2208
2209 extra_body: Add additional JSON properties to the request
2210
2211 timeout: Override the client-level default timeout for this request, in seconds
2212 """
2213 if not thread_id:
2214 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
2215 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
2216 return self._get_api_list(
2217 f"/threads/{thread_id}/runs",
2218 page=AsyncCursorPage[Run],
2219 options=make_request_options(
2220 extra_headers=extra_headers,
2221 extra_query=extra_query,
2222 extra_body=extra_body,
2223 timeout=timeout,
2224 query=maybe_transform(
2225 {
2226 "after": after,
2227 "before": before,
2228 "limit": limit,
2229 "order": order,
2230 },
2231 run_list_params.RunListParams,
2232 ),
2233 ),
2234 model=Run,
2235 )
2236
2237 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2238 async def cancel(
2239 self,
2240 run_id: str,
2241 *,
2242 thread_id: str,
2243 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2244 # The extra values given here take precedence over values defined on the client or passed to this method.
2245 extra_headers: Headers | None = None,
2246 extra_query: Query | None = None,
2247 extra_body: Body | None = None,
2248 timeout: float | httpx.Timeout | None | NotGiven = not_given,
2249 ) -> Run:
2250 """
2251 Cancels a run that is `in_progress`.
2252
2253 Args:
2254 extra_headers: Send extra headers
2255
2256 extra_query: Add additional query parameters to the request
2257
2258 extra_body: Add additional JSON properties to the request
2259
2260 timeout: Override the client-level default timeout for this request, in seconds
2261 """
2262 if not thread_id:
2263 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
2264 if not run_id:
2265 raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
2266 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
2267 return await self._post(
2268 f"/threads/{thread_id}/runs/{run_id}/cancel",
2269 options=make_request_options(
2270 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
2271 ),
2272 cast_to=Run,
2273 )
2274
2275 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2276 async def create_and_poll(
2277 self,
2278 *,
2279 assistant_id: str,
2280 include: List[RunStepInclude] | Omit = omit,
2281 additional_instructions: Optional[str] | Omit = omit,
2282 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
2283 instructions: Optional[str] | Omit = omit,
2284 max_completion_tokens: Optional[int] | Omit = omit,
2285 max_prompt_tokens: Optional[int] | Omit = omit,
2286 metadata: Optional[Metadata] | Omit = omit,
2287 model: Union[str, ChatModel, None] | Omit = omit,
2288 parallel_tool_calls: bool | Omit = omit,
2289 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
2290 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
2291 temperature: Optional[float] | Omit = omit,
2292 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
2293 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
2294 top_p: Optional[float] | Omit = omit,
2295 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
2296 poll_interval_ms: int | Omit = omit,
2297 thread_id: str,
2298 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2299 # The extra values given here take precedence over values defined on the client or passed to this method.
2300 extra_headers: Headers | None = None,
2301 extra_query: Query | None = None,
2302 extra_body: Body | None = None,
2303 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
2304 ) -> Run:
2305 """
2306 A helper to create a run an poll for a terminal state. More information on Run
2307 lifecycles can be found here:
2308 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
2309 """
2310 run = await self.create( # pyright: ignore[reportDeprecated]
2311 thread_id=thread_id,
2312 assistant_id=assistant_id,
2313 include=include,
2314 additional_instructions=additional_instructions,
2315 additional_messages=additional_messages,
2316 instructions=instructions,
2317 max_completion_tokens=max_completion_tokens,
2318 max_prompt_tokens=max_prompt_tokens,
2319 metadata=metadata,
2320 model=model,
2321 response_format=response_format,
2322 temperature=temperature,
2323 tool_choice=tool_choice,
2324 parallel_tool_calls=parallel_tool_calls,
2325 reasoning_effort=reasoning_effort,
2326 # We assume we are not streaming when polling
2327 stream=False,
2328 tools=tools,
2329 truncation_strategy=truncation_strategy,
2330 top_p=top_p,
2331 extra_headers=extra_headers,
2332 extra_query=extra_query,
2333 extra_body=extra_body,
2334 timeout=timeout,
2335 )
2336 return await self.poll( # pyright: ignore[reportDeprecated]
2337 run.id,
2338 thread_id=thread_id,
2339 extra_headers=extra_headers,
2340 extra_query=extra_query,
2341 extra_body=extra_body,
2342 poll_interval_ms=poll_interval_ms,
2343 timeout=timeout,
2344 )
2345
2346 @overload
2347 @typing_extensions.deprecated("use `stream` instead")
2348 def create_and_stream(
2349 self,
2350 *,
2351 assistant_id: str,
2352 additional_instructions: Optional[str] | Omit = omit,
2353 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
2354 instructions: Optional[str] | Omit = omit,
2355 max_completion_tokens: Optional[int] | Omit = omit,
2356 max_prompt_tokens: Optional[int] | Omit = omit,
2357 metadata: Optional[Metadata] | Omit = omit,
2358 model: Union[str, ChatModel, None] | Omit = omit,
2359 parallel_tool_calls: bool | Omit = omit,
2360 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
2361 temperature: Optional[float] | Omit = omit,
2362 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
2363 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
2364 top_p: Optional[float] | Omit = omit,
2365 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
2366 thread_id: str,
2367 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2368 # The extra values given here take precedence over values defined on the client or passed to this method.
2369 extra_headers: Headers | None = None,
2370 extra_query: Query | None = None,
2371 extra_body: Body | None = None,
2372 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
2373 ) -> AsyncAssistantStreamManager[AsyncAssistantEventHandler]:
2374 """Create a Run stream"""
2375 ...
2376
2377 @overload
2378 @typing_extensions.deprecated("use `stream` instead")
2379 def create_and_stream(
2380 self,
2381 *,
2382 assistant_id: str,
2383 additional_instructions: Optional[str] | Omit = omit,
2384 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
2385 instructions: Optional[str] | Omit = omit,
2386 max_completion_tokens: Optional[int] | Omit = omit,
2387 max_prompt_tokens: Optional[int] | Omit = omit,
2388 metadata: Optional[Metadata] | Omit = omit,
2389 model: Union[str, ChatModel, None] | Omit = omit,
2390 parallel_tool_calls: bool | Omit = omit,
2391 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
2392 temperature: Optional[float] | Omit = omit,
2393 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
2394 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
2395 top_p: Optional[float] | Omit = omit,
2396 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
2397 thread_id: str,
2398 event_handler: AsyncAssistantEventHandlerT,
2399 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2400 # The extra values given here take precedence over values defined on the client or passed to this method.
2401 extra_headers: Headers | None = None,
2402 extra_query: Query | None = None,
2403 extra_body: Body | None = None,
2404 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
2405 ) -> AsyncAssistantStreamManager[AsyncAssistantEventHandlerT]:
2406 """Create a Run stream"""
2407 ...
2408
2409 @typing_extensions.deprecated("use `stream` instead")
2410 def create_and_stream(
2411 self,
2412 *,
2413 assistant_id: str,
2414 additional_instructions: Optional[str] | Omit = omit,
2415 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
2416 instructions: Optional[str] | Omit = omit,
2417 max_completion_tokens: Optional[int] | Omit = omit,
2418 max_prompt_tokens: Optional[int] | Omit = omit,
2419 metadata: Optional[Metadata] | Omit = omit,
2420 model: Union[str, ChatModel, None] | Omit = omit,
2421 parallel_tool_calls: bool | Omit = omit,
2422 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
2423 temperature: Optional[float] | Omit = omit,
2424 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
2425 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
2426 top_p: Optional[float] | Omit = omit,
2427 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
2428 thread_id: str,
2429 event_handler: AsyncAssistantEventHandlerT | None = None,
2430 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2431 # The extra values given here take precedence over values defined on the client or passed to this method.
2432 extra_headers: Headers | None = None,
2433 extra_query: Query | None = None,
2434 extra_body: Body | None = None,
2435 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
2436 ) -> (
2437 AsyncAssistantStreamManager[AsyncAssistantEventHandler]
2438 | AsyncAssistantStreamManager[AsyncAssistantEventHandlerT]
2439 ):
2440 """Create a Run stream"""
2441 if not thread_id:
2442 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
2443
2444 extra_headers = {
2445 "OpenAI-Beta": "assistants=v2",
2446 "X-Stainless-Stream-Helper": "threads.runs.create_and_stream",
2447 "X-Stainless-Custom-Event-Handler": "true" if event_handler else "false",
2448 **(extra_headers or {}),
2449 }
2450 request = self._post(
2451 f"/threads/{thread_id}/runs",
2452 body=maybe_transform(
2453 {
2454 "assistant_id": assistant_id,
2455 "additional_instructions": additional_instructions,
2456 "additional_messages": additional_messages,
2457 "instructions": instructions,
2458 "max_completion_tokens": max_completion_tokens,
2459 "max_prompt_tokens": max_prompt_tokens,
2460 "metadata": metadata,
2461 "model": model,
2462 "response_format": response_format,
2463 "temperature": temperature,
2464 "tool_choice": tool_choice,
2465 "stream": True,
2466 "tools": tools,
2467 "truncation_strategy": truncation_strategy,
2468 "top_p": top_p,
2469 "parallel_tool_calls": parallel_tool_calls,
2470 },
2471 run_create_params.RunCreateParams,
2472 ),
2473 options=make_request_options(
2474 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
2475 ),
2476 cast_to=Run,
2477 stream=True,
2478 stream_cls=AsyncStream[AssistantStreamEvent],
2479 )
2480 return AsyncAssistantStreamManager(request, event_handler=event_handler or AsyncAssistantEventHandler())
2481
2482 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2483 async def poll(
2484 self,
2485 run_id: str,
2486 thread_id: str,
2487 extra_headers: Headers | None = None,
2488 extra_query: Query | None = None,
2489 extra_body: Body | None = None,
2490 timeout: float | httpx.Timeout | None | NotGiven = not_given,
2491 poll_interval_ms: int | Omit = omit,
2492 ) -> Run:
2493 """
2494 A helper to poll a run status until it reaches a terminal state. More
2495 information on Run lifecycles can be found here:
2496 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
2497 """
2498 extra_headers = {"X-Stainless-Poll-Helper": "true", **(extra_headers or {})}
2499
2500 if is_given(poll_interval_ms):
2501 extra_headers["X-Stainless-Custom-Poll-Interval"] = str(poll_interval_ms)
2502
2503 terminal_states = {"requires_action", "cancelled", "completed", "failed", "expired", "incomplete"}
2504 while True:
2505 response = await self.with_raw_response.retrieve( # pyright: ignore[reportDeprecated]
2506 thread_id=thread_id,
2507 run_id=run_id,
2508 extra_headers=extra_headers,
2509 extra_body=extra_body,
2510 extra_query=extra_query,
2511 timeout=timeout,
2512 )
2513
2514 run = response.parse()
2515 # Return if we reached a terminal state
2516 if run.status in terminal_states:
2517 return run
2518
2519 if not is_given(poll_interval_ms):
2520 from_header = response.headers.get("openai-poll-after-ms")
2521 if from_header is not None:
2522 poll_interval_ms = int(from_header)
2523 else:
2524 poll_interval_ms = 1000
2525
2526 await self._sleep(poll_interval_ms / 1000)
2527
2528 @overload
2529 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2530 def stream(
2531 self,
2532 *,
2533 assistant_id: str,
2534 additional_instructions: Optional[str] | Omit = omit,
2535 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
2536 instructions: Optional[str] | Omit = omit,
2537 max_completion_tokens: Optional[int] | Omit = omit,
2538 max_prompt_tokens: Optional[int] | Omit = omit,
2539 metadata: Optional[Metadata] | Omit = omit,
2540 model: Union[str, ChatModel, None] | Omit = omit,
2541 parallel_tool_calls: bool | Omit = omit,
2542 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
2543 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
2544 temperature: Optional[float] | Omit = omit,
2545 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
2546 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
2547 top_p: Optional[float] | Omit = omit,
2548 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
2549 thread_id: str,
2550 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2551 # The extra values given here take precedence over values defined on the client or passed to this method.
2552 extra_headers: Headers | None = None,
2553 extra_query: Query | None = None,
2554 extra_body: Body | None = None,
2555 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
2556 ) -> AsyncAssistantStreamManager[AsyncAssistantEventHandler]:
2557 """Create a Run stream"""
2558 ...
2559
2560 @overload
2561 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2562 def stream(
2563 self,
2564 *,
2565 assistant_id: str,
2566 include: List[RunStepInclude] | Omit = omit,
2567 additional_instructions: Optional[str] | Omit = omit,
2568 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
2569 instructions: Optional[str] | Omit = omit,
2570 max_completion_tokens: Optional[int] | Omit = omit,
2571 max_prompt_tokens: Optional[int] | Omit = omit,
2572 metadata: Optional[Metadata] | Omit = omit,
2573 model: Union[str, ChatModel, None] | Omit = omit,
2574 parallel_tool_calls: bool | Omit = omit,
2575 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
2576 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
2577 temperature: Optional[float] | Omit = omit,
2578 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
2579 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
2580 top_p: Optional[float] | Omit = omit,
2581 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
2582 thread_id: str,
2583 event_handler: AsyncAssistantEventHandlerT,
2584 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2585 # The extra values given here take precedence over values defined on the client or passed to this method.
2586 extra_headers: Headers | None = None,
2587 extra_query: Query | None = None,
2588 extra_body: Body | None = None,
2589 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
2590 ) -> AsyncAssistantStreamManager[AsyncAssistantEventHandlerT]:
2591 """Create a Run stream"""
2592 ...
2593
2594 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2595 def stream(
2596 self,
2597 *,
2598 assistant_id: str,
2599 include: List[RunStepInclude] | Omit = omit,
2600 additional_instructions: Optional[str] | Omit = omit,
2601 additional_messages: Optional[Iterable[run_create_params.AdditionalMessage]] | Omit = omit,
2602 instructions: Optional[str] | Omit = omit,
2603 max_completion_tokens: Optional[int] | Omit = omit,
2604 max_prompt_tokens: Optional[int] | Omit = omit,
2605 metadata: Optional[Metadata] | Omit = omit,
2606 model: Union[str, ChatModel, None] | Omit = omit,
2607 parallel_tool_calls: bool | Omit = omit,
2608 reasoning_effort: Optional[ReasoningEffort] | Omit = omit,
2609 response_format: Optional[AssistantResponseFormatOptionParam] | Omit = omit,
2610 temperature: Optional[float] | Omit = omit,
2611 tool_choice: Optional[AssistantToolChoiceOptionParam] | Omit = omit,
2612 tools: Optional[Iterable[AssistantToolParam]] | Omit = omit,
2613 top_p: Optional[float] | Omit = omit,
2614 truncation_strategy: Optional[run_create_params.TruncationStrategy] | Omit = omit,
2615 thread_id: str,
2616 event_handler: AsyncAssistantEventHandlerT | None = None,
2617 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2618 # The extra values given here take precedence over values defined on the client or passed to this method.
2619 extra_headers: Headers | None = None,
2620 extra_query: Query | None = None,
2621 extra_body: Body | None = None,
2622 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
2623 ) -> (
2624 AsyncAssistantStreamManager[AsyncAssistantEventHandler]
2625 | AsyncAssistantStreamManager[AsyncAssistantEventHandlerT]
2626 ):
2627 """Create a Run stream"""
2628 if not thread_id:
2629 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
2630
2631 extra_headers = {
2632 "OpenAI-Beta": "assistants=v2",
2633 "X-Stainless-Stream-Helper": "threads.runs.create_and_stream",
2634 "X-Stainless-Custom-Event-Handler": "true" if event_handler else "false",
2635 **(extra_headers or {}),
2636 }
2637 request = self._post(
2638 f"/threads/{thread_id}/runs",
2639 body=maybe_transform(
2640 {
2641 "assistant_id": assistant_id,
2642 "additional_instructions": additional_instructions,
2643 "additional_messages": additional_messages,
2644 "instructions": instructions,
2645 "max_completion_tokens": max_completion_tokens,
2646 "max_prompt_tokens": max_prompt_tokens,
2647 "metadata": metadata,
2648 "model": model,
2649 "response_format": response_format,
2650 "temperature": temperature,
2651 "tool_choice": tool_choice,
2652 "stream": True,
2653 "tools": tools,
2654 "parallel_tool_calls": parallel_tool_calls,
2655 "reasoning_effort": reasoning_effort,
2656 "truncation_strategy": truncation_strategy,
2657 "top_p": top_p,
2658 },
2659 run_create_params.RunCreateParams,
2660 ),
2661 options=make_request_options(
2662 extra_headers=extra_headers,
2663 extra_query=extra_query,
2664 extra_body=extra_body,
2665 timeout=timeout,
2666 query=maybe_transform({"include": include}, run_create_params.RunCreateParams),
2667 ),
2668 cast_to=Run,
2669 stream=True,
2670 stream_cls=AsyncStream[AssistantStreamEvent],
2671 )
2672 return AsyncAssistantStreamManager(request, event_handler=event_handler or AsyncAssistantEventHandler())
2673
2674 @overload
2675 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2676 async def submit_tool_outputs(
2677 self,
2678 run_id: str,
2679 *,
2680 thread_id: str,
2681 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
2682 stream: Optional[Literal[False]] | Omit = omit,
2683 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2684 # The extra values given here take precedence over values defined on the client or passed to this method.
2685 extra_headers: Headers | None = None,
2686 extra_query: Query | None = None,
2687 extra_body: Body | None = None,
2688 timeout: float | httpx.Timeout | None | NotGiven = not_given,
2689 ) -> Run:
2690 """
2691 When a run has the `status: "requires_action"` and `required_action.type` is
2692 `submit_tool_outputs`, this endpoint can be used to submit the outputs from the
2693 tool calls once they're all completed. All outputs must be submitted in a single
2694 request.
2695
2696 Args:
2697 tool_outputs: A list of tools for which the outputs are being submitted.
2698
2699 stream: If `true`, returns a stream of events that happen during the Run as server-sent
2700 events, terminating when the Run enters a terminal state with a `data: [DONE]`
2701 message.
2702
2703 extra_headers: Send extra headers
2704
2705 extra_query: Add additional query parameters to the request
2706
2707 extra_body: Add additional JSON properties to the request
2708
2709 timeout: Override the client-level default timeout for this request, in seconds
2710 """
2711 ...
2712
2713 @overload
2714 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2715 async def submit_tool_outputs(
2716 self,
2717 run_id: str,
2718 *,
2719 thread_id: str,
2720 stream: Literal[True],
2721 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
2722 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2723 # The extra values given here take precedence over values defined on the client or passed to this method.
2724 extra_headers: Headers | None = None,
2725 extra_query: Query | None = None,
2726 extra_body: Body | None = None,
2727 timeout: float | httpx.Timeout | None | NotGiven = not_given,
2728 ) -> AsyncStream[AssistantStreamEvent]:
2729 """
2730 When a run has the `status: "requires_action"` and `required_action.type` is
2731 `submit_tool_outputs`, this endpoint can be used to submit the outputs from the
2732 tool calls once they're all completed. All outputs must be submitted in a single
2733 request.
2734
2735 Args:
2736 stream: If `true`, returns a stream of events that happen during the Run as server-sent
2737 events, terminating when the Run enters a terminal state with a `data: [DONE]`
2738 message.
2739
2740 tool_outputs: A list of tools for which the outputs are being submitted.
2741
2742 extra_headers: Send extra headers
2743
2744 extra_query: Add additional query parameters to the request
2745
2746 extra_body: Add additional JSON properties to the request
2747
2748 timeout: Override the client-level default timeout for this request, in seconds
2749 """
2750 ...
2751
2752 @overload
2753 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2754 async def submit_tool_outputs(
2755 self,
2756 run_id: str,
2757 *,
2758 thread_id: str,
2759 stream: bool,
2760 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
2761 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2762 # The extra values given here take precedence over values defined on the client or passed to this method.
2763 extra_headers: Headers | None = None,
2764 extra_query: Query | None = None,
2765 extra_body: Body | None = None,
2766 timeout: float | httpx.Timeout | None | NotGiven = not_given,
2767 ) -> Run | AsyncStream[AssistantStreamEvent]:
2768 """
2769 When a run has the `status: "requires_action"` and `required_action.type` is
2770 `submit_tool_outputs`, this endpoint can be used to submit the outputs from the
2771 tool calls once they're all completed. All outputs must be submitted in a single
2772 request.
2773
2774 Args:
2775 stream: If `true`, returns a stream of events that happen during the Run as server-sent
2776 events, terminating when the Run enters a terminal state with a `data: [DONE]`
2777 message.
2778
2779 tool_outputs: A list of tools for which the outputs are being submitted.
2780
2781 extra_headers: Send extra headers
2782
2783 extra_query: Add additional query parameters to the request
2784
2785 extra_body: Add additional JSON properties to the request
2786
2787 timeout: Override the client-level default timeout for this request, in seconds
2788 """
2789 ...
2790
2791 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2792 @required_args(["thread_id", "tool_outputs"], ["thread_id", "stream", "tool_outputs"])
2793 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2794 async def submit_tool_outputs(
2795 self,
2796 run_id: str,
2797 *,
2798 thread_id: str,
2799 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
2800 stream: Optional[Literal[False]] | Literal[True] | Omit = omit,
2801 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2802 # The extra values given here take precedence over values defined on the client or passed to this method.
2803 extra_headers: Headers | None = None,
2804 extra_query: Query | None = None,
2805 extra_body: Body | None = None,
2806 timeout: float | httpx.Timeout | None | NotGiven = not_given,
2807 ) -> Run | AsyncStream[AssistantStreamEvent]:
2808 if not thread_id:
2809 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
2810 if not run_id:
2811 raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
2812 extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
2813 return await self._post(
2814 f"/threads/{thread_id}/runs/{run_id}/submit_tool_outputs",
2815 body=await async_maybe_transform(
2816 {
2817 "tool_outputs": tool_outputs,
2818 "stream": stream,
2819 },
2820 run_submit_tool_outputs_params.RunSubmitToolOutputsParamsStreaming
2821 if stream
2822 else run_submit_tool_outputs_params.RunSubmitToolOutputsParamsNonStreaming,
2823 ),
2824 options=make_request_options(
2825 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
2826 ),
2827 cast_to=Run,
2828 stream=stream or False,
2829 stream_cls=AsyncStream[AssistantStreamEvent],
2830 )
2831
2832 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2833 async def submit_tool_outputs_and_poll(
2834 self,
2835 *,
2836 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
2837 run_id: str,
2838 thread_id: str,
2839 poll_interval_ms: int | Omit = omit,
2840 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2841 # The extra values given here take precedence over values defined on the client or passed to this method.
2842 extra_headers: Headers | None = None,
2843 extra_query: Query | None = None,
2844 extra_body: Body | None = None,
2845 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
2846 ) -> Run:
2847 """
2848 A helper to submit a tool output to a run and poll for a terminal run state.
2849 More information on Run lifecycles can be found here:
2850 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
2851 """
2852 run = await self.submit_tool_outputs( # pyright: ignore[reportDeprecated]
2853 run_id=run_id,
2854 thread_id=thread_id,
2855 tool_outputs=tool_outputs,
2856 stream=False,
2857 extra_headers=extra_headers,
2858 extra_query=extra_query,
2859 extra_body=extra_body,
2860 timeout=timeout,
2861 )
2862 return await self.poll( # pyright: ignore[reportDeprecated]
2863 run_id=run.id,
2864 thread_id=thread_id,
2865 extra_headers=extra_headers,
2866 extra_query=extra_query,
2867 extra_body=extra_body,
2868 timeout=timeout,
2869 poll_interval_ms=poll_interval_ms,
2870 )
2871
2872 @overload
2873 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2874 def submit_tool_outputs_stream(
2875 self,
2876 *,
2877 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
2878 run_id: str,
2879 thread_id: str,
2880 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2881 # The extra values given here take precedence over values defined on the client or passed to this method.
2882 extra_headers: Headers | None = None,
2883 extra_query: Query | None = None,
2884 extra_body: Body | None = None,
2885 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
2886 ) -> AsyncAssistantStreamManager[AsyncAssistantEventHandler]:
2887 """
2888 Submit the tool outputs from a previous run and stream the run to a terminal
2889 state. More information on Run lifecycles can be found here:
2890 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
2891 """
2892 ...
2893
2894 @overload
2895 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2896 def submit_tool_outputs_stream(
2897 self,
2898 *,
2899 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
2900 run_id: str,
2901 thread_id: str,
2902 event_handler: AsyncAssistantEventHandlerT,
2903 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2904 # The extra values given here take precedence over values defined on the client or passed to this method.
2905 extra_headers: Headers | None = None,
2906 extra_query: Query | None = None,
2907 extra_body: Body | None = None,
2908 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
2909 ) -> AsyncAssistantStreamManager[AsyncAssistantEventHandlerT]:
2910 """
2911 Submit the tool outputs from a previous run and stream the run to a terminal
2912 state. More information on Run lifecycles can be found here:
2913 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
2914 """
2915 ...
2916
2917 @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
2918 def submit_tool_outputs_stream(
2919 self,
2920 *,
2921 tool_outputs: Iterable[run_submit_tool_outputs_params.ToolOutput],
2922 run_id: str,
2923 thread_id: str,
2924 event_handler: AsyncAssistantEventHandlerT | None = None,
2925 # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
2926 # The extra values given here take precedence over values defined on the client or passed to this method.
2927 extra_headers: Headers | None = None,
2928 extra_query: Query | None = None,
2929 extra_body: Body | None = None,
2930 timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
2931 ) -> (
2932 AsyncAssistantStreamManager[AsyncAssistantEventHandler]
2933 | AsyncAssistantStreamManager[AsyncAssistantEventHandlerT]
2934 ):
2935 """
2936 Submit the tool outputs from a previous run and stream the run to a terminal
2937 state. More information on Run lifecycles can be found here:
2938 https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
2939 """
2940 if not run_id:
2941 raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
2942
2943 if not thread_id:
2944 raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
2945
2946 extra_headers = {
2947 "OpenAI-Beta": "assistants=v2",
2948 "X-Stainless-Stream-Helper": "threads.runs.submit_tool_outputs_stream",
2949 "X-Stainless-Custom-Event-Handler": "true" if event_handler else "false",
2950 **(extra_headers or {}),
2951 }
2952 request = self._post(
2953 f"/threads/{thread_id}/runs/{run_id}/submit_tool_outputs",
2954 body=maybe_transform(
2955 {
2956 "tool_outputs": tool_outputs,
2957 "stream": True,
2958 },
2959 run_submit_tool_outputs_params.RunSubmitToolOutputsParams,
2960 ),
2961 options=make_request_options(
2962 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
2963 ),
2964 cast_to=Run,
2965 stream=True,
2966 stream_cls=AsyncStream[AssistantStreamEvent],
2967 )
2968 return AsyncAssistantStreamManager(request, event_handler=event_handler or AsyncAssistantEventHandler())
2969
2970
2971class RunsWithRawResponse:
2972 def __init__(self, runs: Runs) -> None:
2973 self._runs = runs
2974
2975 self.create = ( # pyright: ignore[reportDeprecated]
2976 _legacy_response.to_raw_response_wrapper(
2977 runs.create, # pyright: ignore[reportDeprecated],
2978 )
2979 )
2980 self.retrieve = ( # pyright: ignore[reportDeprecated]
2981 _legacy_response.to_raw_response_wrapper(
2982 runs.retrieve, # pyright: ignore[reportDeprecated],
2983 )
2984 )
2985 self.update = ( # pyright: ignore[reportDeprecated]
2986 _legacy_response.to_raw_response_wrapper(
2987 runs.update, # pyright: ignore[reportDeprecated],
2988 )
2989 )
2990 self.list = ( # pyright: ignore[reportDeprecated]
2991 _legacy_response.to_raw_response_wrapper(
2992 runs.list, # pyright: ignore[reportDeprecated],
2993 )
2994 )
2995 self.cancel = ( # pyright: ignore[reportDeprecated]
2996 _legacy_response.to_raw_response_wrapper(
2997 runs.cancel, # pyright: ignore[reportDeprecated],
2998 )
2999 )
3000 self.submit_tool_outputs = ( # pyright: ignore[reportDeprecated]
3001 _legacy_response.to_raw_response_wrapper(
3002 runs.submit_tool_outputs, # pyright: ignore[reportDeprecated],
3003 )
3004 )
3005
3006 @cached_property
3007 def steps(self) -> StepsWithRawResponse:
3008 return StepsWithRawResponse(self._runs.steps)
3009
3010
3011class AsyncRunsWithRawResponse:
3012 def __init__(self, runs: AsyncRuns) -> None:
3013 self._runs = runs
3014
3015 self.create = ( # pyright: ignore[reportDeprecated]
3016 _legacy_response.async_to_raw_response_wrapper(
3017 runs.create, # pyright: ignore[reportDeprecated],
3018 )
3019 )
3020 self.retrieve = ( # pyright: ignore[reportDeprecated]
3021 _legacy_response.async_to_raw_response_wrapper(
3022 runs.retrieve, # pyright: ignore[reportDeprecated],
3023 )
3024 )
3025 self.update = ( # pyright: ignore[reportDeprecated]
3026 _legacy_response.async_to_raw_response_wrapper(
3027 runs.update, # pyright: ignore[reportDeprecated],
3028 )
3029 )
3030 self.list = ( # pyright: ignore[reportDeprecated]
3031 _legacy_response.async_to_raw_response_wrapper(
3032 runs.list, # pyright: ignore[reportDeprecated],
3033 )
3034 )
3035 self.cancel = ( # pyright: ignore[reportDeprecated]
3036 _legacy_response.async_to_raw_response_wrapper(
3037 runs.cancel, # pyright: ignore[reportDeprecated],
3038 )
3039 )
3040 self.submit_tool_outputs = ( # pyright: ignore[reportDeprecated]
3041 _legacy_response.async_to_raw_response_wrapper(
3042 runs.submit_tool_outputs, # pyright: ignore[reportDeprecated],
3043 )
3044 )
3045
3046 @cached_property
3047 def steps(self) -> AsyncStepsWithRawResponse:
3048 return AsyncStepsWithRawResponse(self._runs.steps)
3049
3050
3051class RunsWithStreamingResponse:
3052 def __init__(self, runs: Runs) -> None:
3053 self._runs = runs
3054
3055 self.create = ( # pyright: ignore[reportDeprecated]
3056 to_streamed_response_wrapper(
3057 runs.create, # pyright: ignore[reportDeprecated],
3058 )
3059 )
3060 self.retrieve = ( # pyright: ignore[reportDeprecated]
3061 to_streamed_response_wrapper(
3062 runs.retrieve, # pyright: ignore[reportDeprecated],
3063 )
3064 )
3065 self.update = ( # pyright: ignore[reportDeprecated]
3066 to_streamed_response_wrapper(
3067 runs.update, # pyright: ignore[reportDeprecated],
3068 )
3069 )
3070 self.list = ( # pyright: ignore[reportDeprecated]
3071 to_streamed_response_wrapper(
3072 runs.list, # pyright: ignore[reportDeprecated],
3073 )
3074 )
3075 self.cancel = ( # pyright: ignore[reportDeprecated]
3076 to_streamed_response_wrapper(
3077 runs.cancel, # pyright: ignore[reportDeprecated],
3078 )
3079 )
3080 self.submit_tool_outputs = ( # pyright: ignore[reportDeprecated]
3081 to_streamed_response_wrapper(
3082 runs.submit_tool_outputs, # pyright: ignore[reportDeprecated],
3083 )
3084 )
3085
3086 @cached_property
3087 def steps(self) -> StepsWithStreamingResponse:
3088 return StepsWithStreamingResponse(self._runs.steps)
3089
3090
3091class AsyncRunsWithStreamingResponse:
3092 def __init__(self, runs: AsyncRuns) -> None:
3093 self._runs = runs
3094
3095 self.create = ( # pyright: ignore[reportDeprecated]
3096 async_to_streamed_response_wrapper(
3097 runs.create, # pyright: ignore[reportDeprecated],
3098 )
3099 )
3100 self.retrieve = ( # pyright: ignore[reportDeprecated]
3101 async_to_streamed_response_wrapper(
3102 runs.retrieve, # pyright: ignore[reportDeprecated],
3103 )
3104 )
3105 self.update = ( # pyright: ignore[reportDeprecated]
3106 async_to_streamed_response_wrapper(
3107 runs.update, # pyright: ignore[reportDeprecated],
3108 )
3109 )
3110 self.list = ( # pyright: ignore[reportDeprecated]
3111 async_to_streamed_response_wrapper(
3112 runs.list, # pyright: ignore[reportDeprecated],
3113 )
3114 )
3115 self.cancel = ( # pyright: ignore[reportDeprecated]
3116 async_to_streamed_response_wrapper(
3117 runs.cancel, # pyright: ignore[reportDeprecated],
3118 )
3119 )
3120 self.submit_tool_outputs = ( # pyright: ignore[reportDeprecated]
3121 async_to_streamed_response_wrapper(
3122 runs.submit_tool_outputs, # pyright: ignore[reportDeprecated],
3123 )
3124 )
3125
3126 @cached_property
3127 def steps(self) -> AsyncStepsWithStreamingResponse:
3128 return AsyncStepsWithStreamingResponse(self._runs.steps)