# googleapis/go-genai architecture

> Architecture diagram of the GitHub repository googleapis/go-genai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/googleapis/go-genai
- Repository: https://github.com/googleapis/go-genai

## Overview

##### Project Type and Purpose
- This is a Go SDK that enables developers to access Google’s generative AI capabilities (including both text-only and multimodal features like Gemini and Vertex AI).
- Its primary purpose is to abstract the complex interactions with Google’s generative AI backends, exposing simple client APIs that let developers integrate AI-powered content generation into their applications.

##### Analyzing the File Structure
- Top-Level Go Files: Files like api_client.go, client.go, models.go, operations.go, transformer.go, and pages.go indicate modularized functionality.
- Test Files: A large number of tests (e.g., *_test.go files, testdata directory) show high emphasis on reliability and validation of each component.
- Samples Directory: Contains multiple examples (e.g., generate_text.go, live_streaming_server.go) which demonstrate real usage patterns of the SDK.
- Configuration Files: Files such as go.mod, go.sum, and GitHub workflows show build, dependency and CI/CD setup.

##### Core Components
a. SDK Client and Configuration:
- Components: client.go, api_client.go, replay_api_client.go
- Role: Provide client instantiation functions (e.g., NewClient) that accept configuration parameters (e.g., API keys, project details) and set the backend (Gemini API vs. Vertex AI).

b. API Interaction & Communication Layer:
- Components: operations.go, live.go, pages.go
- Role: Handle actual API operations (sending requests, streaming real-time data for multimodal live support) and manage various endpoints.

c. Data Models and Helpers:
- Components: models.go, models_helpers.go, types.go, and their related test files
- Role: Define the data structures (including parts like text, inline image data) required for API interaction.

d. Utility Components:
- Components: caches.go, common.go, transformer.go
- Role: Support the main operations by providing caching mechanisms and transformation utilities (potentially for data conversion, error handling or intermediate processing).

e. Sample Use Cases:
- Components: Files in the samples/ directory (e.g., generate_text.go, live_streaming_server.go)
- Role: Demonstrate how to integrate and use the SDK in various scenarios including live-streaming and multimodal content generation.

f. Testing and CI/CD:
- Components: .github/workflows, testdata, and many *_test.go files
- Role: Ensure that each module behaves as expected under different scenarios and inputs.

##### Relationships and Interactions
- Data flow from “SDK Client” to “API Operations” where generation requests are processed.
- “Data Models & Utils” providing necessary formatted data to “API Operations” and receiving responses to format them back.
- “Utilities” such as caching and transformation modules working alongside the API operations to improve performance and data integrity.
- Communication with external services (Google Gemini API and Vertex AI endpoints) from the “API Operations” component.
- Samples representing usage scenarios that tie back to the “SDK Client” and invoke the operations chain.

