Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

Unified Gemini API client.

Mirrors the GoogleGenAI class from @google/genai (js-genai). Provides access to both Live (WebSocket) and REST APIs through a single authenticated entry point.

§Construction

use gemini_genai_rs::Client;

// From API key (Google AI)
let client = Client::from_api_key("your-api-key");

// From Vertex AI credentials
let client = Client::from_vertex("project-id", "us-central1", "access-token");

// Live WebSocket session on the platform's default Live model
let session = client.live(None).connect().await?;

Implementations§

Source§

impl Client

Source

pub async fn list_batch_jobs( &self, ) -> Result<ListBatchJobsResponse, BatchesError>

List batch jobs.

Source

pub async fn create_batch_job( &self, config: CreateBatchJobConfig, ) -> Result<BatchJob, BatchesError>

Create a batch prediction job.

Source

pub async fn create_batch_embeddings( &self, config: CreateBatchJobConfig, ) -> Result<BatchJob, BatchesError>

Create a batch embeddings job.

Source

pub async fn get_batch_job(&self, name: &str) -> Result<BatchJob, BatchesError>

Get a batch job by name.

Source

pub async fn cancel_batch_job(&self, name: &str) -> Result<(), BatchesError>

Cancel a batch job by name.

Source

pub async fn delete_batch_job(&self, name: &str) -> Result<(), BatchesError>

Delete a batch job by name.

Source§

impl Client

Source

pub async fn list_cached_contents( &self, ) -> Result<ListCachedContentsResponse, CachesError>

List cached contents.

Source

pub async fn create_cached_content( &self, config: CreateCachedContentConfig, ) -> Result<CachedContent, CachesError>

Create a new cached content.

Source

pub async fn get_cached_content( &self, name: &str, ) -> Result<CachedContent, CachesError>

Get a cached content by name.

Source

pub async fn update_cached_content( &self, name: &str, updates: UpdateCachedContentRequest, ) -> Result<CachedContent, CachesError>

Update a cached content (TTL or expiration time).

Source

pub async fn delete_cached_content(&self, name: &str) -> Result<(), CachesError>

Delete a cached content by name.

Source§

impl Client

Source

pub fn chat(&self) -> ChatSessionBuilder<'_>

Start a new chat session with the default model.

Source§

impl Client

Source

pub fn from_api_key(api_key: impl Into<String>) -> Self

Create a client with Google AI API key authentication.

Source

pub fn from_access_token(access_token: impl Into<String>) -> Self

Create a client with Google AI OAuth2 token authentication.

Source

pub fn from_vertex( project: impl Into<String>, location: impl Into<String>, access_token: impl Into<String>, ) -> Self

Create a client with Vertex AI authentication.

Source

pub fn from_vertex_refreshable( project: impl Into<String>, location: impl Into<String>, refresher: impl Fn() -> String + Send + Sync + 'static, ) -> Self

Create a client with Vertex AI authentication and dynamic token refresh.

The refresher closure is called on every REST API request and on every Live connection attempt (including reconnects) to obtain a fresh Bearer token. It should cache internally (see GcloudTokenProvider in gemini-adk-rs for an example).

This is the right constructor for anything that outlives a token’s ~1 h lifetime.

Source

pub fn model(self, model: impl Into<ModelId>) -> Self

Set the default model for all API calls.

Source

pub fn http_config(self, config: HttpConfig) -> Self

Configure the HTTP client (timeouts, retries, etc.).

Source

pub fn auth(&self) -> &dyn AuthProvider

Get a reference to the underlying auth provider.

Source

pub fn default_model(&self) -> &ModelId

Get the default model.

Source

pub fn rest_url(&self, endpoint: ServiceEndpoint) -> String

Build the REST URL for a given service endpoint, using the default model.

Source

pub fn rest_url_for(&self, endpoint: ServiceEndpoint, model: &ModelId) -> String

Build the REST URL for a given service endpoint with a specific model.

Source

pub async fn auth_headers(&self) -> Result<Vec<(String, String)>, AuthError>

Get auth headers for REST API calls.

Source

pub fn live(&self, model: Option<ModelId>) -> ConnectBuilder

A Live session on this client’s credentials.

None connects to the platform’s default Live model (the REST default model is a text model and would not do). Tune the session with ConnectBuilder::configure, then .connect().await.

Source

pub fn http_client(&self) -> &HttpClient

Get a reference to the HTTP client for making REST API calls.

Source

pub async fn rest_request( &self, endpoint: ServiceEndpoint, body: &impl Serialize, ) -> Result<Value, HttpError>

Make a raw REST API request (low-level).

Higher-level module methods (e.g., generate_content()) should be preferred.

Source§

impl Client

Source

pub async fn embed_content( &self, text: impl Into<String>, ) -> Result<EmbedContentResponse, EmbedError>

Embed text content using the default model.

Source

pub async fn embed_content_with( &self, config: EmbedContentConfig, model: Option<&ModelId>, ) -> Result<EmbedContentResponse, EmbedError>

Embed content with full configuration.

Source§

impl Client

Source

pub async fn list_files(&self) -> Result<ListFilesResponse, FilesError>

List files.

Source

pub async fn get_file(&self, name: &str) -> Result<File, FilesError>

Get a file by name.

Source

pub async fn delete_file(&self, name: &str) -> Result<(), FilesError>

Delete a file by name.

Source

pub async fn upload_file( &self, data: Vec<u8>, config: UploadFileConfig, ) -> Result<File, FilesError>

Upload a file from a byte buffer.

Source

pub async fn download_file(&self, name: &str) -> Result<Vec<u8>, FilesError>

Download a file’s content by name.

Source

pub async fn register_files( &self, sources: Vec<FileSource>, ) -> Result<Vec<File>, FilesError>

Register external files by URI (Vertex AI only).

Source§

impl Client

Source

pub async fn generate_content( &self, prompt: impl Into<String>, ) -> Result<GenerateContentResponse, GenerateError>

Generate content from a text prompt using the default model.

Source

pub async fn generate_content_with( &self, config: GenerateContentConfig, model: Option<&ModelId>, ) -> Result<GenerateContentResponse, GenerateError>

Generate content with full configuration and optional model override.

Source§

impl Client

Source

pub async fn list_models(&self) -> Result<ListModelsResponse, ModelsError>

List available models.

Source

pub async fn get_model(&self, model: &ModelId) -> Result<ModelInfo, ModelsError>

Get metadata for a specific model.

Source§

impl Client

Source

pub async fn count_tokens( &self, text: impl Into<String>, ) -> Result<CountTokensResponse, TokensError>

Count tokens for text content.

Source

pub async fn count_tokens_for( &self, contents: Vec<Content>, model: Option<&ModelId>, ) -> Result<CountTokensResponse, TokensError>

Count tokens for content with optional model override.

Source§

impl Client

Source

pub async fn list_tuning_jobs( &self, ) -> Result<ListTuningJobsResponse, TuningsError>

List tuning jobs.

Source

pub async fn get_tuning_job( &self, name: &str, ) -> Result<TuningJob, TuningsError>

Get a tuning job by name.

Source

pub async fn create_tuning_job( &self, config: CreateTuningJobConfig, ) -> Result<TuningJob, TuningsError>

Create a new tuning job.

Source

pub async fn cancel_tuning_job(&self, name: &str) -> Result<(), TuningsError>

Cancel a tuning job by name.

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,