You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The OpenAI Python library provides convenient access to the OpenAI REST API from any Python 3.7+
6
-
application. It includes type definitions for all request params and response fields,
6
+
application. The library includes type definitions for all request params and response fields,
7
7
and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx).
8
8
9
9
## Documentation
10
10
11
-
The API documentation can be found [here](https://platform.openai.com/docs).
11
+
The API documentation can be found at [https://platform.openai.com/docs](https://platform.openai.com/docs).
12
12
13
13
## Installation
14
14
@@ -43,7 +43,7 @@ print(completion.choices)
43
43
While you can provide an `api_key` keyword argument, we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
44
44
and adding `OPENAI_API_KEY="my api key"` to your `.env` file so that your API Key is not stored in source control.
45
45
46
-
## Async Usage
46
+
## Async usage
47
47
48
48
Simply import `AsyncOpenAI` instead of `OpenAI` and use `await` with each API call:
49
49
@@ -148,11 +148,11 @@ We recommend that you always instantiate a client (e.g., with `client = OpenAI()
148
148
- It's harder to mock for testing purposes
149
149
- It's not possible to control cleanup of network connections
150
150
151
-
## Using Types
151
+
## Using types
152
152
153
-
Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev), which provide helper methods for things like serializing back into json ([v1](https://docs.pydantic.dev/1.10/usage/models/), [v2](https://docs.pydantic.dev/latest/usage/serialization/)). To get a dictionary, you can call `dict(model)`.
153
+
Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev), which provide helper methods for things like serializing back into JSON ([v1](https://docs.pydantic.dev/1.10/usage/models/), [v2](https://docs.pydantic.dev/latest/usage/serialization/)). To get a dictionary, call `dict(model)`.
154
154
155
-
This helps 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"`.
155
+
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`.
156
156
157
157
## Pagination
158
158
@@ -273,10 +273,10 @@ await client.files.create(
273
273
274
274
## Handling errors
275
275
276
-
When the library is unable to connect to the API (e.g., due to network connection problems or a timeout), a subclass of `openai.APIConnectionError` is raised.
276
+
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.
277
277
278
-
When the API returns a non-success status code (i.e., 4xx or 5xx
279
-
response), a subclass of `openai.APIStatusError`will be raised, containing `status_code` and `response` properties.
278
+
When the API returns a non-success status code (that is, 4xx or 5xx
279
+
response), a subclass of `openai.APIStatusError`is raised, containing `status_code` and `response` properties.
280
280
281
281
All errors inherit from `openai.APIError`.
282
282
@@ -316,11 +316,11 @@ Error codes are as followed:
316
316
317
317
### Retries
318
318
319
-
Certain errors will be automatically retried 2 times by default, with a short exponential backoff.
319
+
Certain errors are automatically retried 2 times by default, with a short exponential backoff.
320
320
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,
321
-
429 Rate Limit, and >=500 Internal errors will all be retried by default.
321
+
429 Rate Limit, and >=500 Internal errors are all retried by default.
322
322
323
-
You can use the `max_retries` option to configure or disable this:
323
+
You can use the `max_retries` option to configure or disable retry settings:
Note that requests which time out will be[retried twice by default](#retries).
379
+
Note that requests that time out are[retried twice by default](#retries).
380
380
381
381
## Advanced
382
382
383
383
### How to tell whether `None` means `null` or missing
384
384
385
-
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`:
385
+
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`:
386
386
387
387
```py
388
388
if response.my_field isNone:
@@ -392,27 +392,30 @@ if response.my_field is None:
392
392
print('Got json like {"my_field": null}.')
393
393
```
394
394
395
-
### Configuring custom URLs, proxies, and transports
395
+
### Configuring the HTTP client
396
396
397
-
You can configure the following keyword arguments when instantiating the client:
397
+
You can directly override the [httpx client](https://www.python-httpx.org/api/#client) to customize it for your use case, including:
See the httpx documentation for information about the [`proxies`](https://www.python-httpx.org/advanced/#http-proxying) and [`transport`](https://www.python-httpx.org/advanced/#custom-transports) keyword arguments.
412
-
413
416
### Managing HTTP resources
414
417
415
-
By default we will close the underlying HTTP connections whenever the client is [garbage collected](https://docs.python.org/3/reference/datamodel.html#object.__del__) is called but you can also manually close the client using the `.close()` method if desired, or with a context manager that closes when exiting.
418
+
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.
0 commit comments