Skip to content

Commit dd2e5bd

Browse files
authored
Add xai-sdk-version to the request metadata (#41)
# Add xai-sdk-version to the request metadata ## Checklist - [x] I have read both the [CONTRIBUTING.md](CONTRIBUTING.md) and [Contributor License Agreement](CLA.md) documents. - [x] I have created an issue or feature request and received approval from xAI maintainers. (minor changes like fixing typos can skip this step) - [x] I have tested my changes locally and they pass all CI checks. - [x] I have added necessary documentation or updated existing documentation. ## Description Add `xai-sdk-version` as a new metadata into the request header. Tested locally, the sdk-version can be successfully read from the server side. ## Related Issue If applicable, link to the related feature request or bug report issue (e.g., #123). If none, state "N/A". ## Type of Change - [ ] Bug fix - [x] New feature - [ ] Documentation update - [ ] Other (please specify) ## Additional Notes Add any other information or context that might be helpful for reviewers.
1 parent 0e44cea commit dd2e5bd

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/xai_sdk/aio/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def _init(
2323
management_api_key: Optional[str],
2424
api_host: str,
2525
management_api_host: str,
26-
metadata: Optional[tuple[tuple[str, str]]],
26+
metadata: Optional[tuple[tuple[str, str], ...]],
2727
channel_options: Sequence[tuple[str, Any]],
2828
timeout: float,
2929
) -> None:
@@ -55,7 +55,7 @@ def _make_grpc_channel(
5555
self,
5656
api_key: str,
5757
api_host: str,
58-
metadata: Optional[tuple[tuple[str, str]]],
58+
metadata: Optional[tuple[tuple[str, str], ...]],
5959
channel_options: Sequence[tuple[str, Any]],
6060
timeout: float,
6161
) -> grpc.aio.Channel:

src/xai_sdk/client.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import grpc
88

9+
from .__about__ import __version__
10+
911
# Retries if the service returns an UNAVAILABLE error.
1012
_DEFAULT_SERVICE_CONFIG_JSON = json.dumps(
1113
{
@@ -57,7 +59,7 @@ def __init__(
5759
*,
5860
api_host: str = "api.x.ai",
5961
management_api_host: str = "management-api.x.ai",
60-
metadata: Optional[tuple[tuple[str, str]]] = None,
62+
metadata: Optional[tuple[tuple[str, str], ...]] = None,
6163
channel_options: Optional[list[tuple[str, Any]]] = None,
6264
timeout: Optional[float] = None,
6365
) -> None:
@@ -86,6 +88,9 @@ def __init__(
8688
default_options = [option for option in _DEFAULT_CHANNEL_OPTIONS if option[0] not in user_defined_options]
8789
timeout = timeout or _DEFAULT_RPC_TIMEOUT_SECONDS
8890

91+
# Add the xAI SDK version to the metadata
92+
metadata = (metadata or ()) + (("xai-sdk-version", __version__),)
93+
8994
self._init(
9095
api_key,
9196
management_api_key,
@@ -103,15 +108,15 @@ def _init(
103108
management_api_key: Optional[str],
104109
api_host: str,
105110
management_api_host: str,
106-
metadata: Optional[tuple[tuple[str, str]]],
111+
metadata: Optional[tuple[tuple[str, str], ...]],
107112
channel_options: Sequence[tuple[str, Any]],
108113
timeout: float,
109114
) -> None:
110115
"""Initializes the client instance."""
111116

112117

113118
def create_channel_credentials(
114-
api_key: str, api_host: str, metadata: Optional[tuple[tuple[str, str]]]
119+
api_key: str, api_host: str, metadata: Optional[tuple[tuple[str, str], ...]]
115120
) -> grpc.ChannelCredentials:
116121
"""Creates the credentials for the gRPC channel.
117122
@@ -138,7 +143,7 @@ def create_channel_credentials(
138143
class _APIAuthPlugin(grpc.AuthMetadataPlugin):
139144
"""A specification for API-key based authentication."""
140145

141-
def __init__(self, api_key: str, metadata: Optional[tuple[tuple[str, str]]]) -> None:
146+
def __init__(self, api_key: str, metadata: Optional[tuple[tuple[str, str], ...]]) -> None:
142147
"""Initializes a new instance of the `_APIAuthPlugin` class.
143148
144149
Args:

src/xai_sdk/sync/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def _init(
2323
management_api_key: Optional[str],
2424
api_host: str,
2525
management_api_host: str,
26-
metadata: Optional[tuple[tuple[str, str]]],
26+
metadata: Optional[tuple[tuple[str, str], ...]],
2727
channel_options: Sequence[tuple[str, Any]],
2828
timeout: float,
2929
) -> None:
@@ -54,7 +54,7 @@ def _make_grpc_channel(
5454
self,
5555
api_key: str,
5656
api_host: str,
57-
metadata: Optional[tuple[tuple[str, str]]],
57+
metadata: Optional[tuple[tuple[str, str], ...]],
5858
channel_options: Sequence[tuple[str, Any]],
5959
timeout: float,
6060
) -> grpc.Channel:

0 commit comments

Comments
 (0)