Table of Contents

Class TuningClient

Namespace
GenerativeAI.Clients
Assembly
GenerativeAI.dll

A client for model tuning, mirroring client.tunings of the google-genai SDK.

public class TuningClient : BaseClient
Inheritance
TuningClient
Inherited Members
Extension Methods

Remarks

On Vertex AI it uses the projects/{project}/locations/{location}/tuningJobs resource and supports supervised fine tuning, preference tuning, distillation and reinforcement tuning, listing, cancellation and reward validation.

On the Gemini Developer API it uses the tunedModels resource: TuneAsync(string, TuningDataset, CreateTuningJobConfig?, CancellationToken), GetAsync(string, CancellationToken), CancelAsync(string, CancellationToken) and WaitForCompletionAsync(string, TimeSpan?, TimeSpan?, CancellationToken) are supported, and tuned models are reported as TuningJobs.

Constructors

TuningClient(IPlatformAdapter, HttpClient?, ILogger?)

Initializes a new instance of the TuningClient class.

public TuningClient(IPlatformAdapter platform, HttpClient? httpClient = null, ILogger? logger = null)

Parameters

platform IPlatformAdapter

The platform adapter used to manage API endpoints and authentication.

httpClient HttpClient

The HTTP client instance used for making API requests.

logger ILogger

An optional logger instance for logging operations.

Methods

CancelAsync(string, CancellationToken)

Cancels a tuning job.

public Task CancelAsync(string name, CancellationToken cancellationToken = default)

Parameters

name string

The tuning job (Vertex AI) or tuned model (Gemini API) name.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task

A task that completes when the cancellation request was accepted.

GetAsync(string, CancellationToken)

Gets a tuning job (Vertex AI) or a tuned model reported as a tuning job (Gemini API).

public Task<TuningJob> GetAsync(string name, CancellationToken cancellationToken = default)

Parameters

name string

The tuning job resource name (projects/.../tuningJobs/..., tuningJobs/... or the job id) on Vertex AI, or the tuned model name (tunedModels/... or the id) on the Gemini API.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<TuningJob>

The TuningJob.

ListAllAsync(int?, string?, CancellationToken)

Lists all tuning jobs, fetching further pages as needed (Vertex AI only).

public IAsyncEnumerable<TuningJob> ListAllAsync(int? pageSize = null, string? filter = null, CancellationToken cancellationToken = default)

Parameters

pageSize int?

The page size used for each request.

filter string

An optional list filter.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

IAsyncEnumerable<TuningJob>

An async sequence of tuning jobs.

ListAsync(int?, string?, string?, CancellationToken)

Lists tuning jobs (Vertex AI only).

public Task<ListTuningJobsResponse> ListAsync(int? pageSize = null, string? pageToken = null, string? filter = null, CancellationToken cancellationToken = default)

Parameters

pageSize int?

The maximum number of tuning jobs to return.

pageToken string

A page token from a previous call.

filter string

An optional list filter.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<ListTuningJobsResponse>

A page of tuning jobs.

Exceptions

NotSupportedException

Called on the Gemini Developer API.

TuneAsync(string, TuningDataset, CreateTuningJobConfig?, CancellationToken)

Creates a tuning job (Vertex AI) or a tuned model (Gemini API) and returns it as a TuningJob.

public Task<TuningJob> TuneAsync(string baseModel, TuningDataset trainingDataset, CreateTuningJobConfig? config = null, CancellationToken cancellationToken = default)

Parameters

baseModel string

The model to tune. On Vertex AI, a tuned model resource name (projects/.../models/...) continues tuning from that model (with PreTunedModelCheckpointId as checkpoint).

trainingDataset TuningDataset

The training dataset.

config CreateTuningJobConfig

Optional tuning configuration.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<TuningJob>

The created TuningJob. On the Gemini API only Name (the tuned model name) and State (JOB_STATE_QUEUED) are set.

Exceptions

NotSupportedException

An option is not supported on the current platform.

ValidateRewardAsync(Content, ReinforcementTuningExample, SingleReinforcementTuningRewardConfig?, CompositeReinforcementTuningRewardConfig?, string?, CancellationToken)

Validates a reinforcement tuning reward configuration against a sample response and example, without creating a tuning job (Vertex AI only).

public Task<ValidateRewardResponse> ValidateRewardAsync(Content sampleResponse, ReinforcementTuningExample example, SingleReinforcementTuningRewardConfig? singleRewardConfig = null, CompositeReinforcementTuningRewardConfig? compositeRewardConfig = null, string? parent = null, CancellationToken cancellationToken = default)

Parameters

sampleResponse Content

The sample model response to score.

example ReinforcementTuningExample

The example (prompt and references) the response belongs to.

singleRewardConfig SingleReinforcementTuningRewardConfig

A single reward configuration. Mutually exclusive with compositeRewardConfig.

compositeRewardConfig CompositeReinforcementTuningRewardConfig

A composite reward configuration. Mutually exclusive with singleRewardConfig.

parent string

The location to validate in, projects/{project}/locations/{location}. Defaults to the platform's project and location.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<ValidateRewardResponse>

The computed reward(s).

Exceptions

NotSupportedException

Called on the Gemini Developer API.

WaitForCompletionAsync(string, TimeSpan?, TimeSpan?, CancellationToken)

Polls a tuning job until it reaches a terminal state (succeeded, partially succeeded, failed, cancelled or expired).

public Task<TuningJob> WaitForCompletionAsync(string name, TimeSpan? pollInterval = null, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

name string

The tuning job (Vertex AI) or tuned model (Gemini API) name.

pollInterval TimeSpan?

The delay between polls. Defaults to 30 seconds.

timeout TimeSpan?

The maximum time to wait. Defaults to no limit.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<TuningJob>

The completed TuningJob.

Exceptions

TimeoutException

The job did not complete within timeout.

See Also