ContentBuilder

Struct ContentBuilder 

Source
pub struct ContentBuilder {
    pub contents: Vec<Content>,
    /* private fields */
}
Expand description

Builder for content generation requests

Fields§

§contents: Vec<Content>

Implementations§

Source§

impl ContentBuilder

Source

pub fn with_safety_settings(self, safety_settings: Vec<SafetySetting>) -> Self

Sets the safety settings for the request.

Source

pub fn with_system_prompt(self, text: impl Into<String>) -> Self

Sets the system prompt for the request.

This is an alias for with_system_instruction().

Source

pub fn with_system_instruction(self, text: impl Into<String>) -> Self

Sets the system instruction for the request.

System instructions are used to provide high-level guidance to the model, such as setting a persona, providing context, or defining the desired output format.

Source

pub fn with_user_message(self, text: impl Into<String>) -> Self

Adds a user message to the conversation history.

Source

pub fn with_user_message_and_file( self, text: impl Into<String>, file_handle: &FileHandle, ) -> Result<Self, FilesError>

Adds a user message, together with coordinates for a previously uploaded file.

Uploading a file and using it avoids encoding large files and sending them, in particular when this would need to happen more than once with a file.

§Errors

Returns an error if the file metadata is incomplete (missing MIME type or URI).

Source

pub fn with_model_message(self, text: impl Into<String>) -> Self

Adds a model message to the conversation history.

Source

pub fn with_inline_data( self, data: impl Into<String>, mime_type: impl Into<String>, ) -> Self

Adds inline data (e.g., an image) to the request.

The data should be base64-encoded.

Source

pub fn with_inline_data_and_resolution( self, data: impl Into<String>, mime_type: impl Into<String>, resolution: MediaResolutionLevel, ) -> Self

Adds inline data with explicit media resolution control.

This allows fine-grained control over the resolution used for processing the inline data, which affects both quality and token consumption. This method is useful for optimizing token usage. The data should be base64-encoded.

Source

pub fn with_function_response<Response>( self, name: impl Into<String>, response: Response, ) -> Result<Self, Error>
where Response: Serialize,

Adds a function response to the request using a Serialize response.

This is used to provide the model with the result of a function call it has requested.

Source

pub fn with_function_response_str( self, name: impl Into<String>, response: impl Into<String>, ) -> Result<Self, Error>

Adds a function response to the request using a JSON string.

This is a convenience method that parses the string into a serde_json::Value.

Source

pub fn with_message(self, message: Message) -> Self

Adds a Message to the conversation history.

Source

pub fn with_cached_content(self, cached_content: &CachedContentHandle) -> Self

Uses cached content for this request.

This allows reusing previously cached system instructions and conversation history, which can reduce latency and cost.

Source

pub fn with_messages(self, messages: impl IntoIterator<Item = Message>) -> Self

Adds multiple messages to the conversation history.

Source

pub fn with_generation_config(self, config: GenerationConfig) -> Self

Sets the generation configuration for the request.

Source

pub fn with_temperature(self, temperature: f32) -> Self

Sets the temperature for the request.

Temperature controls the randomness of the output. Higher values (e.g., 1.0) produce more creative results, while lower values (e.g., 0.2) produce more deterministic results.

Source

pub fn with_top_p(self, top_p: f32) -> Self

Sets the top-p value for the request.

Top-p is a sampling method that selects the next token from a cumulative probability distribution. It can be used to control the diversity of the output.

Source

pub fn with_top_k(self, top_k: i32) -> Self

Sets the top-k value for the request.

Top-k is a sampling method that selects the next token from the k most likely candidates.

Source

pub fn with_max_output_tokens(self, max_output_tokens: i32) -> Self

Sets the maximum number of output tokens for the request.

Source

pub fn with_candidate_count(self, candidate_count: i32) -> Self

Sets the number of candidate responses to generate.

Source

pub fn with_stop_sequences(self, stop_sequences: Vec<String>) -> Self

Sets the stop sequences for the request.

The model will stop generating text when it encounters one of these sequences.

Source

