Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
0d801ee
Add workload identity federation credentials
mingminglang-baseten Nov 16, 2026
86025e8
Send credentials tokens once and pass options to Credentials
mingminglang-baseten Nov 17, 2026
965c65c
Resend sandbox requests only on TOKEN_REVOKED, as before
mingminglang-baseten Nov 17, 2026
c816618
Share failed-mint retries, fall back to the previous token, and tight…
mingminglang-baseten Nov 17, 2026
23c0d44
Replace an access token Baseten refused, and move the sandbox resend …
mingminglang-baseten Nov 18, 2026
0be20eb
Merge remote-tracking branch 'origin/main' into mingming/wif-credentials
mingminglang-baseten Nov 19, 2026
fcf1363
Regenerate the API clients from the production spec
mingminglang-baseten Nov 19, 2026
0713562
Regenerate the API clients from the production spec
mingminglang-baseten Nov 19, 2026
9a1d96e
Revert "Regenerate the API clients from the production spec"
mingminglang-baseten Nov 19, 2026
112c7c2
Merge the regeneration branch into mingming/wif-credentials
mingminglang-baseten Nov 19, 2026
7fbcf7b
Exchange identity tokens through the generated management client
mingminglang-baseten Nov 19, 2026
c7776fa
Merge origin/main into mingming/wif-credentials
mingminglang-baseten Nov 19, 2026
ddf3592
Bound the whole sync exchange and keep it free of client auth
mingminglang-baseten Nov 20, 2026
f1056db
Match Go on empty api_key and identity token error context
mingminglang-baseten Nov 20, 2026
ee8b5a6
Address review: keyword-only token dataclasses, record a refused toke…
mingminglang-baseten Nov 20, 2026
62be2e1
Drop the exchange timeout option and the exchange thread
mingminglang-baseten Nov 20, 2026
2ff83bb
Pass the identity token provider a context object
mingminglang-baseten Nov 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions baseten/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
for the inference API. :class:`SandboxClient` and :class:`AsyncSandboxClient`
reach one sandbox's execution API directly; the higher-level surface lives in
:mod:`baseten.sandbox`.

To authenticate without an API key, pass :class:`WorkloadIdentityCredentials`
or :class:`AsyncWorkloadIdentityCredentials` as a client's ``credentials``.
"""

from baseten.client._inference import (
Expand All @@ -22,15 +25,37 @@
SandboxClient,
SandboxClientOptions,
)
from baseten.client._token_auth import (
AccessToken,
AccessTokenOptions,
AsyncCredentials,
Credentials,
)
from baseten.client._workload_identity import (
AsyncIdentityTokenProvider,
AsyncWorkloadIdentityCredentials,
IdentityTokenProvider,
IdentityTokenProviderContext,
WorkloadIdentityCredentials,
)

__all__ = [
"AccessToken",
"AccessTokenOptions",
"AsyncCredentials",
"AsyncIdentityTokenProvider",
"AsyncInferenceClient",
"AsyncManagementClient",
"AsyncSandboxClient",
"AsyncWorkloadIdentityCredentials",
"Credentials",
"IdentityTokenProvider",
"IdentityTokenProviderContext",
"InferenceClient",
"InferenceClientOptions",
"ManagementClient",
"ManagementClientOptions",
"SandboxClient",
"SandboxClientOptions",
"WorkloadIdentityCredentials",
]
78 changes: 68 additions & 10 deletions baseten/client/_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
import httpx

import baseten.client.inferenceapi
from baseten.client._token_auth import (
AsyncCredentials,
AsyncCredentialsAuth,
Credentials,
CredentialsAuth,
check_client_auth,
credentials_origin,
)
from baseten.client._user_agent import with_user_agent


Expand All @@ -18,8 +26,8 @@ class InferenceClientOptions:
was constructed with.
"""

api_key: str
"""API key for authentication."""
api_key: str | None
"""API key for authentication, or ``None`` when *credentials* is set."""

headers: Mapping[str, str] | None = None
"""Additional headers to send on every request."""
Expand All @@ -37,6 +45,9 @@ class InferenceClientOptions:
"""Explicit base URL override. When set, *model_id*, *chain_id*, and
*environment* are ignored."""

credentials: Credentials | AsyncCredentials | None = None
"""Supplies short-lived access tokens, used in place of *api_key*."""

