// 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
//
//      http://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.

// Code generated by the Google Gen AI SDK generator DO NOT EDIT.

package genai

import (
	"context"
	"fmt"
	"iter"
	"log"
	"net/http"
	"strings"
	"sync"
)

func cancelTuningJobParametersToMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		InternalSetValueByPath(toObject, []string{"_url", "name"}, fromName)
	}

	return toObject, nil
}

func cancelTuningJobParametersToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		InternalSetValueByPath(toObject, []string{"_url", "name"}, fromName)
	}

	return toObject, nil
}

func cancelTuningJobResponseFromMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromSdkHttpResponse := InternalGetValueByPath(fromObject, []string{"sdkHttpResponse"})
	if fromSdkHttpResponse != nil {
		InternalSetValueByPath(toObject, []string{"sdkHttpResponse"}, fromSdkHttpResponse)
	}

	return toObject, nil
}

func cancelTuningJobResponseFromVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromSdkHttpResponse := InternalGetValueByPath(fromObject, []string{"sdkHttpResponse"})
	if fromSdkHttpResponse != nil {
		InternalSetValueByPath(toObject, []string{"sdkHttpResponse"}, fromSdkHttpResponse)
	}

	return toObject, nil
}

func createTuningJobConfigToMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	if InternalGetValueByPath(fromObject, []string{"validationDataset"}) != nil {
		return nil, fmt.Errorf("validationDataset parameter is not supported in Gemini API")
	}

	fromTunedModelDisplayName := InternalGetValueByPath(fromObject, []string{"tunedModelDisplayName"})
	if fromTunedModelDisplayName != nil {
		InternalSetValueByPath(parentObject, []string{"displayName"}, fromTunedModelDisplayName)
	}

	if InternalGetValueByPath(fromObject, []string{"description"}) != nil {
		return nil, fmt.Errorf("description parameter is not supported in Gemini API")
	}

	fromEpochCount := InternalGetValueByPath(fromObject, []string{"epochCount"})
	if fromEpochCount != nil {
		InternalSetValueByPath(parentObject, []string{"tuningTask", "hyperparameters", "epochCount"}, fromEpochCount)
	}

	fromLearningRateMultiplier := InternalGetValueByPath(fromObject, []string{"learningRateMultiplier"})
	if fromLearningRateMultiplier != nil {
		InternalSetValueByPath(toObject, []string{"tuningTask", "hyperparameters", "learningRateMultiplier"}, fromLearningRateMultiplier)
	}

	if InternalGetValueByPath(fromObject, []string{"exportLastCheckpointOnly"}) != nil {
		return nil, fmt.Errorf("exportLastCheckpointOnly parameter is not supported in Gemini API")
	}

	if InternalGetValueByPath(fromObject, []string{"preTunedModelCheckpointId"}) != nil {
		return nil, fmt.Errorf("preTunedModelCheckpointId parameter is not supported in Gemini API")
	}

	if InternalGetValueByPath(fromObject, []string{"adapterSize"}) != nil {
		return nil, fmt.Errorf("adapterSize parameter is not supported in Gemini API")
	}

	if InternalGetValueByPath(fromObject, []string{"tuningMode"}) != nil {
		return nil, fmt.Errorf("tuningMode parameter is not supported in Gemini API")
	}

	if InternalGetValueByPath(fromObject, []string{"customBaseModel"}) != nil {
		return nil, fmt.Errorf("customBaseModel parameter is not supported in Gemini API")
	}

	fromBatchSize := InternalGetValueByPath(fromObject, []string{"batchSize"})
	if fromBatchSize != nil {
		InternalSetValueByPath(parentObject, []string{"tuningTask", "hyperparameters", "batchSize"}, fromBatchSize)
	}

	fromLearningRate := InternalGetValueByPath(fromObject, []string{"learningRate"})
	if fromLearningRate != nil {
		InternalSetValueByPath(parentObject, []string{"tuningTask", "hyperparameters", "learningRate"}, fromLearningRate)
	}

	if InternalGetValueByPath(fromObject, []string{"labels"}) != nil {
		return nil, fmt.Errorf("labels parameter is not supported in Gemini API")
	}

	if InternalGetValueByPath(fromObject, []string{"beta"}) != nil {
		return nil, fmt.Errorf("beta parameter is not supported in Gemini API")
	}

	if InternalGetValueByPath(fromObject, []string{"baseTeacherModel"}) != nil {
		return nil, fmt.Errorf("baseTeacherModel parameter is not supported in Gemini API")
	}

	if InternalGetValueByPath(fromObject, []string{"tunedTeacherModelSource"}) != nil {
		return nil, fmt.Errorf("tunedTeacherModelSource parameter is not supported in Gemini API")
	}

	if InternalGetValueByPath(fromObject, []string{"sftLossWeightMultiplier"}) != nil {
		return nil, fmt.Errorf("sftLossWeightMultiplier parameter is not supported in Gemini API")
	}

	if InternalGetValueByPath(fromObject, []string{"outputUri"}) != nil {
		return nil, fmt.Errorf("outputUri parameter is not supported in Gemini API")
	}

	if InternalGetValueByPath(fromObject, []string{"encryptionSpec"}) != nil {
		return nil, fmt.Errorf("encryptionSpec parameter is not supported in Gemini API")
	}

	return toObject, nil
}

func createTuningJobConfigToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	var discriminatorValidationDataset any = InternalGetValueByPath(rootObject, []string{"config", "method"})
	if discriminatorValidationDataset == nil {
		discriminatorValidationDataset = "SUPERVISED_FINE_TUNING"
	}
	if discriminatorValidationDataset.(string) == "SUPERVISED_FINE_TUNING" {
		fromValidationDataset := InternalGetValueByPath(fromObject, []string{"validationDataset"})
		if fromValidationDataset != nil {
			fromValidationDataset, err = tuningValidationDatasetToVertex(fromValidationDataset.(map[string]any), toObject, rootObject)
			if err != nil {
				return nil, err
			}

			InternalSetValueByPath(parentObject, []string{"supervisedTuningSpec"}, fromValidationDataset)
		}
	} else if discriminatorValidationDataset.(string) == "PREFERENCE_TUNING" {
		fromValidationDataset := InternalGetValueByPath(fromObject, []string{"validationDataset"})
		if fromValidationDataset != nil {
			fromValidationDataset, err = tuningValidationDatasetToVertex(fromValidationDataset.(map[string]any), toObject, rootObject)
			if err != nil {
				return nil, err
			}

			InternalSetValueByPath(parentObject, []string{"preferenceOptimizationSpec"}, fromValidationDataset)
		}
	} else if discriminatorValidationDataset.(string) == "DISTILLATION" {
		fromValidationDataset := InternalGetValueByPath(fromObject, []string{"validationDataset"})
		if fromValidationDataset != nil {
			fromValidationDataset, err = tuningValidationDatasetToVertex(fromValidationDataset.(map[string]any), toObject, rootObject)
			if err != nil {
				return nil, err
			}

			InternalSetValueByPath(parentObject, []string{"distillationSpec"}, fromValidationDataset)
		}
	}

	fromTunedModelDisplayName := InternalGetValueByPath(fromObject, []string{"tunedModelDisplayName"})
	if fromTunedModelDisplayName != nil {
		InternalSetValueByPath(parentObject, []string{"tunedModelDisplayName"}, fromTunedModelDisplayName)
	}

	fromDescription := InternalGetValueByPath(fromObject, []string{"description"})
	if fromDescription != nil {
		InternalSetValueByPath(parentObject, []string{"description"}, fromDescription)
	}

	var discriminatorEpochCount any = InternalGetValueByPath(rootObject, []string{"config", "method"})
	if discriminatorEpochCount == nil {
		discriminatorEpochCount = "SUPERVISED_FINE_TUNING"
	}
	if discriminatorEpochCount.(string) == "SUPERVISED_FINE_TUNING" {
		fromEpochCount := InternalGetValueByPath(fromObject, []string{"epochCount"})
		if fromEpochCount != nil {
			InternalSetValueByPath(parentObject, []string{"supervisedTuningSpec", "hyperParameters", "epochCount"}, fromEpochCount)
		}
	} else if discriminatorEpochCount.(string) == "PREFERENCE_TUNING" {
		fromEpochCount := InternalGetValueByPath(fromObject, []string{"epochCount"})
		if fromEpochCount != nil {
			InternalSetValueByPath(parentObject, []string{"preferenceOptimizationSpec", "hyperParameters", "epochCount"}, fromEpochCount)
		}
	} else if discriminatorEpochCount.(string) == "DISTILLATION" {
		fromEpochCount := InternalGetValueByPath(fromObject, []string{"epochCount"})
		if fromEpochCount != nil {
			InternalSetValueByPath(parentObject, []string{"distillationSpec", "hyperParameters", "epochCount"}, fromEpochCount)
		}
	}

	var discriminatorLearningRateMultiplier any = InternalGetValueByPath(rootObject, []string{"config", "method"})
	if discriminatorLearningRateMultiplier == nil {
		discriminatorLearningRateMultiplier = "SUPERVISED_FINE_TUNING"
	}
	if discriminatorLearningRateMultiplier.(string) == "SUPERVISED_FINE_TUNING" {
		fromLearningRateMultiplier := InternalGetValueByPath(fromObject, []string{"learningRateMultiplier"})
		if fromLearningRateMultiplier != nil {
			InternalSetValueByPath(parentObject, []string{"supervisedTuningSpec", "hyperParameters", "learningRateMultiplier"}, fromLearningRateMultiplier)
		}
	} else if discriminatorLearningRateMultiplier.(string) == "PREFERENCE_TUNING" {
		fromLearningRateMultiplier := InternalGetValueByPath(fromObject, []string{"learningRateMultiplier"})
		if fromLearningRateMultiplier != nil {
			InternalSetValueByPath(parentObject, []string{"preferenceOptimizationSpec", "hyperParameters", "learningRateMultiplier"}, fromLearningRateMultiplier)
		}
	} else if discriminatorLearningRateMultiplier.(string) == "DISTILLATION" {
		fromLearningRateMultiplier := InternalGetValueByPath(fromObject, []string{"learningRateMultiplier"})
		if fromLearningRateMultiplier != nil {
			InternalSetValueByPath(parentObject, []string{"distillationSpec", "hyperParameters", "learningRateMultiplier"}, fromLearningRateMultiplier)
		}
	}

	var discriminatorExportLastCheckpointOnly any = InternalGetValueByPath(rootObject, []string{"config", "method"})
	if discriminatorExportLastCheckpointOnly == nil {
		discriminatorExportLastCheckpointOnly = "SUPERVISED_FINE_TUNING"
	}
	if discriminatorExportLastCheckpointOnly.(string) == "SUPERVISED_FINE_TUNING" {
		fromExportLastCheckpointOnly := InternalGetValueByPath(fromObject, []string{"exportLastCheckpointOnly"})
		if fromExportLastCheckpointOnly != nil {
			InternalSetValueByPath(parentObject, []string{"supervisedTuningSpec", "exportLastCheckpointOnly"}, fromExportLastCheckpointOnly)
		}
	} else if discriminatorExportLastCheckpointOnly.(string) == "PREFERENCE_TUNING" {
		fromExportLastCheckpointOnly := InternalGetValueByPath(fromObject, []string{"exportLastCheckpointOnly"})
		if fromExportLastCheckpointOnly != nil {
			InternalSetValueByPath(parentObject, []string{"preferenceOptimizationSpec", "exportLastCheckpointOnly"}, fromExportLastCheckpointOnly)
		}
	} else if discriminatorExportLastCheckpointOnly.(string) == "DISTILLATION" {
		fromExportLastCheckpointOnly := InternalGetValueByPath(fromObject, []string{"exportLastCheckpointOnly"})
		if fromExportLastCheckpointOnly != nil {
			InternalSetValueByPath(parentObject, []string{"distillationSpec", "exportLastCheckpointOnly"}, fromExportLastCheckpointOnly)
		}
	}

	var discriminatorAdapterSize any = InternalGetValueByPath(rootObject, []string{"config", "method"})
	if discriminatorAdapterSize == nil {
		discriminatorAdapterSize = "SUPERVISED_FINE_TUNING"
	}
	if discriminatorAdapterSize.(string) == "SUPERVISED_FINE_TUNING" {
		fromAdapterSize := InternalGetValueByPath(fromObject, []string{"adapterSize"})
		if fromAdapterSize != nil {
			InternalSetValueByPath(parentObject, []string{"supervisedTuningSpec", "hyperParameters", "adapterSize"}, fromAdapterSize)
		}
	} else if discriminatorAdapterSize.(string) == "PREFERENCE_TUNING" {
		fromAdapterSize := InternalGetValueByPath(fromObject, []string{"adapterSize"})
		if fromAdapterSize != nil {
			InternalSetValueByPath(parentObject, []string{"preferenceOptimizationSpec", "hyperParameters", "adapterSize"}, fromAdapterSize)
		}
	} else if discriminatorAdapterSize.(string) == "DISTILLATION" {
		fromAdapterSize := InternalGetValueByPath(fromObject, []string{"adapterSize"})
		if fromAdapterSize != nil {
			InternalSetValueByPath(parentObject, []string{"distillationSpec", "hyperParameters", "adapterSize"}, fromAdapterSize)
		}
	}

	var discriminatorTuningMode any = InternalGetValueByPath(rootObject, []string{"config", "method"})
	if discriminatorTuningMode == nil {
		discriminatorTuningMode = "SUPERVISED_FINE_TUNING"
	}
	if discriminatorTuningMode.(string) == "SUPERVISED_FINE_TUNING" {
		fromTuningMode := InternalGetValueByPath(fromObject, []string{"tuningMode"})
		if fromTuningMode != nil {
			InternalSetValueByPath(parentObject, []string{"supervisedTuningSpec", "tuningMode"}, fromTuningMode)
		}
	} else if discriminatorTuningMode.(string) == "DISTILLATION" {
		fromTuningMode := InternalGetValueByPath(fromObject, []string{"tuningMode"})
		if fromTuningMode != nil {
			InternalSetValueByPath(parentObject, []string{"distillationSpec", "tuningMode"}, fromTuningMode)
		}
	}

	fromCustomBaseModel := InternalGetValueByPath(fromObject, []string{"customBaseModel"})
	if fromCustomBaseModel != nil {
		InternalSetValueByPath(parentObject, []string{"customBaseModel"}, fromCustomBaseModel)
	}

	var discriminatorBatchSize any = InternalGetValueByPath(rootObject, []string{"config", "method"})
	if discriminatorBatchSize == nil {
		discriminatorBatchSize = "SUPERVISED_FINE_TUNING"
	}
	if discriminatorBatchSize.(string) == "SUPERVISED_FINE_TUNING" {
		fromBatchSize := InternalGetValueByPath(fromObject, []string{"batchSize"})
		if fromBatchSize != nil {
			InternalSetValueByPath(parentObject, []string{"supervisedTuningSpec", "hyperParameters", "batchSize"}, fromBatchSize)
		}
	} else if discriminatorBatchSize.(string) == "DISTILLATION" {
		fromBatchSize := InternalGetValueByPath(fromObject, []string{"batchSize"})
		if fromBatchSize != nil {
			InternalSetValueByPath(parentObject, []string{"distillationSpec", "hyperParameters", "batchSize"}, fromBatchSize)
		}
	}

	var discriminatorLearningRate any = InternalGetValueByPath(rootObject, []string{"config", "method"})
	if discriminatorLearningRate == nil {
		discriminatorLearningRate = "SUPERVISED_FINE_TUNING"
	}
	if discriminatorLearningRate.(string) == "SUPERVISED_FINE_TUNING" {
		fromLearningRate := InternalGetValueByPath(fromObject, []string{"learningRate"})
		if fromLearningRate != nil {
			InternalSetValueByPath(parentObject, []string{"supervisedTuningSpec", "hyperParameters", "learningRate"}, fromLearningRate)
		}
	} else if discriminatorLearningRate.(string) == "DISTILLATION" {
		fromLearningRate := InternalGetValueByPath(fromObject, []string{"learningRate"})
		if fromLearningRate != nil {
			InternalSetValueByPath(parentObject, []string{"distillationSpec", "hyperParameters", "learningRate"}, fromLearningRate)
		}
	}

	fromLabels := InternalGetValueByPath(fromObject, []string{"labels"})
	if fromLabels != nil {
		InternalSetValueByPath(parentObject, []string{"labels"}, fromLabels)
	}

	fromBeta := InternalGetValueByPath(fromObject, []string{"beta"})
	if fromBeta != nil {
		InternalSetValueByPath(parentObject, []string{"preferenceOptimizationSpec", "hyperParameters", "beta"}, fromBeta)
	}

	fromBaseTeacherModel := InternalGetValueByPath(fromObject, []string{"baseTeacherModel"})
	if fromBaseTeacherModel != nil {
		InternalSetValueByPath(parentObject, []string{"distillationSpec", "baseTeacherModel"}, fromBaseTeacherModel)
	}

	fromTunedTeacherModelSource := InternalGetValueByPath(fromObject, []string{"tunedTeacherModelSource"})
	if fromTunedTeacherModelSource != nil {
		InternalSetValueByPath(parentObject, []string{"distillationSpec", "tunedTeacherModelSource"}, fromTunedTeacherModelSource)
	}

	fromSftLossWeightMultiplier := InternalGetValueByPath(fromObject, []string{"sftLossWeightMultiplier"})
	if fromSftLossWeightMultiplier != nil {
		InternalSetValueByPath(parentObject, []string{"distillationSpec", "hyperParameters", "sftLossWeightMultiplier"}, fromSftLossWeightMultiplier)
	}

	fromOutputUri := InternalGetValueByPath(fromObject, []string{"outputUri"})
	if fromOutputUri != nil {
		InternalSetValueByPath(parentObject, []string{"outputUri"}, fromOutputUri)
	}

	fromEncryptionSpec := InternalGetValueByPath(fromObject, []string{"encryptionSpec"})
	if fromEncryptionSpec != nil {
		InternalSetValueByPath(parentObject, []string{"encryptionSpec"}, fromEncryptionSpec)
	}

	return toObject, nil
}

func createTuningJobParametersPrivateToMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromBaseModel := InternalGetValueByPath(fromObject, []string{"baseModel"})
	if fromBaseModel != nil {
		InternalSetValueByPath(toObject, []string{"baseModel"}, fromBaseModel)
	}

	fromPreTunedModel := InternalGetValueByPath(fromObject, []string{"preTunedModel"})
	if fromPreTunedModel != nil {
		InternalSetValueByPath(toObject, []string{"preTunedModel"}, fromPreTunedModel)
	}

	fromTrainingDataset := InternalGetValueByPath(fromObject, []string{"trainingDataset"})
	if fromTrainingDataset != nil {
		_, err = tuningDatasetToMldev(fromTrainingDataset.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	fromConfig := InternalGetValueByPath(fromObject, []string{"config"})
	if fromConfig != nil {
		_, err = createTuningJobConfigToMldev(fromConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	return toObject, nil
}

func createTuningJobParametersPrivateToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromBaseModel := InternalGetValueByPath(fromObject, []string{"baseModel"})
	if fromBaseModel != nil {
		InternalSetValueByPath(toObject, []string{"baseModel"}, fromBaseModel)
	}

	fromPreTunedModel := InternalGetValueByPath(fromObject, []string{"preTunedModel"})
	if fromPreTunedModel != nil {
		InternalSetValueByPath(toObject, []string{"preTunedModel"}, fromPreTunedModel)
	}

	fromTrainingDataset := InternalGetValueByPath(fromObject, []string{"trainingDataset"})
	if fromTrainingDataset != nil {
		_, err = tuningDatasetToVertex(fromTrainingDataset.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	fromConfig := InternalGetValueByPath(fromObject, []string{"config"})
	if fromConfig != nil {
		_, err = createTuningJobConfigToVertex(fromConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	return toObject, nil
}

func getTuningJobParametersToMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		InternalSetValueByPath(toObject, []string{"_url", "name"}, fromName)
	}

	return toObject, nil
}

func getTuningJobParametersToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		InternalSetValueByPath(toObject, []string{"_url", "name"}, fromName)
	}

	return toObject, nil
}

func listTuningJobsConfigToMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromPageSize := InternalGetValueByPath(fromObject, []string{"pageSize"})
	if fromPageSize != nil {
		InternalSetValueByPath(parentObject, []string{"_query", "pageSize"}, fromPageSize)
	}

	fromPageToken := InternalGetValueByPath(fromObject, []string{"pageToken"})
	if fromPageToken != nil {
		InternalSetValueByPath(parentObject, []string{"_query", "pageToken"}, fromPageToken)
	}

	fromFilter := InternalGetValueByPath(fromObject, []string{"filter"})
	if fromFilter != nil {
		InternalSetValueByPath(parentObject, []string{"_query", "filter"}, fromFilter)
	}

	return toObject, nil
}

func listTuningJobsConfigToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromPageSize := InternalGetValueByPath(fromObject, []string{"pageSize"})
	if fromPageSize != nil {
		InternalSetValueByPath(parentObject, []string{"_query", "pageSize"}, fromPageSize)
	}

	fromPageToken := InternalGetValueByPath(fromObject, []string{"pageToken"})
	if fromPageToken != nil {
		InternalSetValueByPath(parentObject, []string{"_query", "pageToken"}, fromPageToken)
	}

	fromFilter := InternalGetValueByPath(fromObject, []string{"filter"})
	if fromFilter != nil {
		InternalSetValueByPath(parentObject, []string{"_query", "filter"}, fromFilter)
	}

	return toObject, nil
}

func listTuningJobsParametersToMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromConfig := InternalGetValueByPath(fromObject, []string{"config"})
	if fromConfig != nil {
		_, err = listTuningJobsConfigToMldev(fromConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	return toObject, nil
}

func listTuningJobsParametersToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromConfig := InternalGetValueByPath(fromObject, []string{"config"})
	if fromConfig != nil {
		_, err = listTuningJobsConfigToVertex(fromConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	return toObject, nil
}

func listTuningJobsResponseFromMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromSdkHttpResponse := InternalGetValueByPath(fromObject, []string{"sdkHttpResponse"})
	if fromSdkHttpResponse != nil {
		InternalSetValueByPath(toObject, []string{"sdkHttpResponse"}, fromSdkHttpResponse)
	}

	fromNextPageToken := InternalGetValueByPath(fromObject, []string{"nextPageToken"})
	if fromNextPageToken != nil {
		InternalSetValueByPath(toObject, []string{"nextPageToken"}, fromNextPageToken)
	}

	fromTuningJobs := InternalGetValueByPath(fromObject, []string{"tunedModels"})
	if fromTuningJobs != nil {
		fromTuningJobs, err = applyConverterToSliceWithRoot(fromTuningJobs.([]any), tuningJobFromMldev, rootObject)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"tuningJobs"}, fromTuningJobs)
	}

	return toObject, nil
}

func listTuningJobsResponseFromVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromSdkHttpResponse := InternalGetValueByPath(fromObject, []string{"sdkHttpResponse"})
	if fromSdkHttpResponse != nil {
		InternalSetValueByPath(toObject, []string{"sdkHttpResponse"}, fromSdkHttpResponse)
	}

	fromNextPageToken := InternalGetValueByPath(fromObject, []string{"nextPageToken"})
	if fromNextPageToken != nil {
		InternalSetValueByPath(toObject, []string{"nextPageToken"}, fromNextPageToken)
	}

	fromTuningJobs := InternalGetValueByPath(fromObject, []string{"tuningJobs"})
	if fromTuningJobs != nil {
		fromTuningJobs, err = applyConverterToSliceWithRoot(fromTuningJobs.([]any), tuningJobFromVertex, rootObject)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"tuningJobs"}, fromTuningJobs)
	}

	return toObject, nil
}

func tunedModelFromMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromModel := InternalGetValueByPath(fromObject, []string{"name"})
	if fromModel != nil {
		InternalSetValueByPath(toObject, []string{"model"}, fromModel)
	}

	fromEndpoint := InternalGetValueByPath(fromObject, []string{"name"})
	if fromEndpoint != nil {
		InternalSetValueByPath(toObject, []string{"endpoint"}, fromEndpoint)
	}

	return toObject, nil
}

func tuningDatasetToMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)
	if InternalGetValueByPath(fromObject, []string{"gcsUri"}) != nil {
		return nil, fmt.Errorf("gcsUri parameter is not supported in Gemini API")
	}

	if InternalGetValueByPath(fromObject, []string{"vertexDatasetResource"}) != nil {
		return nil, fmt.Errorf("vertexDatasetResource parameter is not supported in Gemini API")
	}

	fromExamples := InternalGetValueByPath(fromObject, []string{"examples"})
	if fromExamples != nil {
		InternalSetValueByPath(toObject, []string{"examples", "examples"}, fromExamples)
	}

	return toObject, nil
}

func tuningDatasetToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	var discriminatorGcsUri any = InternalGetValueByPath(rootObject, []string{"config", "method"})
	if discriminatorGcsUri == nil {
		discriminatorGcsUri = "SUPERVISED_FINE_TUNING"
	}
	if discriminatorGcsUri.(string) == "SUPERVISED_FINE_TUNING" {
		fromGcsUri := InternalGetValueByPath(fromObject, []string{"gcsUri"})
		if fromGcsUri != nil {
			InternalSetValueByPath(parentObject, []string{"supervisedTuningSpec", "trainingDatasetUri"}, fromGcsUri)
		}
	} else if discriminatorGcsUri.(string) == "PREFERENCE_TUNING" {
		fromGcsUri := InternalGetValueByPath(fromObject, []string{"gcsUri"})
		if fromGcsUri != nil {
			InternalSetValueByPath(parentObject, []string{"preferenceOptimizationSpec", "trainingDatasetUri"}, fromGcsUri)
		}
	} else if discriminatorGcsUri.(string) == "DISTILLATION" {
		fromGcsUri := InternalGetValueByPath(fromObject, []string{"gcsUri"})
		if fromGcsUri != nil {
			InternalSetValueByPath(parentObject, []string{"distillationSpec", "promptDatasetUri"}, fromGcsUri)
		}
	}

	var discriminatorVertexDatasetResource any = InternalGetValueByPath(rootObject, []string{"config", "method"})
	if discriminatorVertexDatasetResource == nil {
		discriminatorVertexDatasetResource = "SUPERVISED_FINE_TUNING"
	}
	if discriminatorVertexDatasetResource.(string) == "SUPERVISED_FINE_TUNING" {
		fromVertexDatasetResource := InternalGetValueByPath(fromObject, []string{"vertexDatasetResource"})
		if fromVertexDatasetResource != nil {
			InternalSetValueByPath(parentObject, []string{"supervisedTuningSpec", "trainingDatasetUri"}, fromVertexDatasetResource)
		}
	} else if discriminatorVertexDatasetResource.(string) == "PREFERENCE_TUNING" {
		fromVertexDatasetResource := InternalGetValueByPath(fromObject, []string{"vertexDatasetResource"})
		if fromVertexDatasetResource != nil {
			InternalSetValueByPath(parentObject, []string{"preferenceOptimizationSpec", "trainingDatasetUri"}, fromVertexDatasetResource)
		}
	} else if discriminatorVertexDatasetResource.(string) == "DISTILLATION" {
		fromVertexDatasetResource := InternalGetValueByPath(fromObject, []string{"vertexDatasetResource"})
		if fromVertexDatasetResource != nil {
			InternalSetValueByPath(parentObject, []string{"distillationSpec", "promptDatasetUri"}, fromVertexDatasetResource)
		}
	}

	if InternalGetValueByPath(fromObject, []string{"examples"}) != nil {
		return nil, fmt.Errorf("examples parameter is not supported in Vertex AI")
	}

	return toObject, nil
}

func tuningJobFromMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromSdkHttpResponse := InternalGetValueByPath(fromObject, []string{"sdkHttpResponse"})
	if fromSdkHttpResponse != nil {
		InternalSetValueByPath(toObject, []string{"sdkHttpResponse"}, fromSdkHttpResponse)
	}

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		InternalSetValueByPath(toObject, []string{"name"}, fromName)
	}

	fromState := InternalGetValueByPath(fromObject, []string{"state"})
	if fromState != nil {
		fromState, err = InternalTTuningJobStatus(fromState)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"state"}, fromState)
	}

	fromCreateTime := InternalGetValueByPath(fromObject, []string{"createTime"})
	if fromCreateTime != nil {
		InternalSetValueByPath(toObject, []string{"createTime"}, fromCreateTime)
	}

	fromStartTime := InternalGetValueByPath(fromObject, []string{"tuningTask", "startTime"})
	if fromStartTime != nil {
		InternalSetValueByPath(toObject, []string{"startTime"}, fromStartTime)
	}

	fromEndTime := InternalGetValueByPath(fromObject, []string{"tuningTask", "completeTime"})
	if fromEndTime != nil {
		InternalSetValueByPath(toObject, []string{"endTime"}, fromEndTime)
	}

	fromUpdateTime := InternalGetValueByPath(fromObject, []string{"updateTime"})
	if fromUpdateTime != nil {
		InternalSetValueByPath(toObject, []string{"updateTime"}, fromUpdateTime)
	}

	fromDescription := InternalGetValueByPath(fromObject, []string{"description"})
	if fromDescription != nil {
		InternalSetValueByPath(toObject, []string{"description"}, fromDescription)
	}

	fromBaseModel := InternalGetValueByPath(fromObject, []string{"baseModel"})
	if fromBaseModel != nil {
		InternalSetValueByPath(toObject, []string{"baseModel"}, fromBaseModel)
	}

	fromTunedModel := InternalGetValueByPath(fromObject, []string{"_self"})
	if fromTunedModel != nil {
		fromTunedModel, err = tunedModelFromMldev(fromTunedModel.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"tunedModel"}, fromTunedModel)
	}

	return toObject, nil
}

func tuningJobFromVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromSdkHttpResponse := InternalGetValueByPath(fromObject, []string{"sdkHttpResponse"})
	if fromSdkHttpResponse != nil {
		InternalSetValueByPath(toObject, []string{"sdkHttpResponse"}, fromSdkHttpResponse)
	}

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		InternalSetValueByPath(toObject, []string{"name"}, fromName)
	}

	fromState := InternalGetValueByPath(fromObject, []string{"state"})
	if fromState != nil {
		fromState, err = InternalTTuningJobStatus(fromState)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"state"}, fromState)
	}

	fromCreateTime := InternalGetValueByPath(fromObject, []string{"createTime"})
	if fromCreateTime != nil {
		InternalSetValueByPath(toObject, []string{"createTime"}, fromCreateTime)
	}

	fromStartTime := InternalGetValueByPath(fromObject, []string{"startTime"})
	if fromStartTime != nil {
		InternalSetValueByPath(toObject, []string{"startTime"}, fromStartTime)
	}

	fromEndTime := InternalGetValueByPath(fromObject, []string{"endTime"})
	if fromEndTime != nil {
		InternalSetValueByPath(toObject, []string{"endTime"}, fromEndTime)
	}

	fromUpdateTime := InternalGetValueByPath(fromObject, []string{"updateTime"})
	if fromUpdateTime != nil {
		InternalSetValueByPath(toObject, []string{"updateTime"}, fromUpdateTime)
	}

	fromError := InternalGetValueByPath(fromObject, []string{"error"})
	if fromError != nil {
		InternalSetValueByPath(toObject, []string{"error"}, fromError)
	}

	fromDescription := InternalGetValueByPath(fromObject, []string{"description"})
	if fromDescription != nil {
		InternalSetValueByPath(toObject, []string{"description"}, fromDescription)
	}

	fromBaseModel := InternalGetValueByPath(fromObject, []string{"baseModel"})
	if fromBaseModel != nil {
		InternalSetValueByPath(toObject, []string{"baseModel"}, fromBaseModel)
	}

	fromTunedModel := InternalGetValueByPath(fromObject, []string{"tunedModel"})
	if fromTunedModel != nil {
		InternalSetValueByPath(toObject, []string{"tunedModel"}, fromTunedModel)
	}

	fromPreTunedModel := InternalGetValueByPath(fromObject, []string{"preTunedModel"})
	if fromPreTunedModel != nil {
		InternalSetValueByPath(toObject, []string{"preTunedModel"}, fromPreTunedModel)
	}

	fromSupervisedTuningSpec := InternalGetValueByPath(fromObject, []string{"supervisedTuningSpec"})
	if fromSupervisedTuningSpec != nil {
		InternalSetValueByPath(toObject, []string{"supervisedTuningSpec"}, fromSupervisedTuningSpec)
	}

	fromPreferenceOptimizationSpec := InternalGetValueByPath(fromObject, []string{"preferenceOptimizationSpec"})
	if fromPreferenceOptimizationSpec != nil {
		InternalSetValueByPath(toObject, []string{"preferenceOptimizationSpec"}, fromPreferenceOptimizationSpec)
	}

	fromDistillationSpec := InternalGetValueByPath(fromObject, []string{"distillationSpec"})
	if fromDistillationSpec != nil {
		InternalSetValueByPath(toObject, []string{"distillationSpec"}, fromDistillationSpec)
	}

	fromTuningDataStats := InternalGetValueByPath(fromObject, []string{"tuningDataStats"})
	if fromTuningDataStats != nil {
		InternalSetValueByPath(toObject, []string{"tuningDataStats"}, fromTuningDataStats)
	}

	fromEncryptionSpec := InternalGetValueByPath(fromObject, []string{"encryptionSpec"})
	if fromEncryptionSpec != nil {
		InternalSetValueByPath(toObject, []string{"encryptionSpec"}, fromEncryptionSpec)
	}

	fromPartnerModelTuningSpec := InternalGetValueByPath(fromObject, []string{"partnerModelTuningSpec"})
	if fromPartnerModelTuningSpec != nil {
		InternalSetValueByPath(toObject, []string{"partnerModelTuningSpec"}, fromPartnerModelTuningSpec)
	}

	fromCustomBaseModel := InternalGetValueByPath(fromObject, []string{"customBaseModel"})
	if fromCustomBaseModel != nil {
		InternalSetValueByPath(toObject, []string{"customBaseModel"}, fromCustomBaseModel)
	}

	fromEvaluateDatasetRuns := InternalGetValueByPath(fromObject, []string{"evaluateDatasetRuns"})
	if fromEvaluateDatasetRuns != nil {
		InternalSetValueByPath(toObject, []string{"evaluateDatasetRuns"}, fromEvaluateDatasetRuns)
	}

	fromExperiment := InternalGetValueByPath(fromObject, []string{"experiment"})
	if fromExperiment != nil {
		InternalSetValueByPath(toObject, []string{"experiment"}, fromExperiment)
	}

	fromFullFineTuningSpec := InternalGetValueByPath(fromObject, []string{"fullFineTuningSpec"})
	if fromFullFineTuningSpec != nil {
		InternalSetValueByPath(toObject, []string{"fullFineTuningSpec"}, fromFullFineTuningSpec)
	}

	fromLabels := InternalGetValueByPath(fromObject, []string{"labels"})
	if fromLabels != nil {
		InternalSetValueByPath(toObject, []string{"labels"}, fromLabels)
	}

	fromOutputUri := InternalGetValueByPath(fromObject, []string{"outputUri"})
	if fromOutputUri != nil {
		InternalSetValueByPath(toObject, []string{"outputUri"}, fromOutputUri)
	}

	fromPipelineJob := InternalGetValueByPath(fromObject, []string{"pipelineJob"})
	if fromPipelineJob != nil {
		InternalSetValueByPath(toObject, []string{"pipelineJob"}, fromPipelineJob)
	}

	fromServiceAccount := InternalGetValueByPath(fromObject, []string{"serviceAccount"})
	if fromServiceAccount != nil {
		InternalSetValueByPath(toObject, []string{"serviceAccount"}, fromServiceAccount)
	}

	fromTunedModelDisplayName := InternalGetValueByPath(fromObject, []string{"tunedModelDisplayName"})
	if fromTunedModelDisplayName != nil {
		InternalSetValueByPath(toObject, []string{"tunedModelDisplayName"}, fromTunedModelDisplayName)
	}

	fromTuningJobState := InternalGetValueByPath(fromObject, []string{"tuningJobState"})
	if fromTuningJobState != nil {
		InternalSetValueByPath(toObject, []string{"tuningJobState"}, fromTuningJobState)
	}

	fromVeoTuningSpec := InternalGetValueByPath(fromObject, []string{"veoTuningSpec"})
	if fromVeoTuningSpec != nil {
		InternalSetValueByPath(toObject, []string{"veoTuningSpec"}, fromVeoTuningSpec)
	}

	fromDistillationSamplingSpec := InternalGetValueByPath(fromObject, []string{"distillationSamplingSpec"})
	if fromDistillationSamplingSpec != nil {
		InternalSetValueByPath(toObject, []string{"distillationSamplingSpec"}, fromDistillationSamplingSpec)
	}

	fromTuningJobMetadata := InternalGetValueByPath(fromObject, []string{"tuningJobMetadata"})
	if fromTuningJobMetadata != nil {
		InternalSetValueByPath(toObject, []string{"tuningJobMetadata"}, fromTuningJobMetadata)
	}

	return toObject, nil
}

func tuningOperationFromMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromSdkHttpResponse := InternalGetValueByPath(fromObject, []string{"sdkHttpResponse"})
	if fromSdkHttpResponse != nil {
		InternalSetValueByPath(toObject, []string{"sdkHttpResponse"}, fromSdkHttpResponse)
	}

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		InternalSetValueByPath(toObject, []string{"name"}, fromName)
	}

	fromMetadata := InternalGetValueByPath(fromObject, []string{"metadata"})
	if fromMetadata != nil {
		InternalSetValueByPath(toObject, []string{"metadata"}, fromMetadata)
	}

	fromDone := InternalGetValueByPath(fromObject, []string{"done"})
	if fromDone != nil {
		InternalSetValueByPath(toObject, []string{"done"}, fromDone)
	}

	fromError := InternalGetValueByPath(fromObject, []string{"error"})
	if fromError != nil {
		InternalSetValueByPath(toObject, []string{"error"}, fromError)
	}

	return toObject, nil
}

func tuningValidationDatasetToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromGcsUri := InternalGetValueByPath(fromObject, []string{"gcsUri"})
	if fromGcsUri != nil {
		InternalSetValueByPath(toObject, []string{"validationDatasetUri"}, fromGcsUri)
	}

	fromVertexDatasetResource := InternalGetValueByPath(fromObject, []string{"vertexDatasetResource"})
	if fromVertexDatasetResource != nil {
		InternalSetValueByPath(toObject, []string{"validationDatasetUri"}, fromVertexDatasetResource)
	}

	return toObject, nil
}

type Tunings struct {
	apiClient *InternalAPIClient
}

