openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
README.md
638lines · modeblame
08b8179aDavid Schnurr2 years ago | 1 | # OpenAI Python API library |
3c6d4cd6Greg Brockman5 years ago | 2 | |
08b8179aDavid Schnurr2 years ago | 3 | [](https://pypi.org/project/openai/) |
3c6d4cd6Greg Brockman5 years ago | 4 | |
08b8179aDavid Schnurr2 years ago | 5 | The OpenAI Python library provides convenient access to the OpenAI REST API from any Python 3.7+ |
| 6 | application. The library includes type definitions for all request params and response fields, | |
| 7 | and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx). | |
| 8 | | |
| 9 | It is generated from our [OpenAPI specification](https://github.com/openai/openai-openapi) with [Stainless](https://stainlessapi.com/). | |
| 10 | | |
| 11 | ## Documentation | |
| 12 | | |
986f3128Stainless Bot2 years ago | 13 | The REST API documentation can be found [on platform.openai.com](https://platform.openai.com/docs). The full API of this library can be found in [api.md](api.md). |
3c6d4cd6Greg Brockman5 years ago | 14 | |
| 15 | ## Installation | |
| 16 | | |
448ac7d0Stainless Bot2 years ago | 17 | > [!IMPORTANT] |
db6cd764Stainless Bot2 years ago | 18 | > The SDK was rewritten in v1, which was released November 6th 2023. See the [v1 migration guide](https://github.com/openai/openai-python/discussions/742), which includes scripts to automatically update your code. |
448ac7d0Stainless Bot2 years ago | 19 | |
3c6d4cd6Greg Brockman5 years ago | 20 | ```sh |
1879c97aStainless Bot2 years ago | 21 | # install from PyPI |
6d217096Robert Craigie2 years ago | 22 | pip install openai |
3c6d4cd6Greg Brockman5 years ago | 23 | ``` |
| 24 | | |
08b8179aDavid Schnurr2 years ago | 25 | ## Usage |
| 26 | | |
986f3128Stainless Bot2 years ago | 27 | The full API of this library can be found in [api.md](api.md). |
376dd199Logan Kilpatrick2 years ago | 28 | |
| 29 | ```python | |
fb5ba01cStainless Bot2 years ago | 30 | import os |
08b8179aDavid Schnurr2 years ago | 31 | from openai import OpenAI |
| 32 | | |
| 33 | client = OpenAI( | |
fb5ba01cStainless Bot2 years ago | 34 | # This is the default and can be omitted |
| 35 | api_key=os.environ.get("OPENAI_API_KEY"), | |
08b8179aDavid Schnurr2 years ago | 36 | ) |
| 37 | | |
| 38 | chat_completion = client.chat.completions.create( | |
| 39 | messages=[ | |
| 40 | { | |
| 41 | "role": "user", | |
| 42 | "content": "Say this is a test", | |
| 43 | } | |
| 44 | ], | |
| 45 | model="gpt-3.5-turbo", | |
| 46 | ) | |
376dd199Logan Kilpatrick2 years ago | 47 | ``` |
| 48 | | |
08b8179aDavid Schnurr2 years ago | 49 | While you can provide an `api_key` keyword argument, |
| 50 | we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/) | |
| 51 | to add `OPENAI_API_KEY="My API Key"` to your `.env` file | |
| 52 | so that your API Key is not stored in source control. | |
3c6d4cd6Greg Brockman5 years ago | 53 | |
595f3b36Stainless Bot2 years ago | 54 | ### Polling Helpers |
| 55 | | |
5b20698dStainless Bot2 years ago | 56 | When interacting with the API some actions such as starting a Run and adding files to vector stores are asynchronous and take time to complete. The SDK includes |
595f3b36Stainless Bot2 years ago | 57 | helper functions which will poll the status until it reaches a terminal state and then return the resulting object. |
| 58 | If an API method results in an action which could benefit from polling there will be a corresponding version of the | |
| 59 | method ending in '\_and_poll'. | |
| 60 | | |
| 61 | For instance to create a Run and poll until it reaches a terminal state you can run: | |
| 62 | | |
| 63 | ```python | |
| 64 | run = client.beta.threads.runs.create_and_poll( | |
| 65 | thread_id=thread.id, | |
| 66 | assistant_id=assistant.id, | |
| 67 | ) | |
| 68 | ``` | |
| 69 | | |
| 70 | More information on the lifecycle of a Run can be found in the [Run Lifecycle Documentation](https://platform.openai.com/docs/assistants/how-it-works/run-lifecycle) | |
| 71 | | |
5b20698dStainless Bot2 years ago | 72 | ### Bulk Upload Helpers |
| 73 | | |
| 74 | When creating an interacting with vector stores, you can use the polling helpers to monitor the status of operations. | |
| 75 | For convenience, we also provide a bulk upload helper to allow you to simultaneously upload several files at once. | |
| 76 | | |
| 77 | ```python | |
| 78 | sample_files = [Path("sample-paper.pdf"), ...] | |
| 79 | | |
| 80 | batch = await client.vector_stores.file_batches.upload_and_poll( | |
| 81 | store.id, | |
| 82 | files=sample_files, | |
| 83 | ) | |
| 84 | ``` | |
| 85 | | |
5cfb125aStainless Bot2 years ago | 86 | ### Streaming Helpers |
| 87 | | |
| 88 | The SDK also includes helpers to process streams and handle the incoming events. | |
| 89 | | |
| 90 | ```python | |
595f3b36Stainless Bot2 years ago | 91 | with client.beta.threads.runs.stream( |
5cfb125aStainless Bot2 years ago | 92 | thread_id=thread.id, |
| 93 | assistant_id=assistant.id, | |
| 94 | instructions="Please address the user as Jane Doe. The user has a premium account.", | |
| 95 | ) as stream: | |
| 96 | for event in stream: | |
| 97 | # Print the text from text delta events | |
| 98 | if event.type == "thread.message.delta" and event.data.delta.content: | |
| 99 | print(event.data.delta.content[0].text) | |
| 100 | ``` | |
| 101 | | |
| 102 | More information on streaming helpers can be found in the dedicated documentation: [helpers.md](helpers.md) | |
| 103 | | |
08b8179aDavid Schnurr2 years ago | 104 | ## Async usage |
3c6d4cd6Greg Brockman5 years ago | 105 | |
08b8179aDavid Schnurr2 years ago | 106 | Simply import `AsyncOpenAI` instead of `OpenAI` and use `await` with each API call: |
ede08829Jakub Roztocil3 years ago | 107 | |
08b8179aDavid Schnurr2 years ago | 108 | ```python |
fb5ba01cStainless Bot2 years ago | 109 | import os |
08b8179aDavid Schnurr2 years ago | 110 | import asyncio |
| 111 | from openai import AsyncOpenAI | |
ede08829Jakub Roztocil3 years ago | 112 | |
08b8179aDavid Schnurr2 years ago | 113 | client = AsyncOpenAI( |
fb5ba01cStainless Bot2 years ago | 114 | # This is the default and can be omitted |
| 115 | api_key=os.environ.get("OPENAI_API_KEY"), | |
08b8179aDavid Schnurr2 years ago | 116 | ) |
ede08829Jakub Roztocil3 years ago | 117 | |
| 118 | | |
08b8179aDavid Schnurr2 years ago | 119 | async def main() -> None: |
| 120 | chat_completion = await client.chat.completions.create( | |
| 121 | messages=[ | |
| 122 | { | |
| 123 | "role": "user", | |
| 124 | "content": "Say this is a test", | |
| 125 | } | |
| 126 | ], | |
| 127 | model="gpt-3.5-turbo", | |
| 128 | ) | |
ede08829Jakub Roztocil3 years ago | 129 | |
| 130 | | |
08b8179aDavid Schnurr2 years ago | 131 | asyncio.run(main()) |
2b21516eAtty Eleti3 years ago | 132 | ``` |
ede08829Jakub Roztocil3 years ago | 133 | |
08b8179aDavid Schnurr2 years ago | 134 | Functionality between the synchronous and asynchronous clients is otherwise identical. |
| 135 | | |
2b23eb53Stainless Bot2 years ago | 136 | ## Streaming responses |
08b8179aDavid Schnurr2 years ago | 137 | |
| 138 | We provide support for streaming responses using Server Side Events (SSE). | |
d53d9efbRachel Lim5 years ago | 139 | |
08b8179aDavid Schnurr2 years ago | 140 | ```python |
| 141 | from openai import OpenAI | |
| 142 | | |
| 143 | client = OpenAI() | |
d53d9efbRachel Lim5 years ago | 144 | |
08b8179aDavid Schnurr2 years ago | 145 | stream = client.chat.completions.create( |
| 146 | model="gpt-4", | |
| 147 | messages=[{"role": "user", "content": "Say this is a test"}], | |
| 148 | stream=True, | |
| 149 | ) | |
54c7c512Stainless Bot2 years ago | 150 | for chunk in stream: |
a3da0196Stainless Bot2 years ago | 151 | print(chunk.choices[0].delta.content or "", end="") |
d53d9efbRachel Lim5 years ago | 152 | ``` |
| 153 | | |
08b8179aDavid Schnurr2 years ago | 154 | The async client uses the exact same interface. |
d53d9efbRachel Lim5 years ago | 155 | |
| 156 | ```python | |
08b8179aDavid Schnurr2 years ago | 157 | from openai import AsyncOpenAI |
| 158 | | |
| 159 | client = AsyncOpenAI() | |
| 160 | | |
a3da0196Stainless Bot2 years ago | 161 | |
dfe1c8daSahand Sojoodi2 years ago | 162 | async def main(): |
| 163 | stream = await client.chat.completions.create( | |
| 164 | model="gpt-4", | |
| 165 | messages=[{"role": "user", "content": "Say this is a test"}], | |
| 166 | stream=True, | |
| 167 | ) | |
| 168 | async for chunk in stream: | |
a3da0196Stainless Bot2 years ago | 169 | print(chunk.choices[0].delta.content or "", end="") |
| 170 | | |
dfe1c8daSahand Sojoodi2 years ago | 171 | |
| 172 | asyncio.run(main()) | |
53e5ba4bt-asutedjo4 years ago | 173 | ``` |
2b21516eAtty Eleti3 years ago | 174 | |
08b8179aDavid Schnurr2 years ago | 175 | ## Module-level client |
d53d9efbRachel Lim5 years ago | 176 | |
08b8179aDavid Schnurr2 years ago | 177 | > [!IMPORTANT] |
| 178 | > We highly recommend instantiating client instances instead of relying on the global client. | |
d53d9efbRachel Lim5 years ago | 179 | |
08b8179aDavid Schnurr2 years ago | 180 | We also expose a global client instance that is accessible in a similar fashion to versions prior to v1. |
6572ef4fZheNing Hu3 years ago | 181 | |
08b8179aDavid Schnurr2 years ago | 182 | ```py |
| 183 | import openai | |
62b73b9bAtty Eleti3 years ago | 184 | |
08b8179aDavid Schnurr2 years ago | 185 | # optional; defaults to `os.environ['OPENAI_API_KEY']` |
| 186 | openai.api_key = '...' | |
| 187 | | |
| 188 | # all client options can be configured just like the `OpenAI` instantiation counterpart | |
| 189 | openai.base_url = "https://..." | |
| 190 | openai.default_headers = {"x-foo": "true"} | |
| 191 | | |
| 192 | completion = openai.chat.completions.create( | |
| 193 | model="gpt-4", | |
| 194 | messages=[ | |
| 195 | { | |
| 196 | "role": "user", | |
| 197 | "content": "How do I output all files in a directory using Python?", | |
| 198 | }, | |
| 199 | ], | |
| 200 | ) | |
62b73b9bAtty Eleti3 years ago | 201 | print(completion.choices[0].message.content) |
| 202 | ``` | |
| 203 | | |
08b8179aDavid Schnurr2 years ago | 204 | The API is the exact same as the standard client instance based API. |
376dd199Logan Kilpatrick2 years ago | 205 | |
08b8179aDavid Schnurr2 years ago | 206 | This is intended to be used within REPLs or notebooks for faster iteration, **not** in application code. |
2b21516eAtty Eleti3 years ago | 207 | |
08b8179aDavid Schnurr2 years ago | 208 | We recommend that you always instantiate a client (e.g., with `client = OpenAI()`) in application code because: |
2b21516eAtty Eleti3 years ago | 209 | |
08b8179aDavid Schnurr2 years ago | 210 | - It can be difficult to reason about where client options are configured |
| 211 | - It's not possible to change certain client options without potentially causing race conditions | |
| 212 | - It's harder to mock for testing purposes | |
| 213 | - It's not possible to control cleanup of network connections | |
| 214 | | |
| 215 | ## Using types | |
| 216 | | |
47656567Stainless Bot2 years ago | 217 | 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 | 218 | |
47656567Stainless Bot2 years ago | 219 | - Serializing back into JSON, `model.to_json()` |
| 220 | - Converting to a dictionary, `model.to_dict()` | |
2b21516eAtty Eleti3 years ago | 221 | |
08b8179aDavid Schnurr2 years ago | 222 | 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 | 223 | |
08b8179aDavid Schnurr2 years ago | 224 | ## Pagination |
0e21703eTed Sanders4 years ago | 225 | |
08b8179aDavid Schnurr2 years ago | 226 | List methods in the OpenAI API are paginated. |
| 227 | | |
| 228 | This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually: | |
0e21703eTed Sanders4 years ago | 229 | |
| 230 | ```python | |
08b8179aDavid Schnurr2 years ago | 231 | import openai |
0e21703eTed Sanders4 years ago | 232 | |
08b8179aDavid Schnurr2 years ago | 233 | client = OpenAI() |
0e21703eTed Sanders4 years ago | 234 | |
08b8179aDavid Schnurr2 years ago | 235 | all_jobs = [] |
| 236 | # Automatically fetches more pages as needed. | |
| 237 | for job in client.fine_tuning.jobs.list( | |
| 238 | limit=20, | |
| 239 | ): | |
| 240 | # Do something with job here | |
| 241 | all_jobs.append(job) | |
| 242 | print(all_jobs) | |
0e21703eTed Sanders4 years ago | 243 | ``` |
| 244 | | |
08b8179aDavid Schnurr2 years ago | 245 | Or, asynchronously: |
0e21703eTed Sanders4 years ago | 246 | |
08b8179aDavid Schnurr2 years ago | 247 | ```python |
| 248 | import asyncio | |
| 249 | import openai | |
0e21703eTed Sanders4 years ago | 250 | |
08b8179aDavid Schnurr2 years ago | 251 | client = AsyncOpenAI() |
0e21703eTed Sanders4 years ago | 252 | |
| 253 | | |
08b8179aDavid Schnurr2 years ago | 254 | async def main() -> None: |
| 255 | all_jobs = [] | |
| 256 | # Iterate through items across all pages, issuing requests as needed. | |
| 257 | async for job in client.fine_tuning.jobs.list( | |
| 258 | limit=20, | |
| 259 | ): | |
| 260 | all_jobs.append(job) | |
| 261 | print(all_jobs) | |
0e21703eTed Sanders4 years ago | 262 | |
62b51ca0Boris Dayma4 years ago | 263 | |
08b8179aDavid Schnurr2 years ago | 264 | asyncio.run(main()) |
| 265 | ``` | |
2942bf4bLogan Kilpatrick2 years ago | 266 | |
08b8179aDavid Schnurr2 years ago | 267 | 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 | 268 | |
08b8179aDavid Schnurr2 years ago | 269 | ```python |
| 270 | first_page = await client.fine_tuning.jobs.list( | |
| 271 | limit=20, | |
| 272 | ) | |
| 273 | if first_page.has_next_page(): | |
| 274 | print(f"will fetch next page using these details: {first_page.next_page_info()}") | |
| 275 | next_page = await first_page.get_next_page() | |
| 276 | print(f"number of items we just fetched: {len(next_page.data)}") | |
| 277 | | |
| 278 | # Remove `await` for non-async usage. | |
62b51ca0Boris Dayma4 years ago | 279 | ``` |
| 280 | | |
08b8179aDavid Schnurr2 years ago | 281 | Or just work directly with the returned data: |
0e21703eTed Sanders4 years ago | 282 | |
08b8179aDavid Schnurr2 years ago | 283 | ```python |
| 284 | first_page = await client.fine_tuning.jobs.list( | |
| 285 | limit=20, | |
| 286 | ) | |
e389823bMorgan McGuire2 years ago | 287 | |
08b8179aDavid Schnurr2 years ago | 288 | print(f"next page cursor: {first_page.after}") # => "next page cursor: ..." |
| 289 | for job in first_page.data: | |
| 290 | print(job.id) | |
e389823bMorgan McGuire2 years ago | 291 | |
08b8179aDavid Schnurr2 years ago | 292 | # Remove `await` for non-async usage. |
| 293 | ``` | |
e389823bMorgan McGuire2 years ago | 294 | |
08b8179aDavid Schnurr2 years ago | 295 | ## Nested params |
3c00e856hallacy3 years ago | 296 | |
08b8179aDavid Schnurr2 years ago | 297 | Nested parameters are dictionaries, typed using `TypedDict`, for example: |
3c00e856hallacy3 years ago | 298 | |
| 299 | ```python | |
08b8179aDavid Schnurr2 years ago | 300 | from openai import OpenAI |
3c00e856hallacy3 years ago | 301 | |
08b8179aDavid Schnurr2 years ago | 302 | client = OpenAI() |
| 303 | | |
aa681899Stainless Bot2 years ago | 304 | completion = client.chat.completions.create( |
| 305 | messages=[ | |
| 306 | { | |
| 307 | "role": "user", | |
| 308 | "content": "Can you generate an example json object describing a fruit?", | |
| 309 | } | |
| 310 | ], | |
9c8789c2Stainless Bot2 years ago | 311 | model="gpt-3.5-turbo-1106", |
aa681899Stainless Bot2 years ago | 312 | response_format={"type": "json_object"}, |
| 313 | ) | |
08b8179aDavid Schnurr2 years ago | 314 | ``` |
3c00e856hallacy3 years ago | 315 | |
2b23eb53Stainless Bot2 years ago | 316 | ## File uploads |
dc33cb9dMichelle Pokrass3 years ago | 317 | |
08b8179aDavid Schnurr2 years ago | 318 | Request parameters that correspond to file uploads can be passed as `bytes`, 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 | 319 | |
376dd199Logan Kilpatrick2 years ago | 320 | ```python |
08b8179aDavid Schnurr2 years ago | 321 | from pathlib import Path |
| 322 | from openai import OpenAI | |
| 323 | | |
| 324 | client = OpenAI() | |
| 325 | | |
| 326 | client.files.create( | |
| 327 | file=Path("input.jsonl"), | |
| 328 | purpose="fine-tune", | |
| 329 | ) | |
dc33cb9dMichelle Pokrass3 years ago | 330 | ``` |
| 331 | | |
08b8179aDavid Schnurr2 years ago | 332 | 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 | 333 | |
08b8179aDavid Schnurr2 years ago | 334 | ## Handling errors |
376dd199Logan Kilpatrick2 years ago | 335 | |
08b8179aDavid Schnurr2 years ago | 336 | 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 | 337 | |
08b8179aDavid Schnurr2 years ago | 338 | When the API returns a non-success status code (that is, 4xx or 5xx |
| 339 | response), a subclass of `openai.APIStatusError` is raised, containing `status_code` and `response` properties. | |
62b73b9bAtty Eleti3 years ago | 340 | |
08b8179aDavid Schnurr2 years ago | 341 | All errors inherit from `openai.APIError`. |
| 342 | | |
| 343 | ```python | |
| 344 | import openai | |
| 345 | from openai import OpenAI | |
| 346 | | |
| 347 | client = OpenAI() | |
| 348 | | |
| 349 | try: | |
8dab1421Stainless Bot2 years ago | 350 | client.fine_tuning.jobs.create( |
| 351 | model="gpt-3.5-turbo", | |
| 352 | training_file="file-abc123", | |
08b8179aDavid Schnurr2 years ago | 353 | ) |
| 354 | except openai.APIConnectionError as e: | |
| 355 | print("The server could not be reached") | |
| 356 | print(e.__cause__) # an underlying Exception, likely raised within httpx. | |
| 357 | except openai.RateLimitError as e: | |
| 358 | print("A 429 status code was received; we should back off a bit.") | |
| 359 | except openai.APIStatusError as e: | |
| 360 | print("Another non-200-range status code was received") | |
| 361 | print(e.status_code) | |
| 362 | print(e.response) | |
62b73b9bAtty Eleti3 years ago | 363 | ``` |
| 364 | | |
08b8179aDavid Schnurr2 years ago | 365 | Error codes are as followed: |
| 366 | | |
| 367 | | Status Code | Error Type | | |
| 368 | | ----------- | -------------------------- | | |
| 369 | | 400 | `BadRequestError` | | |
| 370 | | 401 | `AuthenticationError` | | |
| 371 | | 403 | `PermissionDeniedError` | | |
| 372 | | 404 | `NotFoundError` | | |
| 373 | | 422 | `UnprocessableEntityError` | | |
| 374 | | 429 | `RateLimitError` | | |
| 375 | | >=500 | `InternalServerError` | | |
| 376 | | N/A | `APIConnectionError` | | |
| 377 | | |
| 378 | ### Retries | |
376dd199Logan Kilpatrick2 years ago | 379 | |
08b8179aDavid Schnurr2 years ago | 380 | Certain errors are automatically retried 2 times by default, with a short exponential backoff. |
| 381 | Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, | |
| 382 | 429 Rate Limit, and >=500 Internal errors are all retried by default. | |
0abf6413Andrew Chen Wang3 years ago | 383 | |
08b8179aDavid Schnurr2 years ago | 384 | You can use the `max_retries` option to configure or disable retry settings: |
0abf6413Andrew Chen Wang3 years ago | 385 | |
| 386 | ```python | |
08b8179aDavid Schnurr2 years ago | 387 | from openai import OpenAI |
| 388 | | |
| 389 | # Configure the default for all requests: | |
| 390 | client = OpenAI( | |
| 391 | # default is 2 | |
| 392 | max_retries=0, | |
| 393 | ) | |
| 394 | | |
| 395 | # Or, configure per-request: | |
| 396 | client.with_options(max_retries=5).chat.completions.create( | |
| 397 | messages=[ | |
| 398 | { | |
| 399 | "role": "user", | |
| 400 | "content": "How can I get the name of the current day in Node.js?", | |
| 401 | } | |
| 402 | ], | |
| 403 | model="gpt-3.5-turbo", | |
| 404 | ) | |
0abf6413Andrew Chen Wang3 years ago | 405 | ``` |
| 406 | | |
08b8179aDavid Schnurr2 years ago | 407 | ### Timeouts |
0abf6413Andrew Chen Wang3 years ago | 408 | |
08b8179aDavid Schnurr2 years ago | 409 | By default requests time out after 10 minutes. You can configure this with a `timeout` option, |
| 410 | which accepts a float or an [`httpx.Timeout`](https://www.python-httpx.org/advanced/#fine-tuning-the-configuration) object: | |
376dd199Logan Kilpatrick2 years ago | 411 | |
08b8179aDavid Schnurr2 years ago | 412 | ```python |
| 413 | from openai import OpenAI | |
| 414 | | |
| 415 | # Configure the default for all requests: | |
| 416 | client = OpenAI( | |
1381f46eStainless Bot2 years ago | 417 | # 20 seconds (default is 10 minutes) |
08b8179aDavid Schnurr2 years ago | 418 | timeout=20.0, |
| 419 | ) | |
| 420 | | |
| 421 | # More granular control: | |
| 422 | client = OpenAI( | |
| 423 | timeout=httpx.Timeout(60.0, read=5.0, write=10.0, connect=2.0), | |
| 424 | ) | |
| 425 | | |
| 426 | # Override per-request: | |
2a678e30Stainless Bot2 years ago | 427 | client.with_options(timeout=5.0).chat.completions.create( |
08b8179aDavid Schnurr2 years ago | 428 | messages=[ |
| 429 | { | |
| 430 | "role": "user", | |
| 431 | "content": "How can I list all files in a directory using Python?", | |
| 432 | } | |
| 433 | ], | |
| 434 | model="gpt-3.5-turbo", | |
| 435 | ) | |
0abf6413Andrew Chen Wang3 years ago | 436 | ``` |
| 437 | | |
08b8179aDavid Schnurr2 years ago | 438 | On timeout, an `APITimeoutError` is thrown. |
376dd199Logan Kilpatrick2 years ago | 439 | |
08b8179aDavid Schnurr2 years ago | 440 | Note that requests that time out are [retried twice by default](#retries). |
376dd199Logan Kilpatrick2 years ago | 441 | |
08b8179aDavid Schnurr2 years ago | 442 | ## Advanced |
376dd199Logan Kilpatrick2 years ago | 443 | |
08b8179aDavid Schnurr2 years ago | 444 | ### Logging |
376dd199Logan Kilpatrick2 years ago | 445 | |
08b8179aDavid Schnurr2 years ago | 446 | We use the standard library [`logging`](https://docs.python.org/3/library/logging.html) module. |
376dd199Logan Kilpatrick2 years ago | 447 | |
08b8179aDavid Schnurr2 years ago | 448 | You can enable logging by setting the environment variable `OPENAI_LOG` to `debug`. |
376dd199Logan Kilpatrick2 years ago | 449 | |
08b8179aDavid Schnurr2 years ago | 450 | ```shell |
| 451 | $ export OPENAI_LOG=debug | |
376dd199Logan Kilpatrick2 years ago | 452 | ``` |
| 453 | | |
08b8179aDavid Schnurr2 years ago | 454 | ### How to tell whether `None` means `null` or missing |
dc33cb9dMichelle Pokrass3 years ago | 455 | |
08b8179aDavid Schnurr2 years ago | 456 | 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 | 457 | |
08b8179aDavid Schnurr2 years ago | 458 | ```py |
| 459 | if response.my_field is None: | |
| 460 | if 'my_field' not in response.model_fields_set: | |
| 461 | print('Got json like {}, without a "my_field" key present at all.') | |
| 462 | else: | |
| 463 | print('Got json like {"my_field": null}.') | |
| 464 | ``` | |
376dd199Logan Kilpatrick2 years ago | 465 | |
08b8179aDavid Schnurr2 years ago | 466 | ### Accessing raw response data (e.g. headers) |
376dd199Logan Kilpatrick2 years ago | 467 | |
86379b44Stainless Bot2 years ago | 468 | The "raw" Response object can be accessed by prefixing `.with_raw_response.` to any HTTP method call, e.g., |
08b8179aDavid Schnurr2 years ago | 469 | |
| 470 | ```py | |
| 471 | from openai import OpenAI | |
| 472 | | |
| 473 | client = OpenAI() | |
| 474 | response = client.chat.completions.with_raw_response.create( | |
| 475 | messages=[{ | |
| 476 | "role": "user", | |
| 477 | "content": "Say this is a test", | |
| 478 | }], | |
| 479 | model="gpt-3.5-turbo", | |
| 480 | ) | |
| 481 | print(response.headers.get('X-My-Header')) | |
| 482 | | |
| 483 | completion = response.parse() # get the object that `chat.completions.create()` would have returned | |
| 484 | print(completion) | |
376dd199Logan Kilpatrick2 years ago | 485 | ``` |
| 486 | | |
86379b44Stainless Bot2 years ago | 487 | These methods return an [`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. |
| 488 | | |
| 489 | For the sync client this will mostly be the same with the exception | |
| 490 | of `content` & `text` will be methods instead of properties. In the | |
| 491 | async client, all methods will be async. | |
| 492 | | |
| 493 | A migration script will be provided & the migration in general should | |
| 494 | be smooth. | |
| 495 | | |
| 496 | #### `.with_streaming_response` | |
| 497 | | |
| 498 | The above interface eagerly reads the full response body when you make the request, which may not always be what you want. | |
| 499 | | |
| 500 | 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. | |
| 501 | | |
| 502 | 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. | |
| 503 | | |
| 504 | ```python | |
| 505 | with client.chat.completions.with_streaming_response.create( | |
| 506 | messages=[ | |
| 507 | { | |
| 508 | "role": "user", | |
| 509 | "content": "Say this is a test", | |
| 510 | } | |
| 511 | ], | |
| 512 | model="gpt-3.5-turbo", | |
| 513 | ) as response: | |
| 514 | print(response.headers.get("X-My-Header")) | |
| 515 | | |
| 516 | for line in response.iter_lines(): | |
| 517 | print(line) | |
| 518 | ``` | |
| 519 | | |
| 520 | The context manager is required so that the response will reliably be closed. | |
3c6d4cd6Greg Brockman5 years ago | 521 | |
73869eeeStainless Bot2 years ago | 522 | ### Making custom/undocumented requests |
| 523 | | |
7931ebaaStainless Bot2 years ago | 524 | This library is typed for convenient access to the documented API. |
73869eeeStainless Bot2 years ago | 525 | |
| 526 | If you need to access undocumented endpoints, params, or response properties, the library can still be used. | |
| 527 | | |
| 528 | #### Undocumented endpoints | |
| 529 | | |
| 530 | To make requests to undocumented endpoints, you can make requests using `client.get`, `client.post`, and other | |
| 531 | http verbs. Options on the client will be respected (such as retries) will be respected when making this | |
| 532 | request. | |
| 533 | | |
| 534 | ```py | |
| 535 | import httpx | |
| 536 | | |
| 537 | response = client.post( | |
| 538 | "/foo", | |
| 539 | cast_to=httpx.Response, | |
| 540 | body={"my_param": True}, | |
| 541 | ) | |
| 542 | | |
| 543 | print(response.headers.get("x-foo")) | |
| 544 | ``` | |
| 545 | | |
802819c8Stainless Bot2 years ago | 546 | #### Undocumented request params |
73869eeeStainless Bot2 years ago | 547 | |
| 548 | If you want to explicitly send an extra param, you can do so with the `extra_query`, `extra_body`, and `extra_headers` request | |
| 549 | options. | |
| 550 | | |
802819c8Stainless Bot2 years ago | 551 | #### Undocumented response properties |
73869eeeStainless Bot2 years ago | 552 | |
| 553 | To access undocumented response properties, you can access the extra fields like `response.unknown_prop`. You | |
| 554 | can also get all the extra fields on the Pydantic model as a dict with | |
| 555 | [`response.model_extra`](https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_extra). | |
| 556 | | |
08b8179aDavid Schnurr2 years ago | 557 | ### Configuring the HTTP client |
376dd199Logan Kilpatrick2 years ago | 558 | |
08b8179aDavid Schnurr2 years ago | 559 | 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 | 560 | |
08b8179aDavid Schnurr2 years ago | 561 | - Support for proxies |
| 562 | - Custom transports | |
| 563 | - Additional [advanced](https://www.python-httpx.org/advanced/#client-instances) functionality | |
376dd199Logan Kilpatrick2 years ago | 564 | |
| 565 | ```python | |
347363edStainless Bot2 years ago | 566 | from openai import OpenAI, DefaultHttpxClient |
08b8179aDavid Schnurr2 years ago | 567 | |
| 568 | client = OpenAI( | |
0733934fStainless Bot2 years ago | 569 | # Or use the `OPENAI_BASE_URL` env var |
08b8179aDavid Schnurr2 years ago | 570 | base_url="http://my.test.server.example.com:8083", |
347363edStainless Bot2 years ago | 571 | http_client=DefaultHttpxClient( |
08b8179aDavid Schnurr2 years ago | 572 | proxies="http://my.test.proxy.example.com", |
| 573 | transport=httpx.HTTPTransport(local_address="0.0.0.0"), | |
| 574 | ), | |
| 575 | ) | |
| 576 | ``` | |
| 577 | | |
| 578 | ### Managing HTTP resources | |
| 579 | | |
| 580 | 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. | |
| 581 | | |
| 582 | ## Microsoft Azure OpenAI | |
| 583 | | |
| 584 | To use this library with [Azure OpenAI](https://learn.microsoft.com/en-us/azure/ai-services/openai/overview), use the `AzureOpenAI` | |
| 585 | class instead of the `OpenAI` class. | |
| 586 | | |
| 587 | > [!IMPORTANT] | |
| 588 | > The Azure API shape differs from the core API shape which means that the static types for responses / params | |
| 589 | > won't always be correct. | |
376dd199Logan Kilpatrick2 years ago | 590 | |
08b8179aDavid Schnurr2 years ago | 591 | ```py |
| 592 | from openai import AzureOpenAI | |
376dd199Logan Kilpatrick2 years ago | 593 | |
08b8179aDavid Schnurr2 years ago | 594 | # gets the API Key from environment variable AZURE_OPENAI_API_KEY |
| 595 | client = AzureOpenAI( | |
| 596 | # https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning | |
819ae68dJackYu2 years ago | 597 | api_version="2023-07-01-preview", |
08b8179aDavid Schnurr2 years ago | 598 | # https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/create-resource?pivots=web-portal#create-a-resource |
| 599 | azure_endpoint="https://example-endpoint.openai.azure.com", | |
| 600 | ) | |
| 601 | | |
| 602 | completion = client.chat.completions.create( | |
| 603 | model="deployment-name", # e.g. gpt-35-instant | |
| 604 | messages=[ | |
| 605 | { | |
| 606 | "role": "user", | |
| 607 | "content": "How do I output all files in a directory using Python?", | |
| 608 | }, | |
| 609 | ], | |
| 610 | ) | |
47656567Stainless Bot2 years ago | 611 | print(completion.to_json()) |
376dd199Logan Kilpatrick2 years ago | 612 | ``` |
3c6d4cd6Greg Brockman5 years ago | 613 | |
08b8179aDavid Schnurr2 years ago | 614 | In addition to the options provided in the base `OpenAI` client, the following options are provided: |
| 615 | | |
7758c54bStainless Bot2 years ago | 616 | - `azure_endpoint` (or the `AZURE_OPENAI_ENDPOINT` environment variable) |
08b8179aDavid Schnurr2 years ago | 617 | - `azure_deployment` |
7758c54bStainless Bot2 years ago | 618 | - `api_version` (or the `OPENAI_API_VERSION` environment variable) |
| 619 | - `azure_ad_token` (or the `AZURE_OPENAI_AD_TOKEN` environment variable) | |
08b8179aDavid Schnurr2 years ago | 620 | - `azure_ad_token_provider` |
| 621 | | |
812839cbMikyo King2 years ago | 622 | An example of using the client with Azure Active Directory can be found [here](https://github.com/openai/openai-python/blob/main/examples/azure_ad.py). |
08b8179aDavid Schnurr2 years ago | 623 | |
| 624 | ## Versioning | |
| 625 | | |
| 626 | 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: | |
| 627 | | |
| 628 | 1. Changes that only affect static types, without breaking runtime behavior. | |
| 629 | 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)_. | |
| 630 | 3. Changes that we do not expect to impact the vast majority of users in practice. | |
| 631 | | |
| 632 | We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience. | |
| 633 | | |
| 634 | We are keen for your feedback; please open an [issue](https://www.github.com/openai/openai-python/issues) with questions, bugs, or suggestions. | |
| 635 | | |
| 636 | ## Requirements | |
3c6d4cd6Greg Brockman5 years ago | 637 | |
08b8179aDavid Schnurr2 years ago | 638 | Python 3.7 or higher. |