diff --git a/.github/workflows/documentation_cookbook_tests.yml b/.github/workflows/documentation_cookbook_tests.yml index f0253c3918..20890e7774 100644 --- a/.github/workflows/documentation_cookbook_tests.yml +++ b/.github/workflows/documentation_cookbook_tests.yml @@ -43,6 +43,8 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install -U ipython nbconvert + # - name: Install opik # Uncomment if you want to run cookbooks with local version of opik + # run: pip install sdks/python - name: Prepare env variables run: | directory=$(dirname -- "${NOTEBOOK_TO_TEST}") diff --git a/sdks/python/src/opik/api_objects/opik_client.py b/sdks/python/src/opik/api_objects/opik_client.py index 8e95ec509c..fd140a98fc 100644 --- a/sdks/python/src/opik/api_objects/opik_client.py +++ b/sdks/python/src/opik/api_objects/opik_client.py @@ -35,6 +35,7 @@ ) LOGGER = logging.getLogger(__name__) +OPIK_API_REQUESTS_TIMEOUT_SECONDS = 5.0 class Opik: @@ -90,6 +91,7 @@ def _initialize_streamer( base_url=base_url, httpx_client=httpx_client_, ) + self._rest_client._client_wrapper._timeout = OPIK_API_REQUESTS_TIMEOUT_SECONDS # See https://github.com/fern-api/fern/issues/5321 rest_client_configurator.configure(self._rest_client) self._streamer = streamer_constructors.construct_online_streamer( n_consumers=workers, @@ -118,6 +120,14 @@ def _display_created_dataset_url(self, workspace: str, dataset_name: str) -> Non LOGGER.info(f'Created a "{dataset_name}" dataset at {dataset_url}.') + def auth_check(self) -> None: + """ + Checks if current API key user has an access to the configured workspace and its content. + """ + self._rest_client.check.access( + request={} + ) # empty body for future backward compatibility + def trace( self, id: Optional[str] = None, diff --git a/sdks/python/src/opik/rest_client_configurator/retry_decorators.py b/sdks/python/src/opik/rest_client_configurator/retry_decorators.py index 20d2f2075d..1afbf81b51 100644 --- a/sdks/python/src/opik/rest_client_configurator/retry_decorators.py +++ b/sdks/python/src/opik/rest_client_configurator/retry_decorators.py @@ -8,7 +8,7 @@ ( httpx.RemoteProtocolError, # handle retries for expired connections httpx.ConnectError, - httpx.ConnectTimeout, + httpx.TimeoutException, ) ), ) diff --git a/sdks/python/tests/e2e/test_backend_check.py b/sdks/python/tests/e2e/test_backend_check.py new file mode 100644 index 0000000000..f1cb97e813 --- /dev/null +++ b/sdks/python/tests/e2e/test_backend_check.py @@ -0,0 +1,17 @@ +import opik +import pytest +from opik.rest_api.core import api_error + + +def test_auth_check__happyflow(opik_client: opik.Opik): + # Assuming opik client is correctly configured for tests, no + # exceptions must be raised. + assert opik_client.auth_check() is None + + +def test_auth_check__not_existing_workspace__api_error_raised(): + opik_client = opik.Opik( + workspace="workspace-that-does-not-exist-in-any-installation" + ) + with pytest.raises(api_error.ApiError): + opik_client.auth_check()