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

[FEATURE] Add retries to the internal httpx.Client used by the SDK #5386

Merged
merged 4 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion argilla/src/argilla/_api/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ class APIClient:
def __init__(
self,
api_url: Optional[str] = DEFAULT_HTTP_CONFIG.api_url,
api_key: str = DEFAULT_HTTP_CONFIG.api_key,
api_key: Optional[str] = DEFAULT_HTTP_CONFIG.api_key,
timeout: int = DEFAULT_HTTP_CONFIG.timeout,
retries: int = DEFAULT_HTTP_CONFIG.retries,
**http_client_args,
):
if not api_url:
Expand All @@ -120,6 +121,7 @@ def __init__(

http_client_args = http_client_args or {}
http_client_args["timeout"] = timeout
http_client_args["retries"] = retries

self.http_client = create_http_client(
api_url=self.api_url, # type: ignore
Expand Down
18 changes: 10 additions & 8 deletions argilla/src/argilla/_api/_http/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ class HTTPClientConfig:

api_url: str
api_key: str
timeout: int = None

def __post_init__(self):
self.api_url = self.api_url
self.api_key = self.api_key
self.timeout = self.timeout or 60
timeout: int = 60

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gabrielmbmb I think we can add this to the constants.py file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gabrielmbmb The refactor of timeout and retries looks good to me. It's more consistent and avoids background passing with **client_args.

@davidberenstein1957 A few PRs have used constants in context lately. I think we should tackle this in a dedicated PR because using a single constant file is not universally good.

retries: int = 5


def create_http_client(api_url: str, api_key: str, **client_args) -> httpx.Client:
Expand All @@ -37,5 +33,11 @@ def create_http_client(api_url: str, api_key: str, **client_args) -> httpx.Clien

headers = client_args.pop("headers", {})
headers["X-Argilla-Api-Key"] = api_key

return httpx.Client(base_url=api_url, headers=headers, **client_args)
retries = client_args.pop("retries", 0)

return httpx.Client(
base_url=api_url,
headers=headers,
transport=httpx.HTTPTransport(retries=retries),
frascuchon marked this conversation as resolved.
Show resolved Hide resolved
**client_args,
)
23 changes: 16 additions & 7 deletions argilla/src/argilla/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ class Argilla(_api.APIClient):
datasets: A collection of datasets.
users: A collection of users.
me: The current user.

"""

workspaces: "Workspaces"
datasets: "Datasets"
users: "Users"
me: "User"

# Default instance of Argilla
_default_client: Optional["Argilla"] = None

Expand All @@ -57,9 +51,24 @@ def __init__(
api_url: Optional[str] = DEFAULT_HTTP_CONFIG.api_url,
api_key: Optional[str] = DEFAULT_HTTP_CONFIG.api_key,
timeout: int = DEFAULT_HTTP_CONFIG.timeout,
retries: int = DEFAULT_HTTP_CONFIG.retries,
**http_client_args,
) -> None:
super().__init__(api_url=api_url, api_key=api_key, timeout=timeout, **http_client_args)
"""Inits the `Argilla` client.

Args:
api_url: the URL of the Argilla API. If not provided, then the value will try
to be set from `ARGILLA_API_URL` environment variable. Defaults to
`"http://localhost:6900"`.
api_key: the key to be used to authenticate in the Argilla API. If not provided,
then the value will try to be set from `ARGILLA_API_KEY` environment variable.
Defaults to `None`.
timeout: the maximum time in seconds to wait for a request to the Argilla API
to be completed before raising an exception. Defaults to `60`.
retries: the number of times to retry a failed HTTP request to the Argilla API
before raising an exception. Defaults to `5`.
burtenshaw marked this conversation as resolved.
Show resolved Hide resolved
"""
super().__init__(api_url=api_url, api_key=api_key, timeout=timeout, retries=retries, **http_client_args)

self._set_default(self)

Expand Down