images.py 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. from typing import Union, Mapping, Optional, cast
  4. from typing_extensions import Literal, overload
  5. import httpx
  6. from .. import _legacy_response
  7. from ..types import image_edit_params, image_generate_params, image_create_variation_params
  8. from .._types import Body, Omit, Query, Headers, NotGiven, FileTypes, SequenceNotStr, omit, not_given
  9. from .._utils import extract_files, required_args, maybe_transform, deepcopy_minimal, async_maybe_transform
  10. from .._compat import cached_property
  11. from .._resource import SyncAPIResource, AsyncAPIResource
  12. from .._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper
  13. from .._streaming import Stream, AsyncStream
  14. from .._base_client import make_request_options
  15. from ..types.image_model import ImageModel
  16. from ..types.images_response import ImagesResponse
  17. from ..types.image_gen_stream_event import ImageGenStreamEvent
  18. from ..types.image_edit_stream_event import ImageEditStreamEvent
  19. __all__ = ["Images", "AsyncImages"]
  20. class Images(SyncAPIResource):
  21. @cached_property
  22. def with_raw_response(self) -> ImagesWithRawResponse:
  23. """
  24. This property can be used as a prefix for any HTTP method call to return
  25. the raw response object instead of the parsed content.
  26. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  27. """
  28. return ImagesWithRawResponse(self)
  29. @cached_property
  30. def with_streaming_response(self) -> ImagesWithStreamingResponse:
  31. """
  32. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  33. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  34. """
  35. return ImagesWithStreamingResponse(self)
  36. def create_variation(
  37. self,
  38. *,
  39. image: FileTypes,
  40. model: Union[str, ImageModel, None] | Omit = omit,
  41. n: Optional[int] | Omit = omit,
  42. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  43. size: Optional[Literal["256x256", "512x512", "1024x1024"]] | Omit = omit,
  44. user: str | Omit = omit,
  45. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  46. # The extra values given here take precedence over values defined on the client or passed to this method.
  47. extra_headers: Headers | None = None,
  48. extra_query: Query | None = None,
  49. extra_body: Body | None = None,
  50. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  51. ) -> ImagesResponse:
  52. """Creates a variation of a given image.
  53. This endpoint only supports `dall-e-2`.
  54. Args:
  55. image: The image to use as the basis for the variation(s). Must be a valid PNG file,
  56. less than 4MB, and square.
  57. model: The model to use for image generation. Only `dall-e-2` is supported at this
  58. time.
  59. n: The number of images to generate. Must be between 1 and 10.
  60. response_format: The format in which the generated images are returned. Must be one of `url` or
  61. `b64_json`. URLs are only valid for 60 minutes after the image has been
  62. generated.
  63. size: The size of the generated images. Must be one of `256x256`, `512x512`, or
  64. `1024x1024`.
  65. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  66. and detect abuse.
  67. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  68. extra_headers: Send extra headers
  69. extra_query: Add additional query parameters to the request
  70. extra_body: Add additional JSON properties to the request
  71. timeout: Override the client-level default timeout for this request, in seconds
  72. """
  73. body = deepcopy_minimal(
  74. {
  75. "image": image,
  76. "model": model,
  77. "n": n,
  78. "response_format": response_format,
  79. "size": size,
  80. "user": user,
  81. }
  82. )
  83. files = extract_files(cast(Mapping[str, object], body), paths=[["image"]])
  84. # It should be noted that the actual Content-Type header that will be
  85. # sent to the server will contain a `boundary` parameter, e.g.
  86. # multipart/form-data; boundary=---abc--
  87. extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
  88. return self._post(
  89. "/images/variations",
  90. body=maybe_transform(body, image_create_variation_params.ImageCreateVariationParams),
  91. files=files,
  92. options=make_request_options(
  93. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  94. ),
  95. cast_to=ImagesResponse,
  96. )
  97. @overload
  98. def edit(
  99. self,
  100. *,
  101. image: Union[FileTypes, SequenceNotStr[FileTypes]],
  102. prompt: str,
  103. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  104. input_fidelity: Optional[Literal["high", "low"]] | Omit = omit,
  105. mask: FileTypes | Omit = omit,
  106. model: Union[str, ImageModel, None] | Omit = omit,
  107. n: Optional[int] | Omit = omit,
  108. output_compression: Optional[int] | Omit = omit,
  109. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  110. partial_images: Optional[int] | Omit = omit,
  111. quality: Optional[Literal["standard", "low", "medium", "high", "auto"]] | Omit = omit,
  112. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  113. size: Optional[Literal["256x256", "512x512", "1024x1024", "1536x1024", "1024x1536", "auto"]] | Omit = omit,
  114. stream: Optional[Literal[False]] | Omit = omit,
  115. user: str | Omit = omit,
  116. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  117. # The extra values given here take precedence over values defined on the client or passed to this method.
  118. extra_headers: Headers | None = None,
  119. extra_query: Query | None = None,
  120. extra_body: Body | None = None,
  121. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  122. ) -> ImagesResponse:
  123. """Creates an edited or extended image given one or more source images and a
  124. prompt.
  125. This endpoint only supports `gpt-image-1` and `dall-e-2`.
  126. Args:
  127. image: The image(s) to edit. Must be a supported image file or an array of images.
  128. For the GPT image models (`gpt-image-1`, `gpt-image-1-mini`, and
  129. `gpt-image-1.5`), each image should be a `png`, `webp`, or `jpg` file less than
  130. 50MB. You can provide up to 16 images.
  131. For `dall-e-2`, you can only provide one image, and it should be a square `png`
  132. file less than 4MB.
  133. prompt: A text description of the desired image(s). The maximum length is 1000
  134. characters for `dall-e-2`, and 32000 characters for the GPT image models.
  135. background: Allows to set transparency for the background of the generated image(s). This
  136. parameter is only supported for the GPT image models. Must be one of
  137. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  138. model will automatically determine the best background for the image.
  139. If `transparent`, the output format needs to support transparency, so it should
  140. be set to either `png` (default value) or `webp`.
  141. input_fidelity: Control how much effort the model will exert to match the style and features,
  142. especially facial features, of input images. This parameter is only supported
  143. for `gpt-image-1`. Unsupported for `gpt-image-1-mini`. Supports `high` and
  144. `low`. Defaults to `low`.
  145. mask: An additional image whose fully transparent areas (e.g. where alpha is zero)
  146. indicate where `image` should be edited. If there are multiple images provided,
  147. the mask will be applied on the first image. Must be a valid PNG file, less than
  148. 4MB, and have the same dimensions as `image`.
  149. model: The model to use for image generation. Only `dall-e-2` and the GPT image models
  150. are supported. Defaults to `dall-e-2` unless a parameter specific to the GPT
  151. image models is used.
  152. n: The number of images to generate. Must be between 1 and 10.
  153. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  154. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  155. defaults to 100.
  156. output_format: The format in which the generated images are returned. This parameter is only
  157. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`. The
  158. default value is `png`.
  159. partial_images: The number of partial images to generate. This parameter is used for streaming
  160. responses that return partial images. Value must be between 0 and 3. When set to
  161. 0, the response will be a single image sent in one streaming event.
  162. Note that the final image may be sent before the full number of partial images
  163. are generated if the full image is generated more quickly.
  164. quality: The quality of the image that will be generated. `high`, `medium` and `low` are
  165. only supported for the GPT image models. `dall-e-2` only supports `standard`
  166. quality. Defaults to `auto`.
  167. response_format: The format in which the generated images are returned. Must be one of `url` or
  168. `b64_json`. URLs are only valid for 60 minutes after the image has been
  169. generated. This parameter is only supported for `dall-e-2`, as the GPT image
  170. models always return base64-encoded images.
  171. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  172. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  173. models, and one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
  174. stream: Edit the image in streaming mode. Defaults to `false`. See the
  175. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  176. for more information.
  177. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  178. and detect abuse.
  179. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  180. extra_headers: Send extra headers
  181. extra_query: Add additional query parameters to the request
  182. extra_body: Add additional JSON properties to the request
  183. timeout: Override the client-level default timeout for this request, in seconds
  184. """
  185. ...
  186. @overload
  187. def edit(
  188. self,
  189. *,
  190. image: Union[FileTypes, SequenceNotStr[FileTypes]],
  191. prompt: str,
  192. stream: Literal[True],
  193. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  194. input_fidelity: Optional[Literal["high", "low"]] | Omit = omit,
  195. mask: FileTypes | Omit = omit,
  196. model: Union[str, ImageModel, None] | Omit = omit,
  197. n: Optional[int] | Omit = omit,
  198. output_compression: Optional[int] | Omit = omit,
  199. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  200. partial_images: Optional[int] | Omit = omit,
  201. quality: Optional[Literal["standard", "low", "medium", "high", "auto"]] | Omit = omit,
  202. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  203. size: Optional[Literal["256x256", "512x512", "1024x1024", "1536x1024", "1024x1536", "auto"]] | Omit = omit,
  204. user: str | Omit = omit,
  205. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  206. # The extra values given here take precedence over values defined on the client or passed to this method.
  207. extra_headers: Headers | None = None,
  208. extra_query: Query | None = None,
  209. extra_body: Body | None = None,
  210. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  211. ) -> Stream[ImageEditStreamEvent]:
  212. """Creates an edited or extended image given one or more source images and a
  213. prompt.
  214. This endpoint only supports `gpt-image-1` and `dall-e-2`.
  215. Args:
  216. image: The image(s) to edit. Must be a supported image file or an array of images.
  217. For the GPT image models (`gpt-image-1`, `gpt-image-1-mini`, and
  218. `gpt-image-1.5`), each image should be a `png`, `webp`, or `jpg` file less than
  219. 50MB. You can provide up to 16 images.
  220. For `dall-e-2`, you can only provide one image, and it should be a square `png`
  221. file less than 4MB.
  222. prompt: A text description of the desired image(s). The maximum length is 1000
  223. characters for `dall-e-2`, and 32000 characters for the GPT image models.
  224. stream: Edit the image in streaming mode. Defaults to `false`. See the
  225. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  226. for more information.
  227. background: Allows to set transparency for the background of the generated image(s). This
  228. parameter is only supported for the GPT image models. Must be one of
  229. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  230. model will automatically determine the best background for the image.
  231. If `transparent`, the output format needs to support transparency, so it should
  232. be set to either `png` (default value) or `webp`.
  233. input_fidelity: Control how much effort the model will exert to match the style and features,
  234. especially facial features, of input images. This parameter is only supported
  235. for `gpt-image-1`. Unsupported for `gpt-image-1-mini`. Supports `high` and
  236. `low`. Defaults to `low`.
  237. mask: An additional image whose fully transparent areas (e.g. where alpha is zero)
  238. indicate where `image` should be edited. If there are multiple images provided,
  239. the mask will be applied on the first image. Must be a valid PNG file, less than
  240. 4MB, and have the same dimensions as `image`.
  241. model: The model to use for image generation. Only `dall-e-2` and the GPT image models
  242. are supported. Defaults to `dall-e-2` unless a parameter specific to the GPT
  243. image models is used.
  244. n: The number of images to generate. Must be between 1 and 10.
  245. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  246. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  247. defaults to 100.
  248. output_format: The format in which the generated images are returned. This parameter is only
  249. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`. The
  250. default value is `png`.
  251. partial_images: The number of partial images to generate. This parameter is used for streaming
  252. responses that return partial images. Value must be between 0 and 3. When set to
  253. 0, the response will be a single image sent in one streaming event.
  254. Note that the final image may be sent before the full number of partial images
  255. are generated if the full image is generated more quickly.
  256. quality: The quality of the image that will be generated. `high`, `medium` and `low` are
  257. only supported for the GPT image models. `dall-e-2` only supports `standard`
  258. quality. Defaults to `auto`.
  259. response_format: The format in which the generated images are returned. Must be one of `url` or
  260. `b64_json`. URLs are only valid for 60 minutes after the image has been
  261. generated. This parameter is only supported for `dall-e-2`, as the GPT image
  262. models always return base64-encoded images.
  263. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  264. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  265. models, and one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
  266. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  267. and detect abuse.
  268. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  269. extra_headers: Send extra headers
  270. extra_query: Add additional query parameters to the request
  271. extra_body: Add additional JSON properties to the request
  272. timeout: Override the client-level default timeout for this request, in seconds
  273. """
  274. ...
  275. @overload
  276. def edit(
  277. self,
  278. *,
  279. image: Union[FileTypes, SequenceNotStr[FileTypes]],
  280. prompt: str,
  281. stream: bool,
  282. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  283. input_fidelity: Optional[Literal["high", "low"]] | Omit = omit,
  284. mask: FileTypes | Omit = omit,
  285. model: Union[str, ImageModel, None] | Omit = omit,
  286. n: Optional[int] | Omit = omit,
  287. output_compression: Optional[int] | Omit = omit,
  288. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  289. partial_images: Optional[int] | Omit = omit,
  290. quality: Optional[Literal["standard", "low", "medium", "high", "auto"]] | Omit = omit,
  291. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  292. size: Optional[Literal["256x256", "512x512", "1024x1024", "1536x1024", "1024x1536", "auto"]] | Omit = omit,
  293. user: str | Omit = omit,
  294. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  295. # The extra values given here take precedence over values defined on the client or passed to this method.
  296. extra_headers: Headers | None = None,
  297. extra_query: Query | None = None,
  298. extra_body: Body | None = None,
  299. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  300. ) -> ImagesResponse | Stream[ImageEditStreamEvent]:
  301. """Creates an edited or extended image given one or more source images and a
  302. prompt.
  303. This endpoint only supports `gpt-image-1` and `dall-e-2`.
  304. Args:
  305. image: The image(s) to edit. Must be a supported image file or an array of images.
  306. For the GPT image models (`gpt-image-1`, `gpt-image-1-mini`, and
  307. `gpt-image-1.5`), each image should be a `png`, `webp`, or `jpg` file less than
  308. 50MB. You can provide up to 16 images.
  309. For `dall-e-2`, you can only provide one image, and it should be a square `png`
  310. file less than 4MB.
  311. prompt: A text description of the desired image(s). The maximum length is 1000
  312. characters for `dall-e-2`, and 32000 characters for the GPT image models.
  313. stream: Edit the image in streaming mode. Defaults to `false`. See the
  314. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  315. for more information.
  316. background: Allows to set transparency for the background of the generated image(s). This
  317. parameter is only supported for the GPT image models. Must be one of
  318. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  319. model will automatically determine the best background for the image.
  320. If `transparent`, the output format needs to support transparency, so it should
  321. be set to either `png` (default value) or `webp`.
  322. input_fidelity: Control how much effort the model will exert to match the style and features,
  323. especially facial features, of input images. This parameter is only supported
  324. for `gpt-image-1`. Unsupported for `gpt-image-1-mini`. Supports `high` and
  325. `low`. Defaults to `low`.
  326. mask: An additional image whose fully transparent areas (e.g. where alpha is zero)
  327. indicate where `image` should be edited. If there are multiple images provided,
  328. the mask will be applied on the first image. Must be a valid PNG file, less than
  329. 4MB, and have the same dimensions as `image`.
  330. model: The model to use for image generation. Only `dall-e-2` and the GPT image models
  331. are supported. Defaults to `dall-e-2` unless a parameter specific to the GPT
  332. image models is used.
  333. n: The number of images to generate. Must be between 1 and 10.
  334. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  335. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  336. defaults to 100.
  337. output_format: The format in which the generated images are returned. This parameter is only
  338. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`. The
  339. default value is `png`.
  340. partial_images: The number of partial images to generate. This parameter is used for streaming
  341. responses that return partial images. Value must be between 0 and 3. When set to
  342. 0, the response will be a single image sent in one streaming event.
  343. Note that the final image may be sent before the full number of partial images
  344. are generated if the full image is generated more quickly.
  345. quality: The quality of the image that will be generated. `high`, `medium` and `low` are
  346. only supported for the GPT image models. `dall-e-2` only supports `standard`
  347. quality. Defaults to `auto`.
  348. response_format: The format in which the generated images are returned. Must be one of `url` or
  349. `b64_json`. URLs are only valid for 60 minutes after the image has been
  350. generated. This parameter is only supported for `dall-e-2`, as the GPT image
  351. models always return base64-encoded images.
  352. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  353. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  354. models, and one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
  355. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  356. and detect abuse.
  357. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  358. extra_headers: Send extra headers
  359. extra_query: Add additional query parameters to the request
  360. extra_body: Add additional JSON properties to the request
  361. timeout: Override the client-level default timeout for this request, in seconds
  362. """
  363. ...
  364. @required_args(["image", "prompt"], ["image", "prompt", "stream"])
  365. def edit(
  366. self,
  367. *,
  368. image: Union[FileTypes, SequenceNotStr[FileTypes]],
  369. prompt: str,
  370. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  371. input_fidelity: Optional[Literal["high", "low"]] | Omit = omit,
  372. mask: FileTypes | Omit = omit,
  373. model: Union[str, ImageModel, None] | Omit = omit,
  374. n: Optional[int] | Omit = omit,
  375. output_compression: Optional[int] | Omit = omit,
  376. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  377. partial_images: Optional[int] | Omit = omit,
  378. quality: Optional[Literal["standard", "low", "medium", "high", "auto"]] | Omit = omit,
  379. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  380. size: Optional[Literal["256x256", "512x512", "1024x1024", "1536x1024", "1024x1536", "auto"]] | Omit = omit,
  381. stream: Optional[Literal[False]] | Literal[True] | Omit = omit,
  382. user: str | Omit = omit,
  383. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  384. # The extra values given here take precedence over values defined on the client or passed to this method.
  385. extra_headers: Headers | None = None,
  386. extra_query: Query | None = None,
  387. extra_body: Body | None = None,
  388. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  389. ) -> ImagesResponse | Stream[ImageEditStreamEvent]:
  390. body = deepcopy_minimal(
  391. {
  392. "image": image,
  393. "prompt": prompt,
  394. "background": background,
  395. "input_fidelity": input_fidelity,
  396. "mask": mask,
  397. "model": model,
  398. "n": n,
  399. "output_compression": output_compression,
  400. "output_format": output_format,
  401. "partial_images": partial_images,
  402. "quality": quality,
  403. "response_format": response_format,
  404. "size": size,
  405. "stream": stream,
  406. "user": user,
  407. }
  408. )
  409. files = extract_files(cast(Mapping[str, object], body), paths=[["image"], ["image", "<array>"], ["mask"]])
  410. # It should be noted that the actual Content-Type header that will be
  411. # sent to the server will contain a `boundary` parameter, e.g.
  412. # multipart/form-data; boundary=---abc--
  413. extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
  414. return self._post(
  415. "/images/edits",
  416. body=maybe_transform(
  417. body,
  418. image_edit_params.ImageEditParamsStreaming if stream else image_edit_params.ImageEditParamsNonStreaming,
  419. ),
  420. files=files,
  421. options=make_request_options(
  422. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  423. ),
  424. cast_to=ImagesResponse,
  425. stream=stream or False,
  426. stream_cls=Stream[ImageEditStreamEvent],
  427. )
  428. @overload
  429. def generate(
  430. self,
  431. *,
  432. prompt: str,
  433. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  434. model: Union[str, ImageModel, None] | Omit = omit,
  435. moderation: Optional[Literal["low", "auto"]] | Omit = omit,
  436. n: Optional[int] | Omit = omit,
  437. output_compression: Optional[int] | Omit = omit,
  438. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  439. partial_images: Optional[int] | Omit = omit,
  440. quality: Optional[Literal["standard", "hd", "low", "medium", "high", "auto"]] | Omit = omit,
  441. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  442. size: Optional[
  443. Literal["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]
  444. ]
  445. | Omit = omit,
  446. stream: Optional[Literal[False]] | Omit = omit,
  447. style: Optional[Literal["vivid", "natural"]] | Omit = omit,
  448. user: str | Omit = omit,
  449. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  450. # The extra values given here take precedence over values defined on the client or passed to this method.
  451. extra_headers: Headers | None = None,
  452. extra_query: Query | None = None,
  453. extra_body: Body | None = None,
  454. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  455. ) -> ImagesResponse:
  456. """
  457. Creates an image given a prompt.
  458. [Learn more](https://platform.openai.com/docs/guides/images).
  459. Args:
  460. prompt: A text description of the desired image(s). The maximum length is 32000
  461. characters for the GPT image models, 1000 characters for `dall-e-2` and 4000
  462. characters for `dall-e-3`.
  463. background: Allows to set transparency for the background of the generated image(s). This
  464. parameter is only supported for the GPT image models. Must be one of
  465. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  466. model will automatically determine the best background for the image.
  467. If `transparent`, the output format needs to support transparency, so it should
  468. be set to either `png` (default value) or `webp`.
  469. model: The model to use for image generation. One of `dall-e-2`, `dall-e-3`, or a GPT
  470. image model (`gpt-image-1`, `gpt-image-1-mini`, `gpt-image-1.5`). Defaults to
  471. `dall-e-2` unless a parameter specific to the GPT image models is used.
  472. moderation: Control the content-moderation level for images generated by the GPT image
  473. models. Must be either `low` for less restrictive filtering or `auto` (default
  474. value).
  475. n: The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only
  476. `n=1` is supported.
  477. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  478. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  479. defaults to 100.
  480. output_format: The format in which the generated images are returned. This parameter is only
  481. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
  482. partial_images: The number of partial images to generate. This parameter is used for streaming
  483. responses that return partial images. Value must be between 0 and 3. When set to
  484. 0, the response will be a single image sent in one streaming event.
  485. Note that the final image may be sent before the full number of partial images
  486. are generated if the full image is generated more quickly.
  487. quality: The quality of the image that will be generated.
  488. - `auto` (default value) will automatically select the best quality for the
  489. given model.
  490. - `high`, `medium` and `low` are supported for the GPT image models.
  491. - `hd` and `standard` are supported for `dall-e-3`.
  492. - `standard` is the only option for `dall-e-2`.
  493. response_format: The format in which generated images with `dall-e-2` and `dall-e-3` are
  494. returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes
  495. after the image has been generated. This parameter isn't supported for the GPT
  496. image models, which always return base64-encoded images.
  497. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  498. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  499. models, one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`, and one of
  500. `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3`.
  501. stream: Generate the image in streaming mode. Defaults to `false`. See the
  502. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  503. for more information. This parameter is only supported for the GPT image models.
  504. style: The style of the generated images. This parameter is only supported for
  505. `dall-e-3`. Must be one of `vivid` or `natural`. Vivid causes the model to lean
  506. towards generating hyper-real and dramatic images. Natural causes the model to
  507. produce more natural, less hyper-real looking images.
  508. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  509. and detect abuse.
  510. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  511. extra_headers: Send extra headers
  512. extra_query: Add additional query parameters to the request
  513. extra_body: Add additional JSON properties to the request
  514. timeout: Override the client-level default timeout for this request, in seconds
  515. """
  516. ...
  517. @overload
  518. def generate(
  519. self,
  520. *,
  521. prompt: str,
  522. stream: Literal[True],
  523. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  524. model: Union[str, ImageModel, None] | Omit = omit,
  525. moderation: Optional[Literal["low", "auto"]] | Omit = omit,
  526. n: Optional[int] | Omit = omit,
  527. output_compression: Optional[int] | Omit = omit,
  528. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  529. partial_images: Optional[int] | Omit = omit,
  530. quality: Optional[Literal["standard", "hd", "low", "medium", "high", "auto"]] | Omit = omit,
  531. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  532. size: Optional[
  533. Literal["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]
  534. ]
  535. | Omit = omit,
  536. style: Optional[Literal["vivid", "natural"]] | Omit = omit,
  537. user: str | Omit = omit,
  538. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  539. # The extra values given here take precedence over values defined on the client or passed to this method.
  540. extra_headers: Headers | None = None,
  541. extra_query: Query | None = None,
  542. extra_body: Body | None = None,
  543. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  544. ) -> Stream[ImageGenStreamEvent]:
  545. """
  546. Creates an image given a prompt.
  547. [Learn more](https://platform.openai.com/docs/guides/images).
  548. Args:
  549. prompt: A text description of the desired image(s). The maximum length is 32000
  550. characters for the GPT image models, 1000 characters for `dall-e-2` and 4000
  551. characters for `dall-e-3`.
  552. stream: Generate the image in streaming mode. Defaults to `false`. See the
  553. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  554. for more information. This parameter is only supported for the GPT image models.
  555. background: Allows to set transparency for the background of the generated image(s). This
  556. parameter is only supported for the GPT image models. Must be one of
  557. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  558. model will automatically determine the best background for the image.
  559. If `transparent`, the output format needs to support transparency, so it should
  560. be set to either `png` (default value) or `webp`.
  561. model: The model to use for image generation. One of `dall-e-2`, `dall-e-3`, or a GPT
  562. image model (`gpt-image-1`, `gpt-image-1-mini`, `gpt-image-1.5`). Defaults to
  563. `dall-e-2` unless a parameter specific to the GPT image models is used.
  564. moderation: Control the content-moderation level for images generated by the GPT image
  565. models. Must be either `low` for less restrictive filtering or `auto` (default
  566. value).
  567. n: The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only
  568. `n=1` is supported.
  569. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  570. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  571. defaults to 100.
  572. output_format: The format in which the generated images are returned. This parameter is only
  573. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
  574. partial_images: The number of partial images to generate. This parameter is used for streaming
  575. responses that return partial images. Value must be between 0 and 3. When set to
  576. 0, the response will be a single image sent in one streaming event.
  577. Note that the final image may be sent before the full number of partial images
  578. are generated if the full image is generated more quickly.
  579. quality: The quality of the image that will be generated.
  580. - `auto` (default value) will automatically select the best quality for the
  581. given model.
  582. - `high`, `medium` and `low` are supported for the GPT image models.
  583. - `hd` and `standard` are supported for `dall-e-3`.
  584. - `standard` is the only option for `dall-e-2`.
  585. response_format: The format in which generated images with `dall-e-2` and `dall-e-3` are
  586. returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes
  587. after the image has been generated. This parameter isn't supported for the GPT
  588. image models, which always return base64-encoded images.
  589. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  590. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  591. models, one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`, and one of
  592. `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3`.
  593. style: The style of the generated images. This parameter is only supported for
  594. `dall-e-3`. Must be one of `vivid` or `natural`. Vivid causes the model to lean
  595. towards generating hyper-real and dramatic images. Natural causes the model to
  596. produce more natural, less hyper-real looking images.
  597. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  598. and detect abuse.
  599. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  600. extra_headers: Send extra headers
  601. extra_query: Add additional query parameters to the request
  602. extra_body: Add additional JSON properties to the request
  603. timeout: Override the client-level default timeout for this request, in seconds
  604. """
  605. ...
  606. @overload
  607. def generate(
  608. self,
  609. *,
  610. prompt: str,
  611. stream: bool,
  612. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  613. model: Union[str, ImageModel, None] | Omit = omit,
  614. moderation: Optional[Literal["low", "auto"]] | Omit = omit,
  615. n: Optional[int] | Omit = omit,
  616. output_compression: Optional[int] | Omit = omit,
  617. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  618. partial_images: Optional[int] | Omit = omit,
  619. quality: Optional[Literal["standard", "hd", "low", "medium", "high", "auto"]] | Omit = omit,
  620. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  621. size: Optional[
  622. Literal["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]
  623. ]
  624. | Omit = omit,
  625. style: Optional[Literal["vivid", "natural"]] | Omit = omit,
  626. user: str | Omit = omit,
  627. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  628. # The extra values given here take precedence over values defined on the client or passed to this method.
  629. extra_headers: Headers | None = None,
  630. extra_query: Query | None = None,
  631. extra_body: Body | None = None,
  632. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  633. ) -> ImagesResponse | Stream[ImageGenStreamEvent]:
  634. """
  635. Creates an image given a prompt.
  636. [Learn more](https://platform.openai.com/docs/guides/images).
  637. Args:
  638. prompt: A text description of the desired image(s). The maximum length is 32000
  639. characters for the GPT image models, 1000 characters for `dall-e-2` and 4000
  640. characters for `dall-e-3`.
  641. stream: Generate the image in streaming mode. Defaults to `false`. See the
  642. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  643. for more information. This parameter is only supported for the GPT image models.
  644. background: Allows to set transparency for the background of the generated image(s). This
  645. parameter is only supported for the GPT image models. Must be one of
  646. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  647. model will automatically determine the best background for the image.
  648. If `transparent`, the output format needs to support transparency, so it should
  649. be set to either `png` (default value) or `webp`.
  650. model: The model to use for image generation. One of `dall-e-2`, `dall-e-3`, or a GPT
  651. image model (`gpt-image-1`, `gpt-image-1-mini`, `gpt-image-1.5`). Defaults to
  652. `dall-e-2` unless a parameter specific to the GPT image models is used.
  653. moderation: Control the content-moderation level for images generated by the GPT image
  654. models. Must be either `low` for less restrictive filtering or `auto` (default
  655. value).
  656. n: The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only
  657. `n=1` is supported.
  658. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  659. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  660. defaults to 100.
  661. output_format: The format in which the generated images are returned. This parameter is only
  662. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
  663. partial_images: The number of partial images to generate. This parameter is used for streaming
  664. responses that return partial images. Value must be between 0 and 3. When set to
  665. 0, the response will be a single image sent in one streaming event.
  666. Note that the final image may be sent before the full number of partial images
  667. are generated if the full image is generated more quickly.
  668. quality: The quality of the image that will be generated.
  669. - `auto` (default value) will automatically select the best quality for the
  670. given model.
  671. - `high`, `medium` and `low` are supported for the GPT image models.
  672. - `hd` and `standard` are supported for `dall-e-3`.
  673. - `standard` is the only option for `dall-e-2`.
  674. response_format: The format in which generated images with `dall-e-2` and `dall-e-3` are
  675. returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes
  676. after the image has been generated. This parameter isn't supported for the GPT
  677. image models, which always return base64-encoded images.
  678. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  679. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  680. models, one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`, and one of
  681. `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3`.
  682. style: The style of the generated images. This parameter is only supported for
  683. `dall-e-3`. Must be one of `vivid` or `natural`. Vivid causes the model to lean
  684. towards generating hyper-real and dramatic images. Natural causes the model to
  685. produce more natural, less hyper-real looking images.
  686. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  687. and detect abuse.
  688. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  689. extra_headers: Send extra headers
  690. extra_query: Add additional query parameters to the request
  691. extra_body: Add additional JSON properties to the request
  692. timeout: Override the client-level default timeout for this request, in seconds
  693. """
  694. ...
  695. @required_args(["prompt"], ["prompt", "stream"])
  696. def generate(
  697. self,
  698. *,
  699. prompt: str,
  700. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  701. model: Union[str, ImageModel, None] | Omit = omit,
  702. moderation: Optional[Literal["low", "auto"]] | Omit = omit,
  703. n: Optional[int] | Omit = omit,
  704. output_compression: Optional[int] | Omit = omit,
  705. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  706. partial_images: Optional[int] | Omit = omit,
  707. quality: Optional[Literal["standard", "hd", "low", "medium", "high", "auto"]] | Omit = omit,
  708. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  709. size: Optional[
  710. Literal["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]
  711. ]
  712. | Omit = omit,
  713. stream: Optional[Literal[False]] | Literal[True] | Omit = omit,
  714. style: Optional[Literal["vivid", "natural"]] | Omit = omit,
  715. user: str | Omit = omit,
  716. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  717. # The extra values given here take precedence over values defined on the client or passed to this method.
  718. extra_headers: Headers | None = None,
  719. extra_query: Query | None = None,
  720. extra_body: Body | None = None,
  721. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  722. ) -> ImagesResponse | Stream[ImageGenStreamEvent]:
  723. return self._post(
  724. "/images/generations",
  725. body=maybe_transform(
  726. {
  727. "prompt": prompt,
  728. "background": background,
  729. "model": model,
  730. "moderation": moderation,
  731. "n": n,
  732. "output_compression": output_compression,
  733. "output_format": output_format,
  734. "partial_images": partial_images,
  735. "quality": quality,
  736. "response_format": response_format,
  737. "size": size,
  738. "stream": stream,
  739. "style": style,
  740. "user": user,
  741. },
  742. image_generate_params.ImageGenerateParamsStreaming
  743. if stream
  744. else image_generate_params.ImageGenerateParamsNonStreaming,
  745. ),
  746. options=make_request_options(
  747. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  748. ),
  749. cast_to=ImagesResponse,
  750. stream=stream or False,
  751. stream_cls=Stream[ImageGenStreamEvent],
  752. )
  753. class AsyncImages(AsyncAPIResource):
  754. @cached_property
  755. def with_raw_response(self) -> AsyncImagesWithRawResponse:
  756. """
  757. This property can be used as a prefix for any HTTP method call to return
  758. the raw response object instead of the parsed content.
  759. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  760. """
  761. return AsyncImagesWithRawResponse(self)
  762. @cached_property
  763. def with_streaming_response(self) -> AsyncImagesWithStreamingResponse:
  764. """
  765. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  766. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  767. """
  768. return AsyncImagesWithStreamingResponse(self)
  769. async def create_variation(
  770. self,
  771. *,
  772. image: FileTypes,
  773. model: Union[str, ImageModel, None] | Omit = omit,
  774. n: Optional[int] | Omit = omit,
  775. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  776. size: Optional[Literal["256x256", "512x512", "1024x1024"]] | Omit = omit,
  777. user: str | Omit = omit,
  778. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  779. # The extra values given here take precedence over values defined on the client or passed to this method.
  780. extra_headers: Headers | None = None,
  781. extra_query: Query | None = None,
  782. extra_body: Body | None = None,
  783. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  784. ) -> ImagesResponse:
  785. """Creates a variation of a given image.
  786. This endpoint only supports `dall-e-2`.
  787. Args:
  788. image: The image to use as the basis for the variation(s). Must be a valid PNG file,
  789. less than 4MB, and square.
  790. model: The model to use for image generation. Only `dall-e-2` is supported at this
  791. time.
  792. n: The number of images to generate. Must be between 1 and 10.
  793. response_format: The format in which the generated images are returned. Must be one of `url` or
  794. `b64_json`. URLs are only valid for 60 minutes after the image has been
  795. generated.
  796. size: The size of the generated images. Must be one of `256x256`, `512x512`, or
  797. `1024x1024`.
  798. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  799. and detect abuse.
  800. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  801. extra_headers: Send extra headers
  802. extra_query: Add additional query parameters to the request
  803. extra_body: Add additional JSON properties to the request
  804. timeout: Override the client-level default timeout for this request, in seconds
  805. """
  806. body = deepcopy_minimal(
  807. {
  808. "image": image,
  809. "model": model,
  810. "n": n,
  811. "response_format": response_format,
  812. "size": size,
  813. "user": user,
  814. }
  815. )
  816. files = extract_files(cast(Mapping[str, object], body), paths=[["image"]])
  817. # It should be noted that the actual Content-Type header that will be
  818. # sent to the server will contain a `boundary` parameter, e.g.
  819. # multipart/form-data; boundary=---abc--
  820. extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
  821. return await self._post(
  822. "/images/variations",
  823. body=await async_maybe_transform(body, image_create_variation_params.ImageCreateVariationParams),
  824. files=files,
  825. options=make_request_options(
  826. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  827. ),
  828. cast_to=ImagesResponse,
  829. )
  830. @overload
  831. async def edit(
  832. self,
  833. *,
  834. image: Union[FileTypes, SequenceNotStr[FileTypes]],
  835. prompt: str,
  836. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  837. input_fidelity: Optional[Literal["high", "low"]] | Omit = omit,
  838. mask: FileTypes | Omit = omit,
  839. model: Union[str, ImageModel, None] | Omit = omit,
  840. n: Optional[int] | Omit = omit,
  841. output_compression: Optional[int] | Omit = omit,
  842. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  843. partial_images: Optional[int] | Omit = omit,
  844. quality: Optional[Literal["standard", "low", "medium", "high", "auto"]] | Omit = omit,
  845. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  846. size: Optional[Literal["256x256", "512x512", "1024x1024", "1536x1024", "1024x1536", "auto"]] | Omit = omit,
  847. stream: Optional[Literal[False]] | Omit = omit,
  848. user: str | Omit = omit,
  849. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  850. # The extra values given here take precedence over values defined on the client or passed to this method.
  851. extra_headers: Headers | None = None,
  852. extra_query: Query | None = None,
  853. extra_body: Body | None = None,
  854. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  855. ) -> ImagesResponse:
  856. """Creates an edited or extended image given one or more source images and a
  857. prompt.
  858. This endpoint only supports `gpt-image-1` and `dall-e-2`.
  859. Args:
  860. image: The image(s) to edit. Must be a supported image file or an array of images.
  861. For the GPT image models (`gpt-image-1`, `gpt-image-1-mini`, and
  862. `gpt-image-1.5`), each image should be a `png`, `webp`, or `jpg` file less than
  863. 50MB. You can provide up to 16 images.
  864. For `dall-e-2`, you can only provide one image, and it should be a square `png`
  865. file less than 4MB.
  866. prompt: A text description of the desired image(s). The maximum length is 1000
  867. characters for `dall-e-2`, and 32000 characters for the GPT image models.
  868. background: Allows to set transparency for the background of the generated image(s). This
  869. parameter is only supported for the GPT image models. Must be one of
  870. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  871. model will automatically determine the best background for the image.
  872. If `transparent`, the output format needs to support transparency, so it should
  873. be set to either `png` (default value) or `webp`.
  874. input_fidelity: Control how much effort the model will exert to match the style and features,
  875. especially facial features, of input images. This parameter is only supported
  876. for `gpt-image-1`. Unsupported for `gpt-image-1-mini`. Supports `high` and
  877. `low`. Defaults to `low`.
  878. mask: An additional image whose fully transparent areas (e.g. where alpha is zero)
  879. indicate where `image` should be edited. If there are multiple images provided,
  880. the mask will be applied on the first image. Must be a valid PNG file, less than
  881. 4MB, and have the same dimensions as `image`.
  882. model: The model to use for image generation. Only `dall-e-2` and the GPT image models
  883. are supported. Defaults to `dall-e-2` unless a parameter specific to the GPT
  884. image models is used.
  885. n: The number of images to generate. Must be between 1 and 10.
  886. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  887. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  888. defaults to 100.
  889. output_format: The format in which the generated images are returned. This parameter is only
  890. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`. The
  891. default value is `png`.
  892. partial_images: The number of partial images to generate. This parameter is used for streaming
  893. responses that return partial images. Value must be between 0 and 3. When set to
  894. 0, the response will be a single image sent in one streaming event.
  895. Note that the final image may be sent before the full number of partial images
  896. are generated if the full image is generated more quickly.
  897. quality: The quality of the image that will be generated. `high`, `medium` and `low` are
  898. only supported for the GPT image models. `dall-e-2` only supports `standard`
  899. quality. Defaults to `auto`.
  900. response_format: The format in which the generated images are returned. Must be one of `url` or
  901. `b64_json`. URLs are only valid for 60 minutes after the image has been
  902. generated. This parameter is only supported for `dall-e-2`, as the GPT image
  903. models always return base64-encoded images.
  904. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  905. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  906. models, and one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
  907. stream: Edit the image in streaming mode. Defaults to `false`. See the
  908. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  909. for more information.
  910. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  911. and detect abuse.
  912. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  913. extra_headers: Send extra headers
  914. extra_query: Add additional query parameters to the request
  915. extra_body: Add additional JSON properties to the request
  916. timeout: Override the client-level default timeout for this request, in seconds
  917. """
  918. ...
  919. @overload
  920. async def edit(
  921. self,
  922. *,
  923. image: Union[FileTypes, SequenceNotStr[FileTypes]],
  924. prompt: str,
  925. stream: Literal[True],
  926. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  927. input_fidelity: Optional[Literal["high", "low"]] | Omit = omit,
  928. mask: FileTypes | Omit = omit,
  929. model: Union[str, ImageModel, None] | Omit = omit,
  930. n: Optional[int] | Omit = omit,
  931. output_compression: Optional[int] | Omit = omit,
  932. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  933. partial_images: Optional[int] | Omit = omit,
  934. quality: Optional[Literal["standard", "low", "medium", "high", "auto"]] | Omit = omit,
  935. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  936. size: Optional[Literal["256x256", "512x512", "1024x1024", "1536x1024", "1024x1536", "auto"]] | Omit = omit,
  937. user: str | Omit = omit,
  938. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  939. # The extra values given here take precedence over values defined on the client or passed to this method.
  940. extra_headers: Headers | None = None,
  941. extra_query: Query | None = None,
  942. extra_body: Body | None = None,
  943. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  944. ) -> AsyncStream[ImageEditStreamEvent]:
  945. """Creates an edited or extended image given one or more source images and a
  946. prompt.
  947. This endpoint only supports `gpt-image-1` and `dall-e-2`.
  948. Args:
  949. image: The image(s) to edit. Must be a supported image file or an array of images.
  950. For the GPT image models (`gpt-image-1`, `gpt-image-1-mini`, and
  951. `gpt-image-1.5`), each image should be a `png`, `webp`, or `jpg` file less than
  952. 50MB. You can provide up to 16 images.
  953. For `dall-e-2`, you can only provide one image, and it should be a square `png`
  954. file less than 4MB.
  955. prompt: A text description of the desired image(s). The maximum length is 1000
  956. characters for `dall-e-2`, and 32000 characters for the GPT image models.
  957. stream: Edit the image in streaming mode. Defaults to `false`. See the
  958. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  959. for more information.
  960. background: Allows to set transparency for the background of the generated image(s). This
  961. parameter is only supported for the GPT image models. Must be one of
  962. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  963. model will automatically determine the best background for the image.
  964. If `transparent`, the output format needs to support transparency, so it should
  965. be set to either `png` (default value) or `webp`.
  966. input_fidelity: Control how much effort the model will exert to match the style and features,
  967. especially facial features, of input images. This parameter is only supported
  968. for `gpt-image-1`. Unsupported for `gpt-image-1-mini`. Supports `high` and
  969. `low`. Defaults to `low`.
  970. mask: An additional image whose fully transparent areas (e.g. where alpha is zero)
  971. indicate where `image` should be edited. If there are multiple images provided,
  972. the mask will be applied on the first image. Must be a valid PNG file, less than
  973. 4MB, and have the same dimensions as `image`.
  974. model: The model to use for image generation. Only `dall-e-2` and the GPT image models
  975. are supported. Defaults to `dall-e-2` unless a parameter specific to the GPT
  976. image models is used.
  977. n: The number of images to generate. Must be between 1 and 10.
  978. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  979. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  980. defaults to 100.
  981. output_format: The format in which the generated images are returned. This parameter is only
  982. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`. The
  983. default value is `png`.
  984. partial_images: The number of partial images to generate. This parameter is used for streaming
  985. responses that return partial images. Value must be between 0 and 3. When set to
  986. 0, the response will be a single image sent in one streaming event.
  987. Note that the final image may be sent before the full number of partial images
  988. are generated if the full image is generated more quickly.
  989. quality: The quality of the image that will be generated. `high`, `medium` and `low` are
  990. only supported for the GPT image models. `dall-e-2` only supports `standard`
  991. quality. Defaults to `auto`.
  992. response_format: The format in which the generated images are returned. Must be one of `url` or
  993. `b64_json`. URLs are only valid for 60 minutes after the image has been
  994. generated. This parameter is only supported for `dall-e-2`, as the GPT image
  995. models always return base64-encoded images.
  996. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  997. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  998. models, and one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
  999. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  1000. and detect abuse.
  1001. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  1002. extra_headers: Send extra headers
  1003. extra_query: Add additional query parameters to the request
  1004. extra_body: Add additional JSON properties to the request
  1005. timeout: Override the client-level default timeout for this request, in seconds
  1006. """
  1007. ...
  1008. @overload
  1009. async def edit(
  1010. self,
  1011. *,
  1012. image: Union[FileTypes, SequenceNotStr[FileTypes]],
  1013. prompt: str,
  1014. stream: bool,
  1015. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  1016. input_fidelity: Optional[Literal["high", "low"]] | Omit = omit,
  1017. mask: FileTypes | Omit = omit,
  1018. model: Union[str, ImageModel, None] | Omit = omit,
  1019. n: Optional[int] | Omit = omit,
  1020. output_compression: Optional[int] | Omit = omit,
  1021. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  1022. partial_images: Optional[int] | Omit = omit,
  1023. quality: Optional[Literal["standard", "low", "medium", "high", "auto"]] | Omit = omit,
  1024. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  1025. size: Optional[Literal["256x256", "512x512", "1024x1024", "1536x1024", "1024x1536", "auto"]] | Omit = omit,
  1026. user: str | Omit = omit,
  1027. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  1028. # The extra values given here take precedence over values defined on the client or passed to this method.
  1029. extra_headers: Headers | None = None,
  1030. extra_query: Query | None = None,
  1031. extra_body: Body | None = None,
  1032. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  1033. ) -> ImagesResponse | AsyncStream[ImageEditStreamEvent]:
  1034. """Creates an edited or extended image given one or more source images and a
  1035. prompt.
  1036. This endpoint only supports `gpt-image-1` and `dall-e-2`.
  1037. Args:
  1038. image: The image(s) to edit. Must be a supported image file or an array of images.
  1039. For the GPT image models (`gpt-image-1`, `gpt-image-1-mini`, and
  1040. `gpt-image-1.5`), each image should be a `png`, `webp`, or `jpg` file less than
  1041. 50MB. You can provide up to 16 images.
  1042. For `dall-e-2`, you can only provide one image, and it should be a square `png`
  1043. file less than 4MB.
  1044. prompt: A text description of the desired image(s). The maximum length is 1000
  1045. characters for `dall-e-2`, and 32000 characters for the GPT image models.
  1046. stream: Edit the image in streaming mode. Defaults to `false`. See the
  1047. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  1048. for more information.
  1049. background: Allows to set transparency for the background of the generated image(s). This
  1050. parameter is only supported for the GPT image models. Must be one of
  1051. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  1052. model will automatically determine the best background for the image.
  1053. If `transparent`, the output format needs to support transparency, so it should
  1054. be set to either `png` (default value) or `webp`.
  1055. input_fidelity: Control how much effort the model will exert to match the style and features,
  1056. especially facial features, of input images. This parameter is only supported
  1057. for `gpt-image-1`. Unsupported for `gpt-image-1-mini`. Supports `high` and
  1058. `low`. Defaults to `low`.
  1059. mask: An additional image whose fully transparent areas (e.g. where alpha is zero)
  1060. indicate where `image` should be edited. If there are multiple images provided,
  1061. the mask will be applied on the first image. Must be a valid PNG file, less than
  1062. 4MB, and have the same dimensions as `image`.
  1063. model: The model to use for image generation. Only `dall-e-2` and the GPT image models
  1064. are supported. Defaults to `dall-e-2` unless a parameter specific to the GPT
  1065. image models is used.
  1066. n: The number of images to generate. Must be between 1 and 10.
  1067. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  1068. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  1069. defaults to 100.
  1070. output_format: The format in which the generated images are returned. This parameter is only
  1071. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`. The
  1072. default value is `png`.
  1073. partial_images: The number of partial images to generate. This parameter is used for streaming
  1074. responses that return partial images. Value must be between 0 and 3. When set to
  1075. 0, the response will be a single image sent in one streaming event.
  1076. Note that the final image may be sent before the full number of partial images
  1077. are generated if the full image is generated more quickly.
  1078. quality: The quality of the image that will be generated. `high`, `medium` and `low` are
  1079. only supported for the GPT image models. `dall-e-2` only supports `standard`
  1080. quality. Defaults to `auto`.
  1081. response_format: The format in which the generated images are returned. Must be one of `url` or
  1082. `b64_json`. URLs are only valid for 60 minutes after the image has been
  1083. generated. This parameter is only supported for `dall-e-2`, as the GPT image
  1084. models always return base64-encoded images.
  1085. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  1086. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  1087. models, and one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`.
  1088. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  1089. and detect abuse.
  1090. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  1091. extra_headers: Send extra headers
  1092. extra_query: Add additional query parameters to the request
  1093. extra_body: Add additional JSON properties to the request
  1094. timeout: Override the client-level default timeout for this request, in seconds
  1095. """
  1096. ...
  1097. @required_args(["image", "prompt"], ["image", "prompt", "stream"])
  1098. async def edit(
  1099. self,
  1100. *,
  1101. image: Union[FileTypes, SequenceNotStr[FileTypes]],
  1102. prompt: str,
  1103. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  1104. input_fidelity: Optional[Literal["high", "low"]] | Omit = omit,
  1105. mask: FileTypes | Omit = omit,
  1106. model: Union[str, ImageModel, None] | Omit = omit,
  1107. n: Optional[int] | Omit = omit,
  1108. output_compression: Optional[int] | Omit = omit,
  1109. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  1110. partial_images: Optional[int] | Omit = omit,
  1111. quality: Optional[Literal["standard", "low", "medium", "high", "auto"]] | Omit = omit,
  1112. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  1113. size: Optional[Literal["256x256", "512x512", "1024x1024", "1536x1024", "1024x1536", "auto"]] | Omit = omit,
  1114. stream: Optional[Literal[False]] | Literal[True] | Omit = omit,
  1115. user: str | Omit = omit,
  1116. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  1117. # The extra values given here take precedence over values defined on the client or passed to this method.
  1118. extra_headers: Headers | None = None,
  1119. extra_query: Query | None = None,
  1120. extra_body: Body | None = None,
  1121. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  1122. ) -> ImagesResponse | AsyncStream[ImageEditStreamEvent]:
  1123. body = deepcopy_minimal(
  1124. {
  1125. "image": image,
  1126. "prompt": prompt,
  1127. "background": background,
  1128. "input_fidelity": input_fidelity,
  1129. "mask": mask,
  1130. "model": model,
  1131. "n": n,
  1132. "output_compression": output_compression,
  1133. "output_format": output_format,
  1134. "partial_images": partial_images,
  1135. "quality": quality,
  1136. "response_format": response_format,
  1137. "size": size,
  1138. "stream": stream,
  1139. "user": user,
  1140. }
  1141. )
  1142. files = extract_files(cast(Mapping[str, object], body), paths=[["image"], ["image", "<array>"], ["mask"]])
  1143. # It should be noted that the actual Content-Type header that will be
  1144. # sent to the server will contain a `boundary` parameter, e.g.
  1145. # multipart/form-data; boundary=---abc--
  1146. extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
  1147. return await self._post(
  1148. "/images/edits",
  1149. body=await async_maybe_transform(
  1150. body,
  1151. image_edit_params.ImageEditParamsStreaming if stream else image_edit_params.ImageEditParamsNonStreaming,
  1152. ),
  1153. files=files,
  1154. options=make_request_options(
  1155. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  1156. ),
  1157. cast_to=ImagesResponse,
  1158. stream=stream or False,
  1159. stream_cls=AsyncStream[ImageEditStreamEvent],
  1160. )
  1161. @overload
  1162. async def generate(
  1163. self,
  1164. *,
  1165. prompt: str,
  1166. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  1167. model: Union[str, ImageModel, None] | Omit = omit,
  1168. moderation: Optional[Literal["low", "auto"]] | Omit = omit,
  1169. n: Optional[int] | Omit = omit,
  1170. output_compression: Optional[int] | Omit = omit,
  1171. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  1172. partial_images: Optional[int] | Omit = omit,
  1173. quality: Optional[Literal["standard", "hd", "low", "medium", "high", "auto"]] | Omit = omit,
  1174. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  1175. size: Optional[
  1176. Literal["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]
  1177. ]
  1178. | Omit = omit,
  1179. stream: Optional[Literal[False]] | Omit = omit,
  1180. style: Optional[Literal["vivid", "natural"]] | Omit = omit,
  1181. user: str | Omit = omit,
  1182. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  1183. # The extra values given here take precedence over values defined on the client or passed to this method.
  1184. extra_headers: Headers | None = None,
  1185. extra_query: Query | None = None,
  1186. extra_body: Body | None = None,
  1187. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  1188. ) -> ImagesResponse:
  1189. """
  1190. Creates an image given a prompt.
  1191. [Learn more](https://platform.openai.com/docs/guides/images).
  1192. Args:
  1193. prompt: A text description of the desired image(s). The maximum length is 32000
  1194. characters for the GPT image models, 1000 characters for `dall-e-2` and 4000
  1195. characters for `dall-e-3`.
  1196. background: Allows to set transparency for the background of the generated image(s). This
  1197. parameter is only supported for the GPT image models. Must be one of
  1198. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  1199. model will automatically determine the best background for the image.
  1200. If `transparent`, the output format needs to support transparency, so it should
  1201. be set to either `png` (default value) or `webp`.
  1202. model: The model to use for image generation. One of `dall-e-2`, `dall-e-3`, or a GPT
  1203. image model (`gpt-image-1`, `gpt-image-1-mini`, `gpt-image-1.5`). Defaults to
  1204. `dall-e-2` unless a parameter specific to the GPT image models is used.
  1205. moderation: Control the content-moderation level for images generated by the GPT image
  1206. models. Must be either `low` for less restrictive filtering or `auto` (default
  1207. value).
  1208. n: The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only
  1209. `n=1` is supported.
  1210. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  1211. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  1212. defaults to 100.
  1213. output_format: The format in which the generated images are returned. This parameter is only
  1214. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
  1215. partial_images: The number of partial images to generate. This parameter is used for streaming
  1216. responses that return partial images. Value must be between 0 and 3. When set to
  1217. 0, the response will be a single image sent in one streaming event.
  1218. Note that the final image may be sent before the full number of partial images
  1219. are generated if the full image is generated more quickly.
  1220. quality: The quality of the image that will be generated.
  1221. - `auto` (default value) will automatically select the best quality for the
  1222. given model.
  1223. - `high`, `medium` and `low` are supported for the GPT image models.
  1224. - `hd` and `standard` are supported for `dall-e-3`.
  1225. - `standard` is the only option for `dall-e-2`.
  1226. response_format: The format in which generated images with `dall-e-2` and `dall-e-3` are
  1227. returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes
  1228. after the image has been generated. This parameter isn't supported for the GPT
  1229. image models, which always return base64-encoded images.
  1230. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  1231. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  1232. models, one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`, and one of
  1233. `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3`.
  1234. stream: Generate the image in streaming mode. Defaults to `false`. See the
  1235. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  1236. for more information. This parameter is only supported for the GPT image models.
  1237. style: The style of the generated images. This parameter is only supported for
  1238. `dall-e-3`. Must be one of `vivid` or `natural`. Vivid causes the model to lean
  1239. towards generating hyper-real and dramatic images. Natural causes the model to
  1240. produce more natural, less hyper-real looking images.
  1241. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  1242. and detect abuse.
  1243. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  1244. extra_headers: Send extra headers
  1245. extra_query: Add additional query parameters to the request
  1246. extra_body: Add additional JSON properties to the request
  1247. timeout: Override the client-level default timeout for this request, in seconds
  1248. """
  1249. ...
  1250. @overload
  1251. async def generate(
  1252. self,
  1253. *,
  1254. prompt: str,
  1255. stream: Literal[True],
  1256. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  1257. model: Union[str, ImageModel, None] | Omit = omit,
  1258. moderation: Optional[Literal["low", "auto"]] | Omit = omit,
  1259. n: Optional[int] | Omit = omit,
  1260. output_compression: Optional[int] | Omit = omit,
  1261. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  1262. partial_images: Optional[int] | Omit = omit,
  1263. quality: Optional[Literal["standard", "hd", "low", "medium", "high", "auto"]] | Omit = omit,
  1264. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  1265. size: Optional[
  1266. Literal["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]
  1267. ]
  1268. | Omit = omit,
  1269. style: Optional[Literal["vivid", "natural"]] | Omit = omit,
  1270. user: str | Omit = omit,
  1271. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  1272. # The extra values given here take precedence over values defined on the client or passed to this method.
  1273. extra_headers: Headers | None = None,
  1274. extra_query: Query | None = None,
  1275. extra_body: Body | None = None,
  1276. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  1277. ) -> AsyncStream[ImageGenStreamEvent]:
  1278. """
  1279. Creates an image given a prompt.
  1280. [Learn more](https://platform.openai.com/docs/guides/images).
  1281. Args:
  1282. prompt: A text description of the desired image(s). The maximum length is 32000
  1283. characters for the GPT image models, 1000 characters for `dall-e-2` and 4000
  1284. characters for `dall-e-3`.
  1285. stream: Generate the image in streaming mode. Defaults to `false`. See the
  1286. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  1287. for more information. This parameter is only supported for the GPT image models.
  1288. background: Allows to set transparency for the background of the generated image(s). This
  1289. parameter is only supported for the GPT image models. Must be one of
  1290. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  1291. model will automatically determine the best background for the image.
  1292. If `transparent`, the output format needs to support transparency, so it should
  1293. be set to either `png` (default value) or `webp`.
  1294. model: The model to use for image generation. One of `dall-e-2`, `dall-e-3`, or a GPT
  1295. image model (`gpt-image-1`, `gpt-image-1-mini`, `gpt-image-1.5`). Defaults to
  1296. `dall-e-2` unless a parameter specific to the GPT image models is used.
  1297. moderation: Control the content-moderation level for images generated by the GPT image
  1298. models. Must be either `low` for less restrictive filtering or `auto` (default
  1299. value).
  1300. n: The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only
  1301. `n=1` is supported.
  1302. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  1303. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  1304. defaults to 100.
  1305. output_format: The format in which the generated images are returned. This parameter is only
  1306. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
  1307. partial_images: The number of partial images to generate. This parameter is used for streaming
  1308. responses that return partial images. Value must be between 0 and 3. When set to
  1309. 0, the response will be a single image sent in one streaming event.
  1310. Note that the final image may be sent before the full number of partial images
  1311. are generated if the full image is generated more quickly.
  1312. quality: The quality of the image that will be generated.
  1313. - `auto` (default value) will automatically select the best quality for the
  1314. given model.
  1315. - `high`, `medium` and `low` are supported for the GPT image models.
  1316. - `hd` and `standard` are supported for `dall-e-3`.
  1317. - `standard` is the only option for `dall-e-2`.
  1318. response_format: The format in which generated images with `dall-e-2` and `dall-e-3` are
  1319. returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes
  1320. after the image has been generated. This parameter isn't supported for the GPT
  1321. image models, which always return base64-encoded images.
  1322. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  1323. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  1324. models, one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`, and one of
  1325. `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3`.
  1326. style: The style of the generated images. This parameter is only supported for
  1327. `dall-e-3`. Must be one of `vivid` or `natural`. Vivid causes the model to lean
  1328. towards generating hyper-real and dramatic images. Natural causes the model to
  1329. produce more natural, less hyper-real looking images.
  1330. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  1331. and detect abuse.
  1332. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  1333. extra_headers: Send extra headers
  1334. extra_query: Add additional query parameters to the request
  1335. extra_body: Add additional JSON properties to the request
  1336. timeout: Override the client-level default timeout for this request, in seconds
  1337. """
  1338. ...
  1339. @overload
  1340. async def generate(
  1341. self,
  1342. *,
  1343. prompt: str,
  1344. stream: bool,
  1345. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  1346. model: Union[str, ImageModel, None] | Omit = omit,
  1347. moderation: Optional[Literal["low", "auto"]] | Omit = omit,
  1348. n: Optional[int] | Omit = omit,
  1349. output_compression: Optional[int] | Omit = omit,
  1350. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  1351. partial_images: Optional[int] | Omit = omit,
  1352. quality: Optional[Literal["standard", "hd", "low", "medium", "high", "auto"]] | Omit = omit,
  1353. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  1354. size: Optional[
  1355. Literal["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]
  1356. ]
  1357. | Omit = omit,
  1358. style: Optional[Literal["vivid", "natural"]] | Omit = omit,
  1359. user: str | Omit = omit,
  1360. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  1361. # The extra values given here take precedence over values defined on the client or passed to this method.
  1362. extra_headers: Headers | None = None,
  1363. extra_query: Query | None = None,
  1364. extra_body: Body | None = None,
  1365. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  1366. ) -> ImagesResponse | AsyncStream[ImageGenStreamEvent]:
  1367. """
  1368. Creates an image given a prompt.
  1369. [Learn more](https://platform.openai.com/docs/guides/images).
  1370. Args:
  1371. prompt: A text description of the desired image(s). The maximum length is 32000
  1372. characters for the GPT image models, 1000 characters for `dall-e-2` and 4000
  1373. characters for `dall-e-3`.
  1374. stream: Generate the image in streaming mode. Defaults to `false`. See the
  1375. [Image generation guide](https://platform.openai.com/docs/guides/image-generation)
  1376. for more information. This parameter is only supported for the GPT image models.
  1377. background: Allows to set transparency for the background of the generated image(s). This
  1378. parameter is only supported for the GPT image models. Must be one of
  1379. `transparent`, `opaque` or `auto` (default value). When `auto` is used, the
  1380. model will automatically determine the best background for the image.
  1381. If `transparent`, the output format needs to support transparency, so it should
  1382. be set to either `png` (default value) or `webp`.
  1383. model: The model to use for image generation. One of `dall-e-2`, `dall-e-3`, or a GPT
  1384. image model (`gpt-image-1`, `gpt-image-1-mini`, `gpt-image-1.5`). Defaults to
  1385. `dall-e-2` unless a parameter specific to the GPT image models is used.
  1386. moderation: Control the content-moderation level for images generated by the GPT image
  1387. models. Must be either `low` for less restrictive filtering or `auto` (default
  1388. value).
  1389. n: The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only
  1390. `n=1` is supported.
  1391. output_compression: The compression level (0-100%) for the generated images. This parameter is only
  1392. supported for the GPT image models with the `webp` or `jpeg` output formats, and
  1393. defaults to 100.
  1394. output_format: The format in which the generated images are returned. This parameter is only
  1395. supported for the GPT image models. Must be one of `png`, `jpeg`, or `webp`.
  1396. partial_images: The number of partial images to generate. This parameter is used for streaming
  1397. responses that return partial images. Value must be between 0 and 3. When set to
  1398. 0, the response will be a single image sent in one streaming event.
  1399. Note that the final image may be sent before the full number of partial images
  1400. are generated if the full image is generated more quickly.
  1401. quality: The quality of the image that will be generated.
  1402. - `auto` (default value) will automatically select the best quality for the
  1403. given model.
  1404. - `high`, `medium` and `low` are supported for the GPT image models.
  1405. - `hd` and `standard` are supported for `dall-e-3`.
  1406. - `standard` is the only option for `dall-e-2`.
  1407. response_format: The format in which generated images with `dall-e-2` and `dall-e-3` are
  1408. returned. Must be one of `url` or `b64_json`. URLs are only valid for 60 minutes
  1409. after the image has been generated. This parameter isn't supported for the GPT
  1410. image models, which always return base64-encoded images.
  1411. size: The size of the generated images. Must be one of `1024x1024`, `1536x1024`
  1412. (landscape), `1024x1536` (portrait), or `auto` (default value) for the GPT image
  1413. models, one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`, and one of
  1414. `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3`.
  1415. style: The style of the generated images. This parameter is only supported for
  1416. `dall-e-3`. Must be one of `vivid` or `natural`. Vivid causes the model to lean
  1417. towards generating hyper-real and dramatic images. Natural causes the model to
  1418. produce more natural, less hyper-real looking images.
  1419. user: A unique identifier representing your end-user, which can help OpenAI to monitor
  1420. and detect abuse.
  1421. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices#end-user-ids).
  1422. extra_headers: Send extra headers
  1423. extra_query: Add additional query parameters to the request
  1424. extra_body: Add additional JSON properties to the request
  1425. timeout: Override the client-level default timeout for this request, in seconds
  1426. """
  1427. ...
  1428. @required_args(["prompt"], ["prompt", "stream"])
  1429. async def generate(
  1430. self,
  1431. *,
  1432. prompt: str,
  1433. background: Optional[Literal["transparent", "opaque", "auto"]] | Omit = omit,
  1434. model: Union[str, ImageModel, None] | Omit = omit,
  1435. moderation: Optional[Literal["low", "auto"]] | Omit = omit,
  1436. n: Optional[int] | Omit = omit,
  1437. output_compression: Optional[int] | Omit = omit,
  1438. output_format: Optional[Literal["png", "jpeg", "webp"]] | Omit = omit,
  1439. partial_images: Optional[int] | Omit = omit,
  1440. quality: Optional[Literal["standard", "hd", "low", "medium", "high", "auto"]] | Omit = omit,
  1441. response_format: Optional[Literal["url", "b64_json"]] | Omit = omit,
  1442. size: Optional[
  1443. Literal["auto", "1024x1024", "1536x1024", "1024x1536", "256x256", "512x512", "1792x1024", "1024x1792"]
  1444. ]
  1445. | Omit = omit,
  1446. stream: Optional[Literal[False]] | Literal[True] | Omit = omit,
  1447. style: Optional[Literal["vivid", "natural"]] | Omit = omit,
  1448. user: str | Omit = omit,
  1449. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  1450. # The extra values given here take precedence over values defined on the client or passed to this method.
  1451. extra_headers: Headers | None = None,
  1452. extra_query: Query | None = None,
  1453. extra_body: Body | None = None,
  1454. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  1455. ) -> ImagesResponse | AsyncStream[ImageGenStreamEvent]:
  1456. return await self._post(
  1457. "/images/generations",
  1458. body=await async_maybe_transform(
  1459. {
  1460. "prompt": prompt,
  1461. "background": background,
  1462. "model": model,
  1463. "moderation": moderation,
  1464. "n": n,
  1465. "output_compression": output_compression,
  1466. "output_format": output_format,
  1467. "partial_images": partial_images,
  1468. "quality": quality,
  1469. "response_format": response_format,
  1470. "size": size,
  1471. "stream": stream,
  1472. "style": style,
  1473. "user": user,
  1474. },
  1475. image_generate_params.ImageGenerateParamsStreaming
  1476. if stream
  1477. else image_generate_params.ImageGenerateParamsNonStreaming,
  1478. ),
  1479. options=make_request_options(
  1480. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  1481. ),
  1482. cast_to=ImagesResponse,
  1483. stream=stream or False,
  1484. stream_cls=AsyncStream[ImageGenStreamEvent],
  1485. )
  1486. class ImagesWithRawResponse:
  1487. def __init__(self, images: Images) -> None:
  1488. self._images = images
  1489. self.create_variation = _legacy_response.to_raw_response_wrapper(
  1490. images.create_variation,
  1491. )
  1492. self.edit = _legacy_response.to_raw_response_wrapper(
  1493. images.edit,
  1494. )
  1495. self.generate = _legacy_response.to_raw_response_wrapper(
  1496. images.generate,
  1497. )
  1498. class AsyncImagesWithRawResponse:
  1499. def __init__(self, images: AsyncImages) -> None:
  1500. self._images = images
  1501. self.create_variation = _legacy_response.async_to_raw_response_wrapper(
  1502. images.create_variation,
  1503. )
  1504. self.edit = _legacy_response.async_to_raw_response_wrapper(
  1505. images.edit,
  1506. )
  1507. self.generate = _legacy_response.async_to_raw_response_wrapper(
  1508. images.generate,
  1509. )
  1510. class ImagesWithStreamingResponse:
  1511. def __init__(self, images: Images) -> None:
  1512. self._images = images
  1513. self.create_variation = to_streamed_response_wrapper(
  1514. images.create_variation,
  1515. )
  1516. self.edit = to_streamed_response_wrapper(
  1517. images.edit,
  1518. )
  1519. self.generate = to_streamed_response_wrapper(
  1520. images.generate,
  1521. )
  1522. class AsyncImagesWithStreamingResponse:
  1523. def __init__(self, images: AsyncImages) -> None:
  1524. self._images = images
  1525. self.create_variation = async_to_streamed_response_wrapper(
  1526. images.create_variation,
  1527. )
  1528. self.edit = async_to_streamed_response_wrapper(
  1529. images.edit,
  1530. )
  1531. self.generate = async_to_streamed_response_wrapper(
  1532. images.generate,
  1533. )