### Install google-genai with uv Source: https://github.com/googleapis/python-genai/blob/main/README.md Installs the Google GenAI Python client library using uv, an alternative Python package installer. ```sh uv pip install google-genai ``` -------------------------------- ### Install Google Gen AI SDK with uv Source: https://github.com/googleapis/python-genai/blob/main/docs/_sources/index.rst.txt Use uv to install the Google Gen AI Python SDK. ```shell uv pip install google-genai ``` -------------------------------- ### Download File for Upload Example Source: https://github.com/googleapis/python-genai/blob/main/docs/index.html Downloads a sample text file from Google Cloud Storage using "wget" for use in file upload examples. ```bash !wget -q https://storage.googleapis.com/generativeai-downloads/data/a11.txt ``` -------------------------------- ### Install Google Gen AI SDK with uv Source: https://github.com/googleapis/python-genai/blob/main/docs/index.html Use uv to install the Google Gen AI Python SDK into your environment. ```bash uv pip install google-genai ``` -------------------------------- ### Install google-genai with pip Source: https://github.com/googleapis/python-genai/blob/main/README.md Installs the Google GenAI Python client library using pip, the standard Python package installer. ```sh pip install google-genai ``` -------------------------------- ### Install Google Gen AI SDK with pip Source: https://github.com/googleapis/python-genai/blob/main/docs/_sources/index.rst.txt Use pip to install the Google Gen AI Python SDK. ```shell pip install google-genai ``` -------------------------------- ### Install Google Gen AI SDK with pip Source: https://github.com/googleapis/python-genai/blob/main/docs/index.html Use pip to install the Google Gen AI Python SDK into your environment. ```bash pip install google-genai ``` -------------------------------- ### Generate Multimodal Content Stream with Google GenAI Python Client Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html This example demonstrates streaming multimodal content generation, combining a text prompt with an image URI. Ensure the client is initialized as shown in the previous example. ```python async for chunk in await client.aio.models.generate_content_stream( model='gemini-2.0-flash', contents=[ types.Part.from_text('What is shown in this image?'), types.Part.from_uri('gs://generativeai-downloads/images/scones.jpg', 'image/jpeg') ] ): print(chunk.text) # The image shows a flat lay arrangement of freshly baked blueberry # scones. ``` -------------------------------- ### Initialize MCP Client for Experimental Support (Python) Source: https://github.com/googleapis/python-genai/blob/main/docs/_sources/index.rst.txt This snippet shows the initial setup for using experimental Model Context Protocol (MCP) support, including importing necessary libraries and initializing the GenAI client. ```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 ``` -------------------------------- ### Create a client using environment variables Source: https://github.com/googleapis/python-genai/blob/main/docs/_sources/index.rst.txt Initialize a `genai.Client` without explicit parameters when environment variables are configured. ```python from google import genai client = genai.Client() ``` -------------------------------- ### get(*, name, config=None) Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html Gets metadata about a FileSearchStore. ```APIDOC ## get(*, name, config=None) ### Description Gets metadata about a FileSearchStore. ### Method Signature get(*, name, config=None) ### Parameters - **name** (str) - Required - The resource name of the FileSearchStore. Example: FileSearchStores/my-file-search-store-123 - **config** (GetFileSearchStoreConfig | None) - Optional - Optional parameters for the request. ### Returns FileSearchStore - A FileSearchStore object containing the metadata. ``` -------------------------------- ### client.models.generate_content (with system_instruction) Source: https://github.com/googleapis/python-genai/blob/main/docs/index.html Demonstrates how to call `generate_content` with a `system_instruction` and other basic configuration parameters like `max_output_tokens` and `temperature`. ```APIDOC ## SDK Method: client.models.generate_content ### Description This method sends a request to the model to generate content, allowing for basic configuration including a system instruction to guide the model's behavior. ### Method generate_content ### Parameters #### Method Parameters - **model** (string) - Required - The identifier of the model to use (e.g., 'gemini-2.0-flash-001'). - **contents** (string) - Required - The input content for the model. - **config** (types.GenerateContentConfig) - Optional - Configuration settings for content generation. - **system_instruction** (string) - Optional - An instruction provided to the model to influence its behavior. - **max_output_tokens** (integer) - Optional - The maximum number of tokens to generate in the response. - **temperature** (float) - Optional - Controls the randomness of the output. Lower values (near 0) make the 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) ``` ### Response #### Success Response - **text** (string) - The generated text content from the model. ``` -------------------------------- ### get(name, config=None) Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html Gets a batch job. ```APIDOC ## get ### Description Gets a batch job. ### Signature async get(*, name, config=None) ### Parameters - **name** (str) - **config** (None) ``` -------------------------------- ### get(name, config) Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html Gets metadata about a FileSearchStore asynchronously. ```APIDOC ## get(name, config) ### Description Gets metadata about a FileSearchStore. ### Parameters - **name** (str) - Required - The resource name of the FileSearchStore. Example: FileSearchStores/my-file-search-store-123 - **config** (GetFileSearchStoreConfig | None) - Optional - Optional parameters for the request. ### Returns FileSearchStore - A FileSearchStore object containing the metadata. ``` -------------------------------- ### Download a file (Python) Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html This example demonstrates how to find a downloadable file by iterating through the file list and then download its data. Files created by upload cannot be downloaded. ```python for file client.files.list(): if file.download_uri is not None: break else: raise ValueError('No files found with a `download_uri`.') data = client.files.download(file=file) # data = client.files.download(file=file.name) # data = client.files.download(file=file.uri) ``` -------------------------------- ### Python Custom Parser Rubrics Example Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html An example of the list of dictionaries expected when the custom parsing function returns rubrics. ```python [ { \"content\": {\"property\": {\"description\": \"The response is factual.\"}}, \"type\": \"FACTUALITY\", \"importance\": \"HIGH\" }, { \"content\": {\"property\": {\"description\": \"The response is fluent.\"}}, \"type\": \"FLUENCY\", \"importance\": \"MEDIUM\" } ] ``` -------------------------------- ### create(config) Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html Creates a File Search Store synchronously. ```APIDOC ## create(config) ### Description Creates a File Search Store. ### Parameters - **config** (CreateFileSearchStoreConfig | None) - Optional - Optional parameters for the request. ### Returns FileSearchStore ``` -------------------------------- ### Generate video from image with Veo using Python GenAI Source: https://github.com/googleapis/python-genai/blob/main/docs/index.html This example demonstrates generating a video from an input image and an optional prompt. It shows how to load an image from a local file and poll the asynchronous operation for the generated video. Video generation is currently in public preview. ```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() ``` -------------------------------- ### Python Custom Parser Metric Result Example Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html An example of the dictionary expected when the custom parsing function returns a single metric result. ```python { \"score\": 0.8, \"explanation\": \"The model followed most instructions.\", \"rubric_verdicts\": [...] } ``` -------------------------------- ### Create a client for Gemini Developer API Source: https://github.com/googleapis/python-genai/blob/main/docs/index.html Initialize a `genai.Client` instance 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') ``` -------------------------------- ### Create a client for Gemini Developer API Source: https://github.com/googleapis/python-genai/blob/main/docs/_sources/index.rst.txt Initialize a `genai.Client` for the Gemini Developer API, providing your API key. ```python from google import genai # Only run this block for Gemini Developer API client = genai.Client(api_key='GEMINI_API_KEY') ``` -------------------------------- ### Function Parameters Schema Example (YAML-like) Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html This example shows a simple schema for function parameters, defining one required string parameter and one optional integer parameter. ```YAML type: OBJECT properties: param1: type: STRING param2: type: INTEGER required: - param1 ``` -------------------------------- ### AsyncLive.connect() Source: https://github.com/googleapis/python-genai/blob/main/docs/index.html Asynchronously establishes a live connection. ```APIDOC ## AsyncLive.connect() ### Description Asynchronously establishes a live connection. ### Class `AsyncLive` ### Method `connect()` ``` -------------------------------- ### Create a client for Vertex AI API Source: https://github.com/googleapis/python-genai/blob/main/docs/index.html Initialize a `genai.Client` instance for the Vertex AI API, specifying project 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' ) ``` -------------------------------- ### Start Live Session from Audio Stream (Deprecated) Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html This deprecated method starts a live session from an audio stream, processing audio responses. It is recommended to use send_client_content, send_realtime_input, or send_tool_response instead. ```python client = genai.Client(api_key=API_KEY) config = {'response_modalities': ['AUDIO']} async def audio_stream(): stream = read_audio() for data in stream: yield data async with client.aio.live.connect(model='...', config=config) as session: for audio in session.start_stream(stream = audio_stream(), mime_type = 'audio/pcm'): play_audio_chunk(audio.data) ``` -------------------------------- ### Example Return Values for Custom Parsing Function Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html Illustrates the expected JSON structure for the return value of a custom parsing function, showing examples for both rubric results (a list of dictionaries) and a single metric result (a dictionary). ```json [ { "content": {"property": {"description": "The response is factual."}}, "type": "FACTUALITY", "importance": "HIGH" }, { "content": {"property": {"description": "The response is fluent."}}, "type": "FLUENCY", "importance": "MEDIUM" } ] ``` ```json { "score": 0.8, "explanation": "The model followed most instructions.", "rubric_verdicts": [...] } ``` -------------------------------- ### Create a client for Vertex AI API Source: https://github.com/googleapis/python-genai/blob/main/docs/_sources/index.rst.txt Initialize a `genai.Client` for the Vertex AI API, specifying `vertexai=True`, 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' ) ``` -------------------------------- ### Configure generate_content with system instructions in Python Source: https://github.com/googleapis/python-genai/blob/main/docs/index.html Call `client.models.generate_content` with `types.GenerateContentConfig` to set system instructions, maximum output tokens, and temperature for model responses. ```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) ``` -------------------------------- ### AsyncSession.start_stream() Source: https://github.com/googleapis/python-genai/blob/main/docs/index.html Asynchronously starts a streaming session. ```APIDOC ## AsyncSession.start_stream() ### Description Asynchronously starts a streaming session. ### Class `AsyncSession` ### Method `start_stream()` ``` -------------------------------- ### Configure Gemini Model with System Instructions and Parameters (Python) Source: https://github.com/googleapis/python-genai/blob/main/README.md Use GenerateContentConfig to set system instructions, control response length with max_output_tokens, and adjust randomness with temperature for deterministic outputs. ```python from google.genai import types response = client.models.generate_content( model='gemini-3.5-flash', contents='high', config=types.GenerateContentConfig( system_instruction='I say high, you say low', max_output_tokens=3, temperature=0.3, ), ) print(response.text) ``` -------------------------------- ### Caches.get Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html Gets cached content configurations synchronously. ```APIDOC ## Caches.get ### Description Gets cached content configurations. ### Signature ```python get(*, name, config=None) ``` ### Parameters - **name** (string) - Required - The server-generated resource name. - **config** (dict) - Optional - Optional configuration for the get request. ### Returns [`CachedContent`](#genai.types.CachedContent) ### Usage Example ```python client.caches.get(name= ... ) // The server-generated resource name. ``` ``` -------------------------------- ### AsyncCaches.get Source: https://github.com/googleapis/python-genai/blob/main/docs/genai.html Gets cached content configurations asynchronously. ```APIDOC ## AsyncCaches.get ### Description Gets cached content configurations. ### Signature ```python async get(*, name, config=None) ``` ### Parameters - **name** (string) - Required - The server-generated resource name. - **config** (dict) - Optional - Optional configuration for the get request. ### Returns [`CachedContent`](#genai.types.CachedContent) ### Usage Example ```python await client.aio.caches.get(name= ... ) // The server-generated resource name. ``` ``` -------------------------------- ### AsyncSession.start_stream() Source: https://github.com/googleapis/python-genai/blob/main/docs/modules.html Asynchronously starts a stream within the session. ```APIDOC ## `AsyncSession.start_stream()` ### Description Asynchronously starts a stream within the session. ### Class `AsyncSession` ### Method `start_stream` ### Signature `AsyncSession.start_stream()` ```