@property
def base_url(self) -> str:
"""The resolved base URL for the inference API."""
Expand Down Expand Up @@ -88,7 +99,8 @@ def default_base_url(
def __init__(
self,
*,
api_key: str,
api_key: str | None = None,
credentials: Credentials | None = None,
headers: Mapping[str, str] | None = None,
model_id: str | None = None,
chain_id: str | None = None,
Expand All @@ -100,7 +112,13 @@ def __init__(
"""Create a new synchronous inference client.

Args:
api_key: API key for authentication.
api_key: API key for authentication. Exactly one of *api_key* and
*credentials* is required.
credentials: Supplies short-lived access tokens in place of an
API key, such as :class:`WorkloadIdentityCredentials`. A token
is fetched for every request, so a long-lived client picks up
refreshed tokens. It is sent only to this client's API host,
and each request is sent once.
headers: Additional headers to send on every request.
model_id: Model ID. Mutually exclusive with *chain_id*.
chain_id: Chain ID. Mutually exclusive with *model_id*.
Expand All @@ -110,29 +128,46 @@ def __init__(
http_client_override: Pre-configured httpx client. When provided,
the caller is responsible for setting base URL and all
headers.
*credentials* still apply, but only to requests made
through :attr:`api`, which sends a token to this client's
base URL (or, if it has none, the resolved base URL).
Requests sent on the HTTP client directly carry none.
close_http_client_on_close: Whether :meth:`close` should close
the underlying HTTP client. Defaults to ``True`` when the
client is created internally, ``False`` when
*http_client_override* is provided.
"""
check_client_auth(api_key=api_key, credentials=credentials, headers=headers)
self._options = InferenceClientOptions(
api_key=api_key,
headers=headers,
model_id=model_id,
chain_id=chain_id,
environment=environment,
base_url_override=base_url_override,
credentials=credentials,
)
auth = (
None
if credentials is None
else CredentialsAuth(
credentials,
origin=credentials_origin(
http_client_override, lambda: self._options.base_url
),
)
)
if http_client_override is None:
request_headers: dict[str, str] = {**(headers or {})}
# Empty api_key is an advanced opt-out from sending Authorization.
if api_key != "":
if api_key:
request_headers["Authorization"] = f"Bearer {api_key}"
# No read timeout, matching the JS and Go clients.
self._http_client = httpx.Client(
base_url=self._options.base_url,
headers=with_user_agent(request_headers),
timeout=httpx.Timeout(5.0, read=None),
auth=auth,
)
self.close_http_client_on_close = (
True
Expand All @@ -146,7 +181,7 @@ def __init__(
if close_http_client_on_close is None
else close_http_client_on_close
)
self._api = baseten.client.inferenceapi.ApiClient(self._http_client)
self._api = baseten.client.inferenceapi.ApiClient(self._http_client, auth=auth)

@property
def options(self) -> InferenceClientOptions:
Expand Down Expand Up @@ -205,7 +240,8 @@ def default_base_url(
def __init__(
self,
*,
api_key: str,
api_key: str | None = None,
credentials: AsyncCredentials | None = None,
headers: Mapping[str, str] | None = None,
model_id: str | None = None,
chain_id: str | None = None,
Expand All @@ -217,7 +253,10 @@ def __init__(
"""Create a new asynchronous inference client.

Args:
api_key: API key for authentication.
api_key: API key for authentication. Exactly one of *api_key* and
*credentials* is required.
credentials: Supplies short-lived access tokens, as for
:class:`InferenceClient`.
headers: Additional headers to send on every request.
model_id: Model ID. Mutually exclusive with *chain_id*.
chain_id: Chain ID. Mutually exclusive with *model_id*.
Expand All @@ -227,29 +266,46 @@ def __init__(
http_client_override: Pre-configured httpx async client. When
provided, the caller is responsible for setting base URL
and all headers.
*credentials* still apply, but only to requests made
through :attr:`api`, which sends a token to this client's
base URL (or, if it has none, the resolved base URL).
Requests sent on the HTTP client directly carry none.
close_http_client_on_close: Whether :meth:`close` should close
the underlying HTTP client. Defaults to ``True`` when the
client is created internally, ``False`` when
*http_client_override* is provided.
"""
check_client_auth(api_key=api_key, credentials=credentials, headers=headers)
self._options = InferenceClientOptions(
api_key=api_key,
headers=headers,
model_id=model_id,
chain_id=chain_id,
environment=environment,
base_url_override=base_url_override,
credentials=credentials,
)
auth = (
None
if credentials is None
else AsyncCredentialsAuth(
credentials,
origin=credentials_origin(
http_client_override, lambda: self._options.base_url
),
)
)
if http_client_override is None:
request_headers: dict[str, str] = {**(headers or {})}
# Empty api_key is an advanced opt-out from sending Authorization.
if api_key != "":
if api_key:
request_headers["Authorization"] = f"Bearer {api_key}"
# No read timeout, matching the JS and Go clients.
self._http_client = httpx.AsyncClient(
base_url=self._options.base_url,
headers=with_user_agent(request_headers),
timeout=httpx.Timeout(5.0, read=None),
auth=auth,
)
self.close_http_client_on_close = (
True
Expand All @@ -263,7 +319,9 @@ def __init__(
if close_http_client_on_close is None
else close_http_client_on_close
)
self._api = baseten.client.inferenceapi.AsyncApiClient(self._http_client)
self._api = baseten.client.inferenceapi.AsyncApiClient(
self._http_client, auth=auth
)

@property
def options(self) -> InferenceClientOptions:
Expand Down
Loading
Loading