### Install google-genai with uv Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Use this command to install the SDK using uv. ```shell uv pip install google-genai ``` -------------------------------- ### Install Google Gen AI SDK with uv Source: https://googleapis.github.io/python-genai Install the google-genai package using uv. ```bash uv pip install google-genai ``` -------------------------------- ### Install google-genai with pip Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Use this command to install the SDK using pip. ```shell pip install google-genai ``` -------------------------------- ### LiveMusicServerSetupComplete Source: https://googleapis.github.io/python-genai Indicates that the Live Music server setup is complete. ```APIDOC ## LiveMusicServerSetupComplete ### Description Indicates that the Live Music server setup is complete. ``` -------------------------------- ### Generate Content with System Instruction and Config Source: https://googleapis.github.io/python-genai Example of generating content with a system instruction and specific generation configuration, including max output tokens and temperature. ```python from google.genai import types response = client.models.generate_content( model='gemini-2.0-flash-001', contents='high', config=types.GenerateContentConfig( system_instruction='I say high, you say low', max_output_tokens=3, temperature=0.3, ), ) print(response.text) ``` -------------------------------- ### Integrate Local MCP Server as a Tool Source: https://googleapis.github.io/python-genai This example demonstrates integrating a local Model Context Protocol (MCP) server as a tool. It uses `stdio_client` to establish a connection and `ClientSession` to interact with the MCP server for model requests. ```python import os import asyncio from datetime import datetime from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client from google import genai client = genai.Client() # Create server parameters for stdio connection server_params = StdioServerParameters( command="npx", # Executable args=["-y", "@philschmid/weather-mcp"], # MCP Server env=None, # Optional environment variables ) async def run(): async with stdio_client(server_params) as (read, write): async with ClientSession(read, write) as session: # Prompt to get the weather for the current day in London. prompt = f"What is the weather in London in {datetime.now().strftime('%Y-%m-%d')}?" # Initialize the connection between client and server await session.initialize() # Send request to the model with MCP function declarations response = await client.aio.models.generate_content( model="gemini-2.5-flash", contents=prompt, config=genai.types.GenerateContentConfig( temperature=0, tools=[session], # uses the session, will automatically call the tool using automatic function calling ), ) print(response.text) # Start the asyncio event loop and run the main function asyncio.run(run()) ``` -------------------------------- ### Generate Videos (Video to Video - Vertex AI) Source: https://googleapis.github.io/python-genai Currently, only Vertex AI supports Video to Video generation (Video extension). This example shows how to read a local video file. ```python from google.genai import types # Read local video (uses mimetypes.guess_type to infer mime type) video = types.Video.from_file("local/path/video.mp4") ``` -------------------------------- ### Initiate Model Tuning Job Source: https://googleapis.github.io/python-genai Start a supervised fine-tuning job for a base model using a specified training dataset and configuration. Requires Vertex AI. ```python from google.genai import types tuning_job = client.tunings.tune( base_model=model, training_dataset=training_dataset, config=types.CreateTuningJobConfig( epoch_count=1, uned_model_display_name='test_dataset_examples model' ), ) print(tuning_job) ``` -------------------------------- ### Start Model Tuning Job Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Initiates a supervised fine-tuning job for a base model using a specified training dataset and configuration. This is only supported in Vertex AI. ```python from google.genai import types tuning_job = client.tunings.tune( base_model=model, training_dataset=training_dataset, config=types.CreateTuningJobConfig( epoch_count=1, tuned_model_display_name='test_dataset_examples model' ), ) print(tuning_job) ``` -------------------------------- ### Configure Safety Settings Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Control model safety by providing `safety_settings` in the `GenerateContentConfig`. This example blocks only high-severity hate speech. ```python from google.genai import types response = client.models.generate_content( model='gemini-2.5-flash', contents='Say something bad.', config=types.GenerateContentConfig( safety_settings=[ types.SafetySetting( category='HARM_CATEGORY_HATE_SPEECH', threshold='BLOCK_ONLY_HIGH', ) ] ), ) print(response.text) ``` -------------------------------- ### Generate Content with Safety Settings Source: https://googleapis.github.io/python-genai Example of generating content while configuring safety settings to block specific harm categories. ```python from google.genai import types response = client.models.generate_content( model='gemini-2.5-flash', contents='Say something bad.', config=types.GenerateContentConfig( safety_settings=[ types.SafetySetting( category='HARM_CATEGORY_HATE_SPEECH', threshold='BLOCK_ONLY_HIGH', ) ] ), ) print(response.text) ``` -------------------------------- ### Create client using environment variables Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Create a client that automatically picks up configuration from environment variables. ```python from google import genai client = genai.Client() ``` -------------------------------- ### LiveMusicClientSetup Source: https://googleapis.github.io/python-genai Configuration for setting up the Live Music client. ```APIDOC ## LiveMusicClientSetup ### Description Configuration for setting up the Live Music client. ### Properties - `model` (string): The model to be used for music generation. ``` -------------------------------- ### Batch Prediction Request JSON Example Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Example JSON structure for batch prediction requests, defining a request with a specific key and content. ```json {"key":"request_1", "request": {"contents": [{"parts": [{"text": "Explain how AI works in a few words"}]}], "generation_config": {"response_modalities": ["TEXT"]}}} {"key":"request_2", "request": {"contents": [{"parts": [{"text": "Explain how Crypto works in a few words"}]}]}} ``` -------------------------------- ### Create a client using environment variables (Gemini Developer API) Source: https://googleapis.github.io/python-genai Configure the GEMINI_API_KEY or GOOGLE_API_KEY environment variable to automatically pick up credentials for the Gemini Developer API. ```APIDOC ## Create a client using environment variables (Gemini Developer API) ### Description Configure the GEMINI_API_KEY or GOOGLE_API_KEY environment variable to automatically pick up credentials for the Gemini Developer API. GOOGLE_API_KEY takes precedence if both are set. ### Environment Variable Setup ```bash export GEMINI_API_KEY='your-api-key' ``` ``` -------------------------------- ### Get Tuning Job Source: https://googleapis.github.io/python-genai Retrieves a specific tuning job. This is useful for monitoring the status of a tuning job or to get the name of the tuned model once the job is complete. ```APIDOC ## Get Tuning Job ```python tuning_job = client.tunings.get(name=tuning_job.name) print(tuning_job) ``` ```python import time completed_states = set( [ 'JOB_STATE_SUCCEEDED', 'JOB_STATE_FAILED', 'JOB_STATE_CANCELLED', ] ) while tuning_job.state not in completed_states: print(tuning_job.state) tuning_job = client.tunings.get(name=tuning_job.name) time.sleep(10) ``` ``` -------------------------------- ### Delete Batch Job Source: https://googleapis.github.io/python-genai Provides an example of how to delete a batch job resource using its name. ```APIDOC ## Delete This snippet demonstrates how to delete a batch job. ```python # Delete the job resource delete_job = client.batches.delete(name=job.name) delete_job ``` ``` -------------------------------- ### Citation Source: https://googleapis.github.io/python-genai Represents a citation with start and end indices, license, publication date, title, and URI. ```APIDOC ## Citation ### Description Represents a citation with start and end indices, license, publication date, title, and URI. ### Fields - **start_index** (integer) - The starting index of the citation. - **end_index** (integer) - The ending index of the citation. - **license** (string) - The license associated with the citation. - **publication_date** (string) - The publication date of the cited content. - **title** (string) - The title of the cited content. - **uri** (string) - The Uniform Resource Identifier (URI) of the citation. ``` -------------------------------- ### Get Cached Content Source: https://googleapis.github.io/python-genai Retrieves information about a previously created cache. This allows for inspection of cache details. ```APIDOC ## Get ### Description Retrieves information about a previously created cache. This allows for inspection of cache details. ### Method ```python cached_content = client.caches.get(name=cached_content.name) ``` ``` -------------------------------- ### Create a client using environment variables (Gemini API on Vertex AI) Source: https://googleapis.github.io/python-genai Set GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_CLOUD_PROJECT, and GOOGLE_CLOUD_LOCATION environment variables for the Gemini API on Vertex AI. ```APIDOC ## Create a client using environment variables (Gemini API on Vertex AI) ### Description Set GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_CLOUD_PROJECT, and GOOGLE_CLOUD_LOCATION environment variables for the Gemini API on Vertex AI. ### Environment Variable Setup ```bash export GOOGLE_GENAI_USE_VERTEXAI=true export GOOGLE_CLOUD_PROJECT='your-project-id' export GOOGLE_CLOUD_LOCATION='us-central1' ``` ### Code ```python from google import genai client = genai.Client() ``` ``` -------------------------------- ### Integrate External Tools with Stdio Client Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Demonstrates using the `stdio_client` to integrate external tools, enabling automatic function calling. Requires setting up server parameters and initializing the client session. ```python import asyncio from datetime import datetime from google.generativeai.client import genai from google.generativeai.types import StdioServerParameters, ClientSession, stdio_client server_params = StdioServerParameters( command="npx", # Executable args=["-y", "@philschmid/weather-mcp"], # MCP Server env=None, # Optional environment variables ) async def run(): async with stdio_client(server_params) as (read, write): async with ClientSession(read, write) as session: # Prompt to get the weather for the current day in London. prompt = f"What is the weather in London in {datetime.now().strftime('%Y-%m-%d')}?" # Initialize the connection between client and server await session.initialize() # Send request to the model with MCP function declarations response = await client.aio.models.generate_content( model="gemini-2.5-flash", contents=prompt, config=genai.types.GenerateContentConfig( temperature=0, tools=[session], # uses the session, will automatically call the tool using automatic function calling ), ) print(response.text) # Start the asyncio event loop and run the main function asyncio.run(run()) ``` -------------------------------- ### Get Batch Prediction Job Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Retrieves the status and details of a specific batch prediction job by its name. ```APIDOC ## Get Batch Prediction Job ### Description Retrieves the status and details of a specific batch prediction job. ### Method GET ### Endpoint /v1beta/batches/{name} ### Parameters #### Path Parameters - **name** (string) - Required - The name of the batch job to retrieve. ``` -------------------------------- ### Create Function Call Parts Source: https://googleapis.github.io/python-genai Demonstrates how to create a list of function call parts for the SDK. Each part specifies a function name and its arguments. ```python from google.genai import types contents = [ types.Part.from_function_call( name='get_weather_by_location', args={'location': 'Boston'} ), types.Part.from_function_call( name='get_weather_by_location', args={'location': 'New York'} ), ] ``` -------------------------------- ### Get File Information Source: https://googleapis.github.io/python-genai Retrieves information about an uploaded file. This is useful for verifying file status or obtaining metadata. ```APIDOC ## Get ### Description Retrieves information about an uploaded file. This is useful for verifying file status or obtaining metadata. ### Method ```python file1 = client.files.upload(file='2312.11805v3.pdf') file_info = client.files.get(name=file1.name) ``` ``` -------------------------------- ### LiveMusicClientSetupDict Source: https://googleapis.github.io/python-genai Dictionary representation of LiveMusicClientSetup. ```APIDOC ## LiveMusicClientSetupDict ### Description Dictionary representation of LiveMusicClientSetup. ### Properties - `model` (string): The model to be used for music generation. ``` -------------------------------- ### Get Batch Job Status Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Retrieves the current state of a batch job by repeatedly fetching its status until it is completed. ```APIDOC while job.state not in completed_states: print(job.state) job = client.batches.get(name=job.name) time.sleep(30) job ``` -------------------------------- ### System Instructions and Other Configs Source: https://googleapis.github.io/python-genai Demonstrates how to influence model output using the `config` parameter in `generate_content`, including `system_instruction`, `max_output_tokens`, and `temperature`. ```APIDOC ## System Instructions and Other Configs The output of the model can be influenced by several optional settings available in generate_content’s config parameter. For example, increasing max_output_tokens is essential for longer model responses. To make a model more deterministic, lowering the temperature parameter reduces randomness, with values near 0 minimizing variability. Capabilities and parameter defaults for each model is shown in the Vertex AI docs and Gemini API docs respectively. ```python from google.genai import types response = client.models.generate_content( model='gemini-2.0-flash-001', contents='high', config=types.GenerateContentConfig( system_instruction='I say high, you say low', max_output_tokens=3, temperature=0.3, ), ) print(response.text) ``` ``` -------------------------------- ### Generate Content with Configuration Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Demonstrates how to generate content using the `generate_content` method with various configuration options like `max_output_tokens` and `temperature` to influence the model's response. ```APIDOC ## Generate Content with Configuration ### Description This example shows how to use the `generate_content` method with a `GenerateContentConfig` object to control aspects of the model's output, such as system instructions, maximum output tokens, and response randomness. ### Method `client.models.generate_content` ### Parameters #### Request Body - **model** (string) - Required - The model to use for generation. - **contents** (string or Part) - Required - The input prompt for the model. - **config** (GenerateContentConfig) - Optional - Configuration for content generation. - **system_instruction** (string) - Optional - An instruction for the model to follow. - **max_output_tokens** (integer) - Optional - The maximum number of tokens to generate. - **temperature** (float) - Optional - Controls randomness; lower values make output more deterministic. ### Request Example ```python from google.genai import types response = client.models.generate_content( model='gemini-2.0-flash-001', contents='high', config=types.GenerateContentConfig( system_instruction='I say high, you say low', max_output_tokens=3, temperature=0.3, ), ) print(response.text) ``` ``` -------------------------------- ### Get Tuned Model Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Retrieves details about a specific tuned model. This allows you to inspect the configuration and status of your fine-tuned models. ```APIDOC ## Get Tuned Model ### Description Retrieves details about a specific tuned model. ### Method GET ### Endpoint /v1beta/models/{model} ### Parameters #### Path Parameters - **model** (string) - Required - The name or ID of the tuned model to retrieve. ``` -------------------------------- ### Configuring Async Client Arguments Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Demonstrates how to pass additional arguments to the aiohttp client session for faster async performance. ```APIDOC ## Configuring Async Client Arguments ### Description Demonstrates how to pass additional arguments to the aiohttp client session for faster async performance. ### Method ```python http_options = types.HttpOptions( async_client_args={'cookies': ..., 'ssl': ...}, ) client=Client(..., http_options=http_options) ``` ``` -------------------------------- ### Get a Tuned Model Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Retrieves a specific tuned model using its identifier. The 'tuning_job' object should contain the 'tuned_model.model' attribute. ```python tuned_model = client.models.get(model=tuning_job.tuned_model.model) print(tuned_model) ``` -------------------------------- ### Get Batch Prediction Job by Name Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Retrieves a specific batch prediction job using its name and prints its current state. ```python # Get a job by name job = client.batches.get(name=job.name) job.state ``` -------------------------------- ### Client Initialization with Vertex AI Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Initializes a GenAI client for use with Vertex AI, specifying project, location, and API version. ```APIDOC ## Client Initialization with Vertex AI ### Description Initializes a GenAI client for use with Vertex AI, specifying project, location, and API version. ### Method ```python client = genai.Client( vertexai=True, project='your-project-id', location='us-central1', http_options=types.HttpOptions(api_version='v1') ) ``` ``` -------------------------------- ### Use Tuned Model Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Generates content using a specified tuned model. This is useful for getting responses from a model that has been fine-tuned for a specific task. ```APIDOC ## Use Tuned Model ### Description Generates content using a specified tuned model. ### Method POST ### Endpoint /v1beta/models/{model}:generateContent ### Parameters #### Path Parameters - **model** (string) - Required - The name of the tuned model to use. #### Request Body - **contents** (string) - Required - The prompt or content to send to the model. ### Request Example ```json { "contents": "why is the sky blue?" } ``` ### Response #### Success Response (200) - **text** (string) - The generated text response from the model. ``` -------------------------------- ### Provide Contents as a list of types.Content Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Demonstrates the canonical way to provide input to the 'contents' argument by creating a list of types.Content instances. ```python from google.genai import types contents = types.Content( role='user', ``` -------------------------------- ### Get Cached Content Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Retrieves information about a previously created cached content using its name. This is useful for managing or verifying existing caches. ```python cached_content = client.caches.get(name=cached_content.name) ``` -------------------------------- ### Create Vertex AI client Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Create a client for the Vertex AI API. Specify your project ID and location. ```python from google import genai # Only run this block for Vertex AI API client = genai.Client( vertexai=True, project='your-project-id', location='us-central1' ) ``` -------------------------------- ### Client Initialization with Gemini Developer API Key Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Initializes a GenAI client for the Gemini Developer API using an API key and setting the API version to 'v1alpha'. ```APIDOC ## Client Initialization with Gemini Developer API Key ### Description Initializes a GenAI client for the Gemini Developer API using an API key and setting the API version to 'v1alpha'. ### Method ```python client = genai.Client( api_key='GEMINI_API_KEY', http_options=types.HttpOptions(api_version='v1alpha') ) ``` ``` -------------------------------- ### Batch Prediction Request JSON Format Source: https://googleapis.github.io/python-genai Example JSON structure for defining batch prediction requests when using a file. Each request can have a key and a request object. ```json {"key":"request_1", "request": {"contents": [{"parts": [{"text": "Explain how AI works in a few words"}]}], "generation_config": {"response_modalities": ["TEXT"]}}} {"key":"request_2", "request": {"contents": [{"parts": [{"text": "Explain how Crypto works in a few words"}]}]}} ``` -------------------------------- ### Paginate Through Tuning Jobs Asynchronously Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Demonstrates asynchronous pagination for tuning jobs, including accessing page size, the first item, and fetching the next page. ```python async_pager = await client.aio.tunings.list(config={'page_size': 10}) print(async_pager.page_size) print(async_pager[0]) await async_pager.next_page() print(async_pager[0]) ``` -------------------------------- ### Handle API Errors Python Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Catch and process errors from the model service using the `google.genai.errors.APIError` class. This example demonstrates how to access error codes and messages. ```python from google.genai import errors try: client.models.generate_content( model="invalid-model-name", contents="What is your name?", ) except errors.APIError as e: print(e.code) # 404 print(e.message) ``` -------------------------------- ### Get Tuning Job Status Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Retrieves the status and details of a model tuning job using its name. This is useful for monitoring the progress and outcome of the tuning process. ```python tuning_job = client.tunings.get(name=tuning_job.name) print(tuning_job) ``` -------------------------------- ### Provide a list of function call parts Source: https://googleapis.github.io/python-genai Demonstrates how to create a list of function call parts using `types.Part.from_function_call` and how the SDK converts them into `types.ModelContent`. ```APIDOC ## Provide a list of function call parts ```python from google.genai import types contents = [ types.Part.from_function_call( name='get_weather_by_location', args={'location': 'Boston'} ), types.Part.from_function_call( name='get_weather_by_location', args={'location': 'New York'} ), ] ``` The SDK converts a list of function call parts to the a content with a model role: ```python [ types.ModelContent( parts=[ types.Part.from_function_call( name='get_weather_by_location', args={'location': 'Boston'} ), types.Part.from_function_call( name='get_weather_by_location', args={'location': 'New York'} ) ] ) ] ``` Where a types.ModelContent is a subclass of types.Content, the role field in types.ModelContent is fixed to be model. ``` -------------------------------- ### Get File Information Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Uploads a file and then retrieves its information using the file's name. This is useful for verifying upload status or accessing file metadata. ```python file1 = client.files.upload(file='2312.11805v3.pdf') file_info = client.files.get(name=file1.name) ``` -------------------------------- ### Configure async client with aiohttp Source: https://googleapis.github.io/python-genai Pass additional arguments for aiohttp.ClientSession.request() through http_options for faster async client performance. ```APIDOC ## Configure async client with aiohttp ### Description To achieve faster performance, you can install `google-genai[aiohttp]` and pass additional arguments for `aiohttp.ClientSession.request()` through `http_options`. ### Code ```python from google import genai from google.genai import types http_options = types.HttpOptions( async_client_args={'cookies': ..., 'ssl': ...}, ) client=genai.Client(..., http_options=http_options) ``` ``` -------------------------------- ### List Base Models Asynchronously with Pagination Source: https://googleapis.github.io/python-genai Demonstrates asynchronous pagination for listing base models, including fetching the next page. ```python async_pager = await client.aio.models.list(config={'page_size': 10}) print(async_pager.page_size) print(async_pager[0]) await async_pager.next_page() print(async_pager[0]) ``` -------------------------------- ### Initialize Client with Custom Base URL and Headers Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Initializes the GenAI client with a custom base URL, useful for API gateway proxies, and includes custom headers for authentication. ```python base_url = 'https://test-api-gateway-proxy.com' client = Client( vertexai=True, # Currently only vertexai=True is supported http_options={ 'base_url': base_url, 'headers': {'Authorization': 'Bearer test_token'}, }, ) ``` -------------------------------- ### Generate Content with JSON Schema Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Use `response_json_schema` to guide the model to generate JSON output conforming to a specified schema. Ensure the schema is correctly defined. ```python user_profile = { 'properties': { 'age': { 'anyOf': [ {'maximum': 20, 'minimum': 0, 'type': 'integer'}, {'type': 'null'}, ], 'title': 'Age', }, 'username': { 'description': "User's unique name", 'title': 'Username', 'type': 'string', }, }, 'required': ['username', 'age'], 'title': 'User Schema', 'type': 'object', } response = client.models.generate_content( model='gemini-2.5-flash', contents='Give me a random user profile.', config={ 'response_mime_type': 'application/json', 'response_json_schema': user_profile }, ) print(response.parsed) ``` -------------------------------- ### FullFineTuningSpecDict Structure Source: https://googleapis.github.io/python-genai A dictionary representation of FullFineTuningSpec. ```APIDOC ## FullFineTuningSpecDict ### Description A dictionary representation of full fine-tuning parameters. ### Fields - **hyper_parameters** (object) - Hyperparameters for fine-tuning. - **training_dataset_uri** (string) - The URI of the training dataset. - **validation_dataset_uri** (string) - The URI of the validation dataset. ``` -------------------------------- ### Setting Proxy Environment Variables Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Shows how to set environment variables for proxy configuration, including HTTPS proxy and SSL certificate file. ```APIDOC ## Setting Proxy Environment Variables ### Description Shows how to set environment variables for proxy configuration, including HTTPS proxy and SSL certificate file. ### Method ```bash export HTTPS_PROXY='http://username:password@proxy_uri:port' export SSL_CERT_FILE='client.pem' ``` ``` -------------------------------- ### Typed Configuration for Model Generation Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Utilize Pydantic types from `google.genai.types` for API parameters, offering type safety over dictionaries. This example shows setting various generation parameters including temperature, top_p, top_k, and stop sequences. ```python from google.genai import types response = client.models.generate_content( model='gemini-2.0-flash-001', contents=types.Part.from_text(text='Why is the sky blue?'), config=types.GenerateContentConfig( temperature=0, top_p=0.95, top_k=20, candidate_count=1, seed=5, max_output_tokens=100, stop_sequences=['STOP!'], presence_penalty=0.0, frequency_penalty=0.0, ), ) print(response.text) ``` -------------------------------- ### Import genai and types Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Import the necessary modules for using the SDK. ```python from google import genai from google.genai import types ``` -------------------------------- ### List Base Models (Asynchronous) Source: https://googleapis.github.io/python-genai Demonstrates asynchronous model listing using `client.aio.models.list()`, including asynchronous pagination and iteration. ```APIDOC ### List Base Models (Asynchronous) ```python async for job in await client.aio.models.list(): print(job) ``` ```python async_pager = await client.aio.models.list(config={'page_size': 10}) print(async_pager.page_size) print(async_pager[0]) await async_pager.next_page() print(async_pager[0]) ``` ``` -------------------------------- ### Invoke a Function and Get Response Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt This snippet demonstrates how to invoke a tool function based on a model's generated function call and then pass the function's response back to the model for a final answer. Ensure the `get_current_weather` function is defined and accessible. ```python from google.genai import types # Assume client and tool are already initialized # Assume get_current_weather function is defined elsewhere response = client.models.generate_content( model='gemini-2.5-flash', contents='What is the weather like in Boston?', config=types.GenerateContentConfig( tools=[tool], ), ) user_prompt_content = types.Content( role='user', parts=[types.Part.from_text(text='What is the weather like in Boston?')], ) function_call_part = response.function_calls[0] function_call_content = response.candidates[0].content try: function_result = get_current_weather( **function_call_part.function_call.args ) function_response = {'result': function_result} except ( Exception ) as e: # instead of raising the exception, you can let the model handle it function_response = {'error': str(e)} function_response_part = types.Part.from_function_response( name=function_call_part.name, response=function_response, ) function_response_content = types.Content( role='tool', parts=[function_response_part] ) response = client.models.generate_content( model='gemini-2.5-flash', contents=[ user_prompt_content, function_call_content, function_response_content, ], config=types.GenerateContentConfig( tools=[tool], ), ) print(response.text) ``` -------------------------------- ### Edit Image with Imagen Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Use this code to edit an existing image. Image editing requires a separate model and is supported in Vertex AI. You can provide reference images and masks to guide the editing process. Specify the model, prompt, reference images, and editing configuration. ```python # Edit the generated image from above from google.genai import types from google.genai.types import RawReferenceImage, MaskReferenceImage raw_ref_image = RawReferenceImage( reference_id=1, reference_image=response1.generated_images[0].image, ) # Model computes a mask of the background mask_ref_image = MaskReferenceImage( reference_id=2, config=types.MaskReferenceConfig( mask_mode='MASK_MODE_BACKGROUND', mask_dilation=0, ), ) response3 = client.models.edit_image( model='imagen-3.0-capability-001', prompt='Sunlight and clear sky', reference_images=[raw_ref_image, mask_ref_image], config=types.EditImageConfig( edit_mode='EDIT_MODE_INPAINT_INSERTION', number_of_images=1, include_rai_reason=True, output_mime_type='image/jpeg', ), ) response3.generated_images[0].image.show() ``` -------------------------------- ### Configure Faster Async Client with Aiohttp Source: https://googleapis.github.io/python-genai Pass additional arguments for aiohttp.ClientSession.request() to the http_options for faster async client performance. ```python http_options = types.HttpOptions( async_client_args={'cookies': ..., 'ssl': ...}, ) client=Client(..., http_options=http_options) ``` -------------------------------- ### Upload File and Generate Content from File Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Uploads a local file and then generates content by summarizing the uploaded file using the 'gemini-2.5-flash' model. ```console !wget -q https://storage.googleapis.com/generativeai-downloads/data/a11.txt ``` ```python file = client.files.upload(file='a11.txt') response = client.models.generate_content( model='gemini-2.5-flash', contents=['Could you summarize this file?', file] ) print(response.text) ``` -------------------------------- ### Provide Contents as a List of types.Content Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Demonstrates the canonical way to provide input to the `contents` argument using a list of `types.Content` objects. ```APIDOC ## Provide Contents as a List of types.Content ### Description Demonstrates the canonical way to provide input to the `contents` argument using a list of `types.Content` objects. ### Method ```python from google.genai import types contents = types.Content( role='user', ``` ``` -------------------------------- ### Initialize Vertex AI Client with API Version v1 Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Initializes the GenAI client for Vertex AI with a specific project, location, and API version set to 'v1'. ```python from google import genai from google.genai import types client = genai.Client( vertexai=True, project='your-project-id', location='us-central1', http_options=types.HttpOptions(api_version='v1') ) ``` -------------------------------- ### List Base Models with Pagination Source: https://googleapis.github.io/python-genai Demonstrates paginating through the list of base models with a specified page size and accessing model details. ```python pager = client.models.list(config={'page_size': 10}) print(pager.page_size) print(pager[0]) pager.next_page() print(pager[0]) ``` -------------------------------- ### Generate Videos from Text with Veo Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt This snippet shows how to generate videos from a text prompt using the Veo model. The process involves creating an operation and then polling it until completion. Configuration options include number of videos, duration, and prompt enhancement. ```python from google.genai import types # Create operation operation = client.models.generate_videos( model='veo-2.0-generate-001', prompt='A neon hologram of a cat driving at top speed', config=types.GenerateVideosConfig( number_of_videos=1, duration_seconds=5, enhance_prompt=True, ), ) # Poll operation while not operation.done: time.sleep(20) operation = client.operations.get(operation) video = operation.response.generated_videos[0].video video.show() ``` -------------------------------- ### LiveMusicSetConfigParameters Source: https://googleapis.github.io/python-genai Parameters for setting the music generation configuration. ```APIDOC ## LiveMusicSetConfigParameters ### Description Parameters for setting the music generation configuration. ### Properties - `music_generation_config` (LiveMusicGenerationConfig): The music generation configuration. ``` -------------------------------- ### Generate Videos from Video with Veo Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt This snippet illustrates generating videos from an existing video input using the Veo model. Note that Video to Video generation is currently only supported in Vertex AI. The input video must be in Google Cloud Storage (GCS). ```python from google.genai import types # Read local video (uses mimetypes.guess_type to infer mime type) video = types.Video.from_file("local/path/video.mp4") # Create operation operation = client.models.generate_videos( model='veo-2.0-generate-001', # Prompt is optional if Video is provided prompt='Night sky', # Input video must be in GCS video=types.Video( uri="gs://bucket-name/inputs/videos/cat_driving.mp4", ), config=types.GenerateVideosConfig( number_of_videos=1, duration_seconds=5, enhance_prompt=True, ), ) # Poll operation while not operation.done: time.sleep(20) operation = client.operations.get(operation) video = operation.response.generated_videos[0].video video.show() ``` -------------------------------- ### Create Gemini Developer API client Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Create a client for the Gemini Developer API. Ensure you have your GEMINI_API_KEY set. ```python from google import genai # Only run this block for Gemini Developer API client = genai.Client(api_key='GEMINI_API_KEY') ``` -------------------------------- ### Create List of Non-Function Call Parts Source: https://googleapis.github.io/python-genai Shows how to create a list containing both text and URI parts. ```python from google.genai import types contents = [ types.Part.from_text('What is this image about?'), types.Part.from_uri( file_uri: 'gs://generativeai-downloads/images/scones.jpg', mime_type: 'image/jpeg', ) ] ``` -------------------------------- ### PrebuiltVoiceConfig Attributes Source: https://googleapis.github.io/python-genai Attributes for PrebuiltVoiceConfig, specifying pre-built voice settings. ```APIDOC ## PrebuiltVoiceConfig Attributes ### Description Configuration for using a pre-built voice, specifying the name of the voice. ### Attributes - `voice_name` (str): The name of the pre-built voice to use. ### Example ```python # Assuming 'voice_config' is an instance of PrebuiltVoiceConfig voice_config.voice_name = "en-US-Wavenet-A" ``` ``` -------------------------------- ### Paginate Through Tuned Models Asynchronously Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Demonstrates asynchronous pagination for tuned models, including accessing page size, the first item, and fetching the next page. ```python async_pager = await client.aio.models.list(config={'page_size': 10, 'query_base': False}) print(async_pager.page_size) print(async_pager[0]) await async_pager.next_page() print(async_pager[0]) ``` -------------------------------- ### Download File for Upload Source: https://googleapis.github.io/python-genai Download a file from a specified URL using wget for use with the client's file upload functionality. This is a prerequisite for uploading local files. ```bash !wget -q https://storage.googleapis.com/generativeai-downloads/data/a11.txt ``` -------------------------------- ### Initialize Gemini Developer API Client with API Version v1alpha Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Initializes the GenAI client for the Gemini Developer API with an API key and sets the API version to 'v1alpha'. ```python from google import genai from google.genai import types # Only run this block for Gemini Developer API client = genai.Client( api_key='GEMINI_API_KEY', http_options=types.HttpOptions(api_version='v1alpha') ) ``` -------------------------------- ### Set Vertex AI environment variables Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Set environment variables for using the Gemini API on Vertex AI. ```bash export GOOGLE_GENAI_USE_VERTEXAI=true export GOOGLE_CLOUD_PROJECT='your-project-id' export GOOGLE_CLOUD_LOCATION='us-central1' ``` -------------------------------- ### Generate Content (Async) Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Perform content generation asynchronously using `client.aio.models.generate_content`. This is the async equivalent of the synchronous method. ```python response = await client.aio.models.generate_content( model='gemini-2.5-flash', contents='Tell me a story in 300 words.' ) print(response.text) ``` -------------------------------- ### Generate Videos (Image to Video) Source: https://googleapis.github.io/python-genai Generate videos from an input image using the Veo model. The prompt is optional when an image is provided. ```python from google.genai import types # Read local image (uses mimetypes.guess_type to infer mime type) image = types.Image.from_file(location="local/path/file.png") # Create operation operation = client.models.generate_videos( model='veo-2.0-generate-001', # Prompt is optional if image is provided prompt='Night sky', image=image, config=types.GenerateVideosConfig( number_of_videos=1, duration_seconds=5, enhance_prompt=True, # Can also pass an Image into last_frame for frame interpolation ), ) # Poll operation while not operation.done: time.sleep(20) operation = client.operations.get(operation) video = operation.response.generated_videos[0].video video.show() ``` -------------------------------- ### Generate Content Stream (Async) Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Handle asynchronous streaming of content generation. Use `async for` with `client.aio.models.generate_content_stream`. ```python async for chunk in await client.aio.models.generate_content_stream( model='gemini-2.5-flash', contents='Tell me a story in 300 words.' ): print(chunk.text, end='') ``` -------------------------------- ### Paginate Through Tuning Jobs Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Demonstrates using a pager object to access tuning jobs, including retrieving page size, accessing the first item, and fetching the next page. ```python pager = client.tunings.list(config={'page_size': 10}) print(pager.page_size) print(pager[0]) pager.next_page() print(pager[0]) ``` -------------------------------- ### Generate Videos (Text to Video) Source: https://googleapis.github.io/python-genai Generate videos from text prompts using the Veo model. This is a public preview feature. ```python from google.genai import types # Create operation operation = client.models.generate_videos( model='veo-2.0-generate-001', prompt='A neon hologram of a cat driving at top speed', config=types.GenerateVideosConfig( number_of_videos=1, duration_seconds=5, enhance_prompt=True, ), ) # Poll operation while not operation.done: time.sleep(20) operation = client.operations.get(operation) video = operation.response.generated_videos[0].video video.show() ``` -------------------------------- ### Generate Content with Typed Configuration Source: https://googleapis.github.io/python-genai Demonstrates using Pydantic types for generation configuration parameters, including temperature, top_p, top_k, and stop sequences. ```python from google.genai import types response = client.models.generate_content( model='gemini-2.0-flash-001', contents=types.Part.from_text(text='Why is the sky blue?'), config=types.GenerateContentConfig( temperature=0, top_p=0.95, top_k=20, candidate_count=1, seed=5, max_output_tokens=100, stop_sequences=['STOP!'], presence_penalty=0.0, frequency_penalty=0.0, ), ) print(response.text) ``` -------------------------------- ### Generate Videos with Python SDK Source: https://googleapis.github.io/python-genai Use the `generate_videos` method to create videos from a prompt and an input video stored in Google Cloud Storage. The operation is asynchronous and requires polling. ```python operation = client.models.generate_videos( model='veo-2.0-generate-001', # Prompt is optional if Video is provided prompt='Night sky', # Input video must be in GCS video=types.Video( uri="gs://bucket-name/inputs/videos/cat_driving.mp4", ), config=types.GenerateVideosConfig( number_of_videos=1, duration_seconds=5, enhance_prompt=True, ), ) # Poll operation while not operation.done: time.sleep(20) operation = client.operations.get(operation) video = operation.response.generated_videos[0].video video.show() ``` -------------------------------- ### Create Gemini Developer API Client Source: https://googleapis.github.io/python-genai Create a client for the Gemini Developer API using an API key. ```python from google import genai # Only run this block for Gemini Developer API client = genai.Client(api_key='GEMINI_API_KEY') ``` -------------------------------- ### Provide a list of non function call parts Source: https://googleapis.github.io/python-genai Illustrates creating a list of non-function call parts using `types.Part.from_text` and `types.Part.from_uri`, and how the SDK groups them into a single `types.UserContent`. ```APIDOC ## Provide a list of non function call parts ```python from google.genai import types contents = [ types.Part.from_text('What is this image about?'), types.Part.from_uri( file_uri: 'gs://generativeai-downloads/images/scones.jpg', mime_type: 'image/jpeg', ) ] ``` The SDK will convert the list of parts into a content with a user role ```python [ types.UserContent( parts=[ types.Part.from_text('What is this image about?'), types.Part.from_uri( file_uri: 'gs://generativeai-downloads/images/scones.jpg', mime_type: 'image/jpeg', ) ] ) ] ``` ``` -------------------------------- ### Use sync client context manager Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Use the sync client within a 'with' statement to automatically close it upon exiting the block. ```python from google.genai import Client with Client() as client: response_1 = client.models.generate_content( model=MODEL_ID, contents='Hello', ) response_2 = client.models.generate_content( model=MODEL_ID, contents='Ask a question', ) ``` -------------------------------- ### Create a client for Vertex AI API Source: https://googleapis.github.io/python-genai Use this code block to create a client for the Vertex AI API, specifying your project ID and location. ```APIDOC ## Create a client for Vertex AI API ### Description Use this code block to create a client for the Vertex AI API, specifying your project ID and location. ### Code ```python from google import genai client = genai.Client( vertexai=True, project='your-project-id', location='us-central1' ) ``` ``` -------------------------------- ### ContextWindowCompressionConfig Source: https://googleapis.github.io/python-genai Configuration for context window compression, including sliding window and trigger tokens. ```APIDOC ## ContextWindowCompressionConfig ### Description Configuration for context window compression, including sliding window and trigger tokens. ### Fields - **sliding_window** (object) - Configuration for the sliding window. - **trigger_tokens** (integer) - The number of tokens that trigger compression. ``` -------------------------------- ### List Base Models Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Shows how to retrieve a list of available base models using the `client.models.list()` method, including options for pagination. ```APIDOC ## List Base Models ### Description This section demonstrates how to list available base models using the `client.models.list()` method. It also shows how to use the `config` parameter for pagination, such as setting the `page_size`. ### Method `client.models.list` ### Parameters #### Query Parameters - **config** (dict) - Optional - Configuration for listing models. - **page_size** (integer) - Optional - The number of models to return per page. ### Request Example (Synchronous) ```python for model in client.models.list(): print(model) pager = client.models.list(config={'page_size': 10}) print(pager.page_size) print(pager[0]) await pager.next_page() # Note: This example uses await, implying an async context for next_page print(pager[0]) ``` ### Request Example (Asynchronous) ```python # Asynchronous listing async for job in await client.aio.models.list(): print(job) async_pager = await client.aio.models.list(config={'page_size': 10}) print(async_pager.page_size) print(async_pager[0]) await async_pager.next_page() print(async_pager[0]) ``` ``` -------------------------------- ### Configuring SOCKS5 Proxy with httpx Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Configures a SOCKS5 proxy for both synchronous and asynchronous httpx clients. ```APIDOC ## Configuring SOCKS5 Proxy with httpx ### Description Configures a SOCKS5 proxy for both synchronous and asynchronous httpx clients. ### Method ```python http_options = types.HttpOptions( client_args={'proxy': 'socks5://user:pass@host:port'}, async_client_args={'proxy': 'socks5://user:pass@host:port'}, ) client=Client(..., http_options=http_options) ``` ``` -------------------------------- ### Function Calling - Manual Declaration and Invocation Source: https://googleapis.github.io/python-genai/_sources/index.rst.txt Illustrates the manual process of declaring a function using `FunctionDeclaration` and passing it as a tool, allowing for explicit control over function invocation. ```APIDOC ## Function Calling - Manual Declaration and Invocation ### Description This example shows how to manually declare a function using `types.FunctionDeclaration` and pass it as a tool. This approach gives you more control over how and when functions are called, as the response will contain function call parts that you can then process. ### Method `client.models.generate_content` ### Parameters #### Request Body - **model** (string) - Required - The model to use for generation. - **contents** (string) - Required - The input prompt for the model. - **config** (types.GenerateContentConfig) - Optional - Configuration for content generation. - **tools** (list[types.FunctionDeclaration]) - Optional - A list of function declarations to be used as tools. ### Request Example ```python from google.genai import types function = types.FunctionDeclaration( name='get_current_weather', # ... other parameters for FunctionDeclaration ... ) # The rest of the example would involve calling generate_content with this function declaration # and then processing the response to manually invoke the function. ``` ```