func (m Tunings) get(ctx context.Context, name string, config *GetTuningJobConfig) (*TuningJob, error) {
	parameterMap := make(map[string]any)

	kwargs := map[string]any{"name": name, "config": config}
	InternalDeepMarshal(kwargs, &parameterMap)

	var httpOptions *HTTPOptions
	if config == nil || config.HTTPOptions == nil {
		httpOptions = &HTTPOptions{}
	} else {
		httpOptions = config.HTTPOptions
	}
	if httpOptions.Headers == nil {
		httpOptions.Headers = http.Header{}
	}
	var response = new(TuningJob)
	var responseMap map[string]any
	var fromConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	var toConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		toConverter = getTuningJobParametersToVertex
		fromConverter = tuningJobFromVertex
	} else {
		toConverter = getTuningJobParametersToMldev
		fromConverter = tuningJobFromMldev
	}

	body, err := toConverter(parameterMap, nil, parameterMap)
	if err != nil {
		return nil, err
	}

	var path string
	var urlParams map[string]any
	if _, ok := body["_url"]; ok {
		urlParams = body["_url"].(map[string]any)
		delete(body, "_url")
	}
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		path, err = InternalFormatMap("{name}", urlParams)
	} else {
		path, err = InternalFormatMap("{name}", urlParams)
	}
	if err != nil {
		return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
	}
	if _, ok := body["_query"]; ok {
		query, err := InternalCreateURLQuery(body["_query"].(map[string]any))
		if err != nil {
			return nil, err
		}
		path += "?" + query
		delete(body, "_query")
	}
	responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodGet, body, httpOptions)
	if err != nil {
		return nil, err
	}
	if fromConverter != nil {
		responseMap, err = fromConverter(responseMap, nil, parameterMap)
	}
	if err != nil {
		return nil, err
	}
	err = InternalMapToStruct(responseMap, response)
	if err != nil {
		return nil, err
	}

	return response, nil
}

func (m Tunings) list(ctx context.Context, config *ListTuningJobsConfig) (*ListTuningJobsResponse, error) {
	parameterMap := make(map[string]any)

	kwargs := map[string]any{"config": config}
	InternalDeepMarshal(kwargs, &parameterMap)

	var httpOptions *HTTPOptions
	if config == nil || config.HTTPOptions == nil {
		httpOptions = &HTTPOptions{}
	} else {
		httpOptions = config.HTTPOptions
	}
	if httpOptions.Headers == nil {
		httpOptions.Headers = http.Header{}
	}
	var response = new(ListTuningJobsResponse)
	var responseMap map[string]any
	var fromConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	var toConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		toConverter = listTuningJobsParametersToVertex
		fromConverter = listTuningJobsResponseFromVertex
	} else {
		toConverter = listTuningJobsParametersToMldev
		fromConverter = listTuningJobsResponseFromMldev
	}

	body, err := toConverter(parameterMap, nil, parameterMap)
	if err != nil {
		return nil, err
	}

	var path string
	var urlParams map[string]any
	if _, ok := body["_url"]; ok {
		urlParams = body["_url"].(map[string]any)
		delete(body, "_url")
	}
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		path, err = InternalFormatMap("tuningJobs", urlParams)
	} else {
		path, err = InternalFormatMap("tunedModels", urlParams)
	}
	if err != nil {
		return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
	}
	if _, ok := body["_query"]; ok {
		query, err := InternalCreateURLQuery(body["_query"].(map[string]any))
		if err != nil {
			return nil, err
		}
		path += "?" + query
		delete(body, "_query")
	}
	responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodGet, body, httpOptions)
	if err != nil {
		return nil, err
	}
	if fromConverter != nil {
		responseMap, err = fromConverter(responseMap, nil, parameterMap)
	}
	if err != nil {
		return nil, err
	}
	err = InternalMapToStruct(responseMap, response)
	if err != nil {
		return nil, err
	}

	return response, nil
}

// Cancel cancels a tuning job resource.
func (m Tunings) Cancel(ctx context.Context, name string, config *CancelTuningJobConfig) (*CancelTuningJobResponse, error) {
	parameterMap := make(map[string]any)

	kwargs := map[string]any{"name": name, "config": config}
	InternalDeepMarshal(kwargs, &parameterMap)

	var httpOptions *HTTPOptions
	if config == nil || config.HTTPOptions == nil {
		httpOptions = &HTTPOptions{}
	} else {
		httpOptions = config.HTTPOptions
	}
	if httpOptions.Headers == nil {
		httpOptions.Headers = http.Header{}
	}
	var response = new(CancelTuningJobResponse)
	var responseMap map[string]any
	var fromConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	var toConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		toConverter = cancelTuningJobParametersToVertex
		fromConverter = cancelTuningJobResponseFromVertex
	} else {
		toConverter = cancelTuningJobParametersToMldev
		fromConverter = cancelTuningJobResponseFromMldev
	}

	body, err := toConverter(parameterMap, nil, parameterMap)
	if err != nil {
		return nil, err
	}

	var path string
	var urlParams map[string]any
	if _, ok := body["_url"]; ok {
		urlParams = body["_url"].(map[string]any)
		delete(body, "_url")
	}
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		path, err = InternalFormatMap("{name}:cancel", urlParams)
	} else {
		path, err = InternalFormatMap("{name}:cancel", urlParams)
	}
	if err != nil {
		return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
	}
	if _, ok := body["_query"]; ok {
		query, err := InternalCreateURLQuery(body["_query"].(map[string]any))
		if err != nil {
			return nil, err
		}
		path += "?" + query
		delete(body, "_query")
	}
	responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodPost, body, httpOptions)
	if err != nil {
		return nil, err
	}
	if fromConverter != nil {
		responseMap, err = fromConverter(responseMap, nil, parameterMap)
	}
	if err != nil {
		return nil, err
	}
	err = InternalMapToStruct(responseMap, response)
	if err != nil {
		return nil, err
	}

	return response, nil
}

func (m Tunings) tune(ctx context.Context, baseModel *string, preTunedModel *PreTunedModel, trainingDataset *TuningDataset, config *CreateTuningJobConfig) (*TuningJob, error) {
	parameterMap := make(map[string]any)

	kwargs := map[string]any{"baseModel": baseModel, "preTunedModel": preTunedModel, "trainingDataset": trainingDataset, "config": config}
	InternalDeepMarshal(kwargs, &parameterMap)

	var httpOptions *HTTPOptions
	if config == nil || config.HTTPOptions == nil {
		httpOptions = &HTTPOptions{}
	} else {
		httpOptions = config.HTTPOptions
	}
	if httpOptions.Headers == nil {
		httpOptions.Headers = http.Header{}
	}
	var response = new(TuningJob)
	var responseMap map[string]any
	var fromConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	var toConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		toConverter = createTuningJobParametersPrivateToVertex
		fromConverter = tuningJobFromVertex
	} else {

		return nil, fmt.Errorf("method Tune is only supported in the Vertex AI client. You can choose to use Vertex AI by setting ClientConfig.Backend to BackendVertexAI.")

	}

	body, err := toConverter(parameterMap, nil, parameterMap)
	if err != nil {
		return nil, err
	}

	var path string
	var urlParams map[string]any
	if _, ok := body["_url"]; ok {
		urlParams = body["_url"].(map[string]any)
		delete(body, "_url")
	}
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		path, err = InternalFormatMap("tuningJobs", urlParams)
	} else {
		path, err = InternalFormatMap("None", urlParams)
	}
	if err != nil {
		return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
	}
	if _, ok := body["_query"]; ok {
		query, err := InternalCreateURLQuery(body["_query"].(map[string]any))
		if err != nil {
			return nil, err
		}
		path += "?" + query
		delete(body, "_query")
	}
	responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodPost, body, httpOptions)
	if err != nil {
		return nil, err
	}
	if fromConverter != nil {
		responseMap, err = fromConverter(responseMap, nil, parameterMap)
	}
	if err != nil {
		return nil, err
	}
	err = InternalMapToStruct(responseMap, response)
	if err != nil {
		return nil, err
	}

	return response, nil
}

