Skip to content

Commit

Permalink
chore(client): validate that max_retries is not None (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Apr 1, 2024
1 parent 0470d1b commit aa5920a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/openai/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ def __init__(
self._strict_response_validation = _strict_response_validation
self._idempotency_header = None

if max_retries is None: # pyright: ignore[reportUnnecessaryComparison]
raise TypeError(
"max_retries cannot be None. If you want to disable retries, pass `0`; if you want unlimited retries, pass `math.inf` or a very high number; if you want the default behavior, pass `openai.DEFAULT_MAX_RETRIES`"
)

def _enforce_trailing_slash(self, url: URL) -> URL:
if url.raw_path.endswith(b"/"):
return url
Expand Down
10 changes: 10 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ class Model(BaseModel):

assert isinstance(exc.value.__cause__, ValidationError)

def test_client_max_retries_validation(self) -> None:
with pytest.raises(TypeError, match=r"max_retries cannot be None"):
OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True, max_retries=cast(Any, None))

@pytest.mark.respx(base_url=base_url)
def test_default_stream_cls(self, respx_mock: MockRouter) -> None:
class Model(BaseModel):
Expand Down Expand Up @@ -1368,6 +1372,12 @@ class Model(BaseModel):

assert isinstance(exc.value.__cause__, ValidationError)

async def test_client_max_retries_validation(self) -> None:
with pytest.raises(TypeError, match=r"max_retries cannot be None"):
AsyncOpenAI(
base_url=base_url, api_key=api_key, _strict_response_validation=True, max_retries=cast(Any, None)
)

@pytest.mark.respx(base_url=base_url)
@pytest.mark.asyncio
async def test_default_stream_cls(self, respx_mock: MockRouter) -> None:
Expand Down

0 comments on commit aa5920a

Please sign in to comment.