responses.py 169 KB

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