##### Architectural Patterns and Design Principles
- Separation of concerns: Divide the system into clear areas – client initialization, API communications, data modeling, and auxiliary utilities.
- Modularity: Each .go file or related group (e.g., models, operations, transformations) represents a module with a single responsibility.
- Testability: Notice how each component has its own test suite, indicating an emphasis on well-defined interfaces.
- Extensibility: The structure easily accommodates additional APIs or services by adding new modules or expanding the client configuration.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% SDK Client Layer
    subgraph "SDK Client Layer"
        sdk1["Client"] 
        sdk2["API Client"]
        sdk3["Replay API Client"]
    end

    %% API Communication Layer
    subgraph "API Communication Layer"
        api1["Operations"]
        api2["Live"]
        api3["Pages"]
    end

    %% Data Models & Helpers
    subgraph "Data Models & Helpers"
        dm1["Models"]
        dm2["Models Helpers"]
        dm3["Types"]
    end

    %% Utility Services
    subgraph "Utility Services"
        util1["Caches"]
        util2["Common"]
        util3["Transformer"]
    end

    %% External AI Services
    subgraph "External AI Services"
        ext["Google Gemini API / Vertex AI"]
    end

    %% Sample Applications
    subgraph "Sample Applications"
        sample["Sample Use Cases"]
    end

    %% CI/CD & Testing
    subgraph "CI/CD & Testing"
        test["CI/CD & Test Suite"]
    end

    %% Relationships
    %% SDK Client -> API Communication Layer
    sdk1 -->|"calls"| api1
    sdk2 -->|"calls"| api1
    sdk3 -->|"calls"| api1

    %% Data Models & Helpers -> API Communication
    dm1 -->|"provides"| api1
    dm2 -->|"provides"| api1
    dm3 -->|"provides"| api1

    %% Utility Services -> API Communication
    util1 -->|"supports"| api1
    util2 -->|"supports"| api1
    util3 -->|"supports"| api1

    %% API Communication -> External AI Services
    api1 -->|"requests"| ext
    api2 -->|"requests"| ext
    api3 -->|"requests"| ext

    %% SDK Client Layer -> Sample Applications
    sdk1 -->|"usage"| sample
    sdk2 -->|"usage"| sample
    sdk3 -->|"usage"| sample

    %% CI/CD & Testing connected to major components (dashed lines)
    test --- sdk1
    test --- dm1
    test --- util1
    test --- api1
    test --- ext
    test --- sample

    %% Click Events for SDK Client Layer
    click sdk1 "https://github.com/googleapis/go-genai/blob/main//client.go"
    click sdk2 "https://github.com/googleapis/go-genai/blob/main//api_client.go"
    click sdk3 "https://github.com/googleapis/go-genai/blob/main//replay_api_client.go"

    %% Click Events for API Communication Layer
    click api1 "https://github.com/googleapis/go-genai/blob/main//operations.go"
    click api2 "https://github.com/googleapis/go-genai/blob/main//live.go"
    click api3 "https://github.com/googleapis/go-genai/blob/main//pages.go"

    %% Click Events for Data Models & Helpers
    click dm1 "https://github.com/googleapis/go-genai/blob/main//models.go"
    click dm2 "https://github.com/googleapis/go-genai/blob/main//models_helpers.go"
    click dm3 "https://github.com/googleapis/go-genai/blob/main//types.go"

    %% Click Events for Utility Services
    click util1 "https://github.com/googleapis/go-genai/blob/main//caches.go"
    click util2 "https://github.com/googleapis/go-genai/blob/main//common.go"
    click util3 "https://github.com/googleapis/go-genai/blob/main//transformer.go"

    %% Click Event for Sample Applications
    click sample "https://github.com/googleapis/go-genai/tree/main//samples"

    %% Click Events for CI/CD & Testing
    click test "https://github.com/googleapis/go-genai/blob/main//api_client_test.go"
    click test "https://github.com/googleapis/go-genai/blob/main//client_test.go"
    click test "https://github.com/googleapis/go-genai/blob/main//live_test.go"
    click test "https://github.com/googleapis/go-genai/blob/main//models_helpers_test.go"
    click test "https://github.com/googleapis/go-genai/blob/main//models_test.go"
    click test "https://github.com/googleapis/go-genai/blob/main//pages_test.go"
    click test "https://github.com/googleapis/go-genai/blob/main//replay_sanitizer_test.go"
    click test "https://github.com/googleapis/go-genai/blob/main//transformer_test.go"
    click test "https://github.com/googleapis/go-genai/blob/main//types_test.go"
    click test "https://github.com/googleapis/go-genai/tree/main//testdata"
    click test "https://github.com/googleapis/go-genai/tree/main//github/workflows"

    %% Class styles
    classDef sdkClient fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef apiOps fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef dataModels fill:#A9DFBF,stroke:#1E8449,stroke-width:2px;
    classDef utilities fill:#F5CBA7,stroke:#CA6F1E,stroke-width:2px;
    classDef external fill:#D7BDE2,stroke:#6C3483,stroke-width:2px;
    classDef samples fill:#FAD7A0,stroke:#D68910,stroke-width:2px;
    classDef testing fill:#F1948A,stroke:#922B21,stroke-width:2px;

    %% Assign classes
    class sdk1,sdk2,sdk3 sdkClient;
    class api1,api2,api3 apiOps;
    class dm1,dm2,dm3 dataModels;
    class util1,util2,util3 utilities;
    class ext external;
    class sample samples;
    class test testing;
```
