openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
README.md
859lines · modeblame
08b8179aDavid Schnurr2 years ago | 1 | # OpenAI Python API library |
3c6d4cd6Greg Brockman5 years ago | 2 | |
db5c3504stainless-app[bot]11 months ago | 3 | <!-- prettier-ignore --> |
| 4 | [)](https://pypi.org/project/openai/) | |
3c6d4cd6Greg Brockman5 years ago | 5 | |
cb88c2f0stainless-app[bot]1 years ago | 6 | The OpenAI Python library provides convenient access to the OpenAI REST API from any Python 3.8+ |
08b8179aDavid Schnurr2 years ago | 7 | application. The library includes type definitions for all request params and response fields, |
| 8 | and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx). | |
| 9 | | |
| 10 | It is generated from our [OpenAPI specification](https://github.com/openai/openai-openapi) with [Stainless](https://stainlessapi.com/). | |
| 11 | | |
| 12 | ## Documentation | |
| 13 | | |
2954945eRobert Craigie1 years ago | 14 | The REST API documentation can be found on [platform.openai.com](https://platform.openai.com/docs/api-reference). The full API of this library can be found in [api.md](api.md). |
3c6d4cd6Greg Brockman5 years ago | 15 | |
| 16 | ## Installation | |
| 17 | | |
| 18 | ```sh | |
1879c97aStainless Bot2 years ago | 19 | # install from PyPI |
6d217096Robert Craigie2 years ago | 20 | pip install openai |
3c6d4cd6Greg Brockman5 years ago | 21 | ``` |
| 22 | | |
08b8179aDavid Schnurr2 years ago | 23 | ## Usage |
| 24 | | |
986f3128Stainless Bot2 years ago | 25 | The full API of this library can be found in [api.md](api.md). |
376dd199Logan Kilpatrick2 years ago | 26 | |
2954945eRobert Craigie1 years ago | 27 | The primary API for interacting with OpenAI models is the [Responses API](https://platform.openai.com/docs/api-reference/responses). You can generate text from the model with the code below. |
| 28 | | |
376dd199Logan Kilpatrick2 years ago | 29 | ```python |
fb5ba01cStainless Bot2 years ago | 30 | import os |
08b8179aDavid Schnurr2 years ago | 31 | from openai import OpenAI |
| 32 | | |
| 33 | client = OpenAI( | |
2954945eRobert Craigie1 years ago | 34 | # This is the default and can be omitted |
| 35 | api_key=os.environ.get("OPENAI_API_KEY"), | |
| 36 | ) | |
| 37 | | |
| 38 | response = client.responses.create( | |
| 39 | model="gpt-4o", | |
| 40 | instructions="You are a coding assistant that talks like a pirate.", | |
| 41 | input="How do I check if a Python object is an instance of a class?", | |
08b8179aDavid Schnurr2 years ago | 42 | ) |
| 43 | | |
2954945eRobert Craigie1 years ago | 44 | print(response.output_text) |
| 45 | ``` | |
| 46 | | |
| 47 | The previous standard (supported indefinitely) for generating text is the [Chat Completions API](https://platform.openai.com/docs/api-reference/chat). You can use that API to generate text from the model with the code below. | |
| 48 | | |
| 49 | ```python | |
| 50 | from openai import OpenAI | |
| 51 | | |
| 52 | client = OpenAI() | |
| 53 | | |
| 54 | completion = client.chat.completions.create( | |
| 55 | model="gpt-4o", | |
08b8179aDavid Schnurr2 years ago | 56 | messages=[ |
2954945eRobert Craigie1 years ago | 57 | {"role": "developer", "content": "Talk like a pirate."}, |
08b8179aDavid Schnurr2 years ago | 58 | { |
| 59 | "role": "user", | |
2954945eRobert Craigie1 years ago | 60 | "content": "How do I check if a Python object is an instance of a class?", |
| 61 | }, | |
08b8179aDavid Schnurr2 years ago | 62 | ], |
| 63 | ) | |
2954945eRobert Craigie1 years ago | 64 | |
| 65 | print(completion.choices[0].message.content) | |
376dd199Logan Kilpatrick2 years ago | 66 | ``` |
| 67 | | |
08b8179aDavid Schnurr2 years ago | 68 | While you can provide an `api_key` keyword argument, |
| 69 | we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/) | |
| 70 | to add `OPENAI_API_KEY="My API Key"` to your `.env` file | |
2954945eRobert Craigie1 years ago | 71 | so that your API key is not stored in source control. |
| 72 | [Get an API key here](https://platform.openai.com/settings/organization/api-keys). | |
3c6d4cd6Greg Brockman5 years ago | 73 | |
192b8f2bDan Corin1 years ago | 74 | ### Vision |
| 75 | | |
2954945eRobert Craigie1 years ago | 76 | With an image URL: |
192b8f2bDan Corin1 years ago | 77 | |
| 78 | ```python | |
2954945eRobert Craigie1 years ago | 79 | prompt = "What is in this image?" |
| 80 | img_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/2023_06_08_Raccoon1.jpg/1599px-2023_06_08_Raccoon1.jpg" | |
| 81 | | |
| 82 | response = client.responses.create( | |
192b8f2bDan Corin1 years ago | 83 | model="gpt-4o-mini", |
2954945eRobert Craigie1 years ago | 84 | input=[ |
192b8f2bDan Corin1 years ago | 85 | { |
| 86 | "role": "user", | |
| 87 | "content": [ | |
2954945eRobert Craigie1 years ago | 88 | {"type": "input_text", "text": prompt}, |
| 89 | {"type": "input_image", "image_url": f"{img_url}"}, | |
192b8f2bDan Corin1 years ago | 90 | ], |
| 91 | } | |
| 92 | ], | |
| 93 | ) | |
| 94 | ``` | |
| 95 | | |
| 96 | With the image as a base64 encoded string: | |
| 97 | | |
| 98 | ```python | |
2954945eRobert Craigie1 years ago | 99 | import base64 |
| 100 | from openai import OpenAI | |
| 101 | | |
| 102 | client = OpenAI() | |
| 103 | | |
| 104 | prompt = "What is in this image?" | |
| 105 | with open("path/to/image.png", "rb") as image_file: | |
| 106 | b64_image = base64.b64encode(image_file.read()).decode("utf-8") | |
| 107 | | |
| 108 | response = client.responses.create( | |
192b8f2bDan Corin1 years ago | 109 | model="gpt-4o-mini", |
2954945eRobert Craigie1 years ago | 110 | input=[ |
192b8f2bDan Corin1 years ago | 111 | { |
| 112 | "role": "user", | |
| 113 | "content": [ | |
2954945eRobert Craigie1 years ago | 114 | {"type": "input_text", "text": prompt}, |
| 115 | {"type": "input_image", "image_url": f"data:image/png;base64,{b64_image}"}, | |
192b8f2bDan Corin1 years ago | 116 | ], |
| 117 | } | |
| 118 | ], | |
| 119 | ) | |
| 120 | ``` | |
| 121 | | |
08b8179aDavid Schnurr2 years ago | 122 | ## Async usage |
3c6d4cd6Greg Brockman5 years ago | 123 | |
08b8179aDavid Schnurr2 years ago | 124 | Simply import `AsyncOpenAI` instead of `OpenAI` and use `await` with each API call: |
ede08829Jakub Roztocil3 years ago | 125 | |
08b8179aDavid Schnurr2 years ago | 126 | ```python |
fb5ba01cStainless Bot2 years ago | 127 | import os |
08b8179aDavid Schnurr2 years ago | 128 | import asyncio |
| 129 | from openai import AsyncOpenAI | |
ede08829Jakub Roztocil3 years ago | 130 | |
08b8179aDavid Schnurr2 years ago | 131 | client = AsyncOpenAI( |
2954945eRobert Craigie1 years ago | 132 | # This is the default and can be omitted |
| 133 | api_key=os.environ.get("OPENAI_API_KEY"), | |
08b8179aDavid Schnurr2 years ago | 134 | ) |
ede08829Jakub Roztocil3 years ago | 135 | |
| 136 | | |
08b8179aDavid Schnurr2 years ago | 137 | async def main() -> None: |
2954945eRobert Craigie1 years ago | 138 | response = await client.responses.create( |
| 139 | model="gpt-4o", input="Explain disestablishmentarianism to a smart five year old." | |
08b8179aDavid Schnurr2 years ago | 140 | ) |
2954945eRobert Craigie1 years ago | 141 | print(response.output_text) |
ede08829Jakub Roztocil3 years ago | 142 | |
| 143 | | |
08b8179aDavid Schnurr2 years ago | 144 | asyncio.run(main()) |
2b21516eAtty Eleti3 years ago | 145 | ``` |
ede08829Jakub Roztocil3 years ago | 146 | |
08b8179aDavid Schnurr2 years ago | 147 | Functionality between the synchronous and asynchronous clients is otherwise identical. |
| 148 | | |
c62e9907stainless-app[bot]1 years ago | 149 | ### With aiohttp |
| 150 | | |
| 151 | By default, the async client uses `httpx` for HTTP requests. However, for improved concurrency performance you may also use `aiohttp` as the HTTP backend. | |
| 152 | | |
| 153 | You can enable this by installing `aiohttp`: | |
| 154 | | |
| 155 | ```sh | |
| 156 | # install from PyPI | |
| 157 | pip install openai[aiohttp] | |
| 158 | ``` | |
| 159 | | |
| 160 | Then you can enable it by instantiating the client with `http_client=DefaultAioHttpClient()`: | |
| 161 | | |
| 162 | ```python | |
| 163 | import asyncio | |
| 164 | from openai import DefaultAioHttpClient | |
| 165 | from openai import AsyncOpenAI | |
| 166 | | |
| 167 | | |
| 168 | async def main() -> None: | |
| 169 | async with AsyncOpenAI( | |
2028ad2bstainless-app[bot]11 months ago | 170 | api_key="My API Key", |
c62e9907stainless-app[bot]1 years ago | 171 | http_client=DefaultAioHttpClient(), |
| 172 | ) as client: | |
| 173 | chat_completion = await client.chat.completions.create( | |
| 174 | messages=[ | |
| 175 | { | |
| 176 | "role": "user", | |
| 177 | "content": "Say this is a test", | |
| 178 | } | |
| 179 | ], | |
| 180 | model="gpt-4o", | |
| 181 | ) | |
| 182 | | |
| 183 | | |
| 184 | asyncio.run(main()) | |
| 185 | ``` | |
| 186 | | |
2b23eb53Stainless Bot2 years ago | 187 | ## Streaming responses |
08b8179aDavid Schnurr2 years ago | 188 | |
| 189 | We provide support for streaming responses using Server Side Events (SSE). | |
d53d9efbRachel Lim5 years ago | 190 | |
08b8179aDavid Schnurr2 years ago | 191 | ```python |
| 192 | from openai import OpenAI | |
| 193 | | |
| 194 | client = OpenAI() | |
d53d9efbRachel Lim5 years ago | 195 | |
2954945eRobert Craigie1 years ago | 196 | stream = client.responses.create( |
23444ed9Stainless Bot1 years ago | 197 | model="gpt-4o", |
2954945eRobert Craigie1 years ago | 198 | input="Write a one-sentence bedtime story about a unicorn.", |
08b8179aDavid Schnurr2 years ago | 199 | stream=True, |
| 200 | ) | |
2954945eRobert Craigie1 years ago | 201 | |
| 202 | for event in stream: | |
| 203 | print(event) | |
d53d9efbRachel Lim5 years ago | 204 | ``` |
| 205 | | |
08b8179aDavid Schnurr2 years ago | 206 | The async client uses the exact same interface. |
d53d9efbRachel Lim5 years ago | 207 | |
| 208 | ```python | |
db0aa22cAdel Basli1 years ago | 209 | import asyncio |
08b8179aDavid Schnurr2 years ago | 210 | from openai import AsyncOpenAI |
| 211 | | |
| 212 | client = AsyncOpenAI() | |
| 213 | | |
a3da0196Stainless Bot2 years ago | 214 | |
dfe1c8daSahand Sojoodi2 years ago | 215 | async def main(): |
f588695fstainless-app[bot]1 years ago | 216 | stream = await client.responses.create( |
2954945eRobert Craigie1 years ago | 217 | model="gpt-4o", |
| 218 | input="Write a one-sentence bedtime story about a unicorn.", | |
dfe1c8daSahand Sojoodi2 years ago | 219 | stream=True, |
| 220 | ) | |
d53d9efbRachel Lim5 years ago | 221 | |
f588695fstainless-app[bot]1 years ago | 222 | async for event in stream: |
2954945eRobert Craigie1 years ago | 223 | print(event) |
08b8179aDavid Schnurr2 years ago | 224 | |
| 225 | | |
2954945eRobert Craigie1 years ago | 226 | asyncio.run(main()) |
62b73b9bAtty Eleti3 years ago | 227 | ``` |
| 228 | | |
488ec04bRobert Craigie1 years ago | 229 | ## Realtime API beta |
| 230 | | |
| 231 | The Realtime API enables you to build low-latency, multi-modal conversational experiences. It currently supports text and audio as both input and output, as well as [function calling](https://platform.openai.com/docs/guides/function-calling) through a WebSocket connection. | |
| 232 | | |
| 233 | Under the hood the SDK uses the [`websockets`](https://websockets.readthedocs.io/en/stable/) library to manage connections. | |
| 234 | | |
255677d7Robert Craigie1 years ago | 235 | The Realtime API works through a combination of client-sent events and server-sent events. Clients can send events to do things like update session configuration or send text and audio inputs. Server events confirm when audio responses have completed, or when a text response from the model has been received. A full event reference can be found [here](https://platform.openai.com/docs/api-reference/realtime-client-events) and a guide can be found [here](https://platform.openai.com/docs/guides/realtime). |
488ec04bRobert Craigie1 years ago | 236 | |
| 237 | Basic text based example: | |
| 238 | | |
| 239 | ```py | |
| 240 | import asyncio | |
| 241 | from openai import AsyncOpenAI | |
| 242 | | |
| 243 | async def main(): | |
| 244 | client = AsyncOpenAI() | |
| 245 | | |
fd763424Robert Craigie1 years ago | 246 | async with client.beta.realtime.connect(model="gpt-4o-realtime-preview") as connection: |
488ec04bRobert Craigie1 years ago | 247 | await connection.session.update(session={'modalities': ['text']}) |
| 248 | | |
| 249 | await connection.conversation.item.create( | |
| 250 | item={ | |
| 251 | "type": "message", | |
| 252 | "role": "user", | |
| 253 | "content": [{"type": "input_text", "text": "Say hello!"}], | |
| 254 | } | |
| 255 | ) | |
| 256 | await connection.response.create() | |
| 257 | | |
| 258 | async for event in connection: | |
| 259 | if event.type == 'response.text.delta': | |
| 260 | print(event.delta, flush=True, end="") | |
| 261 | | |
| 262 | elif event.type == 'response.text.done': | |
| 263 | print() | |
| 264 | | |
| 265 | elif event.type == "response.done": | |
| 266 | break | |
| 267 | | |
| 268 | asyncio.run(main()) | |
| 269 | ``` | |
| 270 | | |
6935dfdcRobert Craigie1 years ago | 271 | However the real magic of the Realtime API is handling audio inputs / outputs, see this example [TUI script](https://github.com/openai/openai-python/blob/main/examples/realtime/push_to_talk_app.py) for a fully fledged example. |
488ec04bRobert Craigie1 years ago | 272 | |
| 273 | ### Realtime error handling | |
| 274 | | |
2954945eRobert Craigie1 years ago | 275 | Whenever an error occurs, the Realtime API will send an [`error` event](https://platform.openai.com/docs/guides/realtime-model-capabilities#error-handling) and the connection will stay open and remain usable. This means you need to handle it yourself, as _no errors are raised directly_ by the SDK when an `error` event comes in. |
488ec04bRobert Craigie1 years ago | 276 | |
| 277 | ```py | |
| 278 | client = AsyncOpenAI() | |
| 279 | | |
fd763424Robert Craigie1 years ago | 280 | async with client.beta.realtime.connect(model="gpt-4o-realtime-preview") as connection: |
488ec04bRobert Craigie1 years ago | 281 | ... |
| 282 | async for event in connection: | |
| 283 | if event.type == 'error': | |
| 284 | print(event.error.type) | |
| 285 | print(event.error.code) | |
| 286 | print(event.error.event_id) | |
| 287 | print(event.error.message) | |
| 288 | ``` | |
| 289 | | |
08b8179aDavid Schnurr2 years ago | 290 | ## Using types |
| 291 | | |
47656567Stainless Bot2 years ago | 292 | Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev) which also provide helper methods for things like: |
f4b9655fStainless Bot2 years ago | 293 | |
47656567Stainless Bot2 years ago | 294 | - Serializing back into JSON, `model.to_json()` |
| 295 | - Converting to a dictionary, `model.to_dict()` | |
2b21516eAtty Eleti3 years ago | 296 | |
08b8179aDavid Schnurr2 years ago | 297 | Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `basic`. |
0e21703eTed Sanders4 years ago | 298 | |
08b8179aDavid Schnurr2 years ago | 299 | ## Pagination |
0e21703eTed Sanders4 years ago | 300 | |
08b8179aDavid Schnurr2 years ago | 301 | List methods in the OpenAI API are paginated. |
| 302 | | |
| 303 | This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually: | |
0e21703eTed Sanders4 years ago | 304 | |
| 305 | ```python | |
ff5add01stainless-app[bot]1 years ago | 306 | from openai import OpenAI |
0e21703eTed Sanders4 years ago | 307 | |
08b8179aDavid Schnurr2 years ago | 308 | client = OpenAI() |
0e21703eTed Sanders4 years ago | 309 | |
08b8179aDavid Schnurr2 years ago | 310 | all_jobs = [] |
| 311 | # Automatically fetches more pages as needed. | |
| 312 | for job in client.fine_tuning.jobs.list( | |
| 313 | limit=20, | |
| 314 | ): | |
| 315 | # Do something with job here | |
| 316 | all_jobs.append(job) | |
| 317 | print(all_jobs) | |
0e21703eTed Sanders4 years ago | 318 | ``` |
| 319 | | |
08b8179aDavid Schnurr2 years ago | 320 | Or, asynchronously: |
0e21703eTed Sanders4 years ago | 321 | |
08b8179aDavid Schnurr2 years ago | 322 | ```python |
| 323 | import asyncio | |
ff5add01stainless-app[bot]1 years ago | 324 | from openai import AsyncOpenAI |
0e21703eTed Sanders4 years ago | 325 | |
08b8179aDavid Schnurr2 years ago | 326 | client = AsyncOpenAI() |
0e21703eTed Sanders4 years ago | 327 | |
| 328 | | |
08b8179aDavid Schnurr2 years ago | 329 | async def main() -> None: |
| 330 | all_jobs = [] | |
| 331 | # Iterate through items across all pages, issuing requests as needed. | |
| 332 | async for job in client.fine_tuning.jobs.list( | |
| 333 | limit=20, | |
| 334 | ): | |
| 335 | all_jobs.append(job) | |
| 336 | print(all_jobs) | |
0e21703eTed Sanders4 years ago | 337 | |
62b51ca0Boris Dayma4 years ago | 338 | |
08b8179aDavid Schnurr2 years ago | 339 | asyncio.run(main()) |
| 340 | ``` | |
2942bf4bLogan Kilpatrick2 years ago | 341 | |
08b8179aDavid Schnurr2 years ago | 342 | Alternatively, you can use the `.has_next_page()`, `.next_page_info()`, or `.get_next_page()` methods for more granular control working with pages: |
2942bf4bLogan Kilpatrick2 years ago | 343 | |
08b8179aDavid Schnurr2 years ago | 344 | ```python |
| 345 | first_page = await client.fine_tuning.jobs.list( | |
| 346 | limit=20, | |
| 347 | ) | |
| 348 | if first_page.has_next_page(): | |
| 349 | print(f"will fetch next page using these details: {first_page.next_page_info()}") | |
| 350 | next_page = await first_page.get_next_page() | |
| 351 | print(f"number of items we just fetched: {len(next_page.data)}") | |
| 352 | | |
| 353 | # Remove `await` for non-async usage. | |
62b51ca0Boris Dayma4 years ago | 354 | ``` |
| 355 | | |
08b8179aDavid Schnurr2 years ago | 356 | Or just work directly with the returned data: |
0e21703eTed Sanders4 years ago | 357 | |
08b8179aDavid Schnurr2 years ago | 358 | ```python |
| 359 | first_page = await client.fine_tuning.jobs.list( | |
| 360 | limit=20, | |
| 361 | ) | |
e389823bMorgan McGuire2 years ago | 362 | |
08b8179aDavid Schnurr2 years ago | 363 | print(f"next page cursor: {first_page.after}") # => "next page cursor: ..." |
| 364 | for job in first_page.data: | |
| 365 | print(job.id) | |
e389823bMorgan McGuire2 years ago | 366 | |
08b8179aDavid Schnurr2 years ago | 367 | # Remove `await` for non-async usage. |
| 368 | ``` | |
e389823bMorgan McGuire2 years ago | 369 | |
08b8179aDavid Schnurr2 years ago | 370 | ## Nested params |
3c00e856hallacy3 years ago | 371 | |
08b8179aDavid Schnurr2 years ago | 372 | Nested parameters are dictionaries, typed using `TypedDict`, for example: |
3c00e856hallacy3 years ago | 373 | |
| 374 | ```python | |
08b8179aDavid Schnurr2 years ago | 375 | from openai import OpenAI |
3c00e856hallacy3 years ago | 376 | |
08b8179aDavid Schnurr2 years ago | 377 | client = OpenAI() |
| 378 | | |
2954945eRobert Craigie1 years ago | 379 | response = client.chat.responses.create( |
| 380 | input=[ | |
aa681899Stainless Bot2 years ago | 381 | { |
| 382 | "role": "user", | |
2954945eRobert Craigie1 years ago | 383 | "content": "How much ?", |
aa681899Stainless Bot2 years ago | 384 | } |
| 385 | ], | |
23444ed9Stainless Bot1 years ago | 386 | model="gpt-4o", |
aa681899Stainless Bot2 years ago | 387 | response_format={"type": "json_object"}, |
| 388 | ) | |
08b8179aDavid Schnurr2 years ago | 389 | ``` |
3c00e856hallacy3 years ago | 390 | |
2b23eb53Stainless Bot2 years ago | 391 | ## File uploads |
dc33cb9dMichelle Pokrass3 years ago | 392 | |
acf68ef3stainless-app[bot]1 years ago | 393 | Request parameters that correspond to file uploads can be passed as `bytes`, or a [`PathLike`](https://docs.python.org/3/library/os.html#os.PathLike) instance or a tuple of `(filename, contents, media type)`. |
dc33cb9dMichelle Pokrass3 years ago | 394 | |
376dd199Logan Kilpatrick2 years ago | 395 | ```python |
08b8179aDavid Schnurr2 years ago | 396 | from pathlib import Path |
| 397 | from openai import OpenAI | |
| 398 | | |
| 399 | client = OpenAI() | |
| 400 | | |
| 401 | client.files.create( | |
| 402 | file=Path("input.jsonl"), | |
| 403 | purpose="fine-tune", | |
| 404 | ) | |
dc33cb9dMichelle Pokrass3 years ago | 405 | ``` |
| 406 | | |
08b8179aDavid Schnurr2 years ago | 407 | The async client uses the exact same interface. If you pass a [`PathLike`](https://docs.python.org/3/library/os.html#os.PathLike) instance, the file contents will be read asynchronously automatically. |
376dd199Logan Kilpatrick2 years ago | 408 | |
18e0b36astainless-app[bot]1 years ago | 409 | ## Webhook Verification |
| 410 | | |
| 411 | Verifying webhook signatures is _optional but encouraged_. | |
| 412 | | |
4f99c4e6David Meadows1 years ago | 413 | For more information about webhooks, see [the API docs](https://platform.openai.com/docs/guides/webhooks). |
| 414 | | |
18e0b36astainless-app[bot]1 years ago | 415 | ### Parsing webhook payloads |
| 416 | | |
| 417 | For most use cases, you will likely want to verify the webhook and parse the payload at the same time. To achieve this, we provide the method `client.webhooks.unwrap()`, which parses a webhook request and verifies that it was sent by OpenAI. This method will raise an error if the signature is invalid. | |
| 418 | | |
| 419 | Note that the `body` parameter must be the raw JSON string sent from the server (do not parse it first). The `.unwrap()` method will parse this JSON for you into an event object after verifying the webhook was sent from OpenAI. | |
| 420 | | |
| 421 | ```python | |
| 422 | from openai import OpenAI | |
| 423 | from flask import Flask, request | |
| 424 | | |
| 425 | app = Flask(__name__) | |
| 426 | client = OpenAI() # OPENAI_WEBHOOK_SECRET environment variable is used by default | |
| 427 | | |
| 428 | | |
| 429 | @app.route("/webhook", methods=["POST"]) | |
| 430 | def webhook(): | |
| 431 | request_body = request.get_data(as_text=True) | |
| 432 | | |
| 433 | try: | |
| 434 | event = client.webhooks.unwrap(request_body, request.headers) | |
| 435 | | |
| 436 | if event.type == "response.completed": | |
| 437 | print("Response completed:", event.data) | |
| 438 | elif event.type == "response.failed": | |
| 439 | print("Response failed:", event.data) | |
| 440 | else: | |
| 441 | print("Unhandled event type:", event.type) | |
| 442 | | |
| 443 | return "ok" | |
| 444 | except Exception as e: | |
| 445 | print("Invalid signature:", e) | |
| 446 | return "Invalid signature", 400 | |
| 447 | | |
| 448 | | |
| 449 | if __name__ == "__main__": | |
| 450 | app.run(port=8000) | |
| 451 | ``` | |
| 452 | | |
| 453 | ### Verifying webhook payloads directly | |
| 454 | | |
| 455 | In some cases, you may want to verify the webhook separately from parsing the payload. If you prefer to handle these steps separately, we provide the method `client.webhooks.verify_signature()` to _only verify_ the signature of a webhook request. Like `.unwrap()`, this method will raise an error if the signature is invalid. | |
| 456 | | |
| 457 | Note that the `body` parameter must be the raw JSON string sent from the server (do not parse it first). You will then need to parse the body after verifying the signature. | |
| 458 | | |
| 459 | ```python | |
| 460 | import json | |
| 461 | from openai import OpenAI | |
| 462 | from flask import Flask, request | |
| 463 | | |
| 464 | app = Flask(__name__) | |
| 465 | client = OpenAI() # OPENAI_WEBHOOK_SECRET environment variable is used by default | |
| 466 | | |
| 467 | | |
| 468 | @app.route("/webhook", methods=["POST"]) | |
| 469 | def webhook(): | |
| 470 | request_body = request.get_data(as_text=True) | |
| 471 | | |
| 472 | try: | |
| 473 | client.webhooks.verify_signature(request_body, request.headers) | |
| 474 | | |
| 475 | # Parse the body after verification | |
| 476 | event = json.loads(request_body) | |
| 477 | print("Verified event:", event) | |
| 478 | | |
| 479 | return "ok" | |
| 480 | except Exception as e: | |
| 481 | print("Invalid signature:", e) | |
| 482 | return "Invalid signature", 400 | |
| 483 | | |
| 484 | | |
| 485 | if __name__ == "__main__": | |
| 486 | app.run(port=8000) | |
| 487 | ``` | |
| 488 | | |
08b8179aDavid Schnurr2 years ago | 489 | ## Handling errors |
376dd199Logan Kilpatrick2 years ago | 490 | |
08b8179aDavid Schnurr2 years ago | 491 | When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `openai.APIConnectionError` is raised. |
2b21516eAtty Eleti3 years ago | 492 | |
08b8179aDavid Schnurr2 years ago | 493 | When the API returns a non-success status code (that is, 4xx or 5xx |
| 494 | response), a subclass of `openai.APIStatusError` is raised, containing `status_code` and `response` properties. | |
62b73b9bAtty Eleti3 years ago | 495 | |
08b8179aDavid Schnurr2 years ago | 496 | All errors inherit from `openai.APIError`. |
| 497 | | |
| 498 | ```python | |
| 499 | import openai | |
| 500 | from openai import OpenAI | |
| 501 | | |
| 502 | client = OpenAI() | |
| 503 | | |
| 504 | try: | |
8dab1421Stainless Bot2 years ago | 505 | client.fine_tuning.jobs.create( |
23444ed9Stainless Bot1 years ago | 506 | model="gpt-4o", |
8dab1421Stainless Bot2 years ago | 507 | training_file="file-abc123", |
08b8179aDavid Schnurr2 years ago | 508 | ) |
| 509 | except openai.APIConnectionError as e: | |
| 510 | print("The server could not be reached") | |
| 511 | print(e.__cause__) # an underlying Exception, likely raised within httpx. | |
| 512 | except openai.RateLimitError as e: | |
| 513 | print("A 429 status code was received; we should back off a bit.") | |
| 514 | except openai.APIStatusError as e: | |
| 515 | print("Another non-200-range status code was received") | |
| 516 | print(e.status_code) | |
| 517 | print(e.response) | |
62b73b9bAtty Eleti3 years ago | 518 | ``` |
| 519 | | |
fee9c81bstainless-app[bot]1 years ago | 520 | Error codes are as follows: |
08b8179aDavid Schnurr2 years ago | 521 | |
| 522 | | Status Code | Error Type | | |
| 523 | | ----------- | -------------------------- | | |
| 524 | | 400 | `BadRequestError` | | |
| 525 | | 401 | `AuthenticationError` | | |
| 526 | | 403 | `PermissionDeniedError` | | |
| 527 | | 404 | `NotFoundError` | | |
| 528 | | 422 | `UnprocessableEntityError` | | |
| 529 | | 429 | `RateLimitError` | | |
| 530 | | >=500 | `InternalServerError` | | |
| 531 | | N/A | `APIConnectionError` | | |
| 532 | | |
4b302346Robert Craigie1 years ago | 533 | ## Request IDs |
| 534 | | |
| 535 | > For more information on debugging requests, see [these docs](https://platform.openai.com/docs/api-reference/debugging-requests) | |
| 536 | | |
| 537 | All object responses in the SDK provide a `_request_id` property which is added from the `x-request-id` response header so that you can quickly log failing requests and report them back to OpenAI. | |
| 538 | | |
| 539 | ```python | |
2954945eRobert Craigie1 years ago | 540 | response = await client.responses.create( |
| 541 | model="gpt-4o-mini", | |
| 542 | input="Say 'this is a test'.", | |
4b302346Robert Craigie1 years ago | 543 | ) |
2954945eRobert Craigie1 years ago | 544 | print(response._request_id) # req_123 |
4b302346Robert Craigie1 years ago | 545 | ``` |
| 546 | | |
| 547 | Note that unlike other properties that use an `_` prefix, the `_request_id` property | |
2954945eRobert Craigie1 years ago | 548 | _is_ public. Unless documented otherwise, _all_ other `_` prefix properties, |
| 549 | methods and modules are _private_. | |
4b302346Robert Craigie1 years ago | 550 | |
709926ffRobert Craigie1 years ago | 551 | > [!IMPORTANT] |
| 552 | > If you need to access request IDs for failed requests you must catch the `APIStatusError` exception | |
| 553 | | |
| 554 | ```python | |
| 555 | import openai | |
| 556 | | |
| 557 | try: | |
| 558 | completion = await client.chat.completions.create( | |
| 559 | messages=[{"role": "user", "content": "Say this is a test"}], model="gpt-4" | |
| 560 | ) | |
| 561 | except openai.APIStatusError as exc: | |
| 562 | print(exc.request_id) # req_123 | |
| 563 | raise exc | |
| 564 | ``` | |
| 565 | | |
2954945eRobert Craigie1 years ago | 566 | ## Retries |
376dd199Logan Kilpatrick2 years ago | 567 | |
08b8179aDavid Schnurr2 years ago | 568 | Certain errors are automatically retried 2 times by default, with a short exponential backoff. |
| 569 | Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, | |
| 570 | 429 Rate Limit, and >=500 Internal errors are all retried by default. | |
0abf6413Andrew Chen Wang3 years ago | 571 | |
08b8179aDavid Schnurr2 years ago | 572 | You can use the `max_retries` option to configure or disable retry settings: |
0abf6413Andrew Chen Wang3 years ago | 573 | |
| 574 | ```python | |
08b8179aDavid Schnurr2 years ago | 575 | from openai import OpenAI |
| 576 | | |
| 577 | # Configure the default for all requests: | |
| 578 | client = OpenAI( | |
| 579 | # default is 2 | |
| 580 | max_retries=0, | |
| 581 | ) | |
| 582 | | |
| 583 | # Or, configure per-request: | |
| 584 | client.with_options(max_retries=5).chat.completions.create( | |
| 585 | messages=[ | |
| 586 | { | |
| 587 | "role": "user", | |
23444ed9Stainless Bot1 years ago | 588 | "content": "How can I get the name of the current day in JavaScript?", |
08b8179aDavid Schnurr2 years ago | 589 | } |
| 590 | ], | |
23444ed9Stainless Bot1 years ago | 591 | model="gpt-4o", |
08b8179aDavid Schnurr2 years ago | 592 | ) |
0abf6413Andrew Chen Wang3 years ago | 593 | ``` |
| 594 | | |
2954945eRobert Craigie1 years ago | 595 | ## Timeouts |
0abf6413Andrew Chen Wang3 years ago | 596 | |
08b8179aDavid Schnurr2 years ago | 597 | By default requests time out after 10 minutes. You can configure this with a `timeout` option, |
90e3d396Guspan Tanadi1 years ago | 598 | which accepts a float or an [`httpx.Timeout`](https://www.python-httpx.org/advanced/timeouts/#fine-tuning-the-configuration) object: |
376dd199Logan Kilpatrick2 years ago | 599 | |
08b8179aDavid Schnurr2 years ago | 600 | ```python |
| 601 | from openai import OpenAI | |
| 602 | | |
| 603 | # Configure the default for all requests: | |
| 604 | client = OpenAI( | |
1381f46eStainless Bot2 years ago | 605 | # 20 seconds (default is 10 minutes) |
08b8179aDavid Schnurr2 years ago | 606 | timeout=20.0, |
| 607 | ) | |
| 608 | | |
| 609 | # More granular control: | |
| 610 | client = OpenAI( | |
| 611 | timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0), | |
| 612 | ) | |
| 613 | | |
| 614 | # Override per-request: | |
2a678e30Stainless Bot2 years ago | 615 | client.with_options(timeout=5.0).chat.completions.create( |
08b8179aDavid Schnurr2 years ago | 616 | messages=[ |
| 617 | { | |
| 618 | "role": "user", | |
| 619 | "content": "How can I list all files in a directory using Python?", | |
| 620 | } | |
| 621 | ], | |
23444ed9Stainless Bot1 years ago | 622 | model="gpt-4o", |
08b8179aDavid Schnurr2 years ago | 623 | ) |
0abf6413Andrew Chen Wang3 years ago | 624 | ``` |
| 625 | | |
08b8179aDavid Schnurr2 years ago | 626 | On timeout, an `APITimeoutError` is thrown. |
376dd199Logan Kilpatrick2 years ago | 627 | |
08b8179aDavid Schnurr2 years ago | 628 | Note that requests that time out are [retried twice by default](#retries). |
376dd199Logan Kilpatrick2 years ago | 629 | |
08b8179aDavid Schnurr2 years ago | 630 | ## Advanced |
376dd199Logan Kilpatrick2 years ago | 631 | |
08b8179aDavid Schnurr2 years ago | 632 | ### Logging |
376dd199Logan Kilpatrick2 years ago | 633 | |
08b8179aDavid Schnurr2 years ago | 634 | We use the standard library [`logging`](https://docs.python.org/3/library/logging.html) module. |
376dd199Logan Kilpatrick2 years ago | 635 | |
f6199d60stainless-app[bot]1 years ago | 636 | You can enable logging by setting the environment variable `OPENAI_LOG` to `info`. |
376dd199Logan Kilpatrick2 years ago | 637 | |
08b8179aDavid Schnurr2 years ago | 638 | ```shell |
f6199d60stainless-app[bot]1 years ago | 639 | $ export OPENAI_LOG=info |
376dd199Logan Kilpatrick2 years ago | 640 | ``` |
| 641 | | |
f6199d60stainless-app[bot]1 years ago | 642 | Or to `debug` for more verbose logging. |
| 643 | | |
08b8179aDavid Schnurr2 years ago | 644 | ### How to tell whether `None` means `null` or missing |
dc33cb9dMichelle Pokrass3 years ago | 645 | |
08b8179aDavid Schnurr2 years ago | 646 | In an API response, a field may be explicitly `null`, or missing entirely; in either case, its value is `None` in this library. You can differentiate the two cases with `.model_fields_set`: |
3c6d4cd6Greg Brockman5 years ago | 647 | |
08b8179aDavid Schnurr2 years ago | 648 | ```py |
| 649 | if response.my_field is None: | |
| 650 | if 'my_field' not in response.model_fields_set: | |
| 651 | print('Got json like {}, without a "my_field" key present at all.') | |
| 652 | else: | |
| 653 | print('Got json like {"my_field": null}.') | |
| 654 | ``` | |
376dd199Logan Kilpatrick2 years ago | 655 | |
08b8179aDavid Schnurr2 years ago | 656 | ### Accessing raw response data (e.g. headers) |
376dd199Logan Kilpatrick2 years ago | 657 | |
86379b44Stainless Bot2 years ago | 658 | The "raw" Response object can be accessed by prefixing `.with_raw_response.` to any HTTP method call, e.g., |
08b8179aDavid Schnurr2 years ago | 659 | |
| 660 | ```py | |
| 661 | from openai import OpenAI | |
| 662 | | |
| 663 | client = OpenAI() | |
| 664 | response = client.chat.completions.with_raw_response.create( | |
| 665 | messages=[{ | |
| 666 | "role": "user", | |
| 667 | "content": "Say this is a test", | |
| 668 | }], | |
23444ed9Stainless Bot1 years ago | 669 | model="gpt-4o", |
08b8179aDavid Schnurr2 years ago | 670 | ) |
| 671 | print(response.headers.get('X-My-Header')) | |
| 672 | | |
| 673 | completion = response.parse() # get the object that `chat.completions.create()` would have returned | |
| 674 | print(completion) | |
376dd199Logan Kilpatrick2 years ago | 675 | ``` |
| 676 | | |
16315f22stainless-app[bot]1 years ago | 677 | These methods return a [`LegacyAPIResponse`](https://github.com/openai/openai-python/tree/main/src/openai/_legacy_response.py) object. This is a legacy class as we're changing it slightly in the next major version. |
86379b44Stainless Bot2 years ago | 678 | |
| 679 | For the sync client this will mostly be the same with the exception | |
| 680 | of `content` & `text` will be methods instead of properties. In the | |
| 681 | async client, all methods will be async. | |
| 682 | | |
| 683 | A migration script will be provided & the migration in general should | |
| 684 | be smooth. | |
| 685 | | |
| 686 | #### `.with_streaming_response` | |
| 687 | | |
| 688 | The above interface eagerly reads the full response body when you make the request, which may not always be what you want. | |
| 689 | | |
| 690 | To stream the response body, use `.with_streaming_response` instead, which requires a context manager and only reads the response body once you call `.read()`, `.text()`, `.json()`, `.iter_bytes()`, `.iter_text()`, `.iter_lines()` or `.parse()`. In the async client, these are async methods. | |
| 691 | | |
| 692 | As such, `.with_streaming_response` methods return a different [`APIResponse`](https://github.com/openai/openai-python/tree/main/src/openai/_response.py) object, and the async client returns an [`AsyncAPIResponse`](https://github.com/openai/openai-python/tree/main/src/openai/_response.py) object. | |
| 693 | | |
| 694 | ```python | |
| 695 | with client.chat.completions.with_streaming_response.create( | |
| 696 | messages=[ | |
| 697 | { | |
| 698 | "role": "user", | |
| 699 | "content": "Say this is a test", | |
| 700 | } | |
| 701 | ], | |
23444ed9Stainless Bot1 years ago | 702 | model="gpt-4o", |
86379b44Stainless Bot2 years ago | 703 | ) as response: |
| 704 | print(response.headers.get("X-My-Header")) | |
| 705 | | |
| 706 | for line in response.iter_lines(): | |
| 707 | print(line) | |
| 708 | ``` | |
| 709 | | |
| 710 | The context manager is required so that the response will reliably be closed. | |
3c6d4cd6Greg Brockman5 years ago | 711 | |
73869eeeStainless Bot2 years ago | 712 | ### Making custom/undocumented requests |
| 713 | | |
7931ebaaStainless Bot2 years ago | 714 | This library is typed for convenient access to the documented API. |
73869eeeStainless Bot2 years ago | 715 | |
| 716 | If you need to access undocumented endpoints, params, or response properties, the library can still be used. | |
| 717 | | |
| 718 | #### Undocumented endpoints | |
| 719 | | |
| 720 | To make requests to undocumented endpoints, you can make requests using `client.get`, `client.post`, and other | |
fee9c81bstainless-app[bot]1 years ago | 721 | http verbs. Options on the client will be respected (such as retries) when making this request. |
73869eeeStainless Bot2 years ago | 722 | |
| 723 | ```py | |
| 724 | import httpx | |
| 725 | | |
| 726 | response = client.post( | |
| 727 | "/foo", | |
| 728 | cast_to=httpx.Response, | |
| 729 | body={"my_param": True}, | |
| 730 | ) | |
| 731 | | |
| 732 | print(response.headers.get("x-foo")) | |
| 733 | ``` | |
| 734 | | |
802819c8Stainless Bot2 years ago | 735 | #### Undocumented request params |
73869eeeStainless Bot2 years ago | 736 | |
| 737 | If you want to explicitly send an extra param, you can do so with the `extra_query`, `extra_body`, and `extra_headers` request | |
| 738 | options. | |
| 739 | | |
802819c8Stainless Bot2 years ago | 740 | #### Undocumented response properties |
73869eeeStainless Bot2 years ago | 741 | |
| 742 | To access undocumented response properties, you can access the extra fields like `response.unknown_prop`. You | |
| 743 | can also get all the extra fields on the Pydantic model as a dict with | |
| 744 | [`response.model_extra`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_extra). | |
| 745 | | |
08b8179aDavid Schnurr2 years ago | 746 | ### Configuring the HTTP client |
376dd199Logan Kilpatrick2 years ago | 747 | |
08b8179aDavid Schnurr2 years ago | 748 | You can directly override the [httpx client](https://www.python-httpx.org/api/#client) to customize it for your use case, including: |
376dd199Logan Kilpatrick2 years ago | 749 | |
6a1ab551stainless-app[bot]1 years ago | 750 | - Support for [proxies](https://www.python-httpx.org/advanced/proxies/) |
| 751 | - Custom [transports](https://www.python-httpx.org/advanced/transports/) | |
d3254d12stainless-app[bot]2 years ago | 752 | - Additional [advanced](https://www.python-httpx.org/advanced/clients/) functionality |
376dd199Logan Kilpatrick2 years ago | 753 | |
| 754 | ```python | |
6a1ab551stainless-app[bot]1 years ago | 755 | import httpx |
347363edStainless Bot2 years ago | 756 | from openai import OpenAI, DefaultHttpxClient |
08b8179aDavid Schnurr2 years ago | 757 | |
| 758 | client = OpenAI( | |
0733934fStainless Bot2 years ago | 759 | # Or use the `OPENAI_BASE_URL` env var |
38dd5348Adrian Cole1 years ago | 760 | base_url="http://my.test.server.example.com:8083/v1", |
347363edStainless Bot2 years ago | 761 | http_client=DefaultHttpxClient( |
6a1ab551stainless-app[bot]1 years ago | 762 | proxy="http://my.test.proxy.example.com", |
08b8179aDavid Schnurr2 years ago | 763 | transport=httpx.HTTPTransport(local_address="0.0.0.0"), |
| 764 | ), | |
| 765 | ) | |
| 766 | ``` | |
| 767 | | |
f8f01a61stainless-app[bot]1 years ago | 768 | You can also customize the client on a per-request basis by using `with_options()`: |
| 769 | | |
| 770 | ```python | |
| 771 | client.with_options(http_client=DefaultHttpxClient(...)) | |
| 772 | ``` | |
| 773 | | |
08b8179aDavid Schnurr2 years ago | 774 | ### Managing HTTP resources |
| 775 | | |
| 776 | By default the library closes underlying HTTP connections whenever the client is [garbage collected](https://docs.python.org/3/reference/datamodel.html#object.__del__). You can manually close the client using the `.close()` method if desired, or with a context manager that closes when exiting. | |
| 777 | | |
588935e2stainless-app[bot]1 years ago | 778 | ```py |
| 779 | from openai import OpenAI | |
| 780 | | |
| 781 | with OpenAI() as client: | |
| 782 | # make requests here | |
| 783 | ... | |
| 784 | | |
| 785 | # HTTP client is now closed | |
| 786 | ``` | |
| 787 | | |
08b8179aDavid Schnurr2 years ago | 788 | ## Microsoft Azure OpenAI |
| 789 | | |
6e7d8548Scott Addie2 years ago | 790 | To use this library with [Azure OpenAI](https://learn.microsoft.com/azure/ai-services/openai/overview), use the `AzureOpenAI` |
08b8179aDavid Schnurr2 years ago | 791 | class instead of the `OpenAI` class. |
| 792 | | |
| 793 | > [!IMPORTANT] | |
| 794 | > The Azure API shape differs from the core API shape which means that the static types for responses / params | |
| 795 | > won't always be correct. | |
376dd199Logan Kilpatrick2 years ago | 796 | |
08b8179aDavid Schnurr2 years ago | 797 | ```py |
| 798 | from openai import AzureOpenAI | |
376dd199Logan Kilpatrick2 years ago | 799 | |
08b8179aDavid Schnurr2 years ago | 800 | # gets the API Key from environment variable AZURE_OPENAI_API_KEY |
| 801 | client = AzureOpenAI( | |
6e7d8548Scott Addie2 years ago | 802 | # https://learn.microsoft.com/azure/ai-services/openai/reference#rest-api-versioning |
819ae68dJackYu2 years ago | 803 | api_version="2023-07-01-preview", |
6e7d8548Scott Addie2 years ago | 804 | # https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource |
08b8179aDavid Schnurr2 years ago | 805 | azure_endpoint="https://example-endpoint.openai.azure.com", |
| 806 | ) | |
| 807 | | |
| 808 | completion = client.chat.completions.create( | |
| 809 | model="deployment-name", # e.g. gpt-35-instant | |
| 810 | messages=[ | |
| 811 | { | |
| 812 | "role": "user", | |
| 813 | "content": "How do I output all files in a directory using Python?", | |
| 814 | }, | |
| 815 | ], | |
| 816 | ) | |
47656567Stainless Bot2 years ago | 817 | print(completion.to_json()) |
376dd199Logan Kilpatrick2 years ago | 818 | ``` |
3c6d4cd6Greg Brockman5 years ago | 819 | |
08b8179aDavid Schnurr2 years ago | 820 | In addition to the options provided in the base `OpenAI` client, the following options are provided: |
| 821 | | |
7758c54bStainless Bot2 years ago | 822 | - `azure_endpoint` (or the `AZURE_OPENAI_ENDPOINT` environment variable) |
08b8179aDavid Schnurr2 years ago | 823 | - `azure_deployment` |
7758c54bStainless Bot2 years ago | 824 | - `api_version` (or the `OPENAI_API_VERSION` environment variable) |
| 825 | - `azure_ad_token` (or the `AZURE_OPENAI_AD_TOKEN` environment variable) | |
08b8179aDavid Schnurr2 years ago | 826 | - `azure_ad_token_provider` |
| 827 | | |
6e7d8548Scott Addie2 years ago | 828 | An example of using the client with Microsoft Entra ID (formerly known as Azure Active Directory) can be found [here](https://github.com/openai/openai-python/blob/main/examples/azure_ad.py). |
08b8179aDavid Schnurr2 years ago | 829 | |
| 830 | ## Versioning | |
| 831 | | |
| 832 | This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) conventions, though certain backwards-incompatible changes may be released as minor versions: | |
| 833 | | |
| 834 | 1. Changes that only affect static types, without breaking runtime behavior. | |
e502d301Josiah Altschuler1 years ago | 835 | 2. Changes to library internals which are technically public but not intended or documented for external use. _(Please open a GitHub issue to let us know if you are relying on such internals.)_ |
08b8179aDavid Schnurr2 years ago | 836 | 3. Changes that we do not expect to impact the vast majority of users in practice. |
| 837 | | |
| 838 | We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience. | |
| 839 | | |
| 840 | We are keen for your feedback; please open an [issue](https://www.github.com/openai/openai-python/issues) with questions, bugs, or suggestions. | |
| 841 | | |
fee10404stainless-app[bot]1 years ago | 842 | ### Determining the installed version |
| 843 | | |
| 844 | If you've upgraded to the latest version but aren't seeing any new features you were expecting then your python environment is likely still using an older version. | |
| 845 | | |
| 846 | You can determine the version that is being used at runtime with: | |
| 847 | | |
| 848 | ```py | |
| 849 | import openai | |
| 850 | print(openai.__version__) | |
| 851 | ``` | |
| 852 | | |
08b8179aDavid Schnurr2 years ago | 853 | ## Requirements |
3c6d4cd6Greg Brockman5 years ago | 854 | |
cb88c2f0stainless-app[bot]1 years ago | 855 | Python 3.8 or higher. |
a3001d8dStainless Bot1 years ago | 856 | |
| 857 | ## Contributing | |
| 858 | | |
| 859 | See [the contributing documentation](./CONTRIBUTING.md). |