Skip to content

Commit 1639186

Browse files
Fall back on env variable when api_key=None (#422)
* Fall back on env variable when api_key=None * 5.0.2 * Add test_
1 parent bed87ae commit 1639186

5 files changed

Lines changed: 18 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "cohere"
3-
version = "5.0.1"
3+
version = "5.0.2"
44
description = ""
55
readme = "README.md"
66
authors = []

src/cohere/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,17 @@ def fn(*args, **kwargs):
5959
class Client(BaseCohere):
6060
def __init__(
6161
self,
62-
api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = os.getenv("CO_API_KEY"),
62+
api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = None,
6363
*,
6464
base_url: typing.Optional[str] = os.getenv("CO_API_URL"),
6565
environment: ClientEnvironment = ClientEnvironment.PRODUCTION,
6666
client_name: typing.Optional[str] = None,
6767
timeout: typing.Optional[float] = 60,
6868
httpx_client: typing.Optional[httpx.Client] = None,
6969
):
70+
if api_key is None:
71+
api_key = os.getenv("CO_API_KEY")
72+
7073
BaseCohere.__init__(
7174
self,
7275
base_url=base_url,
@@ -130,14 +133,17 @@ def __init__(
130133
class AsyncClient(AsyncBaseCohere):
131134
def __init__(
132135
self,
133-
api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = os.getenv("CO_API_KEY"),
136+
api_key: typing.Optional[typing.Union[str, typing.Callable[[], str]]] = None,
134137
*,
135138
base_url: typing.Optional[str] = os.getenv("CO_API_URL"),
136139
environment: ClientEnvironment = ClientEnvironment.PRODUCTION,
137140
client_name: typing.Optional[str] = None,
138141
timeout: typing.Optional[float] = 60,
139142
httpx_client: typing.Optional[httpx.AsyncClient] = None,
140143
):
144+
if api_key is None:
145+
api_key = os.getenv("CO_API_KEY")
146+
141147
AsyncBaseCohere.__init__(
142148
self,
143149
base_url=base_url,

src/cohere/core/client_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def get_headers(self) -> typing.Dict[str, str]:
2323
headers: typing.Dict[str, str] = {
2424
"X-Fern-Language": "Python",
2525
"X-Fern-SDK-Name": "cohere",
26-
"X-Fern-SDK-Version": "5.0.1",
26+
"X-Fern-SDK-Version": "5.0.2",
2727
}
2828
if self._client_name is not None:
2929
headers["X-Client-Name"] = self._client_name

tests/test_async_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class TestClient(unittest.IsolatedAsyncioTestCase):
1515
def setUp(self) -> None:
1616
self.co = cohere.AsyncClient(timeout=10000)
1717

18+
async def test_token_falls_back_on_env_variable(self) -> None:
19+
cohere.AsyncClient(api_key=None)
20+
cohere.AsyncClient(None)
21+
1822
async def test_chat(self) -> None:
1923
chat = await self.co.chat(
2024
chat_history=[

tests/test_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
class TestClient(unittest.TestCase):
1515

16+
def test_token_falls_back_on_env_variable(self) -> None:
17+
cohere.Client(api_key=None)
18+
cohere.Client(None)
19+
1620
def test_chat(self) -> None:
1721
chat = co.chat(
1822
chat_history=[

0 commit comments

Comments
 (0)