func (m Tunings) tuneMldev(ctx context.Context, baseModel *string, preTunedModel *PreTunedModel, trainingDataset *TuningDataset, config *CreateTuningJobConfig) (*TuningOperation, error) {
	parameterMap := make(map[string]any)

	kwargs := map[string]any{"baseModel": baseModel, "preTunedModel": preTunedModel, "trainingDataset": trainingDataset, "config": config}
	InternalDeepMarshal(kwargs, &parameterMap)

	var httpOptions *HTTPOptions
	if config == nil || config.HTTPOptions == nil {
		httpOptions = &HTTPOptions{}
	} else {
		httpOptions = config.HTTPOptions
	}
	if httpOptions.Headers == nil {
		httpOptions.Headers = http.Header{}
	}
	var response = new(TuningOperation)
	var responseMap map[string]any
	var fromConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	var toConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {

		return nil, fmt.Errorf("method TuneMldev is only supported in the Gemini Developer client. You can choose to use Gemini Developer client by setting ClientConfig.Backend to BackendGeminiAPI.")

	} else {
		toConverter = createTuningJobParametersPrivateToMldev
		fromConverter = tuningOperationFromMldev
	}

	body, err := toConverter(parameterMap, nil, parameterMap)
	if err != nil {
		return nil, err
	}

	var path string
	var urlParams map[string]any
	if _, ok := body["_url"]; ok {
		urlParams = body["_url"].(map[string]any)
		delete(body, "_url")
	}
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		path, err = InternalFormatMap("None", urlParams)
	} else {
		path, err = InternalFormatMap("tunedModels", urlParams)
	}
	if err != nil {
		return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
	}
	if _, ok := body["_query"]; ok {
		query, err := InternalCreateURLQuery(body["_query"].(map[string]any))
		if err != nil {
			return nil, err
		}
		path += "?" + query
		delete(body, "_query")
	}
	responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodPost, body, httpOptions)
	if err != nil {
		return nil, err
	}
	if fromConverter != nil {
		responseMap, err = fromConverter(responseMap, nil, parameterMap)
	}
	if err != nil {
		return nil, err
	}
	err = InternalMapToStruct(responseMap, response)
	if err != nil {
		return nil, err
	}

	return response, nil
}

// List retrieves a paginated list of tuning_jobs resources.
func (m Tunings) List(ctx context.Context, config *ListTuningJobsConfig) (Page[TuningJob], error) {
	listFunc := func(ctx context.Context, config map[string]any) ([]*TuningJob, string, *HTTPResponse, error) {
		var c ListTuningJobsConfig
		if err := InternalMapToStruct(config, &c); err != nil {
			return nil, "", nil, err
		}
		resp, err := m.list(ctx, &c)
		if err != nil {
			return nil, "", nil, err
		}
		return resp.TuningJobs, resp.NextPageToken, resp.SDKHTTPResponse, nil
	}
	c := make(map[string]any)
	InternalDeepMarshal(config, &c)
	return newPage(ctx, "tuningJobs", c, listFunc)
}

// All retrieves all tuning_jobs resources.
//
// This method handles pagination internally, making multiple API calls as needed
// to fetch all entries. It returns an iterator that yields each "tuningJobs"
// entry one by one. You do not need to manage pagination
// tokens or make multiple calls to retrieve all data.
func (m Tunings) All(ctx context.Context) iter.Seq2[*TuningJob, error] {
	listFunc := func(ctx context.Context, config map[string]any) ([]*TuningJob, string, *HTTPResponse, error) {
		var c ListTuningJobsConfig
		if err := InternalMapToStruct(config, &c); err != nil {
			return nil, "", nil, err
		}
		resp, err := m.list(ctx, &c)
		if err != nil {
			return nil, "", nil, err
		}
		return resp.TuningJobs, resp.NextPageToken, resp.SDKHTTPResponse, nil
	}
	p, err := newPage(ctx, "tuningJobs", map[string]any{}, listFunc)
	if err != nil {
		return yieldErrorAndEndIterator[TuningJob](err)
	}
	return p.all(ctx)
}

var experimentalWarningTuningsCreateOperation sync.Once

// Tune creates a tuning job resource.
func (t Tunings) Tune(ctx context.Context, baseModel string, trainingDataset *TuningDataset, config *CreateTuningJobConfig) (*TuningJob, error) {
	experimentalWarningTuningsCreateOperation.Do(func() {
		log.Println("The SDK's tuning implementation is experimental, and may change in future versions.")
	})
	if t.apiClient.clientConfig.Backend == BackendVertexAI {
		if strings.HasPrefix(baseModel, "projects/") {
			preTunedModel := &PreTunedModel{TunedModelName: baseModel}
			if config != nil && config.PreTunedModelCheckpointID != "" {
				preTunedModel.CheckpointID = config.PreTunedModelCheckpointID
			}
			return t.tune(ctx, nil, preTunedModel, trainingDataset, config)
		} else {
			return t.tune(ctx, &baseModel, nil, trainingDataset, config)
		}
	} else {
		operation, err := t.tuneMldev(ctx, &baseModel, nil, trainingDataset, config)
		if err != nil {
			return nil, err
		}
		if operation == nil {
			return nil, fmt.Errorf("operation is nil")
		}
		if operation.Metadata != nil {
			if tunedModel, ok := operation.Metadata["tunedModel"].(string); ok {
				return &TuningJob{
					Name:  tunedModel,
					State: JobStateQueued,
				}, nil
			}
		}

		if operation.Name == "" {
			return nil, fmt.Errorf("operation name is required")
		}

		parts := strings.Split(operation.Name, "/operations/")
		tunedModelName := parts[0]
		return &TuningJob{
			Name:  tunedModelName,
			State: JobStateQueued,
		}, nil
	}
}

// Get retrieves a tuning job resource.
func (t Tunings) Get(ctx context.Context, name string, config *GetTuningJobConfig) (*TuningJob, error) {
	return t.get(ctx, name, config)
}
