steps.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. import typing_extensions
  4. from typing import List
  5. from typing_extensions import Literal
  6. import httpx
  7. from ..... import _legacy_response
  8. from ....._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
  9. from ....._utils import maybe_transform, async_maybe_transform
  10. from ....._compat import cached_property
  11. from ....._resource import SyncAPIResource, AsyncAPIResource
  12. from ....._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
  13. from .....pagination import SyncCursorPage, AsyncCursorPage
  14. from ....._base_client import AsyncPaginator, make_request_options
  15. from .....types.beta.threads.runs import step_list_params, step_retrieve_params
  16. from .....types.beta.threads.runs.run_step import RunStep
  17. from .....types.beta.threads.runs.run_step_include import RunStepInclude
  18. __all__ = ["Steps", "AsyncSteps"]
  19. class Steps(SyncAPIResource):
  20. @cached_property
  21. def with_raw_response(self) -> StepsWithRawResponse:
  22. """
  23. This property can be used as a prefix for any HTTP method call to return
  24. the raw response object instead of the parsed content.
  25. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  26. """
  27. return StepsWithRawResponse(self)
  28. @cached_property
  29. def with_streaming_response(self) -> StepsWithStreamingResponse:
  30. """
  31. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  32. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  33. """
  34. return StepsWithStreamingResponse(self)
  35. @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
  36. def retrieve(
  37. self,
  38. step_id: str,
  39. *,
  40. thread_id: str,
  41. run_id: str,
  42. include: List[RunStepInclude] | Omit = omit,
  43. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  44. # The extra values given here take precedence over values defined on the client or passed to this method.
  45. extra_headers: Headers | None = None,
  46. extra_query: Query | None = None,
  47. extra_body: Body | None = None,
  48. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  49. ) -> RunStep:
  50. """
  51. Retrieves a run step.
  52. Args:
  53. include: A list of additional fields to include in the response. Currently the only
  54. supported value is `step_details.tool_calls[*].file_search.results[*].content`
  55. to fetch the file search result content.
  56. See the
  57. [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
  58. for more information.
  59. extra_headers: Send extra headers
  60. extra_query: Add additional query parameters to the request
  61. extra_body: Add additional JSON properties to the request
  62. timeout: Override the client-level default timeout for this request, in seconds
  63. """
  64. if not thread_id:
  65. raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
  66. if not run_id:
  67. raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
  68. if not step_id:
  69. raise ValueError(f"Expected a non-empty value for `step_id` but received {step_id!r}")
  70. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  71. return self._get(
  72. f"/threads/{thread_id}/runs/{run_id}/steps/{step_id}",
  73. options=make_request_options(
  74. extra_headers=extra_headers,
  75. extra_query=extra_query,
  76. extra_body=extra_body,
  77. timeout=timeout,
  78. query=maybe_transform({"include": include}, step_retrieve_params.StepRetrieveParams),
  79. ),
  80. cast_to=RunStep,
  81. )
  82. @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
  83. def list(
  84. self,
  85. run_id: str,
  86. *,
  87. thread_id: str,
  88. after: str | Omit = omit,
  89. before: str | Omit = omit,
  90. include: List[RunStepInclude] | Omit = omit,
  91. limit: int | Omit = omit,
  92. order: Literal["asc", "desc"] | Omit = omit,
  93. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  94. # The extra values given here take precedence over values defined on the client or passed to this method.
  95. extra_headers: Headers | None = None,
  96. extra_query: Query | None = None,
  97. extra_body: Body | None = None,
  98. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  99. ) -> SyncCursorPage[RunStep]:
  100. """
  101. Returns a list of run steps belonging to a run.
  102. Args:
  103. after: A cursor for use in pagination. `after` is an object ID that defines your place
  104. in the list. For instance, if you make a list request and receive 100 objects,
  105. ending with obj_foo, your subsequent call can include after=obj_foo in order to
  106. fetch the next page of the list.
  107. before: A cursor for use in pagination. `before` is an object ID that defines your place
  108. in the list. For instance, if you make a list request and receive 100 objects,
  109. starting with obj_foo, your subsequent call can include before=obj_foo in order
  110. to fetch the previous page of the list.
  111. include: A list of additional fields to include in the response. Currently the only
  112. supported value is `step_details.tool_calls[*].file_search.results[*].content`
  113. to fetch the file search result content.
  114. See the
  115. [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
  116. for more information.
  117. limit: A limit on the number of objects to be returned. Limit can range between 1 and
  118. 100, and the default is 20.
  119. order: Sort order by the `created_at` timestamp of the objects. `asc` for ascending
  120. order and `desc` for descending order.
  121. extra_headers: Send extra headers
  122. extra_query: Add additional query parameters to the request
  123. extra_body: Add additional JSON properties to the request
  124. timeout: Override the client-level default timeout for this request, in seconds
  125. """
  126. if not thread_id:
  127. raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
  128. if not run_id:
  129. raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
  130. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  131. return self._get_api_list(
  132. f"/threads/{thread_id}/runs/{run_id}/steps",
  133. page=SyncCursorPage[RunStep],
  134. options=make_request_options(
  135. extra_headers=extra_headers,
  136. extra_query=extra_query,
  137. extra_body=extra_body,
  138. timeout=timeout,
  139. query=maybe_transform(
  140. {
  141. "after": after,
  142. "before": before,
  143. "include": include,
  144. "limit": limit,
  145. "order": order,
  146. },
  147. step_list_params.StepListParams,
  148. ),
  149. ),
  150. model=RunStep,
  151. )
  152. class AsyncSteps(AsyncAPIResource):
  153. @cached_property
  154. def with_raw_response(self) -> AsyncStepsWithRawResponse:
  155. """
  156. This property can be used as a prefix for any HTTP method call to return
  157. the raw response object instead of the parsed content.
  158. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  159. """
  160. return AsyncStepsWithRawResponse(self)
  161. @cached_property
  162. def with_streaming_response(self) -> AsyncStepsWithStreamingResponse:
  163. """
  164. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  165. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  166. """
  167. return AsyncStepsWithStreamingResponse(self)
  168. @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
  169. async def retrieve(
  170. self,
  171. step_id: str,
  172. *,
  173. thread_id: str,
  174. run_id: str,
  175. include: List[RunStepInclude] | Omit = omit,
  176. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  177. # The extra values given here take precedence over values defined on the client or passed to this method.
  178. extra_headers: Headers | None = None,
  179. extra_query: Query | None = None,
  180. extra_body: Body | None = None,
  181. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  182. ) -> RunStep:
  183. """
  184. Retrieves a run step.
  185. Args:
  186. include: A list of additional fields to include in the response. Currently the only
  187. supported value is `step_details.tool_calls[*].file_search.results[*].content`
  188. to fetch the file search result content.
  189. See the
  190. [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
  191. for more information.
  192. extra_headers: Send extra headers
  193. extra_query: Add additional query parameters to the request
  194. extra_body: Add additional JSON properties to the request
  195. timeout: Override the client-level default timeout for this request, in seconds
  196. """
  197. if not thread_id:
  198. raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
  199. if not run_id:
  200. raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
  201. if not step_id:
  202. raise ValueError(f"Expected a non-empty value for `step_id` but received {step_id!r}")
  203. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  204. return await self._get(
  205. f"/threads/{thread_id}/runs/{run_id}/steps/{step_id}",
  206. options=make_request_options(
  207. extra_headers=extra_headers,
  208. extra_query=extra_query,
  209. extra_body=extra_body,
  210. timeout=timeout,
  211. query=await async_maybe_transform({"include": include}, step_retrieve_params.StepRetrieveParams),
  212. ),
  213. cast_to=RunStep,
  214. )
  215. @typing_extensions.deprecated("The Assistants API is deprecated in favor of the Responses API")
  216. def list(
  217. self,
  218. run_id: str,
  219. *,
  220. thread_id: str,
  221. after: str | Omit = omit,
  222. before: str | Omit = omit,
  223. include: List[RunStepInclude] | Omit = omit,
  224. limit: int | Omit = omit,
  225. order: Literal["asc", "desc"] | Omit = omit,
  226. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  227. # The extra values given here take precedence over values defined on the client or passed to this method.
  228. extra_headers: Headers | None = None,
  229. extra_query: Query | None = None,
  230. extra_body: Body | None = None,
  231. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  232. ) -> AsyncPaginator[RunStep, AsyncCursorPage[RunStep]]:
  233. """
  234. Returns a list of run steps belonging to a run.
  235. Args:
  236. after: A cursor for use in pagination. `after` is an object ID that defines your place
  237. in the list. For instance, if you make a list request and receive 100 objects,
  238. ending with obj_foo, your subsequent call can include after=obj_foo in order to
  239. fetch the next page of the list.
  240. before: A cursor for use in pagination. `before` is an object ID that defines your place
  241. in the list. For instance, if you make a list request and receive 100 objects,
  242. starting with obj_foo, your subsequent call can include before=obj_foo in order
  243. to fetch the previous page of the list.
  244. include: A list of additional fields to include in the response. Currently the only
  245. supported value is `step_details.tool_calls[*].file_search.results[*].content`
  246. to fetch the file search result content.
  247. See the
  248. [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search#customizing-file-search-settings)
  249. for more information.
  250. limit: A limit on the number of objects to be returned. Limit can range between 1 and
  251. 100, and the default is 20.
  252. order: Sort order by the `created_at` timestamp of the objects. `asc` for ascending
  253. order and `desc` for descending order.
  254. extra_headers: Send extra headers
  255. extra_query: Add additional query parameters to the request
  256. extra_body: Add additional JSON properties to the request
  257. timeout: Override the client-level default timeout for this request, in seconds
  258. """
  259. if not thread_id:
  260. raise ValueError(f"Expected a non-empty value for `thread_id` but received {thread_id!r}")
  261. if not run_id:
  262. raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
  263. extra_headers = {"OpenAI-Beta": "assistants=v2", **(extra_headers or {})}
  264. return self._get_api_list(
  265. f"/threads/{thread_id}/runs/{run_id}/steps",
  266. page=AsyncCursorPage[RunStep],
  267. options=make_request_options(
  268. extra_headers=extra_headers,
  269. extra_query=extra_query,
  270. extra_body=extra_body,
  271. timeout=timeout,
  272. query=maybe_transform(
  273. {
  274. "after": after,
  275. "before": before,
  276. "include": include,
  277. "limit": limit,
  278. "order": order,
  279. },
  280. step_list_params.StepListParams,
  281. ),
  282. ),
  283. model=RunStep,
  284. )
  285. class StepsWithRawResponse:
  286. def __init__(self, steps: Steps) -> None:
  287. self._steps = steps
  288. self.retrieve = ( # pyright: ignore[reportDeprecated]
  289. _legacy_response.to_raw_response_wrapper(
  290. steps.retrieve, # pyright: ignore[reportDeprecated],
  291. )
  292. )
  293. self.list = ( # pyright: ignore[reportDeprecated]
  294. _legacy_response.to_raw_response_wrapper(
  295. steps.list, # pyright: ignore[reportDeprecated],
  296. )
  297. )
  298. class AsyncStepsWithRawResponse:
  299. def __init__(self, steps: AsyncSteps) -> None:
  300. self._steps = steps
  301. self.retrieve = ( # pyright: ignore[reportDeprecated]
  302. _legacy_response.async_to_raw_response_wrapper(
  303. steps.retrieve, # pyright: ignore[reportDeprecated],
  304. )
  305. )
  306. self.list = ( # pyright: ignore[reportDeprecated]
  307. _legacy_response.async_to_raw_response_wrapper(
  308. steps.list, # pyright: ignore[reportDeprecated],
  309. )
  310. )
  311. class StepsWithStreamingResponse:
  312. def __init__(self, steps: Steps) -> None:
  313. self._steps = steps
  314. self.retrieve = ( # pyright: ignore[reportDeprecated]
  315. to_streamed_response_wrapper(
  316. steps.retrieve, # pyright: ignore[reportDeprecated],
  317. )
  318. )
  319. self.list = ( # pyright: ignore[reportDeprecated]
  320. to_streamed_response_wrapper(
  321. steps.list, # pyright: ignore[reportDeprecated],
  322. )
  323. )
  324. class AsyncStepsWithStreamingResponse:
  325. def __init__(self, steps: AsyncSteps) -> None:
  326. self._steps = steps
  327. self.retrieve = ( # pyright: ignore[reportDeprecated]
  328. async_to_streamed_response_wrapper(
  329. steps.retrieve, # pyright: ignore[reportDeprecated],
  330. )
  331. )
  332. self.list = ( # pyright: ignore[reportDeprecated]
  333. async_to_streamed_response_wrapper(
  334. steps.list, # pyright: ignore[reportDeprecated],
  335. )
  336. )