Skip to content

Commit

Permalink
fix: do not sent user header if version is unavailable
Browse files Browse the repository at this point in the history
When building the package with pip (e.g. pip install git+url), pip can not infer
the package versions. Using the default version of 0.0.0 in the user headers
would cause the api-scheduler to omit some fields, so no user header is send
for these edge cases
  • Loading branch information
moldhouse committed Nov 12, 2024
1 parent 53e41c4 commit 651436f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
9 changes: 3 additions & 6 deletions aleph_alpha_client/aleph_alpha_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from urllib3.util.retry import Retry
from tqdm.asyncio import tqdm

import aleph_alpha_client
from aleph_alpha_client.explanation import (
ExplanationRequest,
ExplanationResponse,
Expand Down Expand Up @@ -54,7 +53,7 @@
SemanticEmbeddingRequest,
SemanticEmbeddingResponse,
)
from aleph_alpha_client.version import MIN_API_VERSION
from aleph_alpha_client.version import MIN_API_VERSION, user_agent_headers

POOLING_OPTIONS = ["mean", "max", "last_token", "abs_max"]
RETRY_STATUS_CODES = frozenset({408, 429, 500, 502, 503, 504})
Expand Down Expand Up @@ -200,8 +199,7 @@ def __init__(
self.session.headers = CaseInsensitiveDict(
{
"Authorization": "Bearer " + self.token,
"User-Agent": "Aleph-Alpha-Python-Client-"
+ aleph_alpha_client.__version__,
**user_agent_headers(),
}
)
self.session.mount("https://", adapter)
Expand Down Expand Up @@ -720,8 +718,7 @@ def __init__(
timeout=aiohttp.ClientTimeout(self.request_timeout_seconds),
headers={
"Authorization": "Bearer " + self.token,
"User-Agent": "Aleph-Alpha-Python-Client-"
+ aleph_alpha_client.__version__,
**user_agent_headers(),
},
connector=connector,
)
Expand Down
21 changes: 19 additions & 2 deletions aleph_alpha_client/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import importlib.metadata

from typing import Dict
import re
from pathlib import Path
import logging
Expand All @@ -21,7 +21,9 @@ def pyproject_version() -> str:
To not break imports in cases where both, the pyproject.toml file and the package
metadata are not available, no error is raised and a default version of 0.0.0
will be returned.
will be returned. One such case is building the package with `pip install git+url`,
where pip is not able to read the [tool.poetry.version] field and also does not
keep the pyproject.toml file when the package is installed.
"""
NO_VERSION = "0.0.0"
pyproject_path = Path(__file__).resolve().parent.parent / "pyproject.toml"
Expand All @@ -46,3 +48,18 @@ def pyproject_version() -> str:

if __version__ == "0.0.0":
__version__ = pyproject_version()


def user_agent_headers() -> Dict[str, str]:
"""User agent that should be send for specific versions of the SDK.
For some installations, the package version is not available (== 0.0.0).
Setting the user agent header to "Aleph-Alpha-Python-Client-0.0.0" causes the
API to return a response with some fields omitted (due to a bug with older
clients which can not handle the new fields). These omitted fields in turn cause
new clients to fail on deserialization. To prevent these errors, we omit the
user agent header in cases where the version is not available (== 0.0.0).
"""
if __version__ != "0.0.0":
return {"User-Agent": "Aleph-Alpha-Python-Client-" + __version__}
return {}

0 comments on commit 651436f

Please sign in to comment.