Skip to content

Commit

Permalink
feat(vertex): add credentials argument (#542)
Browse files Browse the repository at this point in the history
Co-authored-by: Anshuman Mitra <mitraan@deshaw.com>
  • Loading branch information
2 people authored and stainless-app[bot] committed Jun 26, 2024
1 parent 9babfb0 commit d0f5fb5
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/anthropic/lib/vertex/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def __init__(
proxies: ProxiesTypes | None = None,
# See httpx documentation for [limits](https://www.python-httpx.org/advanced/#pool-limit-configuration)
connection_pool_limits: httpx.Limits | None = None,
credentials: GoogleCredentials | None = None,
_strict_response_validation: bool = False,
) -> None:
if not is_given(region):
Expand Down Expand Up @@ -161,7 +162,7 @@ def __init__(

self.region = region
self.access_token = access_token
self._credentials: GoogleCredentials | None = None
self.credentials = credentials

self.messages = Messages(self)

Expand All @@ -179,18 +180,18 @@ def _ensure_access_token(self) -> str:
if self.access_token is not None:
return self.access_token

if not self._credentials:
self._credentials, project_id = load_auth(project_id=self.project_id)
if not self.credentials:
self.credentials, project_id = load_auth(project_id=self.project_id)
if not self.project_id:
self.project_id = project_id
else:
refresh_auth(self._credentials)
refresh_auth(self.credentials)

if not self._credentials.token:
if not self.credentials.token:
raise RuntimeError("Could not resolve API token from the environment")

assert isinstance(self._credentials.token, str)
return self._credentials.token
assert isinstance(self.credentials.token, str)
return self.credentials.token


class AsyncAnthropicVertex(BaseVertexClient[httpx.AsyncClient, AsyncStream[Any]], AsyncAPIClient):
Expand All @@ -215,6 +216,7 @@ def __init__(
proxies: ProxiesTypes | None = None,
# See httpx documentation for [limits](https://www.python-httpx.org/advanced/#pool-limit-configuration)
connection_pool_limits: httpx.Limits | None = None,
credentials: GoogleCredentials | None = None,
_strict_response_validation: bool = False,
) -> None:
if not is_given(region):
Expand Down Expand Up @@ -248,7 +250,7 @@ def __init__(

self.region = region
self.access_token = access_token
self._credentials: GoogleCredentials | None = None
self.credentials = credentials

self.messages = AsyncMessages(self)

Expand All @@ -266,15 +268,15 @@ async def _ensure_access_token(self) -> str:
if self.access_token is not None:
return self.access_token

if not self._credentials:
self._credentials, project_id = await asyncify(load_auth)(project_id=self.project_id)
if not self.credentials:
self.credentials, project_id = await asyncify(load_auth)(project_id=self.project_id)
if not self.project_id:
self.project_id = project_id
else:
await asyncify(refresh_auth)(self._credentials)
await asyncify(refresh_auth)(self.credentials)

if not self._credentials.token:
if not self.credentials.token:
raise RuntimeError("Could not resolve API token from the environment")

assert isinstance(self._credentials.token, str)
return self._credentials.token
assert isinstance(self.credentials.token, str)
return self.credentials.token

0 comments on commit d0f5fb5

Please sign in to comment.