items.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. from typing import Any, List, Iterable, cast
  4. from typing_extensions import Literal
  5. import httpx
  6. from ... import _legacy_response
  7. from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
  8. from ..._utils import maybe_transform, async_maybe_transform
  9. from ..._compat import cached_property
  10. from ..._resource import SyncAPIResource, AsyncAPIResource
  11. from ..._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
  12. from ...pagination import SyncConversationCursorPage, AsyncConversationCursorPage
  13. from ..._base_client import AsyncPaginator, make_request_options
  14. from ...types.conversations import item_list_params, item_create_params, item_retrieve_params
  15. from ...types.conversations.conversation import Conversation
  16. from ...types.responses.response_includable import ResponseIncludable
  17. from ...types.conversations.conversation_item import ConversationItem
  18. from ...types.responses.response_input_item_param import ResponseInputItemParam
  19. from ...types.conversations.conversation_item_list import ConversationItemList
  20. __all__ = ["Items", "AsyncItems"]
  21. class Items(SyncAPIResource):
  22. @cached_property
  23. def with_raw_response(self) -> ItemsWithRawResponse:
  24. """
  25. This property can be used as a prefix for any HTTP method call to return
  26. the raw response object instead of the parsed content.
  27. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  28. """
  29. return ItemsWithRawResponse(self)
  30. @cached_property
  31. def with_streaming_response(self) -> ItemsWithStreamingResponse:
  32. """
  33. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  34. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  35. """
  36. return ItemsWithStreamingResponse(self)
  37. def create(
  38. self,
  39. conversation_id: str,
  40. *,
  41. items: Iterable[ResponseInputItemParam],
  42. include: List[ResponseIncludable] | 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. ) -> ConversationItemList:
  50. """
  51. Create items in a conversation with the given ID.
  52. Args:
  53. items: The items to add to the conversation. You may add up to 20 items at a time.
  54. include: Additional fields to include in the response. See the `include` parameter for
  55. [listing Conversation items above](https://platform.openai.com/docs/api-reference/conversations/list-items#conversations_list_items-include)
  56. for more information.
  57. extra_headers: Send extra headers
  58. extra_query: Add additional query parameters to the request
  59. extra_body: Add additional JSON properties to the request
  60. timeout: Override the client-level default timeout for this request, in seconds
  61. """
  62. if not conversation_id:
  63. raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
  64. return self._post(
  65. f"/conversations/{conversation_id}/items",
  66. body=maybe_transform({"items": items}, item_create_params.ItemCreateParams),
  67. options=make_request_options(
  68. extra_headers=extra_headers,
  69. extra_query=extra_query,
  70. extra_body=extra_body,
  71. timeout=timeout,
  72. query=maybe_transform({"include": include}, item_create_params.ItemCreateParams),
  73. ),
  74. cast_to=ConversationItemList,
  75. )
  76. def retrieve(
  77. self,
  78. item_id: str,
  79. *,
  80. conversation_id: str,
  81. include: List[ResponseIncludable] | Omit = omit,
  82. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  83. # The extra values given here take precedence over values defined on the client or passed to this method.
  84. extra_headers: Headers | None = None,
  85. extra_query: Query | None = None,
  86. extra_body: Body | None = None,
  87. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  88. ) -> ConversationItem:
  89. """
  90. Get a single item from a conversation with the given IDs.
  91. Args:
  92. include: Additional fields to include in the response. See the `include` parameter for
  93. [listing Conversation items above](https://platform.openai.com/docs/api-reference/conversations/list-items#conversations_list_items-include)
  94. for more information.
  95. extra_headers: Send extra headers
  96. extra_query: Add additional query parameters to the request
  97. extra_body: Add additional JSON properties to the request
  98. timeout: Override the client-level default timeout for this request, in seconds
  99. """
  100. if not conversation_id:
  101. raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
  102. if not item_id:
  103. raise ValueError(f"Expected a non-empty value for `item_id` but received {item_id!r}")
  104. return cast(
  105. ConversationItem,
  106. self._get(
  107. f"/conversations/{conversation_id}/items/{item_id}",
  108. options=make_request_options(
  109. extra_headers=extra_headers,
  110. extra_query=extra_query,
  111. extra_body=extra_body,
  112. timeout=timeout,
  113. query=maybe_transform({"include": include}, item_retrieve_params.ItemRetrieveParams),
  114. ),
  115. cast_to=cast(Any, ConversationItem), # Union types cannot be passed in as arguments in the type system
  116. ),
  117. )
  118. def list(
  119. self,
  120. conversation_id: str,
  121. *,
  122. after: str | Omit = omit,
  123. include: List[ResponseIncludable] | Omit = omit,
  124. limit: int | Omit = omit,
  125. order: Literal["asc", "desc"] | Omit = omit,
  126. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  127. # The extra values given here take precedence over values defined on the client or passed to this method.
  128. extra_headers: Headers | None = None,
  129. extra_query: Query | None = None,
  130. extra_body: Body | None = None,
  131. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  132. ) -> SyncConversationCursorPage[ConversationItem]:
  133. """
  134. List all items for a conversation with the given ID.
  135. Args:
  136. after: An item ID to list items after, used in pagination.
  137. include: Specify additional output data to include in the model response. Currently
  138. supported values are:
  139. - `web_search_call.action.sources`: Include the sources of the web search tool
  140. call.
  141. - `code_interpreter_call.outputs`: Includes the outputs of python code execution
  142. in code interpreter tool call items.
  143. - `computer_call_output.output.image_url`: Include image urls from the computer
  144. call output.
  145. - `file_search_call.results`: Include the search results of the file search tool
  146. call.
  147. - `message.input_image.image_url`: Include image urls from the input message.
  148. - `message.output_text.logprobs`: Include logprobs with assistant messages.
  149. - `reasoning.encrypted_content`: Includes an encrypted version of reasoning
  150. tokens in reasoning item outputs. This enables reasoning items to be used in
  151. multi-turn conversations when using the Responses API statelessly (like when
  152. the `store` parameter is set to `false`, or when an organization is enrolled
  153. in the zero data retention program).
  154. limit: A limit on the number of objects to be returned. Limit can range between 1 and
  155. 100, and the default is 20.
  156. order: The order to return the input items in. Default is `desc`.
  157. - `asc`: Return the input items in ascending order.
  158. - `desc`: Return the input items in descending order.
  159. extra_headers: Send extra headers
  160. extra_query: Add additional query parameters to the request
  161. extra_body: Add additional JSON properties to the request
  162. timeout: Override the client-level default timeout for this request, in seconds
  163. """
  164. if not conversation_id:
  165. raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
  166. return self._get_api_list(
  167. f"/conversations/{conversation_id}/items",
  168. page=SyncConversationCursorPage[ConversationItem],
  169. options=make_request_options(
  170. extra_headers=extra_headers,
  171. extra_query=extra_query,
  172. extra_body=extra_body,
  173. timeout=timeout,
  174. query=maybe_transform(
  175. {
  176. "after": after,
  177. "include": include,
  178. "limit": limit,
  179. "order": order,
  180. },
  181. item_list_params.ItemListParams,
  182. ),
  183. ),
  184. model=cast(Any, ConversationItem), # Union types cannot be passed in as arguments in the type system
  185. )
  186. def delete(
  187. self,
  188. item_id: str,
  189. *,
  190. conversation_id: str,
  191. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  192. # The extra values given here take precedence over values defined on the client or passed to this method.
  193. extra_headers: Headers | None = None,
  194. extra_query: Query | None = None,
  195. extra_body: Body | None = None,
  196. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  197. ) -> Conversation:
  198. """
  199. Delete an item from a conversation with the given IDs.
  200. Args:
  201. extra_headers: Send extra headers
  202. extra_query: Add additional query parameters to the request
  203. extra_body: Add additional JSON properties to the request
  204. timeout: Override the client-level default timeout for this request, in seconds
  205. """
  206. if not conversation_id:
  207. raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
  208. if not item_id:
  209. raise ValueError(f"Expected a non-empty value for `item_id` but received {item_id!r}")
  210. return self._delete(
  211. f"/conversations/{conversation_id}/items/{item_id}",
  212. options=make_request_options(
  213. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  214. ),
  215. cast_to=Conversation,
  216. )
  217. class AsyncItems(AsyncAPIResource):
  218. @cached_property
  219. def with_raw_response(self) -> AsyncItemsWithRawResponse:
  220. """
  221. This property can be used as a prefix for any HTTP method call to return
  222. the raw response object instead of the parsed content.
  223. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  224. """
  225. return AsyncItemsWithRawResponse(self)
  226. @cached_property
  227. def with_streaming_response(self) -> AsyncItemsWithStreamingResponse:
  228. """
  229. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  230. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  231. """
  232. return AsyncItemsWithStreamingResponse(self)
  233. async def create(
  234. self,
  235. conversation_id: str,
  236. *,
  237. items: Iterable[ResponseInputItemParam],
  238. include: List[ResponseIncludable] | Omit = omit,
  239. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  240. # The extra values given here take precedence over values defined on the client or passed to this method.
  241. extra_headers: Headers | None = None,
  242. extra_query: Query | None = None,
  243. extra_body: Body | None = None,
  244. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  245. ) -> ConversationItemList:
  246. """
  247. Create items in a conversation with the given ID.
  248. Args:
  249. items: The items to add to the conversation. You may add up to 20 items at a time.
  250. include: Additional fields to include in the response. See the `include` parameter for
  251. [listing Conversation items above](https://platform.openai.com/docs/api-reference/conversations/list-items#conversations_list_items-include)
  252. for more information.
  253. extra_headers: Send extra headers
  254. extra_query: Add additional query parameters to the request
  255. extra_body: Add additional JSON properties to the request
  256. timeout: Override the client-level default timeout for this request, in seconds
  257. """
  258. if not conversation_id:
  259. raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
  260. return await self._post(
  261. f"/conversations/{conversation_id}/items",
  262. body=await async_maybe_transform({"items": items}, item_create_params.ItemCreateParams),
  263. options=make_request_options(
  264. extra_headers=extra_headers,
  265. extra_query=extra_query,
  266. extra_body=extra_body,
  267. timeout=timeout,
  268. query=await async_maybe_transform({"include": include}, item_create_params.ItemCreateParams),
  269. ),
  270. cast_to=ConversationItemList,
  271. )
  272. async def retrieve(
  273. self,
  274. item_id: str,
  275. *,
  276. conversation_id: str,
  277. include: List[ResponseIncludable] | Omit = omit,
  278. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  279. # The extra values given here take precedence over values defined on the client or passed to this method.
  280. extra_headers: Headers | None = None,
  281. extra_query: Query | None = None,
  282. extra_body: Body | None = None,
  283. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  284. ) -> ConversationItem:
  285. """
  286. Get a single item from a conversation with the given IDs.
  287. Args:
  288. include: Additional fields to include in the response. See the `include` parameter for
  289. [listing Conversation items above](https://platform.openai.com/docs/api-reference/conversations/list-items#conversations_list_items-include)
  290. for more information.
  291. extra_headers: Send extra headers
  292. extra_query: Add additional query parameters to the request
  293. extra_body: Add additional JSON properties to the request
  294. timeout: Override the client-level default timeout for this request, in seconds
  295. """
  296. if not conversation_id:
  297. raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
  298. if not item_id:
  299. raise ValueError(f"Expected a non-empty value for `item_id` but received {item_id!r}")
  300. return cast(
  301. ConversationItem,
  302. await self._get(
  303. f"/conversations/{conversation_id}/items/{item_id}",
  304. options=make_request_options(
  305. extra_headers=extra_headers,
  306. extra_query=extra_query,
  307. extra_body=extra_body,
  308. timeout=timeout,
  309. query=await async_maybe_transform({"include": include}, item_retrieve_params.ItemRetrieveParams),
  310. ),
  311. cast_to=cast(Any, ConversationItem), # Union types cannot be passed in as arguments in the type system
  312. ),
  313. )
  314. def list(
  315. self,
  316. conversation_id: str,
  317. *,
  318. after: str | Omit = omit,
  319. include: List[ResponseIncludable] | Omit = omit,
  320. limit: int | Omit = omit,
  321. order: Literal["asc", "desc"] | Omit = omit,
  322. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  323. # The extra values given here take precedence over values defined on the client or passed to this method.
  324. extra_headers: Headers | None = None,
  325. extra_query: Query | None = None,
  326. extra_body: Body | None = None,
  327. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  328. ) -> AsyncPaginator[ConversationItem, AsyncConversationCursorPage[ConversationItem]]:
  329. """
  330. List all items for a conversation with the given ID.
  331. Args:
  332. after: An item ID to list items after, used in pagination.
  333. include: Specify additional output data to include in the model response. Currently
  334. supported values are:
  335. - `web_search_call.action.sources`: Include the sources of the web search tool
  336. call.
  337. - `code_interpreter_call.outputs`: Includes the outputs of python code execution
  338. in code interpreter tool call items.
  339. - `computer_call_output.output.image_url`: Include image urls from the computer
  340. call output.
  341. - `file_search_call.results`: Include the search results of the file search tool
  342. call.
  343. - `message.input_image.image_url`: Include image urls from the input message.
  344. - `message.output_text.logprobs`: Include logprobs with assistant messages.
  345. - `reasoning.encrypted_content`: Includes an encrypted version of reasoning
  346. tokens in reasoning item outputs. This enables reasoning items to be used in
  347. multi-turn conversations when using the Responses API statelessly (like when
  348. the `store` parameter is set to `false`, or when an organization is enrolled
  349. in the zero data retention program).
  350. limit: A limit on the number of objects to be returned. Limit can range between 1 and
  351. 100, and the default is 20.
  352. order: The order to return the input items in. Default is `desc`.
  353. - `asc`: Return the input items in ascending order.
  354. - `desc`: Return the input items in descending order.
  355. extra_headers: Send extra headers
  356. extra_query: Add additional query parameters to the request
  357. extra_body: Add additional JSON properties to the request
  358. timeout: Override the client-level default timeout for this request, in seconds
  359. """
  360. if not conversation_id:
  361. raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
  362. return self._get_api_list(
  363. f"/conversations/{conversation_id}/items",
  364. page=AsyncConversationCursorPage[ConversationItem],
  365. options=make_request_options(
  366. extra_headers=extra_headers,
  367. extra_query=extra_query,
  368. extra_body=extra_body,
  369. timeout=timeout,
  370. query=maybe_transform(
  371. {
  372. "after": after,
  373. "include": include,
  374. "limit": limit,
  375. "order": order,
  376. },
  377. item_list_params.ItemListParams,
  378. ),
  379. ),
  380. model=cast(Any, ConversationItem), # Union types cannot be passed in as arguments in the type system
  381. )
  382. async def delete(
  383. self,
  384. item_id: str,
  385. *,
  386. conversation_id: str,
  387. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  388. # The extra values given here take precedence over values defined on the client or passed to this method.
  389. extra_headers: Headers | None = None,
  390. extra_query: Query | None = None,
  391. extra_body: Body | None = None,
  392. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  393. ) -> Conversation:
  394. """
  395. Delete an item from a conversation with the given IDs.
  396. Args:
  397. extra_headers: Send extra headers
  398. extra_query: Add additional query parameters to the request
  399. extra_body: Add additional JSON properties to the request
  400. timeout: Override the client-level default timeout for this request, in seconds
  401. """
  402. if not conversation_id:
  403. raise ValueError(f"Expected a non-empty value for `conversation_id` but received {conversation_id!r}")
  404. if not item_id:
  405. raise ValueError(f"Expected a non-empty value for `item_id` but received {item_id!r}")
  406. return await self._delete(
  407. f"/conversations/{conversation_id}/items/{item_id}",
  408. options=make_request_options(
  409. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  410. ),
  411. cast_to=Conversation,
  412. )
  413. class ItemsWithRawResponse:
  414. def __init__(self, items: Items) -> None:
  415. self._items = items
  416. self.create = _legacy_response.to_raw_response_wrapper(
  417. items.create,
  418. )
  419. self.retrieve = _legacy_response.to_raw_response_wrapper(
  420. items.retrieve,
  421. )
  422. self.list = _legacy_response.to_raw_response_wrapper(
  423. items.list,
  424. )
  425. self.delete = _legacy_response.to_raw_response_wrapper(
  426. items.delete,
  427. )
  428. class AsyncItemsWithRawResponse:
  429. def __init__(self, items: AsyncItems) -> None:
  430. self._items = items
  431. self.create = _legacy_response.async_to_raw_response_wrapper(
  432. items.create,
  433. )
  434. self.retrieve = _legacy_response.async_to_raw_response_wrapper(
  435. items.retrieve,
  436. )
  437. self.list = _legacy_response.async_to_raw_response_wrapper(
  438. items.list,
  439. )
  440. self.delete = _legacy_response.async_to_raw_response_wrapper(
  441. items.delete,
  442. )
  443. class ItemsWithStreamingResponse:
  444. def __init__(self, items: Items) -> None:
  445. self._items = items
  446. self.create = to_streamed_response_wrapper(
  447. items.create,
  448. )
  449. self.retrieve = to_streamed_response_wrapper(
  450. items.retrieve,
  451. )
  452. self.list = to_streamed_response_wrapper(
  453. items.list,
  454. )
  455. self.delete = to_streamed_response_wrapper(
  456. items.delete,
  457. )
  458. class AsyncItemsWithStreamingResponse:
  459. def __init__(self, items: AsyncItems) -> None:
  460. self._items = items
  461. self.create = async_to_streamed_response_wrapper(
  462. items.create,
  463. )
  464. self.retrieve = async_to_streamed_response_wrapper(
  465. items.retrieve,
  466. )
  467. self.list = async_to_streamed_response_wrapper(
  468. items.list,
  469. )
  470. self.delete = async_to_streamed_response_wrapper(
  471. items.delete,
  472. )