Skip to content
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
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ jobs:
run: ./scripts/lint

build:
if: github.repository == 'stainless-sdks/gradient-python' && (github.event_name == 'push' || github.event.pull_request.head.repo.fork)
if: github.event_name == 'push' || github.event.pull_request.head.repo.fork
timeout-minutes: 10
name: build
permissions:
contents: read
id-token: write
runs-on: depot-ubuntu-24.04
runs-on: ${{ github.repository == 'stainless-sdks/gradient-python' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
steps:
- uses: actions/checkout@v4

Expand All @@ -61,12 +61,14 @@ jobs:
run: rye build

- name: Get GitHub OIDC Token
if: github.repository == 'stainless-sdks/gradient-python'
id: github-oidc
uses: actions/github-script@v6
with:
script: core.setOutput('github_token', await core.getIDToken());

- name: Upload tarball
if: github.repository == 'stainless-sdks/gradient-python'
env:
URL: https://pkg.stainless.com/s
AUTH: ${{ steps.github-oidc.outputs.github_token }}
Expand Down
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "3.0.0-beta.4"
".": "3.0.0-beta.5"
}
4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 170
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/digitalocean%2Fgradient-9aca3802735e1375125412aa28ac36bf2175144b8218610a73d2e7f775694dff.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/digitalocean%2Fgradient-621c3ebf5011c5ca508f78fccbea17de4ca6b35bfe99578c1ae2265021578d6f.yml
openapi_spec_hash: e29d14e3e4679fcf22b3e760e49931b1
config_hash: 99e3cd5dde0beb796f4547410869f726
config_hash: 6c8d569b60ae6536708a165b72ff838f
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# Changelog

## 3.0.0-beta.5 (2025-09-08)

Full Changelog: [v3.0.0-beta.4...v3.0.0-beta.5](https://github.com/digitalocean/gradient-python/compare/v3.0.0-beta.4...v3.0.0-beta.5)

### Features

* **api:** manual updates ([044a233](https://github.com/digitalocean/gradient-python/commit/044a2339f9ae89facbed403d8240d1e4cf3e9c1f))
* **api:** manual updates ([0e8fd1b](https://github.com/digitalocean/gradient-python/commit/0e8fd1b364751ec933cadf02be693afa63a67029))


### Bug Fixes

* avoid newer type syntax ([3d5c35c](https://github.com/digitalocean/gradient-python/commit/3d5c35ca11b4c7344308f7fbd7cd98ec44dd65a0))


### Chores

* **internal:** add Sequence related utils ([2997cfc](https://github.com/digitalocean/gradient-python/commit/2997cfc25bf46b4cc9faf9f0f22cb4680cadca8b))
* **internal:** change ci workflow machines ([5f41b3d](https://github.com/digitalocean/gradient-python/commit/5f41b3d956bf1ae25f90b862d5057c16b06e78a3))
* **internal:** update pyright exclude list ([2a0d1a2](https://github.com/digitalocean/gradient-python/commit/2a0d1a2b174990d6b081ff764b13949b4dfa107f))
* update github action ([369c5d9](https://github.com/digitalocean/gradient-python/commit/369c5d982cfadfaaaeda9481b2c9249e3f87423d))

## 3.0.0-beta.4 (2025-08-12)

Full Changelog: [v3.0.0-beta.3...v3.0.0-beta.4](https://github.com/digitalocean/gradient-python/compare/v3.0.0-beta.3...v3.0.0-beta.4)
Expand Down
9 changes: 7 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "gradient"
version = "3.0.0-beta.4"
version = "3.0.0-beta.5"
description = "The official Python library for the Gradient API"
dynamic = ["readme"]
license = "Apache-2.0"
Expand Down Expand Up @@ -131,7 +131,12 @@ filterwarnings = ["error"]
typeCheckingMode = "strict"
pythonVersion = "3.8"

exclude = ["_dev", ".venv", ".nox"]
exclude = [
"_dev",
".venv",
".nox",
".git",
]

reportImplicitOverride = true
reportOverlappingOverload = false
Expand Down
73 changes: 24 additions & 49 deletions src/gradient/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def __init__(
- `access_token` from `DIGITALOCEAN_ACCESS_TOKEN`
- `model_access_key` from `GRADIENT_MODEL_ACCESS_KEY`
- `agent_access_key` from `GRADIENT_AGENT_ACCESS_KEY`
- `agent_endpoint` from `GRADIENT_AGENT_ENDPOINT`
- `inference_endpoint` from `GRADIENT_INFERENCE_ENDPOINT`
"""
if access_token is None:
if api_key is not None:
Expand Down Expand Up @@ -149,10 +151,7 @@ def __init__(
self._agent_endpoint = agent_endpoint

if inference_endpoint is None:
inference_endpoint = os.environ.get("GRADIENT_INFERENCE_ENDPOINT")
if inference_endpoint is None:
inference_endpoint = "https://inference.do-ai.run"

inference_endpoint = os.environ.get("GRADIENT_INFERENCE_ENDPOINT") or "inference.do-ai.run"
self.inference_endpoint = inference_endpoint

if base_url is None:
Expand Down Expand Up @@ -267,9 +266,7 @@ def default_headers(self) -> dict[str, str | Omit]:

@override
def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
if (
self.access_token or self.agent_access_key or self.model_access_key
) and headers.get("Authorization"):
if (self.access_token or self.agent_access_key or self.model_access_key) and headers.get("Authorization"):
return
if isinstance(custom_headers.get("Authorization"), Omit):
return
Expand Down Expand Up @@ -303,14 +300,10 @@ def copy(
Create a new client instance re-using the same options given to the current client with optional overriding.
"""
if default_headers is not None and set_default_headers is not None:
raise ValueError(
"The `default_headers` and `set_default_headers` arguments are mutually exclusive"
)
raise ValueError("The `default_headers` and `set_default_headers` arguments are mutually exclusive")

if default_query is not None and set_default_query is not None:
raise ValueError(
"The `default_query` and `set_default_query` arguments are mutually exclusive"
)
raise ValueError("The `default_query` and `set_default_query` arguments are mutually exclusive")

headers = self._custom_headers
if default_headers is not None:
Expand Down Expand Up @@ -358,14 +351,10 @@ def _make_status_error(
return _exceptions.BadRequestError(err_msg, response=response, body=body)

if response.status_code == 401:
return _exceptions.AuthenticationError(
err_msg, response=response, body=body
)
return _exceptions.AuthenticationError(err_msg, response=response, body=body)

if response.status_code == 403:
return _exceptions.PermissionDeniedError(
err_msg, response=response, body=body
)
return _exceptions.PermissionDeniedError(err_msg, response=response, body=body)

if response.status_code == 404:
return _exceptions.NotFoundError(err_msg, response=response, body=body)
Expand All @@ -374,17 +363,13 @@ def _make_status_error(
return _exceptions.ConflictError(err_msg, response=response, body=body)

if response.status_code == 422:
return _exceptions.UnprocessableEntityError(
err_msg, response=response, body=body
)
return _exceptions.UnprocessableEntityError(err_msg, response=response, body=body)

if response.status_code == 429:
return _exceptions.RateLimitError(err_msg, response=response, body=body)

if response.status_code >= 500:
return _exceptions.InternalServerError(
err_msg, response=response, body=body
)
return _exceptions.InternalServerError(err_msg, response=response, body=body)
return APIStatusError(err_msg, response=response, body=body)


Expand Down Expand Up @@ -432,6 +417,8 @@ def __init__(
- `access_token` from `DIGITALOCEAN_ACCESS_TOKEN`
- `model_access_key` from `GRADIENT_MODEL_ACCESS_KEY`
- `agent_access_key` from `GRADIENT_AGENT_ACCESS_KEY`
- `agent_endpoint` from `GRADIENT_AGENT_ENDPOINT`
- `inference_endpoint` from `GRADIENT_INFERENCE_ENDPOINT`
"""
if access_token is None:
if api_key is not None:
Expand Down Expand Up @@ -463,8 +450,12 @@ def __init__(
agent_access_key = os.environ.get("GRADIENT_AGENT_KEY")
self.agent_access_key = agent_access_key

if agent_endpoint is None:
agent_endpoint = os.environ.get("GRADIENT_AGENT_ENDPOINT")
self._agent_endpoint = agent_endpoint

if inference_endpoint is None:
inference_endpoint = os.environ.get("GRADIENT_INFERENCE_ENDPOINT") or "inference.do-ai.run"
self.inference_endpoint = inference_endpoint

if base_url is None:
Expand Down Expand Up @@ -579,9 +570,7 @@ def default_headers(self) -> dict[str, str | Omit]:

@override
def _validate_headers(self, headers: Headers, custom_headers: Headers) -> None:
if (
self.access_token or self.agent_access_key or self.model_access_key
) and headers.get("Authorization"):
if (self.access_token or self.agent_access_key or self.model_access_key) and headers.get("Authorization"):
return
if isinstance(custom_headers.get("Authorization"), Omit):
return
Expand Down Expand Up @@ -615,14 +604,10 @@ def copy(
Create a new client instance re-using the same options given to the current client with optional overriding.
"""
if default_headers is not None and set_default_headers is not None:
raise ValueError(
"The `default_headers` and `set_default_headers` arguments are mutually exclusive"
)
raise ValueError("The `default_headers` and `set_default_headers` arguments are mutually exclusive")

if default_query is not None and set_default_query is not None:
raise ValueError(
"The `default_query` and `set_default_query` arguments are mutually exclusive"
)
raise ValueError("The `default_query` and `set_default_query` arguments are mutually exclusive")

headers = self._custom_headers
if default_headers is not None:
Expand Down Expand Up @@ -670,14 +655,10 @@ def _make_status_error(
return _exceptions.BadRequestError(err_msg, response=response, body=body)

if response.status_code == 401:
return _exceptions.AuthenticationError(
err_msg, response=response, body=body
)
return _exceptions.AuthenticationError(err_msg, response=response, body=body)

if response.status_code == 403:
return _exceptions.PermissionDeniedError(
err_msg, response=response, body=body
)
return _exceptions.PermissionDeniedError(err_msg, response=response, body=body)

if response.status_code == 404:
return _exceptions.NotFoundError(err_msg, response=response, body=body)
Expand All @@ -686,17 +667,13 @@ def _make_status_error(
return _exceptions.ConflictError(err_msg, response=response, body=body)

if response.status_code == 422:
return _exceptions.UnprocessableEntityError(
err_msg, response=response, body=body
)
return _exceptions.UnprocessableEntityError(err_msg, response=response, body=body)

if response.status_code == 429:
return _exceptions.RateLimitError(err_msg, response=response, body=body)

if response.status_code >= 500:
return _exceptions.InternalServerError(
err_msg, response=response, body=body
)
return _exceptions.InternalServerError(err_msg, response=response, body=body)
return APIStatusError(err_msg, response=response, body=body)


Expand Down Expand Up @@ -915,9 +892,7 @@ def knowledge_bases(
AsyncKnowledgeBasesResourceWithStreamingResponse,
)

return AsyncKnowledgeBasesResourceWithStreamingResponse(
self._client.knowledge_bases
)
return AsyncKnowledgeBasesResourceWithStreamingResponse(self._client.knowledge_bases)

@cached_property
def models(self) -> models.AsyncModelsResourceWithStreamingResponse:
Expand Down
2 changes: 1 addition & 1 deletion src/gradient/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def model_dump(
exclude_none=exclude_none,
)

return cast(dict[str, Any], json_safe(dumped)) if mode == "json" else dumped
return cast("dict[str, Any]", json_safe(dumped)) if mode == "json" else dumped

@override
def model_dump_json(
Expand Down
36 changes: 35 additions & 1 deletion src/gradient/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@
Mapping,
TypeVar,
Callable,
Iterator,
Optional,
Sequence,
)
from typing_extensions import Set, Literal, Protocol, TypeAlias, TypedDict, override, runtime_checkable
from typing_extensions import (
Set,
Literal,
Protocol,
TypeAlias,
TypedDict,
SupportsIndex,
overload,
override,
runtime_checkable,
)

import httpx
import pydantic
Expand Down Expand Up @@ -217,3 +228,26 @@ class _GenericAlias(Protocol):
class HttpxSendArgs(TypedDict, total=False):
auth: httpx.Auth
follow_redirects: bool


_T_co = TypeVar("_T_co", covariant=True)


if TYPE_CHECKING:
# This works because str.__contains__ does not accept object (either in typeshed or at runtime)
# https://github.com/hauntsaninja/useful_types/blob/5e9710f3875107d068e7679fd7fec9cfab0eff3b/useful_types/__init__.py#L285
class SequenceNotStr(Protocol[_T_co]):
@overload
def __getitem__(self, index: SupportsIndex, /) -> _T_co: ...
@overload
def __getitem__(self, index: slice, /) -> Sequence[_T_co]: ...
def __contains__(self, value: object, /) -> bool: ...
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[_T_co]: ...
def index(self, value: Any, start: int = 0, stop: int = ..., /) -> int: ...
def count(self, value: Any, /) -> int: ...
def __reversed__(self) -> Iterator[_T_co]: ...
else:
# just point this to a normal `Sequence` at runtime to avoid having to special case
# deserializing our custom sequence type
SequenceNotStr = Sequence
1 change: 1 addition & 0 deletions src/gradient/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
extract_type_arg as extract_type_arg,
is_iterable_type as is_iterable_type,
is_required_type as is_required_type,
is_sequence_type as is_sequence_type,
is_annotated_type as is_annotated_type,
is_type_alias_type as is_type_alias_type,
strip_annotated_type as strip_annotated_type,
Expand Down
5 changes: 5 additions & 0 deletions src/gradient/_utils/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def is_list_type(typ: type) -> bool:
return (get_origin(typ) or typ) == list


def is_sequence_type(typ: type) -> bool:
origin = get_origin(typ) or typ
return origin == typing_extensions.Sequence or origin == typing.Sequence or origin == _c_abc.Sequence


def is_iterable_type(typ: type) -> bool:
"""If the given type is `typing.Iterable[T]`"""
origin = get_origin(typ) or typ
Expand Down
2 changes: 1 addition & 1 deletion src/gradient/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

__title__ = "gradient"
__version__ = "3.0.0-beta.4" # x-release-please-version
__version__ = "3.0.0-beta.5" # x-release-please-version
16 changes: 6 additions & 10 deletions src/gradient/resources/agents/chat/completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,9 @@ def create(
headers = {"Authorization": f"Bearer {self._client.agent_access_key}", **headers}

return self._post(
(
"/chat/completions?agent=true"
if self._client._base_url_overridden
else f"{self._client.agent_endpoint}/api/v1/chat/completions?agent=true"
),
"/chat/completions?agent=true"
if self._client._base_url_overridden
else f"{self._client.agent_endpoint}/api/v1/chat/completions?agent=true",
body=maybe_transform(
{
"messages": messages,
Expand Down Expand Up @@ -960,11 +958,9 @@ async def create(
headers = {"Authorization": f"Bearer {self._client.agent_access_key}", **headers}

return await self._post(
(
"/chat/completions?agent=true"
if self._client._base_url_overridden
else f"{self._client.agent_endpoint}/api/v1/chat/completions?agent=true"
),
"/chat/completions?agent=true"
if self._client._base_url_overridden
else f"{self._client.agent_endpoint}/api/v1/chat/completions?agent=true",
body=await async_maybe_transform(
{
"messages": messages,
Expand Down
Loading