Automatically tune a Gemini model using Google Cloud's Vertex AI SFT (Supervised Fine-tuning).

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Tune Gemini models by using supervised fine-tuning](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/tuning/supervised-tuning/use)
- [Tuning API](https://docs.cloud.google.com/gemini-enterprise-agent-platform/reference/models/tuning)

## Code sample

### Python


Before trying this sample, follow the Python setup instructions in the
[Vertex AI quickstart using
client libraries](https://docs.cloud.google.com/vertex-ai/docs/start/client-libraries).


For more information, see the
[Vertex AI Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/aiplatform/latest).


To authenticate to Vertex AI, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).


    import time

    import https://docs.cloud.google.com/python/docs/reference/vertexai/latest
    from vertexai.tuning import https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.tuning.sft.html

    # TODO(developer): Update and un-comment below line
    # PROJECT_ID = "your-project-id"
    https://docs.cloud.google.com/python/docs/reference/vertexai/latest.init(project=PROJECT_ID, location="us-central1")

    sft_tuning_job = https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.tuning.sft.html.https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai.preview.tuning.sft.html(
        source_model="gemini-2.0-flash-001",
        # 1.5 and 2.0 models use the same JSONL format
        train_dataset="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
    )

    # Polling for job completion
    while not sft_tuning_job.has_ended:
        time.sleep(60)
        sft_tuning_job.refresh()

    print(sft_tuning_job.tuned_model_name)
    print(sft_tuning_job.tuned_model_endpoint_name)
    print(sft_tuning_job.https://docs.cloud.google.com/python/docs/reference/vertexai/latest/vertexai._genai.types.CustomJobSpecDict.html#vertexai__genai_types_CustomJobSpecDict_experiment)
    # Example response:
    # projects/123456789012/locations/us-central1/models/1234567890@1
    # projects/123456789012/locations/us-central1/endpoints/123456789012345
    # <google.cloud.aiplatform.metadata.experiment_resources.Experiment object at 0x7b5b4ae07af0>

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=generativeaionvertexai).