pub fn with_response_mime_type(self, mime_type: impl Into<String>) -> Self

Sets the response MIME type for the request.

This can be used to request structured output, such as JSON.

Source

pub fn with_response_schema(self, schema: Value) -> Self

Sets the response schema for structured output.

When used with a JSON MIME type, this schema will be used to validate the model’s output.

Source

pub fn with_tool(self, tool: Tool) -> Self

Adds a tool to the request.

Tools allow the model to interact with external systems, such as APIs or databases.

Source

pub fn with_function(self, function: FunctionDeclaration) -> Self

Adds a function declaration as a tool.

This is a convenience method for creating a Tool from a FunctionDeclaration.

Source

pub fn with_function_calling_mode(self, mode: FunctionCallingMode) -> Self

Sets the function calling mode for the request.

Source

pub fn with_tool_config(self, tool_config: ToolConfig) -> Self

Sets the tool configuration for the request.

Source

pub fn with_thinking_config(self, thinking_config: ThinkingConfig) -> Self

Sets the thinking configuration for the request (Gemini 2.5 series only).

Source

pub fn with_thinking_budget(self, budget: i32) -> Self

Sets the thinking budget for the request (Gemini 2.5 series only).

A budget of -1 enables dynamic thinking. This is mutually exclusive with thinking_level (Gemini 3 models).

Source

pub fn with_dynamic_thinking(self) -> Self

Enables dynamic thinking, which allows the model to decide its own thinking budget (Gemini 2.5 series only).

Note: This only enables the capability. To receive thoughts in the response, you must also call [.with_thoughts_included(true)](Self::with_thoughts_included).

Source

pub fn with_thoughts_included(self, include: bool) -> Self

Includes thought summaries in the response (Gemini 2.5 series only).

This requires with_dynamic_thinking() or with_thinking_budget() to be enabled.

Source

pub fn with_thinking_level(self, level: ThinkingLevel) -> Self

Sets the thinking level for Gemini 3 Pro.

This controls the depth of reasoning the model applies. Use Low for simpler queries requiring faster responses, or High for complex problems requiring deeper analysis.

Note: This is mutually exclusive with thinking_budget (used by Gemini 2.5 models). Setting this will be ignored by Gemini 2.5 models.

Source

pub fn with_media_resolution(self, level: MediaResolutionLevel) -> Self

Sets the global media resolution level.

This controls the token usage for all images and PDFs in the request. Individual parts can override this setting using with_inline_data_and_resolution(). Higher resolutions provide better quality but consume more tokens.

Source

pub fn with_code_execution(self) -> Self

Adds the code execution tool to the request.

This allows the model to generate and execute Python code as part of the generation process. Useful for mathematical calculations, data analysis, and other computational tasks. Currently supports Python only.

Source

pub fn with_audio_output(self) -> Self

Enables audio output (text-to-speech).

Source

pub fn with_image_config(self, image_config: ImageConfig) -> Self

Sets the image generation configuration.

Source

pub fn with_speech_config(self, speech_config: SpeechConfig) -> Self

Sets the speech configuration for text-to-speech generation.

Source

pub fn with_voice(self, voice_name: impl Into<String>) -> Self

Sets a single voice for text-to-speech generation.

Source

pub fn with_multi_speaker_config( self, speakers: Vec<SpeakerVoiceConfig>, ) -> Self

Sets multi-speaker configuration for text-to-speech generation.

Source

pub fn build(self) -> GenerateContentRequest

Builds the GenerateContentRequest.

Source

pub async fn execute(self) -> Result<GenerationResponse, ClientError>

Executes the content generation request.

Source

pub async fn execute_stream(self) -> Result<GenerationStream, ClientError>

Executes the content generation request as a stream.

Source

pub async fn count_tokens(self) -> Result<CountTokensResponse, ClientError>

Counts the number of tokens in the content generation request.

Trait Implementations§

Source§

impl Clone for ContentBuilder

Source§

fn clone(&self) -> ContentBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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> PolicyExt for T
where T: ?Sized,

Source§

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
Source§

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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