-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Python: Add User-Agent to google-genai #13703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
markmcd
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
markmcd:python-google-user-agent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
59d4cab
Python: Conditionally add semantic-kernel User-Agent to google-genai …
markmcd 34c41e7
fix: test coverage, fewer headers
markmcd 27375dd
Merge branch 'main' into python-google-user-agent
markmcd 4586db3
chore: use IS_TELEMETRY_ENABLED instead of APP_INFO
markmcd 1f27ed5
Merge branch 'main' into python-google-user-agent
markmcd ceeb985
Merge branch 'main' into python-google-user-agent
markmcd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
python/tests/unit/connectors/ai/google/google_ai/services/test_google_ai_user_agent.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| from unittest.mock import AsyncMock, patch | ||
|
|
||
| from semantic_kernel.connectors.ai.google.google_ai.google_ai_prompt_execution_settings import ( | ||
| GoogleAIChatPromptExecutionSettings, | ||
| GoogleAIEmbeddingPromptExecutionSettings, | ||
| GoogleAITextPromptExecutionSettings, | ||
| ) | ||
| from semantic_kernel.connectors.ai.google.google_ai.services.google_ai_chat_completion import GoogleAIChatCompletion | ||
| from semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_completion import GoogleAITextCompletion | ||
| from semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_embedding import GoogleAITextEmbedding | ||
| from semantic_kernel.const import USER_AGENT | ||
| from semantic_kernel.contents.chat_history import ChatHistory | ||
|
|
||
|
|
||
| async def test_google_ai_chat_completion_user_agent(google_ai_unit_test_env): | ||
| """Test that GoogleAIChatCompletion sends the User-Agent header.""" | ||
| chat_history = ChatHistory() | ||
| chat_history.add_user_message("hi") | ||
|
|
||
| with patch( | ||
| "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_chat_completion.Client" | ||
| ) as mock_client: | ||
| mock_instance = mock_client.return_value.__enter__.return_value | ||
| mock_instance.aio.models.generate_content = AsyncMock() | ||
|
|
||
| service = GoogleAIChatCompletion(gemini_model_id="gemini-3-flash-preview", api_key="AIza-test-key") | ||
|
|
||
| await service.get_chat_message_contents( | ||
| chat_history=chat_history, settings=GoogleAIChatPromptExecutionSettings() | ||
| ) | ||
|
|
||
| _, kwargs = mock_client.call_args | ||
| assert "http_options" in kwargs | ||
| assert kwargs["http_options"] is not None | ||
| assert "headers" in kwargs["http_options"] | ||
| assert USER_AGENT in kwargs["http_options"]["headers"] | ||
| assert "semantic-kernel-python" in kwargs["http_options"]["headers"][USER_AGENT] | ||
|
|
||
|
|
||
| async def test_google_ai_chat_completion_no_telemetry(google_ai_unit_test_env): | ||
| """Test that GoogleAIChatCompletion does not send the User-Agent header when telemetry is disabled.""" | ||
| chat_history = ChatHistory() | ||
| chat_history.add_user_message("hi") | ||
|
|
||
| with ( | ||
| patch("semantic_kernel.connectors.ai.google.google_ai.services.google_ai_base.IS_TELEMETRY_ENABLED", False), | ||
| patch( | ||
| "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_chat_completion.Client" | ||
| ) as mock_client, | ||
| ): | ||
| mock_instance = mock_client.return_value.__enter__.return_value | ||
| mock_instance.aio.models.generate_content = AsyncMock() | ||
|
|
||
| service = GoogleAIChatCompletion(gemini_model_id="gemini-3-flash-preview", api_key="AIza-test-key") | ||
|
|
||
| await service.get_chat_message_contents( | ||
| chat_history=chat_history, settings=GoogleAIChatPromptExecutionSettings() | ||
| ) | ||
|
|
||
| _, kwargs = mock_client.call_args | ||
| assert "http_options" in kwargs | ||
| assert kwargs["http_options"] is None | ||
markmcd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| async def test_google_ai_text_completion_user_agent(google_ai_unit_test_env): | ||
| """Test that GoogleAITextCompletion sends the User-Agent header.""" | ||
| with patch( | ||
| "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_completion.Client" | ||
| ) as mock_client: | ||
| mock_instance = mock_client.return_value.__enter__.return_value | ||
| mock_instance.aio.models.generate_content = AsyncMock() | ||
|
|
||
| service = GoogleAITextCompletion(gemini_model_id="gemini-3-flash-preview", api_key="AIza-test-key") | ||
|
|
||
| await service.get_text_contents(prompt="hi", settings=GoogleAITextPromptExecutionSettings()) | ||
|
|
||
| _, kwargs = mock_client.call_args | ||
| assert "http_options" in kwargs | ||
| assert kwargs["http_options"] is not None | ||
| assert "headers" in kwargs["http_options"] | ||
| assert USER_AGENT in kwargs["http_options"]["headers"] | ||
| assert "semantic-kernel-python" in kwargs["http_options"]["headers"][USER_AGENT] | ||
|
|
||
|
|
||
| async def test_google_ai_text_completion_no_telemetry(google_ai_unit_test_env): | ||
| """Test that GoogleAITextCompletion does not send the User-Agent header when telemetry is disabled.""" | ||
| with ( | ||
| patch("semantic_kernel.connectors.ai.google.google_ai.services.google_ai_base.IS_TELEMETRY_ENABLED", False), | ||
| patch( | ||
| "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_completion.Client" | ||
| ) as mock_client, | ||
| ): | ||
| mock_instance = mock_client.return_value.__enter__.return_value | ||
| mock_instance.aio.models.generate_content = AsyncMock() | ||
|
|
||
| service = GoogleAITextCompletion(gemini_model_id="gemini-3-flash-preview", api_key="AIza-test-key") | ||
|
|
||
| await service.get_text_contents(prompt="hi", settings=GoogleAITextPromptExecutionSettings()) | ||
|
|
||
| _, kwargs = mock_client.call_args | ||
| assert "http_options" in kwargs | ||
| assert kwargs["http_options"] is None | ||
|
|
||
|
|
||
| async def test_google_ai_text_embedding_user_agent(google_ai_unit_test_env): | ||
| """Test that GoogleAITextEmbedding sends the User-Agent header.""" | ||
| with patch( | ||
| "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_embedding.Client" | ||
| ) as mock_client: | ||
| mock_instance = mock_client.return_value.__enter__.return_value | ||
| mock_instance.aio.models.embed_content = AsyncMock() | ||
|
|
||
| service = GoogleAITextEmbedding(embedding_model_id="gemini-embedding-2-preview", api_key="AIza-test-key") | ||
|
|
||
| await service.generate_embeddings(texts=["hi"], settings=GoogleAIEmbeddingPromptExecutionSettings()) | ||
|
|
||
| _, kwargs = mock_client.call_args | ||
| assert "http_options" in kwargs | ||
| assert kwargs["http_options"] is not None | ||
| assert "headers" in kwargs["http_options"] | ||
| assert USER_AGENT in kwargs["http_options"]["headers"] | ||
| assert "semantic-kernel-python" in kwargs["http_options"]["headers"][USER_AGENT] | ||
|
|
||
|
|
||
| async def test_google_ai_text_embedding_no_telemetry(google_ai_unit_test_env): | ||
| """Test that GoogleAITextEmbedding does not send the User-Agent header when telemetry is disabled.""" | ||
| with ( | ||
| patch("semantic_kernel.connectors.ai.google.google_ai.services.google_ai_base.IS_TELEMETRY_ENABLED", False), | ||
| patch( | ||
| "semantic_kernel.connectors.ai.google.google_ai.services.google_ai_text_embedding.Client" | ||
| ) as mock_client, | ||
| ): | ||
| mock_instance = mock_client.return_value.__enter__.return_value | ||
| mock_instance.aio.models.embed_content = AsyncMock() | ||
|
|
||
| service = GoogleAITextEmbedding(embedding_model_id="gemini-embedding-2-preview", api_key="AIza-test-key") | ||
|
|
||
| await service.generate_embeddings(texts=["hi"], settings=GoogleAIEmbeddingPromptExecutionSettings()) | ||
|
|
||
| _, kwargs = mock_client.call_args | ||
| assert "http_options" in kwargs | ||
| assert kwargs["http_options"] is None | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.