Skip to content

Conversation

yishan-pu
Copy link
Contributor

Description

Continuous tuning accepts a model resource name as the base_model and an optional checkpoint ID to run continuous tuning from.

Fixes #

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Checklist

@yishan-pu yishan-pu requested review from a team as code owners September 27, 2025 01:30
Copy link

snippet-bot bot commented Sep 27, 2025

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Sep 27, 2025
Copy link
Contributor

Summary of Changes

Hello @yishan-pu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request delivers a new code sample for continuous tuning of generative AI models, showcasing how to leverage the Google GenAI SDK to create and monitor tuning jobs. It enables users to specify a base model and an optional checkpoint ID for fine-tuning, ensuring the sample is functional and reliable through an updated SDK dependency and a dedicated unit test.

Highlights

  • New Continuous Tuning Sample: Introduces a new Python code sample (continuous_tuning_create.py) demonstrating how to initiate a continuous tuning job for generative AI models, accepting a base model resource name and an optional checkpoint ID.
  • SDK Version Update: Updates the google-genai SDK dependency from version 1.30.0 to 1.39.1 in requirements.txt.
  • Unit Test for Continuous Tuning: Adds a new unit test (test_continuous_tuning_create) in test_tuning_examples.py to verify the functionality of the new continuous tuning sample.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new code sample for continuous tuning in Google GenAI. The changes include a new script continuous_tuning_create.py with the sample logic, an update to the google-genai dependency, and a new test case. My review focuses on improving code quality and test correctness. I've suggested moving imports and constants to the module level for better structure, renaming a parameter for clarity, and significantly improving the new test case to correctly cover the polling logic in the sample code.

Comment on lines +312 to +329
@patch("google.genai.Client")
def test_continuous_tuning_create(mock_genai_client: MagicMock) -> None:
# Mock the API response
mock_tuning_job = types.TuningJob(
name="test-tuning-job",
experiment="test-experiment",
tuned_model=types.TunedModel(
model="test-model-2",
endpoint="test-endpoint"
)
)
mock_genai_client.return_value.tunings.tune.return_value = mock_tuning_job

response = continuous_tuning_create.create_continuous_tuning_job(tuned_model_name="test-model", checkpoint_id="1")

mock_genai_client.assert_called_once_with(http_options=types.HttpOptions(api_version="v1beta1"))
mock_genai_client.return_value.tunings.tune.assert_called_once()
assert response == "test-tuning-job"
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This test doesn't fully cover the functionality of create_continuous_tuning_job. The polling while loop, which includes calls to client.tunings.get() and time.sleep(), is not being tested because the initial mocked TuningJob doesn't have a state that would cause the loop to execute.

To properly test the polling logic, you should:

  1. Patch time.sleep to prevent test delays.
  2. Mock client.tunings.get() to return a sequence of TuningJob objects that simulate the job's state transitioning from a running state to a completed state.

Here is a more complete test case that replaces the current one:

from unittest.mock import patch, MagicMock

@patch("continuous_tuning_create.time.sleep")
@patch("google.genai.Client")
def test_continuous_tuning_create(mock_genai_client: MagicMock, mock_sleep: MagicMock) -> None:
    # Mock the API response for the initial tune call
    mock_initial_job = types.TuningJob(
        name="test-tuning-job",
        state="JOB_STATE_PENDING",
    )
    mock_genai_client.return_value.tunings.tune.return_value = mock_initial_job

    # Mock the responses for the polling `get` call
    mock_running_job = types.TuningJob(
        name="test-tuning-job",
        state="JOB_STATE_RUNNING",
    )
    mock_succeeded_job = types.TuningJob(
        name="test-tuning-job",
        state="JOB_STATE_SUCCEEDED",
        experiment="test-experiment",
        tuned_model=types.TunedModel(
            model="test-model-2",
            endpoint="test-endpoint",
            checkpoints=[]
        )
    )
    mock_genai_client.return_value.tunings.get.side_effect = [
        mock_running_job, 
        mock_succeeded_job
    ]

    response = continuous_tuning_create.create_continuous_tuning_job(tuned_model_name="test-model", checkpoint_id="1")

    mock_genai_client.assert_called_once_with(http_options=types.HttpOptions(api_version="v1beta1"))
    mock_genai_client.return_value.tunings.tune.assert_called_once()
    
    # Assert that polling happened
    assert mock_genai_client.return_value.tunings.get.call_count == 2
    mock_sleep.assert_called_with(60)
    assert mock_sleep.call_count == 2

    assert response == "test-tuning-job"

# limitations under the License.


def create_continuous_tuning_job(tuned_model_name: str, checkpoint_id: str) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The parameter name tuned_model_name is confusing because it's used as the base_model on line 37. Renaming it to base_model_name would make the function's purpose clearer and improve readability. You will also need to update its usage on line 37.

Suggested change
def create_continuous_tuning_job(tuned_model_name: str, checkpoint_id: str) -> str:
def create_continuous_tuning_job(base_model_name: str, checkpoint_id: str) -> str:

Comment on lines +18 to +21
import time

from google import genai
from google.genai.types import HttpOptions, TuningDataset, CreateTuningJobConfig
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

According to PEP 8, imports should be at the top of the file, outside of any functions.1 While this pattern is sometimes used in code samples for self-containment, moving the imports to the module level improves readability and is a standard Python convention.

Style Guide References

Footnotes

  1. PEP 8 recommends that imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

Comment on lines +46 to +49
running_states = set([
"JOB_STATE_PENDING",
"JOB_STATE_RUNNING",
])
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This set of running states is a constant. It's better to define it at the module level (outside the function) with an all-caps name, like RUNNING_STATES, as per PEP 8 conventions for constants.1 This improves readability and avoids recreating the set on each function call. You would then update line 51 to use this new constant.

Style Guide References

Footnotes

  1. PEP 8 suggests that constants are usually defined on a module level and written in all capital letters with underscores separating words.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant