### Install Dependencies Source: https://github.com/google-gemini/api-examples/blob/main/python/README.md Installs the necessary Python packages for the Gemini API examples. ```bash pip install absl-py google-genai Pillow pyink ``` -------------------------------- ### Go Installation Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Install the google.golang.org/genai Go package. ```bash go get google.golang.org/genai ``` -------------------------------- ### Text Generation Example - Go Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Example of generating content using the Go SDK. ```go response, err := client.Models.GenerateContent( ctx, "gemini-3.5-flash", contents, nil, ) ``` -------------------------------- ### JavaScript/TypeScript Installation Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Install the @google/genai npm package. ```bash npm install @google/genai ``` -------------------------------- ### Python Installation Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Install the google-genai Python package using pip. ```bash pip install google-genai ``` -------------------------------- ### Text Generation Example - Java Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Example of generating content using the Java SDK. ```java GenerateContentResponse response = client.models.generateContent( "gemini-3.5-flash", "Write a story...", null ); ``` -------------------------------- ### Text Generation Example - Python Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Example of generating content using the Python SDK. ```python response = client.models.generate_content( model="gemini-3.5-flash", contents="Write a story..." ) ``` -------------------------------- ### Client Initialization + Request Examples Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Demonstrates client initialization and making a content generation request across Python, JavaScript, Go, and Java SDKs. ```python # Python client = genai.Client() response = client.models.generate_content(...) ``` ```javascript # JavaScript const ai = new GoogleGenAI({...}); const response = await ai.models.generateContent({...}); ``` ```go # Go client, _ := genai.NewClient(ctx, ...) response, _ := client.Models.GenerateContent(ctx, ...) ``` ```java # Java Client client = new Client(); GenerateContentResponse response = client.models.generateContent(...); ``` -------------------------------- ### JavaScript/TypeScript Browser and Node.js Usage Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Example of initializing the client in both Node.js and browser environments. ```typescript // Node.js with environment variable const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); // Browser (pass API key explicitly) const ai = new GoogleGenAI({ apiKey: userProvidedKey }); ``` -------------------------------- ### Text Generation Example - JavaScript Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Example of generating content using the JavaScript SDK. ```typescript const response = await ai.models.generateContent({ model: "gemini-3.5-flash", contents: "Write a story..." }); ``` -------------------------------- ### Run tests Source: https://github.com/google-gemini/api-examples/blob/main/go/README.md Command to run tests for the Go examples. ```bash go test ``` -------------------------------- ### System Instruction Example (Python) Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/api-reference/configuration.md Sets a system instruction to guide the model's behavior. ```python from google import genai from google.genai import types client = genai.Client() response = client.models.generate_content( model="gemini-3.5-flash", contents="Good morning! How are you?", config=types.GenerateContentConfig( system_instruction="You are a cat. Your name is Neko." ), ) print(response.text) ``` -------------------------------- ### System Instruction Example (JavaScript) Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/api-reference/configuration.md Sets a system instruction to guide the model's behavior. ```typescript const response = await ai.models.generateContent({ model: "gemini-3.5-flash", contents: "Good morning! How are you?", config: { systemInstruction: "You are a cat. Your name is Neko." }, }); ``` -------------------------------- ### Go - GenerateContentConfig Example Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/api-reference/configuration.md Example usage of GenerateContentConfig in Go for a content generation request. ```go config := &genai.GenerateContentConfig{ MaxOutputTokens: 20, Temperature: 1.0, StopSequences: []string{"x"}, } response, err := client.Models.GenerateContent(ctx, "gemini-3.5-flash", contents, config) ``` -------------------------------- ### Go Client Initialization Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Initialize the genai client in Go. ```go ctx := context.Background() client, err := genai.NewClient(ctx, &genai.ClientConfig{ APIKey: os.Getenv("GEMINI_API_KEY"), Backend: genai.BackendGeminiAPI, }) if err != nil { log.Fatal(err) } ``` -------------------------------- ### JavaScript/TypeScript Type Definitions Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Example of TypeScript interfaces for request and response objects. ```typescript interface GenerateContentRequest { model: string; contents: string | Content[]; config?: GenerateContentConfig; } interface GenerateContentResponse { text: string; candidates: Candidate[]; usageMetadata: UsageMetadata; } ``` -------------------------------- ### Get Model Info Response Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/endpoints.md Example JSON response with details for a specific model. ```json { "name": "models/gemini-3.5-flash", "displayName": "Gemini 3.5 Flash", "description": "...", "inputTokenLimit": 30720, "outputTokenLimit": 2048, "supportedGenerationMethods": [ "generateContent", "streamGenerateContent", "embedContent" ] } ``` -------------------------------- ### Get File Java Example Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/api-reference/files.md Retrieves a specific file by its name using the Java SDK. ```java File file = client.files.get(String name) ``` -------------------------------- ### Content Creation (Go) Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Creating content for prompts in Go. ```Go contents := []*genai.Content{ genai.NewContentFromText("Your prompt", genai.RoleUser), } ``` -------------------------------- ### Get File Go Example Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/api-reference/files.md Retrieves a specific file by its name using the Go SDK. ```go file, err := client.Files.Get( ctx context.Context, name string, ) if err != nil { log.Fatal(err) } ``` -------------------------------- ### Run a Test File Source: https://github.com/google-gemini/api-examples/blob/main/python/README.md Command to run a Python test file. ```bash python .py ``` -------------------------------- ### Python Resource Management (Cache Deletion) Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Example of deleting a cache resource when no longer needed. ```python # Delete cache when done client.caches.delete(name=cache.name) ``` -------------------------------- ### Client Initialization (Java) Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Initializing the client in Java. API key is read from GEMINI_API_KEY environment variable. ```java Client client = new Client(); ``` -------------------------------- ### Python Resource Management (File Deletion) Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Example of deleting a file resource when no longer needed. ```python # Delete file when done client.files.delete(name=file.name) ``` -------------------------------- ### Go Error Handling Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/api-reference/client-initialization.md Demonstrates error handling for client initialization in Go by checking the returned error. ```go client, err := genai.NewClient(ctx, config) if err != nil { log.Fatal(err) } ``` -------------------------------- ### Schema-Driven Output Example Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/api-reference/configuration.md Demonstrates generating content that conforms to a specific Python TypedDict schema, ensuring JSON output. ```python from typing_extensions import TypedDict class Recipe(TypedDict): recipe_name: str ingredients: list[str] result = client.models.generate_content( model="gemini-3.5-flash", contents="List a few popular cookie recipes.", config=types.GenerateContentConfig( response_mime_type="application/json", response_schema=list[Recipe] ), ) ``` -------------------------------- ### Python text generation example Source: https://github.com/google-gemini/api-examples/blob/main/README.md A Python example demonstrating text generation using the Gemini API. ```python def test_text_gen_text_only_prompt(self): # [START text_gen_text_only_prompt] from google import genai client = genai.Client() response = client.models.generate_content( model="gemini-3.5-flash", contents="Write a story about a magic backpack." ) print(response.text) # [END text_gen_text_only_prompt] ``` -------------------------------- ### Go Client Resources Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/api-reference/client-initialization.md Lists the resource managers available through the Go client instance. ```go client.Models // Models API client.Files // Files API client.Chats // Chats API client.Caches // Caches API ``` -------------------------------- ### GroundingChunk Example Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/types.md Example of accessing GroundingChunk properties. ```Python chunk = types.GroundingChunk( maps: MapsChunk | None = None, web: WebChunk | None = None, # ... other source types ) maps_chunk = chunk.maps # maps_chunk properties: # - title: str (location name) # - uri: str (link to location) ``` -------------------------------- ### UsageMetadata Example Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/types.md Example of how to print usage metadata. ```Python response = client.models.generate_content(...) print(response.usage_metadata) # UsageMetadata(prompt_token_count=11, candidates_token_count=73, total_token_count=84) ``` -------------------------------- ### Go Import Patterns Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Import necessary packages for Go SDK. ```go import ( "context" "google.golang.org/genai" ) ``` -------------------------------- ### GenerateContentStream Java Example Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/api-reference/text-generation.md Example usage of the generateContentStream method in Java. ```java ResponseStream responseStream = client.models.generateContentStream( "gemini-3.5-flash", "Write a story about a magic backpack.", null ); StringBuilder response = new StringBuilder(); for (GenerateContentResponse res : responseStream) { System.out.print(res.text()); response.append(res.text()); } responseStream.close(); ``` -------------------------------- ### Resource Management (Java) Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/language-guide.md Using try-with-resources for streams in Java. ```java try (ResponseStream stream = client.models.generateContentStream(...)) { for (GenerateContentResponse res : stream) { // Process } } ``` -------------------------------- ### GenerateContent Java Example Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/api-reference/text-generation.md Example usage of the generateContent method in Java. ```java Client client = new Client(); GenerateContentResponse response = client.models.generateContent( "gemini-3.5-flash", "Write a story about a magic backpack.", null ); System.out.println(response.text()); ``` -------------------------------- ### Java Error Handling Source: https://github.com/google-gemini/api-examples/blob/main/_autodocs/api-reference/client-initialization.md Demonstrates basic error handling for client initialization in Java using a try-catch block. ```java try { Client client = new Client(); // Use client... } catch (Exception e) { e.printStackTrace(); } ```