Skip to content

Commit a91c142

Browse files
authored
Merge pull request #1 from mmacy/docs-experiment-autogen
integrate mkdocstrings-insiders features++
2 parents 28c538b + 0f598cd commit a91c142

File tree

294 files changed

+1461
-415
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

294 files changed

+1461
-415
lines changed

README.md

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
[![PyPI version](https://img.shields.io/pypi/v/openai.svg)](https://pypi.org/project/openai/)
44

5-
The OpenAI Python library provides access to the OpenAI REST API in Python applications. The library includes type definitions for all request params and response fields, offering clients for both synchronous and asynchronous operations powered by [httpx](https://github.com/encode/httpx).
5+
<!-- ---8<--- [start:get-started] -->
6+
7+
The OpenAI Python library provides access to the OpenAI REST API in Python applications. It includes type definitions for all request params and response fields and has clients for both synchronous and asynchronous operations powered by [httpx](https://github.com/encode/httpx).
68

79
The OpenAI Python library is generated from OpenAI's [OpenAPI specification](https://github.com/openai/openai-openapi) with [Stainless](https://stainlessapi.com/).
810

@@ -11,31 +13,37 @@ The OpenAI Python library is generated from OpenAI's [OpenAPI specification](htt
1113
- [Python](https://www.python.org/) 3.7+
1214
- [OpenAI API key](https://platform.openai.com/account/api-keys)
1315

14-
## Installation
16+
## Install the package
1517

16-
You can install the [openai](https://pypi.org/project/openai/) package from PyPi with `pip`:
18+
You can the [openai](https://pypi.org/project/openai/) package from PyPi with `pip`:
1719

1820
```sh
1921
# Install the package
2022
pip install openai
2123
```
2224

23-
### Migration
25+
## Migrate from earlier versions
26+
27+
Released on November 6th 2023, the OpenAI Python library was rewritten for version `1.0.0`.
28+
29+
If your project used a pre-v1 version of the library, see the [v1 migration guide](https://github.com/openai/openai-python/discussions/742) for information and scripts that can help you update your code.
30+
31+
<!-- ---8<--- [end:get-started] -->
2432

25-
Released on November 6th 2023, the OpenAI Python library was rewritten for v1. If your project used a pre-v1 version of the library, see the [v1 migration guide](https://github.com/openai/openai-python/discussions/742) for information and scripts that can help you update your code.
33+
## Connect
2634

27-
## Usage
35+
<!-- ---8<--- [start:connect] -->
2836

2937
To connect to the OpenAI API:
3038

31-
1. Populate an `OPENAI_API_KEY` environment variable with your [OpenAI API key](https://platform.openai.com/account/api-keys).
39+
1. Populate an `OPENAI_API_KEY` environment variable with your [OpenAI API key](https://platform.openai.com/account/api-keys)
3240
2. Create a synchronous or asynchronous `OpenAI` client object.
3341

34-
!!! Tip
3542

36-
To reduce the risk of committing your OpenAI API key to source control, we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/) and adding `OPENAI_API_KEY="YOUR_API_KEY_HERE"` to your `.env` file.
43+
!!! Tip
44+
To reduce the risk of committing your OpenAI API key to source control, you can use [python-dotenv](https://pypi.org/project/python-dotenv/) and add `OPENAI_API_KEY="YOUR_API_KEY_HERE"` to your `.env` file.
3745

38-
### Synchronous client
46+
## Synchronous client
3947

4048
Create an instance of the [OpenAI][src.openai.OpenAI] client:
4149

@@ -78,7 +86,7 @@ for chunk in stream:
7886

7987
1. :material-chat: This enables response streaming through Server Side Events (SSE).
8088

81-
### Asynchronous client
89+
## Asynchronous client
8290

8391
Create an instance of the [AsyncOpenAI][src.openai.AsyncOpenAI] client and `await` each API call. Functionality between the synchronous and asynchronous clients is otherwise identical.
8492

@@ -132,7 +140,7 @@ asyncio.run(main())
132140

133141
1. :material-chat: This enables response streaming through Server Side Events (SSE).
134142

135-
### Module-level global client
143+
## Module-level global client
136144

137145
Similar to pre-v1 versions of the library, there is also a module-level client available for use in REPLs, notebooks, and other scenarios requiring quick "local loop" iteration.
138146

@@ -163,15 +171,18 @@ completion = openai.chat.completions.create(
163171
print(completion.choices[0].message.content)
164172
```
165173

166-
We recommend you *avoid* using this module-level client your application code because:
174+
We recommend you _avoid_ using this module-level client your application code because:
167175

168176
- It can be difficult to reason about where client options are configured.
169177
- It's impossible to change certain client options without causing the potential for race conditions.
170178
- It's harder to mock for testing purposes.
171179
- It's impossible to control cleanup of network connections.
180+
<!-- ---8<--- [end:connect] -->
172181

173182
## Request types
174183

184+
<!-- ---8<--- [start:request-response] -->
185+
175186
Nested **request** parameters are Python [TypedDicts][typing.TypedDict].
176187

177188
For example, the user message in the following [`chat.completions.create()`][src.openai.resources.chat.completions.Completions.create] request is a [`ChatCompletionUserMessageParam`][src.openai.types.chat.chat_completion_user_message_param.ChatCompletionUserMessageParam], which has a base type of [`TypedDict`][typing.TypedDict]:
@@ -222,8 +233,12 @@ The async client uses the same interface. If you pass a [`PathLike`][os.PathLike
222233

223234
Typed requests and responses enable type checking, autocompletion, and hover-help documentation in editors that support those features. In Visual Studio Code, for example, you can [enable type checking in Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) by setting `python.analysis.typeCheckingMode` to `basic` as described in that article's **Settings and Customization** section.
224235

236+
<!-- ---8<--- [end:request-response] -->
237+
225238
## Handling errors
226239

240+
<!-- ---8<--- [start:handle-errors] -->
241+
227242
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`][src.openai.APIConnectionError] is raised.
228243

229244
When the API returns a non-success status code (that is, 4xx or 5xx
@@ -255,22 +270,23 @@ except openai.APIStatusError as e:
255270

256271
Error codes are as followed:
257272

258-
| Status Code | Error Type |
259-
| ----------- | -------------------------- |
260-
| 400 | `BadRequestError` |
261-
| 401 | `AuthenticationError` |
262-
| 403 | `PermissionDeniedError` |
263-
| 404 | `NotFoundError` |
264-
| 422 | `UnprocessableEntityError` |
265-
| 429 | `RateLimitError` |
266-
| >=500 | `InternalServerError` |
267-
| N/A | `APIConnectionError` |
273+
| Status Code | Error Type |
274+
| :---------: | ----------------------------------------------------------------- |
275+
| 400 | [`BadRequestError`][src.openai.BadRequestError] |
276+
| 401 | [`AuthenticationError`][src.openai.AuthenticationError] |
277+
| 403 | [`PermissionDeniedError`][src.openai.PermissionDeniedError] |
278+
| 404 | [`NotFoundError`][src.openai.NotFoundError] |
279+
| 409 | [`ConflictError`][src.openai.ConflictError] |
280+
| 422 | [`UnprocessableEntityError`][src.openai.UnprocessableEntityError] |
281+
| 429 | [`RateLimitError`][src.openai.RateLimitError] |
282+
| >=500 | [`InternalServerError`][src.openai.InternalServerError] |
283+
| N/A | [`APIConnectionError`][src.openai.APIConnectionError] |
268284

269-
### Retries
285+
## Retries
270286

271287
Certain errors are automatically retried 2 times by default, with a short exponential backoff.
272-
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,
273-
429 Rate Limit, and >=500 Internal errors are all retried by default.
288+
289+
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, and >=500 Internal errors are all retried by default.
274290

275291
You can use the `max_retries` option to configure or disable retry settings:
276292

@@ -295,7 +311,7 @@ client.with_options(max_retries=5).chat.completions.create(
295311
)
296312
```
297313

298-
### Timeouts
314+
## Timeouts
299315

300316
By default requests time out after 10 minutes. You can configure this with a `timeout` option,
301317
which accepts a float or an [`httpx.Timeout`](https://www.python-httpx.org/advanced/#fine-tuning-the-configuration) object:
@@ -330,10 +346,14 @@ On timeout, an `APITimeoutError` is thrown.
330346

331347
Note that requests that time out are [retried twice by default](#retries).
332348

349+
<!-- ---8<--- [end:handle-errors] -->
350+
333351
## Advanced
334352

335353
### Logging
336354

355+
<!-- ---8<--- [start:debugging] -->
356+
337357
We use the standard library [`logging`](https://docs.python.org/3/library/logging.html) module.
338358

339359
You can enable logging by setting the environment variable `OPENAI_LOG` to `debug`.
@@ -354,9 +374,9 @@ if response.my_field is None:
354374
print('Got json like {"my_field": null}.')
355375
```
356376

357-
### Accessing raw response data (e.g. headers)
377+
### Accessing raw response data (headers)
358378

359-
The "raw" Response object can be accessed by prefixing `.with_raw_response.` to any HTTP method call, e.g.,
379+
The "raw" Response object can be accessed by prefixing `.with_raw_response.` to any HTTP method call, for example:
360380

361381
```py
362382
from openai import OpenAI
@@ -410,8 +430,12 @@ with client.chat.completions.with_streaming_response.create(
410430

411431
The context manager is required so that the response will reliably be closed.
412432

433+
<!-- ---8<--- [end:debugging] -->
434+
413435
### Configuring the HTTP client
414436

437+
<!-- ---8<--- [start:advanced] -->
438+
415439
You can directly override the [httpx client](https://www.python-httpx.org/api/#client) to customize it for your use case, including:
416440

417441
- Support for proxies
@@ -489,3 +513,5 @@ This package generally follows [SemVer](https://semver.org/spec/v2.0.0.html) con
489513
We take backwards-compatibility seriously and work hard to ensure you can rely on a smooth upgrade experience.
490514

491515
We are keen for your feedback; please open an [issue](https://www.github.com/openai/openai-python/issues) with questions, bugs, or suggestions.
516+
517+
<!-- ---8<--- [end:advanced] -->

docs/advanced.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Advanced configuration
2+
3+
--8<-- "./README.md:advanced"

docs/connect.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Connect
2+
3+
--8<-- "./README.md:connect"

docs/debugging.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Logging and debugging
2+
3+
--8<-- "./README.md:debugging"

docs/error-handling.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Error handling
2+
3+
--8<-- "./README.md:handle-errors"

docs/get_started.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Get started
1+
# Install
22

3-
!!! quote
3+
??? Info
44

5-
The following is a modified version of the [README.md](https://github.com/openai/openai-python/blob/main/README.md) file in the upstream repo. *—[Marsh](https://github.com/mmacy)*
5+
The pages in this _Get started_ section are adapted from the sections in the [README.md](https://github.com/openai/openai-python/blob/main/README.md) in the upstream repository.
66

7-
--8<-- "./README.md:2"
7+
--8<-- "./README.md:get-started"

docs/index.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Welcome to Marsh's totally unofficial and totally unsupported documentation for
44

55
<div class="grid cards" markdown>
66
- :material-clock-fast: [Get started with the library](./get_started.md)
7-
- :fontawesome-brands-python: [OpenAI Python library reference](openai.md)
7+
- :fontawesome-brands-python: [OpenAI Python library reference](reference/index.md)
88
</div>
99

1010
## About these docs
@@ -29,6 +29,4 @@ That said, I use these docs myself and thus intend to keep them (mostly) current
2929

3030
1. That means you might encounter inaccuracies or you might not find what you think should be here. In either case, you should refer to [openai/openai-python](https://github.com/openai/openai-python) as the source of truth.
3131

32-
!!! quote
33-
34-
If these docs help you, yay! If they don't, don't use 'em. Enjoy! *—[Marsh](https://github.com/mmacy)*
32+
:material-hand-wave: *Enjoy!*[Marsh](https://github.com/mmacy)

docs/openai.md

Lines changed: 0 additions & 69 deletions
This file was deleted.

docs/pagination.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)