Google AI Python SDK (Deprecated)

repository·main·Indexed 25 days ago

https://github.com/google-gemini/deprecated-generative-ai-python

Legacy Python SDK for the Gemini API. This repository has reached end-of-life as of November 30, 2025, and is superseded by the unified Google Gen AI SDK. It provides functionality for content generation via GenerativeModel, multi-turn conversations with ChatSession, embeddings, file management, model tuning, and context caching.

Tokens
59K
Snippets
79
Records
429
Agent score
80%

What's inside google-generativeai

  1. Understand the support status of the deprecated Google AI Python SDK

    main

    This repository is considered legacy and has reached its end-of-life.

    Support Status:

    • End-of-Life Date: All support for this repository ended permanently on November 30, 2025.
    • Maintenance: Development is restricted to critical bug fixes only. No new features will be added.
    • Recommendation: Users should plan an immediate migration to the Google Generative AI SDK to ensure access to the latest capabilities and support.
  2. Primary symbols in google.generativeai

    main

    The google.generativeai package contains the core API for interacting with Gemini models. Key functional areas include:

    • Model Interaction: Using GenerativeModel for content generation and ChatSession for multi-turn conversations.
    • Configuration: Setting up credentials via configure and managing generation parameters with GenerationConfig.
    • Content Management: Uploading and managing files via upload_file, list_files, and delete_file.
    • Embeddings: Generating vector representations using embed_content and embed_content_async.
    • Tuning: Creating and managing fine-tuned models with create_tuned_model, list_tuned_models, and update_tuned_model.
    • Caching: Utilizing caching modules and CachedContent for efficient context reuse.
    • System Operations: Managing models, operations, and files through various get_*, list_*, and delete_* methods.
  3. Understand the CodeExecutionResult object

    main

    The CodeExecutionResult object represents the outcome of executing ExecutableCode. It is only generated when using the CodeExecution feature and always follows a part containing the ExecutableCode in the model's response.

    Use this object to inspect whether the code executed successfully and to retrieve the resulting output or error messages.

  4. Configure key requirements in TypedDict

    main

    By default, all keys defined in a TypedDict are required. You can control key presence using two methods:

    1. The total argument

    When defining the class, you can set total=False to make all keys optional. If total=True (the default), all keys must be present.

    class Point2D(TypedDict, total=False):
        x: int
        y: int
    # Any of the keys can be omitted

    2. Required and NotRequired annotations

    For more granular control, use Required and NotRequired (per PEP 655) to specify requirements on a per-key basis within a class.

    from typing import NotRequired
    
    class Point2D(TypedDict):
        x: int             # Required by default
        y: NotRequired[int] # Can be omitted
  5. Understand content safety and blocking types

    main

    The library provides several types to inspect and manage content safety:

    • BlockedReason: A list of reasons why content may have been blocked.
    • HarmCategory: Supported harm categories for Gemini-family models.
    • HarmProbability: Represents the probability that a piece of content is harmful.
    • HarmBlockThreshold: Defines the threshold at and beyond which content should be blocked.
    • SafetySettingDict: Used to configure safety-blocking behavior.
  6. Use the Tool abstraction for function calling and external capabilities

    main

    A Tool is an abstraction that enables the Gemini model to interact with external systems or perform actions outside its internal knowledge base. You can provide a Tool to the model to enable one of three primary capabilities:

    1. Function Calling: By providing function_declarations, you define a list of functions the model can request to call. Note that the model does not execute the code itself. Instead, it returns a FunctionCall containing the arguments, which your client-side application must execute. You then provide the result back to the model via a FunctionResponse in the next conversation turn.
    2. Google Search Retrieval: Using google_search_retrieval, you enable a retrieval tool powered by Google Search to augment the model's responses.
    3. Code Execution: Using code_execution, you enable the model to execute code as part of its generation process.

    Each of these attributes is optional.

  7. Use the ExecutableCode proto for code execution results

    main

    The google.generativeai.protos.ExecutableCode object represents code generated by the model that is intended to be executed, along with the result returned to the model.

    This object is only generated when using the CodeExecution tool. When this tool is active, the code is automatically executed by the system, and a corresponding CodeExecutionResult is generated.

  8. Use the CodeExecution tool for model-driven code execution

    main

    The google.generativeai.protos.CodeExecution tool allows the Gemini model to generate and execute code automatically. When this tool is enabled, the model can write code, run it in a sandboxed environment, and receive the execution results back to inform its next response.

    When using this tool, the model's output may include ExecutableCode (the code generated) and CodeExecutionResult (the output/result of that code).

  9. Configure training data for create_tuned_model

    main

    The training_data argument accepts several formats to define the dataset for tuning:

    • protos.Dataset: A protocol buffer dataset object.
    • Iterable of examples: An iterable containing:
      • protos.TuningExample objects.
      • Dictionaries in the format {'text_input': text_input, 'output': output}.
      • Tuples in the format (text_input, output).
    • Mapping of Iterable[str]: A dictionary where keys represent columns. Use input_key and output_key to specify which columns to use as input and output.
    • CSV files: A local path (str or pathlib.Path), a URL, or a Google Sheets URL. These are processed as a Mapping using pd.read_csv.
    • JSON files: A local path (str or pathlib.Path). The contents are handled as either an Iterable or a Mapping based on the structure.