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
16 changes: 11 additions & 5 deletions google/genai/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,19 @@ def append_library_version_headers(headers: dict[str, str]) -> None:
library_label = f'google-genai-sdk/{version.__version__}'
language_label = 'gl-python/' + sys.version.split()[0]
version_header_value = f'{library_label} {language_label}'
user_agent_key = next(
(key for key in headers if key.lower() == 'user-agent'),
'User-Agent',
)
if (
'user-agent' in headers
and version_header_value not in headers['user-agent']
user_agent_key in headers
and version_header_value not in headers[user_agent_key]
):
headers['user-agent'] = f'{version_header_value} ' + headers['user-agent']
elif 'user-agent' not in headers:
headers['user-agent'] = version_header_value
headers[user_agent_key] = (
f'{version_header_value} ' + headers[user_agent_key]
)
elif user_agent_key not in headers:
headers[user_agent_key] = version_header_value
if (
'x-goog-api-client' in headers
and version_header_value not in headers['x-goog-api-client']
Expand Down
2 changes: 1 addition & 1 deletion google/genai/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ def _GenerateContentConfig_to_mldev(
)

if getv(from_object, ['labels']) is not None:
raise ValueError('labels parameter is not supported in Gemini API.')
setv(parent_object, ['labels'], getv(from_object, ['labels']))

if getv(from_object, ['cached_content']) is not None:
setv(
Expand Down
14 changes: 14 additions & 0 deletions google/genai/tests/interactions/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from unittest import mock
import httpx
import pytest
from ... import client as client_lib

Expand Down Expand Up @@ -82,3 +83,16 @@ async def test_async_client_timeout():
max_retries=mock.ANY,
client_adapter=mock.ANY,
)


def test_interactions_default_headers_use_single_user_agent():
client = client_lib.Client(
api_key="placeholder",
http_options={"api_version": "v1alpha"},
)

headers = httpx.Headers(client.interactions._client.default_headers)

assert len(headers.get_list("user-agent")) == 1
assert "google-genai-sdk/" in headers["user-agent"]
assert "gl-python/" in headers["user-agent"]
15 changes: 15 additions & 0 deletions google/genai/tests/models/test_generate_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
from .. import pytest_helper
from enum import Enum

from ... import models as models_module

GEMINI_FLASH_LATEST = 'gemini-2.5-flash'
GEMINI_FLASH_2_0 = 'gemini-2.0-flash-001'
GEMINI_FLASH_IMAGE_LATEST = 'gemini-2.5-flash-image'
Expand Down Expand Up @@ -64,6 +66,19 @@ class InstrumentEnum(Enum):
KEYBOARD = 'Keyboard'


def test_generate_content_labels_are_serialized_for_mldev():
request = models_module._GenerateContentConfig_to_mldev(
{
'labels': {'purpose': 'exploration', 'environment': 'development'},
}
)

assert request['labels'] == {
'purpose': 'exploration',
'environment': 'development',
}


test_table: list[pytest_helper.TestTableItem] = [
pytest_helper.TestTableItem(
name='test_http_options_in_method',
Expand Down