Skip to content

Commit 13162be

Browse files
feat(api): fix bearer token which also regressed when guessing with AI
1 parent 5a9b95c commit 13162be

File tree

7 files changed

+153
-105
lines changed

7 files changed

+153
-105
lines changed

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 35
22
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/replicate%2Freplicate-client-efbc8cc2d74644b213e161d3e11e0589d1cef181fb318ea02c8eb6b00f245713.yml
33
openapi_spec_hash: 13da0c06c900b61cd98ab678e024987a
4-
config_hash: 8e80c2bf93f71823213f5773dd297921
4+
config_hash: 8ef6787524fd12bfeb27f8c6acef3dca

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ import os
2828
from replicate import Replicate
2929

3030
client = Replicate(
31-
api_key=os.environ.get("REPLICATE_API_TOKEN"), # This is the default and can be omitted
31+
bearer_token=os.environ.get("REPLICATE_API_TOKEN"), # This is the default and can be omitted
3232
)
3333

3434
account = client.account.get()
3535
print(account.type)
3636
```
3737

38-
While you can provide an `api_key` keyword argument,
38+
While you can provide a `bearer_token` keyword argument,
3939
we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
40-
to add `REPLICATE_API_TOKEN="My API Key"` to your `.env` file
41-
so that your API Key is not stored in source control.
40+
to add `REPLICATE_API_TOKEN="My Bearer Token"` to your `.env` file
41+
so that your Bearer Token is not stored in source control.
4242

4343
## Async usage
4444

@@ -50,7 +50,7 @@ import asyncio
5050
from replicate import AsyncReplicate
5151

5252
client = AsyncReplicate(
53-
api_key=os.environ.get("REPLICATE_API_TOKEN"), # This is the default and can be omitted
53+
bearer_token=os.environ.get("REPLICATE_API_TOKEN"), # This is the default and can be omitted
5454
)
5555

5656

src/replicate/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104

105105
from ._base_client import DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES
106106

107-
api_key: str | None = None
107+
bearer_token: str | None = None
108108

109109
base_url: str | _httpx.URL | None = None
110110

@@ -125,14 +125,14 @@ class _ModuleClient(Replicate):
125125

126126
@property # type: ignore
127127
@override
128-
def api_key(self) -> str | None:
129-
return api_key
128+
def bearer_token(self) -> str | None:
129+
return bearer_token
130130

131-
@api_key.setter # type: ignore
132-
def api_key(self, value: str | None) -> None: # type: ignore
133-
global api_key
131+
@bearer_token.setter # type: ignore
132+
def bearer_token(self, value: str | None) -> None: # type: ignore
133+
global bearer_token
134134

135-
api_key = value
135+
bearer_token = value
136136

137137
@property
138138
@override
@@ -210,7 +210,7 @@ def _load_client() -> Replicate: # type: ignore[reportUnusedFunction]
210210

211211
if _client is None:
212212
_client = _ModuleClient(
213-
api_key=api_key,
213+
bearer_token=bearer_token,
214214
base_url=base_url,
215215
timeout=timeout,
216216
max_retries=max_retries,

src/replicate/_client.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@
5656

5757
class Replicate(SyncAPIClient):
5858
# client options
59-
api_key: str
59+
bearer_token: str
6060

6161
def __init__(
6262
self,
6363
*,
64-
api_key: str | None = None,
64+
bearer_token: str | None = None,
6565
base_url: str | httpx.URL | None = None,
6666
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
6767
max_retries: int = DEFAULT_MAX_RETRIES,
@@ -83,15 +83,15 @@ def __init__(
8383
) -> None:
8484
"""Construct a new synchronous Replicate client instance.
8585
86-
This automatically infers the `api_key` argument from the `REPLICATE_API_TOKEN` environment variable if it is not provided.
86+
This automatically infers the `bearer_token` argument from the `REPLICATE_API_TOKEN` environment variable if it is not provided.
8787
"""
88-
if api_key is None:
89-
api_key = os.environ.get("REPLICATE_API_TOKEN")
90-
if api_key is None:
88+
if bearer_token is None:
89+
bearer_token = os.environ.get("REPLICATE_API_TOKEN")
90+
if bearer_token is None:
9191
raise ReplicateError(
92-
"The api_key client option must be set either by passing api_key to the client or by setting the REPLICATE_API_TOKEN environment variable"
92+
"The bearer_token client option must be set either by passing bearer_token to the client or by setting the REPLICATE_API_TOKEN environment variable"
9393
)
94-
self.api_key = api_key
94+
self.bearer_token = bearer_token
9595

9696
if base_url is None:
9797
base_url = os.environ.get("REPLICATE_BASE_URL")
@@ -179,8 +179,8 @@ def qs(self) -> Querystring:
179179
@property
180180
@override
181181
def auth_headers(self) -> dict[str, str]:
182-
api_key = self.api_key
183-
return {"Authorization": f"Bearer {api_key}"}
182+
bearer_token = self.bearer_token
183+
return {"Authorization": f"Bearer {bearer_token}"}
184184

185185
@property
186186
@override
@@ -194,7 +194,7 @@ def default_headers(self) -> dict[str, str | Omit]:
194194
def copy(
195195
self,
196196
*,
197-
api_key: str | None = None,
197+
bearer_token: str | None = None,
198198
base_url: str | httpx.URL | None = None,
199199
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
200200
http_client: httpx.Client | None = None,
@@ -228,7 +228,7 @@ def copy(
228228

229229
http_client = http_client or self._client
230230
return self.__class__(
231-
api_key=api_key or self.api_key,
231+
bearer_token=bearer_token or self.bearer_token,
232232
base_url=base_url or self.base_url,
233233
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
234234
http_client=http_client,
@@ -278,12 +278,12 @@ def _make_status_error(
278278

279279
class AsyncReplicate(AsyncAPIClient):
280280
# client options
281-
api_key: str
281+
bearer_token: str
282282

283283
def __init__(
284284
self,
285285
*,
286-
api_key: str | None = None,
286+
bearer_token: str | None = None,
287287
base_url: str | httpx.URL | None = None,
288288
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
289289
max_retries: int = DEFAULT_MAX_RETRIES,
@@ -305,15 +305,15 @@ def __init__(
305305
) -> None:
306306
"""Construct a new async AsyncReplicate client instance.
307307
308-
This automatically infers the `api_key` argument from the `REPLICATE_API_TOKEN` environment variable if it is not provided.
308+
This automatically infers the `bearer_token` argument from the `REPLICATE_API_TOKEN` environment variable if it is not provided.
309309
"""
310-
if api_key is None:
311-
api_key = os.environ.get("REPLICATE_API_TOKEN")
312-
if api_key is None:
310+
if bearer_token is None:
311+
bearer_token = os.environ.get("REPLICATE_API_TOKEN")
312+
if bearer_token is None:
313313
raise ReplicateError(
314-
"The api_key client option must be set either by passing api_key to the client or by setting the REPLICATE_API_TOKEN environment variable"
314+
"The bearer_token client option must be set either by passing bearer_token to the client or by setting the REPLICATE_API_TOKEN environment variable"
315315
)
316-
self.api_key = api_key
316+
self.bearer_token = bearer_token
317317

318318
if base_url is None:
319319
base_url = os.environ.get("REPLICATE_BASE_URL")
@@ -401,8 +401,8 @@ def qs(self) -> Querystring:
401401
@property
402402
@override
403403
def auth_headers(self) -> dict[str, str]:
404-
api_key = self.api_key
405-
return {"Authorization": f"Bearer {api_key}"}
404+
bearer_token = self.bearer_token
405+
return {"Authorization": f"Bearer {bearer_token}"}
406406

407407
@property
408408
@override
@@ -416,7 +416,7 @@ def default_headers(self) -> dict[str, str | Omit]:
416416
def copy(
417417
self,
418418
*,
419-
api_key: str | None = None,
419+
bearer_token: str | None = None,
420420
base_url: str | httpx.URL | None = None,
421421
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
422422
http_client: httpx.AsyncClient | None = None,
@@ -450,7 +450,7 @@ def copy(
450450

451451
http_client = http_client or self._client
452452
return self.__class__(
453-
api_key=api_key or self.api_key,
453+
bearer_token=bearer_token or self.bearer_token,
454454
base_url=base_url or self.base_url,
455455
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
456456
http_client=http_client,

tests/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def pytest_collection_modifyitems(items: list[pytest.Function]) -> None:
2828

2929
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
3030

31-
api_key = "My API Key"
31+
bearer_token = "My Bearer Token"
3232

3333

3434
@pytest.fixture(scope="session")
@@ -37,7 +37,7 @@ def client(request: FixtureRequest) -> Iterator[Replicate]:
3737
if not isinstance(strict, bool):
3838
raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
3939

40-
with Replicate(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client:
40+
with Replicate(base_url=base_url, bearer_token=bearer_token, _strict_response_validation=strict) as client:
4141
yield client
4242

4343

@@ -47,5 +47,7 @@ async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncReplicate]
4747
if not isinstance(strict, bool):
4848
raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
4949

50-
async with AsyncReplicate(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client:
50+
async with AsyncReplicate(
51+
base_url=base_url, bearer_token=bearer_token, _strict_response_validation=strict
52+
) as client:
5153
yield client

0 commit comments

Comments
 (0)