pub struct ContentBuilder {
pub contents: Vec<Content>,
/* private fields */
}Expand description
Builder for content generation requests
Fields§
§contents: Vec<Content>Implementations§
Source§impl ContentBuilder
impl ContentBuilder
Sourcepub fn with_safety_settings(self, safety_settings: Vec<SafetySetting>) -> Self
pub fn with_safety_settings(self, safety_settings: Vec<SafetySetting>) -> Self
Sets the safety settings for the request.
Sourcepub fn with_system_prompt(self, text: impl Into<String>) -> Self
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().
Sourcepub fn with_system_instruction(self, text: impl Into<String>) -> Self
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.
Sourcepub fn with_user_message(self, text: impl Into<String>) -> Self
pub fn with_user_message(self, text: impl Into<String>) -> Self
Adds a user message to the conversation history.
Sourcepub fn with_user_message_and_file(
self,
text: impl Into<String>,
file_handle: &FileHandle,
) -> Result<Self, FilesError>
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).
Sourcepub fn with_model_message(self, text: impl Into<String>) -> Self
pub fn with_model_message(self, text: impl Into<String>) -> Self
Adds a model message to the conversation history.
Sourcepub fn with_inline_data(
self,
data: impl Into<String>,
mime_type: impl Into<String>,
) -> Self
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.
Sourcepub fn with_inline_data_and_resolution(
self,
data: impl Into<String>,
mime_type: impl Into<String>,
resolution: MediaResolutionLevel,
) -> Self
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.
Sourcepub fn with_function_response<Response>(
self,
name: impl Into<String>,
response: Response,
) -> Result<Self, Error>where
Response: Serialize,
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.
Sourcepub fn with_function_response_str(
self,
name: impl Into<String>,
response: impl Into<String>,
) -> Result<Self, Error>
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.
Sourcepub fn with_message(self, message: Message) -> Self
pub fn with_message(self, message: Message) -> Self
Adds a Message to the conversation history.
Sourcepub fn with_cached_content(self, cached_content: &CachedContentHandle) -> Self
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.
Sourcepub fn with_messages(self, messages: impl IntoIterator<Item = Message>) -> Self
pub fn with_messages(self, messages: impl IntoIterator<Item = Message>) -> Self
Adds multiple messages to the conversation history.
Sourcepub fn with_generation_config(self, config: GenerationConfig) -> Self
pub fn with_generation_config(self, config: GenerationConfig) -> Self
Sets the generation configuration for the request.
Sourcepub fn with_temperature(self, temperature: f32) -> Self
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.
Sourcepub fn with_top_p(self, top_p: f32) -> Self
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.
Sourcepub fn with_top_k(self, top_k: i32) -> Self
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.
Sourcepub fn with_max_output_tokens(self, max_output_tokens: i32) -> Self
pub fn with_max_output_tokens(self, max_output_tokens: i32) -> Self
Sets the maximum number of output tokens for the request.
Sourcepub fn with_candidate_count(self, candidate_count: i32) -> Self
pub fn with_candidate_count(self, candidate_count: i32) -> Self
Sets the number of candidate responses to generate.
Sourcepub fn with_stop_sequences(self, stop_sequences: Vec<String>) -> Self
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.
Sourcepub fn with_response_mime_type(self, mime_type: impl Into<String>) -> Self
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.
Sourcepub fn with_response_schema(self, schema: Value) -> Self
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.
Sourcepub fn with_tool(self, tool: Tool) -> Self
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.
Sourcepub fn with_function(self, function: FunctionDeclaration) -> Self
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.
Sourcepub fn with_function_calling_mode(self, mode: FunctionCallingMode) -> Self
pub fn with_function_calling_mode(self, mode: FunctionCallingMode) -> Self
Sets the function calling mode for the request.
Sourcepub fn with_tool_config(self, tool_config: ToolConfig) -> Self
pub fn with_tool_config(self, tool_config: ToolConfig) -> Self
Sets the tool configuration for the request.
Sourcepub fn with_thinking_config(self, thinking_config: ThinkingConfig) -> Self
pub fn with_thinking_config(self, thinking_config: ThinkingConfig) -> Self
Sets the thinking configuration for the request (Gemini 2.5 series only).
Sourcepub fn with_thinking_budget(self, budget: i32) -> Self
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).
Sourcepub fn with_dynamic_thinking(self) -> Self
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).
Sourcepub fn with_thoughts_included(self, include: bool) -> Self
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.
Sourcepub fn with_thinking_level(self, level: ThinkingLevel) -> Self
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.
Sourcepub fn with_media_resolution(self, level: MediaResolutionLevel) -> Self
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.
Sourcepub fn with_code_execution(self) -> Self
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.
Sourcepub fn with_audio_output(self) -> Self
pub fn with_audio_output(self) -> Self
Enables audio output (text-to-speech).
Sourcepub fn with_image_config(self, image_config: ImageConfig) -> Self
pub fn with_image_config(self, image_config: ImageConfig) -> Self
Sets the image generation configuration.
Sourcepub fn with_speech_config(self, speech_config: SpeechConfig) -> Self
pub fn with_speech_config(self, speech_config: SpeechConfig) -> Self
Sets the speech configuration for text-to-speech generation.
Sourcepub fn with_voice(self, voice_name: impl Into<String>) -> Self
pub fn with_voice(self, voice_name: impl Into<String>) -> Self
Sets a single voice for text-to-speech generation.
Sourcepub fn with_multi_speaker_config(
self,
speakers: Vec<SpeakerVoiceConfig>,
) -> Self
pub fn with_multi_speaker_config( self, speakers: Vec<SpeakerVoiceConfig>, ) -> Self
Sets multi-speaker configuration for text-to-speech generation.
Sourcepub fn build(self) -> GenerateContentRequest
pub fn build(self) -> GenerateContentRequest
Builds the GenerateContentRequest.
Sourcepub async fn execute(self) -> Result<GenerationResponse, ClientError>
pub async fn execute(self) -> Result<GenerationResponse, ClientError>
Executes the content generation request.
Sourcepub async fn execute_stream(self) -> Result<GenerationStream, ClientError>
pub async fn execute_stream(self) -> Result<GenerationStream, ClientError>
Executes the content generation request as a stream.
Sourcepub async fn count_tokens(self) -> Result<CountTokensResponse, ClientError>
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
impl Clone for ContentBuilder
Source§fn clone(&self) -> ContentBuilder
fn clone(&self) -> ContentBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more