Skip to content
Draft
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
26 changes: 19 additions & 7 deletions genai/tuning/test_tuning_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
# limitations under the License.

from datetime import datetime as dt

from unittest.mock import call, MagicMock, patch

from google.api_core import exceptions
from google.cloud import storage
from google.genai import types
import preference_tuning_job_create
import pytest

import preference_tuning_job_create
import tuning_job_cancel
import tuning_job_create
import tuning_job_get
import tuning_job_list
Expand All @@ -42,11 +43,14 @@ def output_gcs_uri() -> str:

yield f"gs://{GCS_OUTPUT_BUCKET}/{prefix}"

storage_client = storage.Client()
bucket = storage_client.get_bucket(GCS_OUTPUT_BUCKET)
blobs = bucket.list_blobs(prefix=prefix)
for blob in blobs:
blob.delete()
try:
storage_client = storage.Client()
bucket = storage_client.get_bucket(GCS_OUTPUT_BUCKET)
blobs = bucket.list_blobs(prefix=prefix)
for blob in blobs:
blob.delete()
except exceptions.GoogleAPIError:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You prooooobably should be even more specific, i think there are errors for stuff like NotFound, and that kind of stuff, which is why im guessing is you are trying to do with the delete blob.

Now, for importing exceptions, it should be like this:

import google.api_core.exceptions
and you should use the complete path:
except google.api_core.exceptions.NotFound a except_name: ...

pass
Comment thread
Kef131 marked this conversation as resolved.
Comment thread
Kef131 marked this conversation as resolved.


@patch("google.genai.Client")
Expand Down Expand Up @@ -89,6 +93,14 @@ def test_tuning_job_get(mock_genai_client: MagicMock) -> None:
assert response == "test-tuning-job"


@patch("google.genai.Client")
def test_tuning_job_cancel(mock_genai_client: MagicMock) -> None:
tuning_job_cancel.cancel_tuning_job("test-tuning-job")

mock_genai_client.assert_called_once_with(http_options=types.HttpOptions(api_version="v1"))
mock_genai_client.return_value.tunings.cancel.assert_called_once_with(name="test-tuning-job")


@patch("google.genai.Client")
def test_tuning_job_list(mock_genai_client: MagicMock) -> None:
# Mock the API response
Expand Down
27 changes: 27 additions & 0 deletions genai/tuning/tuning_job_cancel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START googlegenaisdk_tuning_job_cancel]

from google import genai
from google.genai.types import HttpOptions
def cancel_tuning_job(tuning_job_name: str) -> None:

client = genai.Client(http_options=HttpOptions(api_version="v1"))

# Cancel the tuning job.
# Eg. tuning_job_name = "projects/123456789012/locations/us-central1/tuningJobs/123456789012345"
client.tunings.cancel(name=tuning_job_name)

# [END googlegenaisdk_tuning_job_cancel]
Comment thread
Kef131 marked this conversation as resolved.