Skip to content
Open
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
8 changes: 7 additions & 1 deletion src/gradient/_base_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import sys
import json
import time
Expand Down Expand Up @@ -678,7 +679,12 @@ def user_agent(self) -> str:
# Format: "Gradient/package/version"
package = self._user_agent_package or "Python"
version = self._user_agent_version if self._user_agent_package and self._user_agent_version else self._version
return f"{self.__class__.__name__}/{package}/{version}"
base_agent = f"{self.__class__.__name__}/{package}/{version}"

deployment_uuid = os.environ.get("AGENT_WORKSPACE_DEPLOYMENT_UUID")
if deployment_uuid:
return f"{base_agent}/GRADIENT_ADK_AGENT/{deployment_uuid}"
return base_agent

@property
def base_url(self) -> URL:
Expand Down
50 changes: 46 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,9 @@ class Model(BaseModel):
],
)
@mock.patch("time.time", mock.MagicMock(return_value=1696004797))
def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str, timeout: float) -> None:
def test_parse_retry_after_header(
self, remaining_retries: int, retry_after: str, timeout: float
) -> None:
client = Gradient(
base_url=base_url,
access_token=access_token,
Expand All @@ -956,7 +958,9 @@ def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str

headers = httpx.Headers({"retry-after": retry_after})
options = FinalRequestOptions(method="get", url="/foo", max_retries=3)
calculated = client._calculate_retry_timeout(remaining_retries, options, headers)
calculated = client._calculate_retry_timeout(
remaining_retries, options, headers
)
assert calculated == pytest.approx(timeout, rel=0.5 * 0.875) # type: ignore[misc]

@mock.patch(
Expand Down Expand Up @@ -1180,6 +1184,40 @@ def test_follow_redirects_disabled(
assert exc_info.value.response.status_code == 302
assert exc_info.value.response.headers["Location"] == f"{base_url}/redirected"

def test_user_agent_includes_adk_agent_when_env_var_set(self) -> None:
# Test that user agent includes GRADIENT_ADK_AGENT when AGENT_WORKSPACE_DEPLOYMENT_UUID is set
test_uuid = "test-deployment-uuid-12345"

with update_env(AGENT_WORKSPACE_DEPLOYMENT_UUID=test_uuid):
client = Gradient(
base_url=base_url,
access_token=access_token,
model_access_key=model_access_key,
agent_access_key=agent_access_key,
_strict_response_validation=True,
)
user_agent = client.user_agent
assert "GRADIENT_ADK_AGENT" in user_agent
assert test_uuid in user_agent
assert user_agent.endswith(f"/GRADIENT_ADK_AGENT/{test_uuid}")
client.close()

def test_user_agent_excludes_adk_agent_when_env_var_not_set(self) -> None:
# Test that user agent does not include GRADIENT_ADK_AGENT when env var is not set
from gradient._types import Omit

with update_env(AGENT_WORKSPACE_DEPLOYMENT_UUID=Omit()):
client = Gradient(
base_url=base_url,
access_token=access_token,
model_access_key=model_access_key,
agent_access_key=agent_access_key,
_strict_response_validation=True,
)
user_agent = client.user_agent
assert "GRADIENT_ADK_AGENT" not in user_agent
client.close()


class TestAsyncGradient:
@pytest.mark.respx(base_url=base_url)
Expand Down Expand Up @@ -2081,7 +2119,9 @@ class Model(BaseModel):
)
@mock.patch("time.time", mock.MagicMock(return_value=1696004797))
@pytest.mark.asyncio
async def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str, timeout: float) -> None:
async def test_parse_retry_after_header(
self, remaining_retries: int, retry_after: str, timeout: float
) -> None:
async_client = AsyncGradient(
base_url=base_url,
access_token=access_token,
Expand All @@ -2092,7 +2132,9 @@ async def test_parse_retry_after_header(self, remaining_retries: int, retry_afte

headers = httpx.Headers({"retry-after": retry_after})
options = FinalRequestOptions(method="get", url="/foo", max_retries=3)
calculated = async_client._calculate_retry_timeout(remaining_retries, options, headers)
calculated = async_client._calculate_retry_timeout(
remaining_retries, options, headers
)
assert calculated == pytest.approx(timeout, rel=0.5 * 0.875) # type: ignore[misc]

@mock.patch(
Expand Down