Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not send user header if version is unavailable #200

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
22 changes: 20 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,19 @@ 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 {}
else:
return {"User-Agent": "Aleph-Alpha-Python-Client-" + __version__}