A helper to poll a run status until it reaches a terminal state. More information on Run lifecycles can be found here: https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps
(
self,
run_id: str,
thread_id: str,
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = not_given,
poll_interval_ms: int | Omit = omit,
)
| 2536 | |
| 2537 | @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API") |
| 2538 | async def poll( |
| 2539 | self, |
| 2540 | run_id: str, |
| 2541 | thread_id: str, |
| 2542 | extra_headers: Headers | None = None, |
| 2543 | extra_query: Query | None = None, |
| 2544 | extra_body: Body | None = None, |
| 2545 | timeout: float | httpx.Timeout | None | NotGiven = not_given, |
| 2546 | poll_interval_ms: int | Omit = omit, |
| 2547 | ) -> Run: |
| 2548 | """ |
| 2549 | A helper to poll a run status until it reaches a terminal state. More |
| 2550 | information on Run lifecycles can be found here: |
| 2551 | https://platform.openai.com/docs/assistants/how-it-works/runs-and-run-steps |
| 2552 | """ |
| 2553 | extra_headers = {"X-Stainless-Poll-Helper": "true", **(extra_headers or {})} |
| 2554 | |
| 2555 | if is_given(poll_interval_ms): |
| 2556 | extra_headers["X-Stainless-Custom-Poll-Interval"] = str(poll_interval_ms) |
| 2557 | |
| 2558 | terminal_states = {"requires_action", "cancelled", "completed", "failed", "expired", "incomplete"} |
| 2559 | while True: |
| 2560 | response = await self.with_raw_response.retrieve( # pyright: ignore[reportDeprecated] |
| 2561 | thread_id=thread_id, |
| 2562 | run_id=run_id, |
| 2563 | extra_headers=extra_headers, |
| 2564 | extra_body=extra_body, |
| 2565 | extra_query=extra_query, |
| 2566 | timeout=timeout, |
| 2567 | ) |
| 2568 | |
| 2569 | run = response.parse() |
| 2570 | # Return if we reached a terminal state |
| 2571 | if run.status in terminal_states: |
| 2572 | return run |
| 2573 | |
| 2574 | if not is_given(poll_interval_ms): |
| 2575 | from_header = response.headers.get("openai-poll-after-ms") |
| 2576 | if from_header is not None: |
| 2577 | poll_interval_ms = int(from_header) |
| 2578 | else: |
| 2579 | poll_interval_ms = 1000 |
| 2580 | |
| 2581 | await self._sleep(poll_interval_ms / 1000) |
| 2582 | |
| 2583 | @overload |
| 2584 | @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API") |