Skip to content
12 changes: 10 additions & 2 deletions genai/tuning/test_tuning_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
# limitations under the License.

from datetime import datetime as dt

from unittest.mock import call, MagicMock, patch

from google.cloud import storage
from google.genai import types
import pytest

import preference_tuning_job_create
import pytest
import tuning_job_cancel
import tuning_job_create
import tuning_job_get
import tuning_job_list
Expand Down Expand Up @@ -89,6 +89,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
29 changes: 29 additions & 0 deletions genai/tuning/tuning_job_cancel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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]