-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add support for labels on generate content requests #1
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
JamesMcMinn
merged 4 commits into
main
from
add-ability-to-add-labels-to-vertex-calls-in-genai-utils-ai-1649
Dec 8, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b0f1a32
feat: add support for labels on generate content requests
JamesMcMinn 45125da
keep mypy and the formatter happy
JamesMcMinn 6650e5d
tests: add tests for label parsing and validation
JamesMcMinn f4d30a8
ci: replace ff_release with public actions
JamesMcMinn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,8 +51,18 @@ jobs: | |
| build: | ||
| needs: test | ||
| runs-on: ubuntu-latest | ||
| if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev' | ||
| steps: | ||
| - name: Build & Release | ||
| uses: FullFact/ff_release@v2 | ||
| - name: Bump version and push tag | ||
| id: tag_version | ||
| uses: mathieudutour/[email protected] | ||
| with: | ||
| docker_build: false | ||
| github_token: ${{ secrets.GITHUB_TOKEN }} | ||
| release_branches: main | ||
| pre_release_branches: dev | ||
| - name: Create a GitHub release | ||
| uses: ncipollo/release-action@v1 | ||
| with: | ||
| tag: ${{ steps.tag_version.outputs.new_tag }} | ||
| name: Release ${{ steps.tag_version.outputs.new_tag }} | ||
| body: ${{ steps.tag_version.outputs.changelog }} | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| import os | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| import pytest | ||
| from google.genai import Client | ||
| from google.genai.models import Models | ||
|
|
||
| from genai_utils.gemini import GeminiError, ModelConfig, run_prompt, validate_labels | ||
|
|
||
|
|
||
| class DummyResponse: | ||
| candidates = "yes!" | ||
| text = "response!" | ||
|
|
||
|
|
||
| def get_dummy(): | ||
| return DummyResponse() | ||
|
|
||
|
|
||
| def test_validate_labels_valid(): | ||
| """Test that valid labels pass validation""" | ||
| valid_labels = { | ||
| "team": "ai", | ||
| "project": "genai-utils", | ||
| "environment": "production", | ||
| "version": "1-2-3", | ||
| "my_label": "my_value", | ||
| } | ||
| # Should not raise any exception | ||
| validate_labels(valid_labels) | ||
|
|
||
|
|
||
| def test_validate_labels_empty_key(): | ||
| """Test that empty keys are rejected""" | ||
| with pytest.raises(GeminiError, match="cannot be empty"): | ||
| validate_labels({"": "value"}) | ||
|
|
||
|
|
||
| def test_validate_labels_key_too_long(): | ||
| """Test that keys exceeding 63 characters are rejected""" | ||
| long_key = "a" * 64 | ||
| with pytest.raises(GeminiError, match="exceeds 63 characters"): | ||
| validate_labels({long_key: "value"}) | ||
|
|
||
|
|
||
| def test_validate_labels_value_too_long(): | ||
| """Test that values exceeding 63 characters are rejected""" | ||
| long_value = "a" * 64 | ||
| with pytest.raises(GeminiError, match="exceeds 63 characters"): | ||
| validate_labels({"key": long_value}) | ||
|
|
||
|
|
||
| def test_validate_labels_key_starts_with_number(): | ||
| """Test that keys starting with numbers are rejected""" | ||
| with pytest.raises(GeminiError, match="must start with a lowercase letter"): | ||
| validate_labels({"1key": "value"}) | ||
|
|
||
|
|
||
| def test_validate_labels_key_starts_with_uppercase(): | ||
| """Test that keys starting with uppercase are rejected""" | ||
| with pytest.raises(GeminiError, match="must start with a lowercase letter"): | ||
| validate_labels({"Key": "value"}) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "invalid_key", ["key@value", "key.name", "key$", "key with space", "key/name"] | ||
| ) | ||
| def test_validate_labels_key_invalid_characters(invalid_key): | ||
| """Test that keys with invalid characters are rejected""" | ||
| with pytest.raises(GeminiError, match="contains invalid characters"): | ||
| validate_labels({invalid_key: "value"}) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "invalid_value", ["value@", "value.txt", "value$", "value with space", "value/"] | ||
| ) | ||
| def test_validate_labels_value_invalid_characters(invalid_value): | ||
| """Test that values with invalid characters are rejected""" | ||
| with pytest.raises(GeminiError, match="contains invalid characters"): | ||
| validate_labels({"key": invalid_value}) | ||
|
|
||
|
|
||
| def test_validate_labels_max_length_valid(): | ||
| """Test that keys and values at exactly 63 characters are valid""" | ||
| max_key = "a" * 63 | ||
| max_value = "b" * 63 | ||
| # Should not raise any exception | ||
| validate_labels({max_key: max_value}) | ||
|
|
||
|
|
||
| def test_validate_labels_valid_special_chars(): | ||
| """Test that valid special characters (hyphens, underscores) are accepted""" | ||
| valid_labels = { | ||
| "my-key": "my-value", | ||
| "my_key": "my_value", | ||
| "my-key_name": "my-value_123", | ||
| "key123": "value456", | ||
| } | ||
| # Should not raise any exception | ||
| validate_labels(valid_labels) | ||
|
|
||
|
|
||
| @patch("genai_utils.gemini.genai.Client") | ||
| def test_run_prompt_with_valid_labels(mock_client): | ||
| """Test that run_prompt accepts and uses valid labels""" | ||
| client = Mock(Client) | ||
| models = Mock(Models) | ||
|
|
||
| models.generate_content.return_value = get_dummy() | ||
| client.models = models | ||
| mock_client.return_value = client | ||
|
|
||
| labels = {"team": "ai", "project": "test"} | ||
|
|
||
| run_prompt( | ||
| "test prompt", | ||
| labels=labels, | ||
| model_config=ModelConfig( | ||
| project="project", location="location", model_name="model" | ||
| ), | ||
| ) | ||
|
|
||
| # Verify the call was made | ||
| assert models.generate_content.called | ||
| call_kwargs = models.generate_content.call_args[1] | ||
| assert "config" in call_kwargs | ||
| assert call_kwargs["config"].labels == labels | ||
|
|
||
|
|
||
| @patch("genai_utils.gemini.genai.Client") | ||
| def test_run_prompt_with_invalid_labels(mock_client): | ||
| """Test that run_prompt rejects invalid labels""" | ||
| client = Mock(Client) | ||
| models = Mock(Models) | ||
|
|
||
| models.generate_content.return_value = get_dummy() | ||
| client.models = models | ||
| mock_client.return_value = client | ||
|
|
||
| invalid_labels = {"Invalid": "value"} # uppercase key | ||
|
|
||
| with pytest.raises(GeminiError, match="must start with a lowercase letter"): | ||
| run_prompt( | ||
| "test prompt", | ||
| labels=invalid_labels, | ||
| model_config=ModelConfig( | ||
| project="project", location="location", model_name="model" | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| @patch("genai_utils.gemini.genai.Client") | ||
| @patch.dict(os.environ, {"GENAI_LABEL_TEAM": "ai", "GENAI_LABEL_ENV": "test"}) | ||
| def test_run_prompt_merges_env_labels(mock_client): | ||
| """Test that run_prompt merges environment labels with request labels""" | ||
| # Need to reload the module to pick up the new environment variables | ||
| import importlib | ||
|
|
||
| import genai_utils.gemini | ||
|
|
||
| importlib.reload(genai_utils.gemini) | ||
|
|
||
| client = Mock(Client) | ||
| models = Mock(Models) | ||
|
|
||
| models.generate_content.return_value = get_dummy() | ||
| client.models = models | ||
| mock_client.return_value = client | ||
|
|
||
| request_labels = {"project": "test"} | ||
|
|
||
| genai_utils.gemini.run_prompt( | ||
| "test prompt", | ||
| labels=request_labels, | ||
| model_config=ModelConfig( | ||
| project="project", location="location", model_name="model" | ||
| ), | ||
| ) | ||
|
|
||
| # Verify the call was made with merged labels | ||
| assert models.generate_content.called | ||
| call_kwargs = models.generate_content.call_args[1] | ||
| assert "config" in call_kwargs | ||
|
|
||
| # Should contain both env labels (team, env) and request label (project) | ||
| merged_labels = call_kwargs["config"].labels | ||
| assert "team" in merged_labels | ||
| assert "env" in merged_labels | ||
| assert "project" in merged_labels |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so you can either set labels as os variables, or as arguments? I think that makes sense/is good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah - the idea being that we might have some shared code and don't necessarily want to have to faff around adding logic to set the correct label for services X and Y if we can just bake them into the Dockerfile or Kubernetes config. The main thinking here is that it's easy to make sure new and existing services get e.g.
GENAI_LABEL_SERVICEset, so we can track spend without having to add code to each project.