Tune a pre-trained code generation model.

## 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.md.txt).

```python
import os

from google import genai

# TODO (Developer) Set environment variables
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
LOCATION_ID = os.getenv("LOCATION_ID", "us-central1")

# Resource format: 'publishers/google/models/{model_id}'
BASE_MODEL_RESOURCE = "publishers/google/models/gemini-2.5-flash"
TRAINING_DATASET = (
    "gs://cloud-samples-data/ai-platform/generative_ai/gemini/text/sft_train_data.jsonl"
)


def tune_code_generation_model() -> genai.types.TuningJob:
    """Submits a supervised fine-tuning job for a Gemini model on code/text tasks."""

    client = genai.Client(
        enterprise=True,
        project=PROJECT_ID,
        location=LOCATION_ID,
    )

    tuning_job = client.tunings.tune(
        base_model=BASE_MODEL_RESOURCE,
        training_dataset=genai.types.TuningDataset(
            gcs_uri=TRAINING_DATASET,
        ),
        config=genai.types.CreateTuningJobConfig(
            tuned_model_display_name="tuned_gemini_code_model",
            epoch_count=2,
            learning_rate_multiplier=1.0,
        ),
    )

    print(f"Tuning job submitted successfully: {tuning_job.name}")
    print(f"Current State: {tuning_job.state}")

    return tuning_job

```

## 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.md.txt?product=googlegenaisdk).