Skip to content

Commit d3d7e1b

Browse files
committed
fix(client): show a helpful error message if the v0 API is used (#743)
1 parent a228a53 commit d3d7e1b

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

Diff for: src/openai/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
from .version import VERSION as VERSION
7575
from .lib.azure import AzureOpenAI as AzureOpenAI
7676
from .lib.azure import AsyncAzureOpenAI as AsyncAzureOpenAI
77+
from .lib._old_api import *
7778

7879
_setup_logging()
7980

Diff for: src/openai/lib/_old_api.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
from typing_extensions import override
5+
6+
from .._utils import LazyProxy
7+
from .._exceptions import OpenAIError
8+
9+
INSTRUCTIONS = """
10+
11+
You tried to access openai.{symbol}, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.
12+
13+
You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface.
14+
15+
Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28`
16+
17+
A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742
18+
"""
19+
20+
21+
class APIRemovedInV1(OpenAIError):
22+
def __init__(self, *, symbol: str) -> None:
23+
super().__init__(INSTRUCTIONS.format(symbol=symbol))
24+
25+
26+
class APIRemovedInV1Proxy(LazyProxy[None]):
27+
def __init__(self, *, symbol: str) -> None:
28+
super().__init__()
29+
self._symbol = symbol
30+
31+
@override
32+
def __load__(self) -> None:
33+
raise APIRemovedInV1(symbol=self._symbol)
34+
35+
36+
SYMBOLS = [
37+
"Edit",
38+
"File",
39+
"Audio",
40+
"Image",
41+
"Model",
42+
"Engine",
43+
"Customer",
44+
"FineTune",
45+
"Embedding",
46+
"Completion",
47+
"Deployment",
48+
"Moderation",
49+
"ErrorObject",
50+
"FineTuningJob",
51+
"ChatCompletion",
52+
]
53+
54+
# we explicitly tell type checkers that nothing is exported
55+
# from this file so that when we re-export the old symbols
56+
# in `openai/__init__.py` they aren't added to the auto-complete
57+
# suggestions given by editors
58+
if TYPE_CHECKING:
59+
__all__: list[str] = []
60+
else:
61+
__all__ = SYMBOLS
62+
63+
64+
__locals = locals()
65+
for symbol in SYMBOLS:
66+
__locals[symbol] = APIRemovedInV1Proxy(symbol=symbol)

0 commit comments

Comments
 (0)