Repository navigation
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
Merged
Evan Mattson (moonbox3)
merged 26 commits into
microsoft:main
from
markmcd:python-google-user-agent
Sep 9, 2026
Merged
Changes from all commits
Commits
Show all changes
26 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 9ea65ae
Merge branch 'main' into python-google-user-agent
markmcd 26fc208
Merge branch 'main' into python-google-user-agent
markmcd e83910d
fix: remove AIza from tests, add vertex test
markmcd 4933641
Merge branch 'main' into python-google-user-agent
markmcd 62f9ed7
Merge branch 'main' into python-google-user-agent
markmcd 10d97af
Merge branch 'main' into python-google-user-agent
markmcd 18a1255
Merge branch 'main' into python-google-user-agent
markmcd b5cfec3
Merge branch 'main' into python-google-user-agent
markmcd 9552739
Merge branch 'main' into python-google-user-agent
markmcd dd3efa9
Merge branch 'main' into python-google-user-agent
markmcd 25908b7
Merge branch 'main' into python-google-user-agent
markmcd 8be4a3d
Merge branch 'main' into python-google-user-agent
markmcd f6fa1cc
Merge branch 'main' into python-google-user-agent
markmcd 17b2a87
Merge branch 'main' into python-google-user-agent
markmcd c6efe51
Merge branch 'main' into python-google-user-agent
markmcd f05e2f0
Merge branch 'main' into python-google-user-agent
markmcd ab241f2
Merge branch 'main' into python-google-user-agent
markmcd 30c1fb9
Merge branch 'main' into python-google-user-agent
markmcd a56412b
re-format
markmcd 930e42d
fix: remove Any types, use genai.types
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
172 changes: 172 additions & 0 deletions
172
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,172 @@ | ||
| # 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="test-api-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_vertex_ai_user_agent(google_ai_unit_test_env): | ||
| """Test that GoogleAIChatCompletion sends the User-Agent header with Vertex AI.""" | ||
| 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", | ||
| use_vertexai=True, | ||
| project_id="test-project", | ||
| region="test-region", | ||
| ) | ||
|
|
||
| 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] | ||
|
|
||
|
|
||
|
markmcd marked this conversation as resolved.
|
||
| 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="test-api-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.
|
||
|
|
||
|
|
||
| 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="test-api-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="test-api-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="test-api-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="test-api-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.