Google Gen AI Python SDK

repository·main·Indexed 26 days ago

https://github.com/googleapis/python-genai

A programmatic interface for integrating Google's generative models (Gemini) into Python applications. The SDK supports both the Gemini Developer API and the Gemini Enterprise Agent Platform, providing capabilities for text and image generation, multi-turn chat sessions, tool calling with automatic Python function support, and structured JSON responses. It also includes support for Imagen (image generation/editing), Veo (video generation), context caching, and the Interactions API for unified model and agent interaction.

Tokens
10.1K
Snippets
32
Records
49
Agent score
87%

What's inside google-genai

  1. Generate Multimodal Output (Images)

    main

    To generate multimodal outputs like images, use a model that supports it (e.g., gemini-3-pro-image-preview) and specify response_modalities=['IMAGE'] in the client.interactions.create call. The output will contain data that can be decoded from base64.

    import base64
    
    interaction = client.interactions.create(
        model='gemini-3-pro-image-preview',
        input='Generate an image of a futuristic city.',
        response_modalities=['IMAGE']
    )
    
    for output in interaction.outputs:
        if output.type == 'image':
            with open("generated_city.png", "wb") as f:
                f.write(base64.b64decode(output.data))
  2. Run Batch Predictions with Gemini Developer API

    main

    For the Gemini Developer API, you can create batch jobs using inlined requests or by uploading a JSON file containing requests.

    # Create a batch job with inlined requests
    batch_job = client.batches.create(
        model="gemini-3.5-flash",
        src=[{
            "contents": [{
                "parts": [{
                    "text": "Hello!",
                }],
                "role": "user",
            }],
            "config": {"response_modalities": ["text"]},
        }],
    )
    
    job
  3. Run Batch Predictions with Gemini Enterprise Agent Platform

    main

    For Gemini Enterprise Agent Platform, you can create batch jobs by specifying a model and a data source. The destination and job display name are automatically populated. Supported sources include BigQuery (bq://) and Google Cloud Storage (gs://).

    # Specify model and source file only, destination and job display name will be auto-populated
    job = client.batches.create(
        model='gemini-3.5-flash',
        src='bq://my-project.my-dataset.my-table',  # or "gs://path/to/input/data"
    )
    
    print(job)
  4. Create Batch Jobs using uploaded JSON files

    main

    To create a batch job from a file, first upload a JSON file where each line is a valid request object (e.g., myrequests.json). Then, use the returned file identifier as the src in client.batches.create.

    # Upload the file
    file = client.files.upload(
        file='myrequests.json',
        config=types.UploadFileConfig(display_name='test-json')
    )
    
    # Create a batch job with file name
    batch_job = client.batches.create(
        model="gemini-3.5-flash",
        src="files/test-json",
    )
  5. Create multi-turn chat sessions

    main

    Use client.chats.create to initialize a chat session. You can then use send_message for synchronous non-streaming responses, send_message_stream for synchronous streaming, or the asynchronous equivalents via client.aio.chats.create for send_message and send_message_stream.

    # Synchronous Non-Streaming
    chat = client.chats.create(model='gemini-3.5-flash')
    response = chat.send_message('tell me a story')
    print(response.text)
    
    # Synchronous Streaming
    chat = client.chats.create(model='gemini-3.5-flash')
    for chunk in chat.send_message_stream('tell me a story'):
        print(chunk.text)
    
    # Asynchronous Non-Streaming
    chat = client.aio.chats.create(model='gemini-3.5-flash')
    response = await chat.send_message('tell me a story')
    print(response.text)
    
    # Asynchronous Streaming
    chat = client.aio.chats.create(model='gemini-3.5-flash')
    async for chunk in await chat.send_message_stream('tell me a story'):
        print(chunk.text)
  6. Manually invoke functions for tool calling

    main

    To bypass automatic calling, manually declare a function using types.FunctionDeclaration, pass it via types.Tool, and handle the response.function_calls by executing the function and passing a types.Part.from_function_response back to the model.

    from google.genai import types
    
    function = types.FunctionDeclaration(
        name='get_current_weather',
        description='Get the current weather in a given location',
        parameters_json_schema={
            'type': 'object',
            'properties': {
                'location': {
                    'type': 'string',
                    'description': 'The city and state, e.g. San Francisco, CA',
                }
            },
            'required': ['location'],
        },
    )
    
    tool = types.Tool(function_declarations=[function])
    
    response = client.models.generate_content(
        model='gemini-3.5-flash',
        contents='What is the weather like in Boston?',
        config=types.GenerateContentConfig(tools=[tool]),
    )
    
    # Handle the function call manually
    function_call_part = response.function_calls[0]
    # ... execute function ...
    function_response_part = types.Part.from_function_response(
        name=function_call_part.name,
        response={'result': 'sunny'},
    )
    function_response_content = types.Content(role='tool', parts=[function_response_part])
    
    # Send response back to model
    response = client.models.generate_content(
        model='gemini-3.5-flash',
        contents=[
            user_prompt_content, # original user prompt
            response.candidates[0].content, # the model's function call
            function_response_content, # your function result
        ],
        config=types.GenerateContentConfig(tools=[tool]),
    )
  7. Initialize a Client for Gemini Developer API

    main

    To use the Gemini Developer API, create a client by providing an api_key. You can also use environment variables GEMINI_API_KEY or GOOGLE_API_KEY (where GOOGLE_API_KEY takes precedence) to avoid passing the key explicitly.

    from google import genai
    
    # Using an explicit API key
    client = genai.Client(api_key='GEMINI_API_KEY')
    
    # Or using environment variables (set GEMINI_API_KEY or GOOGLE_API_KEY)
    client = genai.Client()
  8. Initialize a Client for Agent Platform

    main
    To use the Gemini Enterprise Agent Platform (formerly Vertex AI), initialize the client with enterprise=True and specify your project and location. You can also use environment variables GOOGLE_GENAI_USE_ENTERPRISE=true, GOOGLE_CLOUD_PROJECT, and GOOGLE_CLOUD_LOCATION to configure the client automatically.
  9. Fine-tune models with the Tunings API

    main
    Supported in Gemini Enterprise Agent Platform, client.tunings.tune allows supervised fine-tuning. You provide a base_model and a training_dataset (which can be a types.TuningDataset pointing to a GCS URI). You can monitor the job status using client.tunings.get and check for states like JOB_STATE_SUCCEEDED. Once completed, use the tuned_model.endpoint as the model name in client.models.generate_content.
  10. Use the Interactions API for unified model and agent interaction

    main

    The Interactions API provides a unified interface for Gemini models and agents, supporting stateful conversations, multimodal input, and tool orchestration.

    • Basic Interaction: Use client.interactions.create with a model and input.
    • Stateful Conversation: Pass previous_interaction_id to client.interactions.create to continue a conversation.
    • Agents (Deep Research): Use specialized agents (e.g., deep-research-pro-preview-12-2025) by passing the agent name. For long-running tasks, set background=True and poll client.interactions.get(id=...) until the status is completed.
    # Stateful Conversation
    interaction1 = client.interactions.create(model='gemini-3.5-flash', input='Hi, my name is Amir.')
    interaction2 = client.interactions.create(
        model='gemini-3.5-flash', 
        input='What is my name?', 
        previous_interaction_id=interaction1.id
    )
    print(interaction2.outputs[-1].text)
  11. Use Model Context Protocol (MCP) support

    main

    MCP support is experimental.

    Gemini Developer API: Pass a local MCP server (using mcp library) as a tool directly in the config.tools list.

    Gemini Enterprise Agent Platform: Provide the MCP tool via types.Tool(mcp_servers=[types.McpServer(name='...')]).