diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 54198710..9fc99f6a 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.2.0-alpha.26" + ".": "0.2.0-alpha.31" } \ No newline at end of file diff --git a/.stats.yml b/.stats.yml index 6a8c1428..dd473053 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1 +1 @@ -configured_endpoints: 13 +configured_endpoints: 14 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b47733a9..1a053ce9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,9 +2,13 @@ ### With Rye -We use [Rye](https://rye.astral.sh/) to manage dependencies so we highly recommend [installing it](https://rye.astral.sh/guide/installation/) as it will automatically provision a Python environment with the expected Python version. +We use [Rye](https://rye.astral.sh/) to manage dependencies because it will automatically provision a Python environment with the expected Python version. To set it up, run: -After installing Rye, you'll just have to run this command: +```sh +$ ./scripts/bootstrap +``` + +Or [install Rye manually](https://rye.astral.sh/guide/installation/) and run: ```sh $ rye sync --all-features @@ -31,25 +35,25 @@ $ pip install -r requirements-dev.lock ## Modifying/Adding code -Most of the SDK is generated code, and any modified code will be overridden on the next generation. The -`src/openlayer/lib/` and `examples/` directories are exceptions and will never be overridden. +Most of the SDK is generated code. Modifications to code will be persisted between generations, but may +result in merge conflicts between manual patches and changes from the generator. The generator will never +modify the contents of the `src/openlayer/lib/` and `examples/` directories. ## Adding and running examples -All files in the `examples/` directory are not modified by the Stainless generator and can be freely edited or -added to. +All files in the `examples/` directory are not modified by the generator and can be freely edited or added to. -```bash +```py # add an example to examples/.py #!/usr/bin/env -S rye run python … ``` -``` -chmod +x examples/.py +```sh +$ chmod +x examples/.py # run the example against your api -./examples/.py +$ ./examples/.py ``` ## Using the repository from source @@ -58,8 +62,8 @@ If you’d like to use the repository from source, you can either install from g To install via git: -```bash -pip install git+ssh://git@github.com/openlayer-ai/openlayer-python.git +```sh +$ pip install git+ssh://git@github.com/openlayer-ai/openlayer-python.git ``` Alternatively, you can build from source and install the wheel file: @@ -68,29 +72,29 @@ Building this package will create two files in the `dist/` directory, a `.tar.gz To create a distributable version of the library, all you have to do is run this command: -```bash -rye build +```sh +$ rye build # or -python -m build +$ python -m build ``` Then to install: ```sh -pip install ./path-to-wheel-file.whl +$ pip install ./path-to-wheel-file.whl ``` ## Running tests Most tests require you to [set up a mock server](https://github.com/stoplightio/prism) against the OpenAPI spec to run the tests. -```bash +```sh # you will need npm installed -npx prism mock path/to/your/openapi.yml +$ npx prism mock path/to/your/openapi.yml ``` -```bash -rye run pytest +```sh +$ ./scripts/test ``` ## Linting and formatting @@ -100,14 +104,14 @@ This repository uses [ruff](https://github.com/astral-sh/ruff) and To lint: -```bash -rye run lint +```sh +$ ./scripts/lint ``` To format and fix all ruff issues automatically: -```bash -rye run format +```sh +$ ./scripts/format ``` ## Publishing and releases diff --git a/README.md b/README.md index 1d316a13..93efe936 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ client = Openlayer( api_key=os.environ.get("OPENLAYER_API_KEY"), ) -data_stream_response = client.inference_pipelines.data.stream( +response = client.inference_pipelines.data.stream( inference_pipeline_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", config={ "input_variable_names": ["user_query"], @@ -47,11 +47,11 @@ data_stream_response = client.inference_pipelines.data.stream( "output": "42", "tokens": 7, "cost": 0.02, - "timestamp": 1620000000, + "timestamp": 1610000000, } ], ) -print(data_stream_response.success) +print(response.success) ``` While you can provide an `api_key` keyword argument, @@ -75,7 +75,7 @@ client = AsyncOpenlayer( async def main() -> None: - data_stream_response = await client.inference_pipelines.data.stream( + response = await client.inference_pipelines.data.stream( inference_pipeline_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", config={ "input_variable_names": ["user_query"], @@ -90,11 +90,11 @@ async def main() -> None: "output": "42", "tokens": 7, "cost": 0.02, - "timestamp": 1620000000, + "timestamp": 1610000000, } ], ) - print(data_stream_response.success) + print(response.success) asyncio.run(main()) @@ -142,7 +142,7 @@ try: "output": "42", "tokens": 7, "cost": 0.02, - "timestamp": 1620000000, + "timestamp": 1610000000, } ], ) @@ -203,7 +203,7 @@ client.with_options(max_retries=5).inference_pipelines.data.stream( "output": "42", "tokens": 7, "cost": 0.02, - "timestamp": 1620000000, + "timestamp": 1610000000, } ], ) @@ -244,7 +244,7 @@ client.with_options(timeout=5.0).inference_pipelines.data.stream( "output": "42", "tokens": 7, "cost": 0.02, - "timestamp": 1620000000, + "timestamp": 1610000000, } ], ) @@ -300,7 +300,7 @@ response = client.inference_pipelines.data.with_raw_response.stream( "output": "42", "tokens": 7, "cost": 0.02, - "timestamp": 1620000000, + "timestamp": 1610000000, }], ) print(response.headers.get('X-My-Header')) @@ -335,7 +335,7 @@ with client.inference_pipelines.data.with_streaming_response.stream( "output": "42", "tokens": 7, "cost": 0.02, - "timestamp": 1620000000, + "timestamp": 1610000000, } ], ) as response: @@ -425,6 +425,21 @@ We take backwards-compatibility seriously and work hard to ensure you can rely o We are keen for your feedback; please open an [issue](https://www.github.com/openlayer-ai/openlayer-python/issues) with questions, bugs, or suggestions. +### Determining the installed version + +If you've upgraded to the latest version but aren't seeing any new features you were expecting then your python environment is likely still using an older version. + +You can determine the version that is being used at runtime with: + +```py +import openlayer +print(openlayer.__version__) +``` + ## Requirements Python 3.7 or higher. + +## Contributing + +See [the contributing documentation](./CONTRIBUTING.md). diff --git a/api.md b/api.md index 82e9d940..24e491a6 100644 --- a/api.md +++ b/api.md @@ -38,6 +38,16 @@ Methods: # Commits +Types: + +```python +from openlayer.types import CommitCreateResponse +``` + +Methods: + +- client.commits.create(project_id, \*\*params) -> CommitCreateResponse + ## TestResults Types: diff --git a/pyproject.toml b/pyproject.toml index 8b7d1f5e..6b481e75 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "openlayer" -version = "0.2.0-alpha.26" +version = "0.2.0-alpha.31" description = "The official Python library for the openlayer API" dynamic = ["readme"] license = "Apache-2.0" @@ -15,7 +15,6 @@ dependencies = [ "distro>=1.7.0, <2", "sniffio", "cached-property; python_version < '3.8'", - ] requires-python = ">= 3.7" classifiers = [ @@ -36,8 +35,6 @@ classifiers = [ "License :: OSI Approved :: Apache Software License" ] - - [project.urls] Homepage = "https://github.com/openlayer-ai/openlayer-python" Repository = "https://github.com/openlayer-ai/openlayer-python" @@ -59,7 +56,6 @@ dev-dependencies = [ "dirty-equals>=0.6.0", "importlib-metadata>=6.7.0", "rich>=13.7.1", - ] [tool.rye.scripts] @@ -67,11 +63,11 @@ format = { chain = [ "format:ruff", "format:docs", "fix:ruff", + # run formatting again to fix any inconsistencies when imports are stripped + "format:ruff", ]} -"format:black" = "black ." "format:docs" = "python scripts/utils/ruffen-docs.py README.md api.md" "format:ruff" = "ruff format" -"format:isort" = "isort ." "lint" = { chain = [ "check:ruff", @@ -129,10 +125,6 @@ path = "README.md" pattern = '\[(.+?)\]\(((?!https?://)\S+?)\)' replacement = '[\1](https://github.com/openlayer-ai/openlayer-python/tree/main/\g<2>)' -[tool.black] -line-length = 120 -target-version = ["py37"] - [tool.pytest.ini_options] testpaths = ["tests"] addopts = "--tb=short" diff --git a/requirements-dev.lock b/requirements-dev.lock index 4a4bf4e2..276215ee 100644 --- a/requirements-dev.lock +++ b/requirements-dev.lock @@ -16,8 +16,6 @@ anyio==4.4.0 # via openlayer argcomplete==3.1.2 # via nox -attrs==23.1.0 - # via pytest certifi==2023.7.22 # via httpcore # via httpx @@ -28,8 +26,9 @@ distlib==0.3.7 # via virtualenv distro==1.8.0 # via openlayer -exceptiongroup==1.1.3 +exceptiongroup==1.2.2 # via anyio + # via pytest filelock==3.12.4 # via virtualenv h11==0.14.0 @@ -49,7 +48,7 @@ markdown-it-py==3.0.0 # via rich mdurl==0.1.2 # via markdown-it-py -mypy==1.10.1 +mypy==1.11.2 mypy-extensions==1.0.0 # via mypy nodeenv==1.8.0 @@ -60,27 +59,25 @@ packaging==23.2 # via pytest platformdirs==3.11.0 # via virtualenv -pluggy==1.3.0 - # via pytest -py==1.11.0 +pluggy==1.5.0 # via pytest -pydantic==2.7.1 +pydantic==2.9.2 # via openlayer -pydantic-core==2.18.2 +pydantic-core==2.23.4 # via pydantic pygments==2.18.0 # via rich -pyright==1.1.374 -pytest==7.1.1 +pyright==1.1.380 +pytest==8.3.3 # via pytest-asyncio -pytest-asyncio==0.21.1 +pytest-asyncio==0.24.0 python-dateutil==2.8.2 # via time-machine pytz==2023.3.post1 # via dirty-equals respx==0.20.2 rich==13.7.1 -ruff==0.5.6 +ruff==0.6.9 setuptools==68.2.2 # via nodeenv six==1.16.0 @@ -90,10 +87,10 @@ sniffio==1.3.0 # via httpx # via openlayer time-machine==2.9.0 -tomli==2.0.1 +tomli==2.0.2 # via mypy # via pytest -typing-extensions==4.8.0 +typing-extensions==4.12.2 # via anyio # via mypy # via openlayer diff --git a/requirements.lock b/requirements.lock index 150a0182..4ef2ed1d 100644 --- a/requirements.lock +++ b/requirements.lock @@ -19,7 +19,7 @@ certifi==2023.7.22 # via httpx distro==1.8.0 # via openlayer -exceptiongroup==1.1.3 +exceptiongroup==1.2.2 # via anyio h11==0.14.0 # via httpcore @@ -30,15 +30,15 @@ httpx==0.25.2 idna==3.4 # via anyio # via httpx -pydantic==2.7.1 +pydantic==2.9.2 # via openlayer -pydantic-core==2.18.2 +pydantic-core==2.23.4 # via pydantic sniffio==1.3.0 # via anyio # via httpx # via openlayer -typing-extensions==4.8.0 +typing-extensions==4.12.2 # via anyio # via openlayer # via pydantic diff --git a/src/openlayer/_base_client.py b/src/openlayer/_base_client.py index c47242a6..f37cfc90 100644 --- a/src/openlayer/_base_client.py +++ b/src/openlayer/_base_client.py @@ -143,6 +143,12 @@ def __init__( self.url = url self.params = params + @override + def __repr__(self) -> str: + if self.url: + return f"{self.__class__.__name__}(url={self.url})" + return f"{self.__class__.__name__}(params={self.params})" + class BasePage(GenericModel, Generic[_T]): """ @@ -400,14 +406,7 @@ def _make_status_error( ) -> _exceptions.APIStatusError: raise NotImplementedError() - def _remaining_retries( - self, - remaining_retries: Optional[int], - options: FinalRequestOptions, - ) -> int: - return remaining_retries if remaining_retries is not None else options.get_max_retries(self.max_retries) - - def _build_headers(self, options: FinalRequestOptions) -> httpx.Headers: + def _build_headers(self, options: FinalRequestOptions, *, retries_taken: int = 0) -> httpx.Headers: custom_headers = options.headers or {} headers_dict = _merge_mappings(self.default_headers, custom_headers) self._validate_headers(headers_dict, custom_headers) @@ -419,6 +418,11 @@ def _build_headers(self, options: FinalRequestOptions) -> httpx.Headers: if idempotency_header and options.method.lower() != "get" and idempotency_header not in headers: headers[idempotency_header] = options.idempotency_key or self._idempotency_key() + # Don't set the retry count header if it was already set or removed by the caller. We check + # `custom_headers`, which can contain `Omit()`, instead of `headers` to account for the removal case. + if "x-stainless-retry-count" not in (header.lower() for header in custom_headers): + headers["x-stainless-retry-count"] = str(retries_taken) + return headers def _prepare_url(self, url: str) -> URL: @@ -440,6 +444,8 @@ def _make_sse_decoder(self) -> SSEDecoder | SSEBytesDecoder: def _build_request( self, options: FinalRequestOptions, + *, + retries_taken: int = 0, ) -> httpx.Request: if log.isEnabledFor(logging.DEBUG): log.debug("Request options: %s", model_dump(options, exclude_unset=True)) @@ -455,7 +461,7 @@ def _build_request( else: raise RuntimeError(f"Unexpected JSON data type, {type(json_data)}, cannot merge with `extra_body`") - headers = self._build_headers(options) + headers = self._build_headers(options, retries_taken=retries_taken) params = _merge_mappings(self.default_query, options.params) content_type = headers.get("Content-Type") files = options.files @@ -489,12 +495,17 @@ def _build_request( if not files: files = cast(HttpxRequestFiles, ForceMultipartDict()) + prepared_url = self._prepare_url(options.url) + if "_" in prepared_url.host: + # work around https://github.com/encode/httpx/discussions/2880 + kwargs["extensions"] = {"sni_hostname": prepared_url.host.replace("_", "-")} + # TODO: report this error to httpx return self._client.build_request( # pyright: ignore[reportUnknownMemberType] headers=headers, timeout=self.timeout if isinstance(options.timeout, NotGiven) else options.timeout, method=options.method, - url=self._prepare_url(options.url), + url=prepared_url, # the `Query` type that we use is incompatible with qs' # `Params` type as it needs to be typed as `Mapping[str, object]` # so that passing a `TypedDict` doesn't cause an error. @@ -684,7 +695,8 @@ def _calculate_retry_timeout( if retry_after is not None and 0 < retry_after <= 60: return retry_after - nb_retries = max_retries - remaining_retries + # Also cap retry count to 1000 to avoid any potential overflows with `pow` + nb_retries = min(max_retries - remaining_retries, 1000) # Apply exponential backoff, but not more than the max. sleep_seconds = min(INITIAL_RETRY_DELAY * pow(2.0, nb_retries), MAX_RETRY_DELAY) @@ -933,12 +945,17 @@ def request( stream: bool = False, stream_cls: type[_StreamT] | None = None, ) -> ResponseT | _StreamT: + if remaining_retries is not None: + retries_taken = options.get_max_retries(self.max_retries) - remaining_retries + else: + retries_taken = 0 + return self._request( cast_to=cast_to, options=options, stream=stream, stream_cls=stream_cls, - remaining_retries=remaining_retries, + retries_taken=retries_taken, ) def _request( @@ -946,7 +963,7 @@ def _request( *, cast_to: Type[ResponseT], options: FinalRequestOptions, - remaining_retries: int | None, + retries_taken: int, stream: bool, stream_cls: type[_StreamT] | None, ) -> ResponseT | _StreamT: @@ -958,8 +975,8 @@ def _request( cast_to = self._maybe_override_cast_to(cast_to, options) options = self._prepare_options(options) - retries = self._remaining_retries(remaining_retries, options) - request = self._build_request(options) + remaining_retries = options.get_max_retries(self.max_retries) - retries_taken + request = self._build_request(options, retries_taken=retries_taken) self._prepare_request(request) kwargs: HttpxSendArgs = {} @@ -977,11 +994,11 @@ def _request( except httpx.TimeoutException as err: log.debug("Encountered httpx.TimeoutException", exc_info=True) - if retries > 0: + if remaining_retries > 0: return self._retry_request( input_options, cast_to, - retries, + retries_taken=retries_taken, stream=stream, stream_cls=stream_cls, response_headers=None, @@ -992,11 +1009,11 @@ def _request( except Exception as err: log.debug("Encountered Exception", exc_info=True) - if retries > 0: + if remaining_retries > 0: return self._retry_request( input_options, cast_to, - retries, + retries_taken=retries_taken, stream=stream, stream_cls=stream_cls, response_headers=None, @@ -1019,13 +1036,13 @@ def _request( except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code log.debug("Encountered httpx.HTTPStatusError", exc_info=True) - if retries > 0 and self._should_retry(err.response): + if remaining_retries > 0 and self._should_retry(err.response): err.response.close() return self._retry_request( input_options, cast_to, - retries, - err.response.headers, + retries_taken=retries_taken, + response_headers=err.response.headers, stream=stream, stream_cls=stream_cls, ) @@ -1044,26 +1061,26 @@ def _request( response=response, stream=stream, stream_cls=stream_cls, - retries_taken=options.get_max_retries(self.max_retries) - retries, + retries_taken=retries_taken, ) def _retry_request( self, options: FinalRequestOptions, cast_to: Type[ResponseT], - remaining_retries: int, - response_headers: httpx.Headers | None, *, + retries_taken: int, + response_headers: httpx.Headers | None, stream: bool, stream_cls: type[_StreamT] | None, ) -> ResponseT | _StreamT: - remaining = remaining_retries - 1 - if remaining == 1: + remaining_retries = options.get_max_retries(self.max_retries) - retries_taken + if remaining_retries == 1: log.debug("1 retry left") else: - log.debug("%i retries left", remaining) + log.debug("%i retries left", remaining_retries) - timeout = self._calculate_retry_timeout(remaining, options, response_headers) + timeout = self._calculate_retry_timeout(remaining_retries, options, response_headers) log.info("Retrying request to %s in %f seconds", options.url, timeout) # In a synchronous context we are blocking the entire thread. Up to the library user to run the client in a @@ -1073,7 +1090,7 @@ def _retry_request( return self._request( options=options, cast_to=cast_to, - remaining_retries=remaining, + retries_taken=retries_taken + 1, stream=stream, stream_cls=stream_cls, ) @@ -1491,12 +1508,17 @@ async def request( stream_cls: type[_AsyncStreamT] | None = None, remaining_retries: Optional[int] = None, ) -> ResponseT | _AsyncStreamT: + if remaining_retries is not None: + retries_taken = options.get_max_retries(self.max_retries) - remaining_retries + else: + retries_taken = 0 + return await self._request( cast_to=cast_to, options=options, stream=stream, stream_cls=stream_cls, - remaining_retries=remaining_retries, + retries_taken=retries_taken, ) async def _request( @@ -1506,7 +1528,7 @@ async def _request( *, stream: bool, stream_cls: type[_AsyncStreamT] | None, - remaining_retries: int | None, + retries_taken: int, ) -> ResponseT | _AsyncStreamT: if self._platform is None: # `get_platform` can make blocking IO calls so we @@ -1521,8 +1543,8 @@ async def _request( cast_to = self._maybe_override_cast_to(cast_to, options) options = await self._prepare_options(options) - retries = self._remaining_retries(remaining_retries, options) - request = self._build_request(options) + remaining_retries = options.get_max_retries(self.max_retries) - retries_taken + request = self._build_request(options, retries_taken=retries_taken) await self._prepare_request(request) kwargs: HttpxSendArgs = {} @@ -1538,11 +1560,11 @@ async def _request( except httpx.TimeoutException as err: log.debug("Encountered httpx.TimeoutException", exc_info=True) - if retries > 0: + if remaining_retries > 0: return await self._retry_request( input_options, cast_to, - retries, + retries_taken=retries_taken, stream=stream, stream_cls=stream_cls, response_headers=None, @@ -1553,11 +1575,11 @@ async def _request( except Exception as err: log.debug("Encountered Exception", exc_info=True) - if retries > 0: + if remaining_retries > 0: return await self._retry_request( input_options, cast_to, - retries, + retries_taken=retries_taken, stream=stream, stream_cls=stream_cls, response_headers=None, @@ -1575,13 +1597,13 @@ async def _request( except httpx.HTTPStatusError as err: # thrown on 4xx and 5xx status code log.debug("Encountered httpx.HTTPStatusError", exc_info=True) - if retries > 0 and self._should_retry(err.response): + if remaining_retries > 0 and self._should_retry(err.response): await err.response.aclose() return await self._retry_request( input_options, cast_to, - retries, - err.response.headers, + retries_taken=retries_taken, + response_headers=err.response.headers, stream=stream, stream_cls=stream_cls, ) @@ -1600,26 +1622,26 @@ async def _request( response=response, stream=stream, stream_cls=stream_cls, - retries_taken=options.get_max_retries(self.max_retries) - retries, + retries_taken=retries_taken, ) async def _retry_request( self, options: FinalRequestOptions, cast_to: Type[ResponseT], - remaining_retries: int, - response_headers: httpx.Headers | None, *, + retries_taken: int, + response_headers: httpx.Headers | None, stream: bool, stream_cls: type[_AsyncStreamT] | None, ) -> ResponseT | _AsyncStreamT: - remaining = remaining_retries - 1 - if remaining == 1: + remaining_retries = options.get_max_retries(self.max_retries) - retries_taken + if remaining_retries == 1: log.debug("1 retry left") else: - log.debug("%i retries left", remaining) + log.debug("%i retries left", remaining_retries) - timeout = self._calculate_retry_timeout(remaining, options, response_headers) + timeout = self._calculate_retry_timeout(remaining_retries, options, response_headers) log.info("Retrying request to %s in %f seconds", options.url, timeout) await anyio.sleep(timeout) @@ -1627,7 +1649,7 @@ async def _retry_request( return await self._request( options=options, cast_to=cast_to, - remaining_retries=remaining, + retries_taken=retries_taken + 1, stream=stream, stream_cls=stream_cls, ) diff --git a/src/openlayer/_compat.py b/src/openlayer/_compat.py index 21fe6941..d89920d9 100644 --- a/src/openlayer/_compat.py +++ b/src/openlayer/_compat.py @@ -133,15 +133,17 @@ def model_json(model: pydantic.BaseModel, *, indent: int | None = None) -> str: def model_dump( model: pydantic.BaseModel, *, - exclude: IncEx = None, + exclude: IncEx | None = None, exclude_unset: bool = False, exclude_defaults: bool = False, + warnings: bool = True, ) -> dict[str, Any]: if PYDANTIC_V2: return model.model_dump( exclude=exclude, exclude_unset=exclude_unset, exclude_defaults=exclude_defaults, + warnings=warnings, ) return cast( "dict[str, Any]", diff --git a/src/openlayer/_models.py b/src/openlayer/_models.py index d386eaa3..42551b76 100644 --- a/src/openlayer/_models.py +++ b/src/openlayer/_models.py @@ -176,7 +176,7 @@ def __str__(self) -> str: # Based on https://github.com/samuelcolvin/pydantic/issues/1168#issuecomment-817742836. @classmethod @override - def construct( + def construct( # pyright: ignore[reportIncompatibleMethodOverride] cls: Type[ModelT], _fields_set: set[str] | None = None, **values: object, @@ -248,8 +248,8 @@ def model_dump( self, *, mode: Literal["json", "python"] | str = "python", - include: IncEx = None, - exclude: IncEx = None, + include: IncEx | None = None, + exclude: IncEx | None = None, by_alias: bool = False, exclude_unset: bool = False, exclude_defaults: bool = False, @@ -303,8 +303,8 @@ def model_dump_json( self, *, indent: int | None = None, - include: IncEx = None, - exclude: IncEx = None, + include: IncEx | None = None, + exclude: IncEx | None = None, by_alias: bool = False, exclude_unset: bool = False, exclude_defaults: bool = False, diff --git a/src/openlayer/_response.py b/src/openlayer/_response.py index 364e7503..7234cd68 100644 --- a/src/openlayer/_response.py +++ b/src/openlayer/_response.py @@ -192,6 +192,9 @@ def _parse(self, *, to: type[_T] | None = None) -> R | _T: if cast_to == float: return cast(R, float(response.text)) + if cast_to == bool: + return cast(R, response.text.lower() == "true") + origin = get_origin(cast_to) or cast_to if origin == APIResponse: diff --git a/src/openlayer/_types.py b/src/openlayer/_types.py index 3618c229..4135ae9e 100644 --- a/src/openlayer/_types.py +++ b/src/openlayer/_types.py @@ -16,7 +16,7 @@ Optional, Sequence, ) -from typing_extensions import Literal, Protocol, TypeAlias, TypedDict, override, runtime_checkable +from typing_extensions import Set, Literal, Protocol, TypeAlias, TypedDict, override, runtime_checkable import httpx import pydantic @@ -193,7 +193,9 @@ def get(self, __key: str) -> str | None: ... # Note: copied from Pydantic # https://github.com/pydantic/pydantic/blob/32ea570bf96e84234d2992e1ddf40ab8a565925a/pydantic/main.py#L49 -IncEx: TypeAlias = "set[int] | set[str] | dict[int, Any] | dict[str, Any] | None" +IncEx: TypeAlias = Union[ + Set[int], Set[str], Mapping[int, Union["IncEx", Literal[True]]], Mapping[str, Union["IncEx", Literal[True]]] +] PostParser = Callable[[Any], Any] diff --git a/src/openlayer/_utils/_utils.py b/src/openlayer/_utils/_utils.py index 2fc5a1c6..0bba17ca 100644 --- a/src/openlayer/_utils/_utils.py +++ b/src/openlayer/_utils/_utils.py @@ -363,12 +363,13 @@ def file_from_path(path: str) -> FileTypes: def get_required_header(headers: HeadersLike, header: str) -> str: lower_header = header.lower() - if isinstance(headers, Mapping): - for k, v in headers.items(): + if is_mapping_t(headers): + # mypy doesn't understand the type narrowing here + for k, v in headers.items(): # type: ignore if k.lower() == lower_header and isinstance(v, str): return v - """ to deal with the case where the header looks like Stainless-Event-Id """ + # to deal with the case where the header looks like Stainless-Event-Id intercaps_header = re.sub(r"([^\w])(\w)", lambda pat: pat.group(1) + pat.group(2).upper(), header.capitalize()) for normalized_header in [header, lower_header, header.upper(), intercaps_header]: diff --git a/src/openlayer/_version.py b/src/openlayer/_version.py index 81d0ee49..26025116 100644 --- a/src/openlayer/_version.py +++ b/src/openlayer/_version.py @@ -1,4 +1,4 @@ # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. __title__ = "openlayer" -__version__ = "0.2.0-alpha.26" # x-release-please-version +__version__ = "0.2.0-alpha.31" # x-release-please-version diff --git a/src/openlayer/resources/commits/commits.py b/src/openlayer/resources/commits/commits.py index e9c62f89..774ae94f 100644 --- a/src/openlayer/resources/commits/commits.py +++ b/src/openlayer/resources/commits/commits.py @@ -2,8 +2,24 @@ from __future__ import annotations +from typing import Optional + +import httpx + +from ...types import commit_create_params +from ..._types import NOT_GIVEN, Body, Query, Headers, NotGiven +from ..._utils import ( + maybe_transform, + async_maybe_transform, +) from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource +from ..._response import ( + to_raw_response_wrapper, + to_streamed_response_wrapper, + async_to_raw_response_wrapper, + async_to_streamed_response_wrapper, +) from .test_results import ( TestResultsResource, AsyncTestResultsResource, @@ -12,6 +28,8 @@ TestResultsResourceWithStreamingResponse, AsyncTestResultsResourceWithStreamingResponse, ) +from ..._base_client import make_request_options +from ...types.commit_create_response import CommitCreateResponse __all__ = ["CommitsResource", "AsyncCommitsResource"] @@ -23,12 +41,77 @@ def test_results(self) -> TestResultsResource: @cached_property def with_raw_response(self) -> CommitsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return CommitsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> CommitsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return CommitsResourceWithStreamingResponse(self) + def create( + self, + project_id: str, + *, + commit: commit_create_params.Commit, + storage_uri: str, + archived: Optional[bool] | NotGiven = NOT_GIVEN, + deployment_status: str | NotGiven = NOT_GIVEN, + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. + # The extra values given here take precedence over values defined on the client or passed to this method. + extra_headers: Headers | None = None, + extra_query: Query | None = None, + extra_body: Body | None = None, + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, + ) -> CommitCreateResponse: + """ + Create a new commit (project version) in a project. + + Args: + commit: The details of a commit (project version). + + storage_uri: The storage URI where the commit bundle is stored. + + archived: Whether the commit is archived. + + deployment_status: The deployment status associated with the commit's model. + + extra_headers: Send extra headers + + extra_query: Add additional query parameters to the request + + extra_body: Add additional JSON properties to the request + + timeout: Override the client-level default timeout for this request, in seconds + """ + if not project_id: + raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") + return self._post( + f"/projects/{project_id}/versions", + body=maybe_transform( + { + "commit": commit, + "storage_uri": storage_uri, + "archived": archived, + "deployment_status": deployment_status, + }, + commit_create_params.CommitCreateParams, + ), + options=make_request_options( + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout + ), + cast_to=CommitCreateResponse, + ) + class AsyncCommitsResource(AsyncAPIResource): @cached_property @@ -37,17 +120,86 @@ def test_results(self) -> AsyncTestResultsResource: @cached_property def with_raw_response(self) -> AsyncCommitsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return AsyncCommitsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncCommitsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return AsyncCommitsResourceWithStreamingResponse(self) + async def create( + self, + project_id: str, + *, + commit: commit_create_params.Commit, + storage_uri: str, + archived: Optional[bool] | NotGiven = NOT_GIVEN, + deployment_status: str | NotGiven = NOT_GIVEN, + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. + # The extra values given here take precedence over values defined on the client or passed to this method. + extra_headers: Headers | None = None, + extra_query: Query | None = None, + extra_body: Body | None = None, + timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN, + ) -> CommitCreateResponse: + """ + Create a new commit (project version) in a project. + + Args: + commit: The details of a commit (project version). + + storage_uri: The storage URI where the commit bundle is stored. + + archived: Whether the commit is archived. + + deployment_status: The deployment status associated with the commit's model. + + extra_headers: Send extra headers + + extra_query: Add additional query parameters to the request + + extra_body: Add additional JSON properties to the request + + timeout: Override the client-level default timeout for this request, in seconds + """ + if not project_id: + raise ValueError(f"Expected a non-empty value for `project_id` but received {project_id!r}") + return await self._post( + f"/projects/{project_id}/versions", + body=await async_maybe_transform( + { + "commit": commit, + "storage_uri": storage_uri, + "archived": archived, + "deployment_status": deployment_status, + }, + commit_create_params.CommitCreateParams, + ), + options=make_request_options( + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout + ), + cast_to=CommitCreateResponse, + ) + class CommitsResourceWithRawResponse: def __init__(self, commits: CommitsResource) -> None: self._commits = commits + self.create = to_raw_response_wrapper( + commits.create, + ) + @cached_property def test_results(self) -> TestResultsResourceWithRawResponse: return TestResultsResourceWithRawResponse(self._commits.test_results) @@ -57,6 +209,10 @@ class AsyncCommitsResourceWithRawResponse: def __init__(self, commits: AsyncCommitsResource) -> None: self._commits = commits + self.create = async_to_raw_response_wrapper( + commits.create, + ) + @cached_property def test_results(self) -> AsyncTestResultsResourceWithRawResponse: return AsyncTestResultsResourceWithRawResponse(self._commits.test_results) @@ -66,6 +222,10 @@ class CommitsResourceWithStreamingResponse: def __init__(self, commits: CommitsResource) -> None: self._commits = commits + self.create = to_streamed_response_wrapper( + commits.create, + ) + @cached_property def test_results(self) -> TestResultsResourceWithStreamingResponse: return TestResultsResourceWithStreamingResponse(self._commits.test_results) @@ -75,6 +235,10 @@ class AsyncCommitsResourceWithStreamingResponse: def __init__(self, commits: AsyncCommitsResource) -> None: self._commits = commits + self.create = async_to_streamed_response_wrapper( + commits.create, + ) + @cached_property def test_results(self) -> AsyncTestResultsResourceWithStreamingResponse: return AsyncTestResultsResourceWithStreamingResponse(self._commits.test_results) diff --git a/src/openlayer/resources/commits/test_results.py b/src/openlayer/resources/commits/test_results.py index 3fcba2fa..0d37c7e0 100644 --- a/src/openlayer/resources/commits/test_results.py +++ b/src/openlayer/resources/commits/test_results.py @@ -31,10 +31,21 @@ class TestResultsResource(SyncAPIResource): @cached_property def with_raw_response(self) -> TestResultsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return TestResultsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> TestResultsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return TestResultsResourceWithStreamingResponse(self) def list( @@ -104,10 +115,21 @@ def list( class AsyncTestResultsResource(AsyncAPIResource): @cached_property def with_raw_response(self) -> AsyncTestResultsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return AsyncTestResultsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncTestResultsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return AsyncTestResultsResourceWithStreamingResponse(self) async def list( diff --git a/src/openlayer/resources/inference_pipelines/data.py b/src/openlayer/resources/inference_pipelines/data.py index 9adb0910..f8b4b547 100644 --- a/src/openlayer/resources/inference_pipelines/data.py +++ b/src/openlayer/resources/inference_pipelines/data.py @@ -29,10 +29,21 @@ class DataResource(SyncAPIResource): @cached_property def with_raw_response(self) -> DataResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return DataResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> DataResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return DataResourceWithStreamingResponse(self) def stream( @@ -88,10 +99,21 @@ def stream( class AsyncDataResource(AsyncAPIResource): @cached_property def with_raw_response(self) -> AsyncDataResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return AsyncDataResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncDataResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return AsyncDataResourceWithStreamingResponse(self) async def stream( diff --git a/src/openlayer/resources/inference_pipelines/inference_pipelines.py b/src/openlayer/resources/inference_pipelines/inference_pipelines.py index f64b9dea..bc0f2fe5 100644 --- a/src/openlayer/resources/inference_pipelines/inference_pipelines.py +++ b/src/openlayer/resources/inference_pipelines/inference_pipelines.py @@ -66,10 +66,21 @@ def test_results(self) -> TestResultsResource: @cached_property def with_raw_response(self) -> InferencePipelinesResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return InferencePipelinesResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> InferencePipelinesResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return InferencePipelinesResourceWithStreamingResponse(self) def retrieve( @@ -212,10 +223,21 @@ def test_results(self) -> AsyncTestResultsResource: @cached_property def with_raw_response(self) -> AsyncInferencePipelinesResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return AsyncInferencePipelinesResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncInferencePipelinesResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return AsyncInferencePipelinesResourceWithStreamingResponse(self) async def retrieve( diff --git a/src/openlayer/resources/inference_pipelines/rows.py b/src/openlayer/resources/inference_pipelines/rows.py index d3407927..f763b1ab 100644 --- a/src/openlayer/resources/inference_pipelines/rows.py +++ b/src/openlayer/resources/inference_pipelines/rows.py @@ -29,10 +29,21 @@ class RowsResource(SyncAPIResource): @cached_property def with_raw_response(self) -> RowsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return RowsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> RowsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return RowsResourceWithStreamingResponse(self) def update( @@ -90,10 +101,21 @@ def update( class AsyncRowsResource(AsyncAPIResource): @cached_property def with_raw_response(self) -> AsyncRowsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return AsyncRowsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncRowsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return AsyncRowsResourceWithStreamingResponse(self) async def update( diff --git a/src/openlayer/resources/inference_pipelines/test_results.py b/src/openlayer/resources/inference_pipelines/test_results.py index 37d1fb8e..4bcb435e 100644 --- a/src/openlayer/resources/inference_pipelines/test_results.py +++ b/src/openlayer/resources/inference_pipelines/test_results.py @@ -31,10 +31,21 @@ class TestResultsResource(SyncAPIResource): @cached_property def with_raw_response(self) -> TestResultsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return TestResultsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> TestResultsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return TestResultsResourceWithStreamingResponse(self) def list( @@ -102,10 +113,21 @@ def list( class AsyncTestResultsResource(AsyncAPIResource): @cached_property def with_raw_response(self) -> AsyncTestResultsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return AsyncTestResultsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncTestResultsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return AsyncTestResultsResourceWithStreamingResponse(self) async def list( diff --git a/src/openlayer/resources/projects/commits.py b/src/openlayer/resources/projects/commits.py index f6666180..fd16de8f 100644 --- a/src/openlayer/resources/projects/commits.py +++ b/src/openlayer/resources/projects/commits.py @@ -27,10 +27,21 @@ class CommitsResource(SyncAPIResource): @cached_property def with_raw_response(self) -> CommitsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return CommitsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> CommitsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return CommitsResourceWithStreamingResponse(self) def list( @@ -86,10 +97,21 @@ def list( class AsyncCommitsResource(AsyncAPIResource): @cached_property def with_raw_response(self) -> AsyncCommitsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return AsyncCommitsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncCommitsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return AsyncCommitsResourceWithStreamingResponse(self) async def list( diff --git a/src/openlayer/resources/projects/inference_pipelines.py b/src/openlayer/resources/projects/inference_pipelines.py index 6c8fff28..e8999bdf 100644 --- a/src/openlayer/resources/projects/inference_pipelines.py +++ b/src/openlayer/resources/projects/inference_pipelines.py @@ -30,10 +30,21 @@ class InferencePipelinesResource(SyncAPIResource): @cached_property def with_raw_response(self) -> InferencePipelinesResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return InferencePipelinesResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> InferencePipelinesResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return InferencePipelinesResourceWithStreamingResponse(self) def create( @@ -139,10 +150,21 @@ def list( class AsyncInferencePipelinesResource(AsyncAPIResource): @cached_property def with_raw_response(self) -> AsyncInferencePipelinesResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return AsyncInferencePipelinesResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncInferencePipelinesResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return AsyncInferencePipelinesResourceWithStreamingResponse(self) async def create( diff --git a/src/openlayer/resources/projects/projects.py b/src/openlayer/resources/projects/projects.py index fad7171a..e5e90392 100644 --- a/src/openlayer/resources/projects/projects.py +++ b/src/openlayer/resources/projects/projects.py @@ -55,10 +55,21 @@ def inference_pipelines(self) -> InferencePipelinesResource: @cached_property def with_raw_response(self) -> ProjectsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return ProjectsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> ProjectsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return ProjectsResourceWithStreamingResponse(self) def create( @@ -175,10 +186,21 @@ def inference_pipelines(self) -> AsyncInferencePipelinesResource: @cached_property def with_raw_response(self) -> AsyncProjectsResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return AsyncProjectsResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncProjectsResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return AsyncProjectsResourceWithStreamingResponse(self) async def create( diff --git a/src/openlayer/resources/storage/presigned_url.py b/src/openlayer/resources/storage/presigned_url.py index ad2990e5..5fb6fa1c 100644 --- a/src/openlayer/resources/storage/presigned_url.py +++ b/src/openlayer/resources/storage/presigned_url.py @@ -27,10 +27,21 @@ class PresignedURLResource(SyncAPIResource): @cached_property def with_raw_response(self) -> PresignedURLResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return PresignedURLResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> PresignedURLResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return PresignedURLResourceWithStreamingResponse(self) def create( @@ -76,10 +87,21 @@ def create( class AsyncPresignedURLResource(AsyncAPIResource): @cached_property def with_raw_response(self) -> AsyncPresignedURLResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return AsyncPresignedURLResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncPresignedURLResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return AsyncPresignedURLResourceWithStreamingResponse(self) async def create( diff --git a/src/openlayer/resources/storage/storage.py b/src/openlayer/resources/storage/storage.py index 935bdc43..ea2a3c99 100644 --- a/src/openlayer/resources/storage/storage.py +++ b/src/openlayer/resources/storage/storage.py @@ -23,10 +23,21 @@ def presigned_url(self) -> PresignedURLResource: @cached_property def with_raw_response(self) -> StorageResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return StorageResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> StorageResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return StorageResourceWithStreamingResponse(self) @@ -37,10 +48,21 @@ def presigned_url(self) -> AsyncPresignedURLResource: @cached_property def with_raw_response(self) -> AsyncStorageResourceWithRawResponse: + """ + This property can be used as a prefix for any HTTP method call to return the + the raw response object instead of the parsed content. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#accessing-raw-response-data-eg-headers + """ return AsyncStorageResourceWithRawResponse(self) @cached_property def with_streaming_response(self) -> AsyncStorageResourceWithStreamingResponse: + """ + An alternative to `.with_raw_response` that doesn't eagerly read the response body. + + For more information, see https://www.github.com/openlayer-ai/openlayer-python#with_streaming_response + """ return AsyncStorageResourceWithStreamingResponse(self) diff --git a/src/openlayer/types/__init__.py b/src/openlayer/types/__init__.py index 58883aff..48381166 100644 --- a/src/openlayer/types/__init__.py +++ b/src/openlayer/types/__init__.py @@ -3,8 +3,10 @@ from __future__ import annotations from .project_list_params import ProjectListParams as ProjectListParams +from .commit_create_params import CommitCreateParams as CommitCreateParams from .project_create_params import ProjectCreateParams as ProjectCreateParams from .project_list_response import ProjectListResponse as ProjectListResponse +from .commit_create_response import CommitCreateResponse as CommitCreateResponse from .project_create_response import ProjectCreateResponse as ProjectCreateResponse from .inference_pipeline_update_params import InferencePipelineUpdateParams as InferencePipelineUpdateParams from .inference_pipeline_update_response import InferencePipelineUpdateResponse as InferencePipelineUpdateResponse diff --git a/src/openlayer/types/commit_create_params.py b/src/openlayer/types/commit_create_params.py new file mode 100644 index 00000000..2a7d54de --- /dev/null +++ b/src/openlayer/types/commit_create_params.py @@ -0,0 +1,29 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing import Optional +from typing_extensions import Required, Annotated, TypedDict + +from .._utils import PropertyInfo + +__all__ = ["CommitCreateParams", "Commit"] + + +class CommitCreateParams(TypedDict, total=False): + commit: Required[Commit] + """The details of a commit (project version).""" + + storage_uri: Required[Annotated[str, PropertyInfo(alias="storageUri")]] + """The storage URI where the commit bundle is stored.""" + + archived: Optional[bool] + """Whether the commit is archived.""" + + deployment_status: Annotated[str, PropertyInfo(alias="deploymentStatus")] + """The deployment status associated with the commit's model.""" + + +class Commit(TypedDict, total=False): + message: Required[str] + """The commit message.""" diff --git a/src/openlayer/types/commit_create_response.py b/src/openlayer/types/commit_create_response.py new file mode 100644 index 00000000..82bf6d16 --- /dev/null +++ b/src/openlayer/types/commit_create_response.py @@ -0,0 +1,106 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from typing import Optional +from datetime import datetime +from typing_extensions import Literal + +from pydantic import Field as FieldInfo + +from .._models import BaseModel + +__all__ = ["CommitCreateResponse", "Commit", "Links"] + + +class Commit(BaseModel): + id: str + """The commit id.""" + + author_id: str = FieldInfo(alias="authorId") + """The author id of the commit.""" + + file_size: Optional[int] = FieldInfo(alias="fileSize", default=None) + """The size of the commit bundle in bytes.""" + + message: str + """The commit message.""" + + ml_model_id: Optional[str] = FieldInfo(alias="mlModelId", default=None) + """The model id.""" + + storage_uri: str = FieldInfo(alias="storageUri") + """The storage URI where the commit bundle is stored.""" + + training_dataset_id: Optional[str] = FieldInfo(alias="trainingDatasetId", default=None) + """The training dataset id.""" + + validation_dataset_id: Optional[str] = FieldInfo(alias="validationDatasetId", default=None) + """The validation dataset id.""" + + date_created: Optional[datetime] = FieldInfo(alias="dateCreated", default=None) + """The commit creation date.""" + + git_commit_ref: Optional[str] = FieldInfo(alias="gitCommitRef", default=None) + """The ref of the corresponding git commit.""" + + git_commit_sha: Optional[int] = FieldInfo(alias="gitCommitSha", default=None) + """The SHA of the corresponding git commit.""" + + git_commit_url: Optional[str] = FieldInfo(alias="gitCommitUrl", default=None) + """The URL of the corresponding git commit.""" + + +class Links(BaseModel): + app: str + + +class CommitCreateResponse(BaseModel): + id: str + """The project version (commit) id.""" + + commit: Commit + """The details of a commit (project version).""" + + date_archived: Optional[datetime] = FieldInfo(alias="dateArchived", default=None) + """The commit archive date.""" + + date_created: datetime = FieldInfo(alias="dateCreated") + """The project version (commit) creation date.""" + + failing_goal_count: int = FieldInfo(alias="failingGoalCount") + """The number of tests that are failing for the commit.""" + + ml_model_id: Optional[str] = FieldInfo(alias="mlModelId", default=None) + """The model id.""" + + passing_goal_count: int = FieldInfo(alias="passingGoalCount") + """The number of tests that are passing for the commit.""" + + project_id: str = FieldInfo(alias="projectId") + """The project id.""" + + status: Literal["queued", "running", "paused", "failed", "completed", "unknown"] + """The commit status. + + Initially, the commit is `queued`, then, it switches to `running`. Finally, it + can be `paused`, `failed`, or `completed`. + """ + + status_message: Optional[str] = FieldInfo(alias="statusMessage", default=None) + """The commit status message.""" + + total_goal_count: int = FieldInfo(alias="totalGoalCount") + """The total number of tests for the commit.""" + + training_dataset_id: Optional[str] = FieldInfo(alias="trainingDatasetId", default=None) + """The training dataset id.""" + + validation_dataset_id: Optional[str] = FieldInfo(alias="validationDatasetId", default=None) + """The validation dataset id.""" + + archived: Optional[bool] = None + """Whether the commit is archived.""" + + deployment_status: Optional[str] = FieldInfo(alias="deploymentStatus", default=None) + """The deployment status associated with the commit's model.""" + + links: Optional[Links] = None diff --git a/src/openlayer/types/storage/presigned_url_create_response.py b/src/openlayer/types/storage/presigned_url_create_response.py index 71791bbf..db578318 100644 --- a/src/openlayer/types/storage/presigned_url_create_response.py +++ b/src/openlayer/types/storage/presigned_url_create_response.py @@ -17,4 +17,4 @@ class PresignedURLCreateResponse(BaseModel): """The presigned url.""" fields: Optional[object] = None - """Fields to include in the body of the upload. Only needed by s3.""" + """Fields to include in the body of the upload. Only needed by s3""" diff --git a/tests/api_resources/test_commits.py b/tests/api_resources/test_commits.py new file mode 100644 index 00000000..15e0f5d9 --- /dev/null +++ b/tests/api_resources/test_commits.py @@ -0,0 +1,136 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +import os +from typing import Any, cast + +import pytest + +from openlayer import Openlayer, AsyncOpenlayer +from tests.utils import assert_matches_type +from openlayer.types import CommitCreateResponse + +base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") + + +class TestCommits: + parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) + + @parametrize + def test_method_create(self, client: Openlayer) -> None: + commit = client.commits.create( + project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + commit={"message": "Updated the prompt."}, + storage_uri="s3://...", + ) + assert_matches_type(CommitCreateResponse, commit, path=["response"]) + + @parametrize + def test_method_create_with_all_params(self, client: Openlayer) -> None: + commit = client.commits.create( + project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + commit={"message": "Updated the prompt."}, + storage_uri="s3://...", + archived=False, + deployment_status="Deployed", + ) + assert_matches_type(CommitCreateResponse, commit, path=["response"]) + + @parametrize + def test_raw_response_create(self, client: Openlayer) -> None: + response = client.commits.with_raw_response.create( + project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + commit={"message": "Updated the prompt."}, + storage_uri="s3://...", + ) + + assert response.is_closed is True + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + commit = response.parse() + assert_matches_type(CommitCreateResponse, commit, path=["response"]) + + @parametrize + def test_streaming_response_create(self, client: Openlayer) -> None: + with client.commits.with_streaming_response.create( + project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + commit={"message": "Updated the prompt."}, + storage_uri="s3://...", + ) as response: + assert not response.is_closed + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + + commit = response.parse() + assert_matches_type(CommitCreateResponse, commit, path=["response"]) + + assert cast(Any, response.is_closed) is True + + @parametrize + def test_path_params_create(self, client: Openlayer) -> None: + with pytest.raises(ValueError, match=r"Expected a non-empty value for `project_id` but received ''"): + client.commits.with_raw_response.create( + project_id="", + commit={"message": "Updated the prompt."}, + storage_uri="s3://...", + ) + + +class TestAsyncCommits: + parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) + + @parametrize + async def test_method_create(self, async_client: AsyncOpenlayer) -> None: + commit = await async_client.commits.create( + project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + commit={"message": "Updated the prompt."}, + storage_uri="s3://...", + ) + assert_matches_type(CommitCreateResponse, commit, path=["response"]) + + @parametrize + async def test_method_create_with_all_params(self, async_client: AsyncOpenlayer) -> None: + commit = await async_client.commits.create( + project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + commit={"message": "Updated the prompt."}, + storage_uri="s3://...", + archived=False, + deployment_status="Deployed", + ) + assert_matches_type(CommitCreateResponse, commit, path=["response"]) + + @parametrize + async def test_raw_response_create(self, async_client: AsyncOpenlayer) -> None: + response = await async_client.commits.with_raw_response.create( + project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + commit={"message": "Updated the prompt."}, + storage_uri="s3://...", + ) + + assert response.is_closed is True + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + commit = await response.parse() + assert_matches_type(CommitCreateResponse, commit, path=["response"]) + + @parametrize + async def test_streaming_response_create(self, async_client: AsyncOpenlayer) -> None: + async with async_client.commits.with_streaming_response.create( + project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + commit={"message": "Updated the prompt."}, + storage_uri="s3://...", + ) as response: + assert not response.is_closed + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + + commit = await response.parse() + assert_matches_type(CommitCreateResponse, commit, path=["response"]) + + assert cast(Any, response.is_closed) is True + + @parametrize + async def test_path_params_create(self, async_client: AsyncOpenlayer) -> None: + with pytest.raises(ValueError, match=r"Expected a non-empty value for `project_id` but received ''"): + await async_client.commits.with_raw_response.create( + project_id="", + commit={"message": "Updated the prompt."}, + storage_uri="s3://...", + ) diff --git a/tests/conftest.py b/tests/conftest.py index 0857c182..554ab710 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,11 +1,11 @@ from __future__ import annotations import os -import asyncio import logging from typing import TYPE_CHECKING, Iterator, AsyncIterator import pytest +from pytest_asyncio import is_async_test from openlayer import Openlayer, AsyncOpenlayer @@ -17,11 +17,13 @@ logging.getLogger("openlayer").setLevel(logging.DEBUG) -@pytest.fixture(scope="session") -def event_loop() -> Iterator[asyncio.AbstractEventLoop]: - loop = asyncio.new_event_loop() - yield loop - loop.close() +# automatically add `pytest.mark.asyncio()` to all of our async tests +# so we don't have to add that boilerplate everywhere +def pytest_collection_modifyitems(items: list[pytest.Function]) -> None: + pytest_asyncio_tests = (item for item in items if is_async_test(item)) + session_scope_marker = pytest.mark.asyncio(loop_scope="session") + for async_test in pytest_asyncio_tests: + async_test.add_marker(session_scope_marker, append=False) base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") diff --git a/tests/test_client.py b/tests/test_client.py index 7b312411..b57e50db 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -10,6 +10,7 @@ import tracemalloc from typing import Any, Union, cast from unittest import mock +from typing_extensions import Literal import httpx import pytest @@ -701,6 +702,7 @@ class Model(BaseModel): [3, "", 0.5], [2, "", 0.5 * 2.0], [1, "", 0.5 * 4.0], + [-1100, "", 7.8], # test large number potentially overflowing ], ) @mock.patch("time.time", mock.MagicMock(return_value=1696004797)) @@ -738,7 +740,7 @@ def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter) -> No "output": "42", "tokens": 7, "cost": 0.02, - "timestamp": 1620000000, + "timestamp": 1610000000, } ], ), @@ -775,7 +777,7 @@ def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> Non "output": "42", "tokens": 7, "cost": 0.02, - "timestamp": 1620000000, + "timestamp": 1610000000, } ], ), @@ -789,7 +791,14 @@ def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> Non @pytest.mark.parametrize("failures_before_success", [0, 2, 4]) @mock.patch("openlayer._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) @pytest.mark.respx(base_url=base_url) - def test_retries_taken(self, client: Openlayer, failures_before_success: int, respx_mock: MockRouter) -> None: + @pytest.mark.parametrize("failure_mode", ["status", "exception"]) + def test_retries_taken( + self, + client: Openlayer, + failures_before_success: int, + failure_mode: Literal["status", "exception"], + respx_mock: MockRouter, + ) -> None: client = client.with_options(max_retries=4) nb_retries = 0 @@ -798,6 +807,8 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: nonlocal nb_retries if nb_retries < failures_before_success: nb_retries += 1 + if failure_mode == "exception": + raise RuntimeError("oops") return httpx.Response(500) return httpx.Response(200) @@ -820,6 +831,83 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: ) assert response.retries_taken == failures_before_success + assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success + + @pytest.mark.parametrize("failures_before_success", [0, 2, 4]) + @mock.patch("openlayer._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) + @pytest.mark.respx(base_url=base_url) + def test_omit_retry_count_header( + self, client: Openlayer, failures_before_success: int, respx_mock: MockRouter + ) -> None: + client = client.with_options(max_retries=4) + + nb_retries = 0 + + def retry_handler(_request: httpx.Request) -> httpx.Response: + nonlocal nb_retries + if nb_retries < failures_before_success: + nb_retries += 1 + return httpx.Response(500) + return httpx.Response(200) + + respx_mock.post("/inference-pipelines/182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e/data-stream").mock( + side_effect=retry_handler + ) + + response = client.inference_pipelines.data.with_raw_response.stream( + inference_pipeline_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + config={"output_column_name": "output"}, + rows=[ + { + "user_query": "bar", + "output": "bar", + "tokens": "bar", + "cost": "bar", + "timestamp": "bar", + } + ], + extra_headers={"x-stainless-retry-count": Omit()}, + ) + + assert len(response.http_request.headers.get_list("x-stainless-retry-count")) == 0 + + @pytest.mark.parametrize("failures_before_success", [0, 2, 4]) + @mock.patch("openlayer._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) + @pytest.mark.respx(base_url=base_url) + def test_overwrite_retry_count_header( + self, client: Openlayer, failures_before_success: int, respx_mock: MockRouter + ) -> None: + client = client.with_options(max_retries=4) + + nb_retries = 0 + + def retry_handler(_request: httpx.Request) -> httpx.Response: + nonlocal nb_retries + if nb_retries < failures_before_success: + nb_retries += 1 + return httpx.Response(500) + return httpx.Response(200) + + respx_mock.post("/inference-pipelines/182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e/data-stream").mock( + side_effect=retry_handler + ) + + response = client.inference_pipelines.data.with_raw_response.stream( + inference_pipeline_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + config={"output_column_name": "output"}, + rows=[ + { + "user_query": "bar", + "output": "bar", + "tokens": "bar", + "cost": "bar", + "timestamp": "bar", + } + ], + extra_headers={"x-stainless-retry-count": "42"}, + ) + + assert response.http_request.headers.get("x-stainless-retry-count") == "42" class TestAsyncOpenlayer: @@ -1486,6 +1574,7 @@ class Model(BaseModel): [3, "", 0.5], [2, "", 0.5 * 2.0], [1, "", 0.5 * 4.0], + [-1100, "", 7.8], # test large number potentially overflowing ], ) @mock.patch("time.time", mock.MagicMock(return_value=1696004797)) @@ -1524,7 +1613,7 @@ async def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter) "output": "42", "tokens": 7, "cost": 0.02, - "timestamp": 1620000000, + "timestamp": 1610000000, } ], ), @@ -1561,7 +1650,7 @@ async def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) "output": "42", "tokens": 7, "cost": 0.02, - "timestamp": 1620000000, + "timestamp": 1610000000, } ], ), @@ -1576,8 +1665,13 @@ async def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) @mock.patch("openlayer._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) @pytest.mark.respx(base_url=base_url) @pytest.mark.asyncio + @pytest.mark.parametrize("failure_mode", ["status", "exception"]) async def test_retries_taken( - self, async_client: AsyncOpenlayer, failures_before_success: int, respx_mock: MockRouter + self, + async_client: AsyncOpenlayer, + failures_before_success: int, + failure_mode: Literal["status", "exception"], + respx_mock: MockRouter, ) -> None: client = async_client.with_options(max_retries=4) @@ -1587,6 +1681,8 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: nonlocal nb_retries if nb_retries < failures_before_success: nb_retries += 1 + if failure_mode == "exception": + raise RuntimeError("oops") return httpx.Response(500) return httpx.Response(200) @@ -1609,3 +1705,82 @@ def retry_handler(_request: httpx.Request) -> httpx.Response: ) assert response.retries_taken == failures_before_success + assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success + + @pytest.mark.parametrize("failures_before_success", [0, 2, 4]) + @mock.patch("openlayer._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) + @pytest.mark.respx(base_url=base_url) + @pytest.mark.asyncio + async def test_omit_retry_count_header( + self, async_client: AsyncOpenlayer, failures_before_success: int, respx_mock: MockRouter + ) -> None: + client = async_client.with_options(max_retries=4) + + nb_retries = 0 + + def retry_handler(_request: httpx.Request) -> httpx.Response: + nonlocal nb_retries + if nb_retries < failures_before_success: + nb_retries += 1 + return httpx.Response(500) + return httpx.Response(200) + + respx_mock.post("/inference-pipelines/182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e/data-stream").mock( + side_effect=retry_handler + ) + + response = await client.inference_pipelines.data.with_raw_response.stream( + inference_pipeline_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + config={"output_column_name": "output"}, + rows=[ + { + "user_query": "bar", + "output": "bar", + "tokens": "bar", + "cost": "bar", + "timestamp": "bar", + } + ], + extra_headers={"x-stainless-retry-count": Omit()}, + ) + + assert len(response.http_request.headers.get_list("x-stainless-retry-count")) == 0 + + @pytest.mark.parametrize("failures_before_success", [0, 2, 4]) + @mock.patch("openlayer._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout) + @pytest.mark.respx(base_url=base_url) + @pytest.mark.asyncio + async def test_overwrite_retry_count_header( + self, async_client: AsyncOpenlayer, failures_before_success: int, respx_mock: MockRouter + ) -> None: + client = async_client.with_options(max_retries=4) + + nb_retries = 0 + + def retry_handler(_request: httpx.Request) -> httpx.Response: + nonlocal nb_retries + if nb_retries < failures_before_success: + nb_retries += 1 + return httpx.Response(500) + return httpx.Response(200) + + respx_mock.post("/inference-pipelines/182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e/data-stream").mock( + side_effect=retry_handler + ) + + response = await client.inference_pipelines.data.with_raw_response.stream( + inference_pipeline_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", + config={"output_column_name": "output"}, + rows=[ + { + "user_query": "bar", + "output": "bar", + "tokens": "bar", + "cost": "bar", + "timestamp": "bar", + } + ], + extra_headers={"x-stainless-retry-count": "42"}, + ) + + assert response.http_request.headers.get("x-stainless-retry-count") == "42" diff --git a/tests/test_models.py b/tests/test_models.py index 963a34ff..f019e17b 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -245,7 +245,7 @@ class Model(BaseModel): assert m.foo is True m = Model.construct(foo="CARD_HOLDER") - assert m.foo is "CARD_HOLDER" + assert m.foo == "CARD_HOLDER" m = Model.construct(foo={"bar": False}) assert isinstance(m.foo, Submodel1) diff --git a/tests/test_response.py b/tests/test_response.py index bc0a45bd..544ceeb4 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -190,6 +190,56 @@ async def test_async_response_parse_annotated_type(async_client: AsyncOpenlayer) assert obj.bar == 2 +@pytest.mark.parametrize( + "content, expected", + [ + ("false", False), + ("true", True), + ("False", False), + ("True", True), + ("TrUe", True), + ("FalSe", False), + ], +) +def test_response_parse_bool(client: Openlayer, content: str, expected: bool) -> None: + response = APIResponse( + raw=httpx.Response(200, content=content), + client=client, + stream=False, + stream_cls=None, + cast_to=str, + options=FinalRequestOptions.construct(method="get", url="/foo"), + ) + + result = response.parse(to=bool) + assert result is expected + + +@pytest.mark.parametrize( + "content, expected", + [ + ("false", False), + ("true", True), + ("False", False), + ("True", True), + ("TrUe", True), + ("FalSe", False), + ], +) +async def test_async_response_parse_bool(client: AsyncOpenlayer, content: str, expected: bool) -> None: + response = AsyncAPIResponse( + raw=httpx.Response(200, content=content), + client=client, + stream=False, + stream_cls=None, + cast_to=str, + options=FinalRequestOptions.construct(method="get", url="/foo"), + ) + + result = await response.parse(to=bool) + assert result is expected + + class OtherModel(BaseModel): a: str