SPB Git

Repository Public

file-input-methods.md 29.4 KB 849 lines markdown

1This guide explains the different ways you can include media files such as2images, audio, video, and documents when making requests to the Gemini API.3The new methods are supported in all of the Gemini API endpoints, including4Batch, Interactions and Live API.5Choosing the right method depends on the size of your file, where your data is6stored, and how frequently you plan to use the file.78The simplest way to include a file as your input is to read a local file and9include it in a prompt. The following example shows how to read a local PDF10file. PDFs are limited to 50MB for this method. See the11[Input method comparison table](https://ai.google.dev/gemini-api/docs/file-input-methods#method-comparison) for a complete list of file12input types and limits.1314### Python1516    from google import genai17    import pathlib18    import base641920    client = genai.Client()2122    filepath = pathlib.Path('my_local_file.pdf')2324    prompt = "Summarize this document"25    interaction = client.interactions.create(26        model="gemini-3.8-flash",27        input=[28            {"type": "text", "text": prompt},29            {"type": "document", "data": base64.b64encode(filepath.read_bytes()).decode('utf-8'), "mime_type": "application/pdf"}30        ]31    )32    print(interaction.output_text)3334### JavaScript3536    import { GoogleGenAI } from "@google/genai";37    import * as fs from 'node:fs';3839    const client = new GoogleGenAI({});40    const prompt = "Summarize this document";4142    async function main() {43        const filePath = 'my_local_file.pdf';4445        const interaction = await client.interactions.create({46            model: "gemini-3.8-flash",47            input: [48                { type: "text", text: prompt },49                {50                    type: "document",51                    data: fs.readFileSync(filePath).toString("base64"),52                    mime_type: "application/pdf"53                }54            ]55        });56        console.log(interaction.output_text);57    }5859    main();6061### Java6263    import com.google.genai.Client;64    import com.google.genai.gaos.models.interactions.Content;65    import com.google.genai.gaos.models.interactions.CreateModelInteraction;66    import com.google.genai.gaos.models.interactions.DocumentContent;67    import com.google.genai.gaos.models.interactions.DocumentContentMimeType;68    import com.google.genai.gaos.models.interactions.Interaction;69    import com.google.genai.gaos.models.interactions.InteractionsInput;70    import com.google.genai.gaos.models.interactions.Model;71    import com.google.genai.gaos.models.interactions.TextContent;72    import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;73    import java.nio.file.Files;74    import java.nio.file.Paths;75    import java.util.Arrays;76    import java.util.Base64;77    import java.util.List;7879    Client client = new Client();8081    byte[] pdfBytes = Files.readAllBytes(Paths.get("my_local_file.pdf"));82    String base64Pdf = Base64.getEncoder().encodeToString(pdfBytes);8384    String prompt = "Summarize this document";8586    Content textContent = TextContent.builder().text(prompt).build();87    Content docContent =88        DocumentContent.builder()89            .data(base64Pdf)90            .mimeType(DocumentContentMimeType.APPLICATION_PDF)91            .build();9293    List<Content> contents = Arrays.asList(textContent, docContent);9495    CreateModelInteraction params =96        CreateModelInteraction.builder()97            .model(Model.of("gemini-3.8-flash"))98            .input(InteractionsInput.ofContent(contents))99            .build();100101    Interaction interaction =102        client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();103    System.out.println(interaction.outputText().orElse(""));104105### REST106107    # Encode the local file to base64108    B64_CONTENT=$(base64 -w 0 my_local_file.pdf)109110    curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \111      -H "x-goog-api-key: $GEMINI_API_KEY" \112      -H 'Content-Type: application/json' \113      -d '{114        "model": "gemini-3.8-flash",115        "input": [116          {"type": "text", "text": "Summarize this document"},117          {118            "type": "document",119            "data": "'${B64_CONTENT}'",120            "mime_type": "application/pdf"121          }122        ]123      }'124125## Input method comparison126127The following table compares each input method with file limits and best use128cases. Note that the file size limit may vary depending on the file type and129model or tokenizer used to process the file.130131| Method | Best for | Max file size | Persistence |132|---|---|---|---|133| **Inline data** | Quick testing, small files, real-time applications. | 100 MB per request or payload (**50 MB for PDFs**) | None (sent with every request) |134| **File API upload** | Large files, files used multiple times. | 2 GB per file, up to 20GB per project | 48 Hours |135| **File API GCS URI registration** | Large files already in Google Cloud Storage, files used multiple times. | 2 GB per file, no overall storage limits | None (fetched per request). One time registration can give access for up to 30 days. |136| **External URLs** | Public data or data in cloud buckets (AWS, Azure, GCS) without re-uploading. | 100 MB per request/payload | None (fetched per request) |137138## Inline data139140For smaller files (under 100MB, or 50MB for PDFs), you can pass the data141directly in the request payload. This is the simplest method for quick tests or142applications handling real-time, transient data. You can provide data as143base64 encoded strings or by reading local files directly.144145For an example of reading from a local file, see the example at the beginning of146this page.147148### Fetch from a URL149150You can also fetch a file from a URL, convert it to bytes, and include it in the151input.152153### Python154155    from google import genai156    import httpx157158    client = genai.Client()159160    doc_url = "https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf"161    doc_data = httpx.get(doc_url).content162163    prompt = "Summarize this document"164165    interaction = client.interactions.create(166        model="gemini-3.8-flash",167        input=[168            {"type": "document", "data": base64.b64encode(doc_data).decode('utf-8'), "mime_type": "application/pdf"},169            {"type": "text", "text": prompt}170        ]171    )172    print(interaction.output_text)173174### JavaScript175176    import { GoogleGenAI } from "@google/genai";177178    const client = new GoogleGenAI({});179    const docUrl = 'https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf';180    const prompt = "Summarize this document";181182    async function main() {183        const pdfResp = await fetch(docUrl)184          .then((response) => response.arrayBuffer());185186        const interaction = await client.interactions.create({187            model: "gemini-3.8-flash",188            input: [189                { type: "text", text: prompt },190                {191                    type: "document",192                    data: Buffer.from(pdfResp).toString("base64"),193                    mime_type: "application/pdf"194                }195            ]196        });197        console.log(interaction.output_text);198    }199200    main();201202### Java203204    import com.google.genai.Client;205    import com.google.genai.gaos.models.interactions.Content;206    import com.google.genai.gaos.models.interactions.CreateModelInteraction;207    import com.google.genai.gaos.models.interactions.DocumentContent;208    import com.google.genai.gaos.models.interactions.DocumentContentMimeType;209    import com.google.genai.gaos.models.interactions.Interaction;210    import com.google.genai.gaos.models.interactions.InteractionsInput;211    import com.google.genai.gaos.models.interactions.Model;212    import com.google.genai.gaos.models.interactions.TextContent;213    import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;214    import java.net.URI;215    import java.net.http.HttpClient;216    import java.net.http.HttpRequest;217    import java.net.http.HttpResponse;218    import java.util.Arrays;219    import java.util.Base64;220    import java.util.List;221222    Client client = new Client();223224    String docUrl = "https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf";225    HttpClient httpClient = HttpClient.newHttpClient();226    HttpRequest request = HttpRequest.newBuilder().uri(URI.create(docUrl)).build();227    byte[] docData = httpClient.send(request, HttpResponse.BodyHandlers.ofByteArray()).body();228    String base64Pdf = Base64.getEncoder().encodeToString(docData);229230    String prompt = "Summarize this document";231232    Content docContent =233        DocumentContent.builder()234            .data(base64Pdf)235            .mimeType(DocumentContentMimeType.APPLICATION_PDF)236            .build();237    Content textContent = TextContent.builder().text(prompt).build();238239    List<Content> contents = Arrays.asList(docContent, textContent);240241    CreateModelInteraction params =242        CreateModelInteraction.builder()243            .model(Model.of("gemini-3.8-flash"))244            .input(InteractionsInput.ofContent(contents))245            .build();246247    Interaction interaction =248        client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();249    System.out.println(interaction.outputText().orElse(""));250251### REST252253    DOC_URL="https://discovery.ucl.ac.uk/id/eprint/10089234/1/343019_3_art_0_py4t4l_convrt.pdf"254    PROMPT="Summarize this document"255    DISPLAY_NAME="base64_pdf"256257    # Download the PDF258    wget -O "${DISPLAY_NAME}.pdf" "${DOC_URL}"259260    # Check for FreeBSD base64 and set flags accordingly261    if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then262      B64FLAGS="--input"263    else264      B64FLAGS="-w0"265    fi266267    # Base64 encode the PDF268    ENCODED_PDF=$(base64 $B64FLAGS "${DISPLAY_NAME}.pdf")269270    # Create JSON payload file271    cat <<EOF > payload.json272    {273    "model": "gemini-3.8-flash",274    "input": [275    {"type": "document", "data": "${ENCODED_PDF}", "mime_type": "application/pdf"},276    {"type": "text", "text": "${PROMPT}"}277    ]278    }279    EOF280281    # Generate content using interactions282    curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \283        -H "x-goog-api-key: $GEMINI_API_KEY" \284        -H 'Content-Type: application/json' \285        -d @payload.json 2> /dev/null > response.json286287    cat response.json288    echo289290    jq ".outputs[] | select(.type == \"text\") | .text" response.json291292## Gemini File API293294The File API is designed for larger files (up to 2GB) or files you intend to295use in multiple requests.296297### Standard file upload298299Upload a local file to the Gemini API. Files uploaded this way are stored300temporarily (48 hours) and processed for efficient retrieval by the model.301302### Python303304    from google import genai305306    client = genai.Client()307308    doc_file = client.files.upload(file="path/to/your/sample.pdf")309    prompt = "Summarize this document"310311    interaction = client.interactions.create(312        model="gemini-3.8-flash",313        input=[314            {"type": "text", "text": prompt},315            {"type": "document", "uri": doc_file.uri, "mime_type": doc_file.mime_type}316        ]317    )318    print(interaction.output_text)319320### JavaScript321322    import { GoogleGenAI } from "@google/genai";323324    const client = new GoogleGenAI({});325    const prompt = "Summarize this document";326327    async function main() {328      const filePath = "path/to/your/sample.pdf";329330      const myfile = await client.files.upload({331        file: filePath,332        config: { mime_type: "application/pdf" },333      });334335      const interaction = await client.interactions.create({336        model: "gemini-3.8-flash",337        input: [338            { type: "text", text: prompt },339            { type: "document", uri: myfile.uri, mime_type: myfile.mimeType }340        ]341      });342      console.log(interaction.output_text);343    }344345    await main();346347### Java348349    import com.google.genai.Client;350    import com.google.genai.gaos.models.interactions.Content;351    import com.google.genai.gaos.models.interactions.CreateModelInteraction;352    import com.google.genai.gaos.models.interactions.DocumentContent;353    import com.google.genai.gaos.models.interactions.DocumentContentMimeType;354    import com.google.genai.gaos.models.interactions.Interaction;355    import com.google.genai.gaos.models.interactions.InteractionsInput;356    import com.google.genai.gaos.models.interactions.Model;357    import com.google.genai.gaos.models.interactions.TextContent;358    import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;359    import com.google.genai.types.File;360    import com.google.genai.types.UploadFileConfig;361    import java.util.Arrays;362    import java.util.List;363364    Client client = new Client();365366    File docFile =367        client.files.upload(368            new java.io.File("path/to/your/sample.pdf"),369            UploadFileConfig.builder().mimeType("application/pdf").build());370371    String prompt = "Summarize this document";372373    Content textContent = TextContent.builder().text(prompt).build();374    Content docContent =375        DocumentContent.builder()376            .uri(docFile.uri().orElse(""))377            .mimeType(DocumentContentMimeType.of(docFile.mimeType().orElse("application/pdf")))378            .build();379380    List<Content> contents = Arrays.asList(textContent, docContent);381382    CreateModelInteraction params =383        CreateModelInteraction.builder()384            .model(Model.of("gemini-3.8-flash"))385            .input(InteractionsInput.ofContent(contents))386            .build();387388    Interaction interaction =389        client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();390    System.out.println(interaction.outputText().orElse(""));391392### REST393394    FILE_PATH="path/to/sample.pdf"395    MIME_TYPE=$(file -b --mime-type "${FILE_PATH}")396    NUM_BYTES=$(wc -c < "${FILE_PATH}")397    DISPLAY_NAME=DOCUMENT398399    tmp_header_file=upload-header.tmp400401    # Initial resumable request defining metadata.402    curl "https://generativelanguage.googleapis.com/upload/v1beta/files" \403      -D "${tmp_header_file}" \404      -H "x-goog-api-key: $GEMINI_API_KEY" \405      -H "X-Goog-Upload-Protocol: resumable" \406      -H "X-Goog-Upload-Command: start" \407      -H "X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}" \408      -H "X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}" \409      -H "Content-Type: application/json" \410      -d "{'file': {'display_name': '${DISPLAY_NAME}'}}" 2> /dev/null411412    upload_url=$(grep -i "x-goog-upload-url: " "${tmp_header_file}" | cut -d" " -f2 | tr -d "\r")413    rm "${tmp_header_file}"414415    # Upload the actual bytes.416    curl "${upload_url}" \417      -H "Content-Length: ${NUM_BYTES}" \418      -H "X-Goog-Upload-Offset: 0" \419      -H "X-Goog-Upload-Command: upload, finalize" \420      --data-binary "@${FILE_PATH}" 2> /dev/null > file_info.json421422    file_uri=$(jq ".file.uri" file_info.json)423424    # Now use in an interaction425    curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \426        -H "x-goog-api-key: $GEMINI_API_KEY" \427        -H 'Content-Type: application/json' \428        -d '{429          "model": "gemini-3.8-flash",430          "input": [431            {"type": "text", "text": "Summarize this document"},432            {"type": "document", "uri": '$file_uri', "mime_type": "'${MIME_TYPE}'"}433          ]434        }'435436### Register Google Cloud Storage files437438If your data is already in Google Cloud Storage, you don't need to439download and re-upload it. You can register it directly with the File API.4404411. Grant **Service Agent** access to each bucket442443   1. Enable the Gemini API in your Google Cloud project.444445   2. Create the Service Agent:446447      `gcloud beta services identity create --service=generativelanguage.googleapis.com --project=<your_project>`448   3. **Grant the Gemini API Service Agent permissions** to read your storage449      buckets.450451      The user needs to assign the `Storage Object Viewer`452      [IAM role](https://docs.cloud.google.com/storage/docs/access-control/iam-roles#storage.objectViewer)453      to this service agent on the specific storage buckets they intend to use.454455   This access doesn't expire by default, but can be changed at any time. You can456   also use the457   [Google Cloud Storage IAM SDK](https://cloud.google.com/iam/docs/write-policy-client-libraries)458   commands to grant permissions.4592. Authenticate your service460461   **Prerequisites**462   - Enable API463   - Create a service account or agent with appropriate permissions.464465   You first need to authenticate as the service that has storage object viewer466   permissions. How this happens depends on the environment in which your file467   management code will be running.468469   **Outside of Google Cloud**470471   If your code is running from outside of Google Cloud, such as your desktop,472   download the account credentials from the Google Cloud Console with the473   following steps:474   1. Browse to the [Service Account console](https://console.cloud.google.com/iam-admin/serviceaccounts)475   2. Select the relevant service account476   3. Select the **Keys** tab and choose **Add key, Create new key**477   4. Choose the **JSON** key type, and note where the file was downloaded to on your machine.478479   For more details, see the official Google Cloud documentation on480   [service account key management](https://docs.cloud.google.com/iam/docs/keys-create-delete).481482   Then use the following commands to authenticate. These commands assume your483   service account file is in the current directory, named484   `service-account.json`.485486   ### Python487488       from google.oauth2.service_account import Credentials489490       GCS_READ_SCOPES = [491         'https://www.googleapis.com/auth/devstorage.read_only',492         'https://www.googleapis.com/auth/cloud-platform'493       ]494495       SERVICE_ACCOUNT_FILE = 'service-account.json'496497       credentials = Credentials.from_service_account_file(498           SERVICE_ACCOUNT_FILE,499           scopes=GCS_READ_SCOPES500       )501502   ### Javascript503504       const { GoogleAuth } = require('google-auth-library');505506       const GCS_READ_SCOPES = [507         'https://www.googleapis.com/auth/devstorage.read_only',508         'https://www.googleapis.com/auth/cloud-platform'509       ];510511       const SERVICE_ACCOUNT_FILE = 'service-account.json';512513       const auth = new GoogleAuth({514         keyFile: SERVICE_ACCOUNT_FILE,515         scopes: GCS_READ_SCOPES516       });517518   ### CLI519520       gcloud auth application-default login \521         --client-id-file=service-account.json \522         --scopes='https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/devstorage.read_only'523524   **On Google Cloud**525526   If you are running directly in Google Cloud, for example by using [Cloud527   Run functions](https://cloud.google.com/functions) or a528   [Compute Engine instance](https://cloud.google.com/products/compute), you will529   have implicit credentials but will need to re-authenticate to grant the530   appropriate scopes.531532   ### Python533534   This code expects that the service is running in an environment where535   [Application Default Credentials](https://docs.cloud.google.com/docs/authentication/application-default-credentials)536   can be obtained automatically, such as Cloud Run or Compute Engine.537538       import google.auth539540       GCS_READ_SCOPES = [541         'https://www.googleapis.com/auth/devstorage.read_only',542         'https://www.googleapis.com/auth/cloud-platform'543       ]544545       credentials, project = google.auth.default(scopes=GCS_READ_SCOPES)546547   ### JavaScript548549   This code expects that the service is running in an environment where550   [Application Default Credentials](https://docs.cloud.google.com/docs/authentication/application-default-credentials)551   can be obtained automatically, such as Cloud Run or Compute Engine.552553       const { GoogleAuth } = require('google-auth-library');554555       const auth = new GoogleAuth({556         scopes: [557           'https://www.googleapis.com/auth/devstorage.read_only',558           'https://www.googleapis.com/auth/cloud-platform'559         ]560       });561562### Java563564`java565import com.google.auth.oauth2.GoogleCredentials;566import java.io.FileInputStream;567import java.util.Arrays;568import java.util.List;569570List<String> gcsReadScopes =571Arrays.asList(572"https://www.googleapis.com/auth/devstorage.read_only",573"https://www.googleapis.com/auth/cloud-platform");574575String serviceAccountFile = "service-account.json";576577GoogleCredentials credentials =578GoogleCredentials.fromStream(new FileInputStream(serviceAccountFile))579.createScoped(gcsReadScopes);`580581### CLI582583This is an interactive command. For services like Compute Engine you can attach scopes to584the running service at the config level. See the [user-managed service585docs](https://docs.cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances#using)586for an example.587588    gcloud auth application-default login \589    --scopes="https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/devstorage.read_only"5905911. File registration (Files API) Use the Files API to register files and produce a Files API path that can directly be used in the Gemini API.592593   ### Python594595       from google import genai596597       client = genai.Client(credentials=credentials)598599       registered_gcs_files = client.files.register_files(600           uris=["gs://my_bucket/some_object.pdf", "gs://bucket2/object2.txt"]601       )602       prompt = "Summarize this file."603604       for f in registered_gcs_files.files:605         print(f.name)606         interaction = client.interactions.create(607           model="gemini-3.8-flash",608           input=[609             {"type": "text", "text": prompt},610             {"type": "document", "uri": f.uri, "mime_type": f.mime_type}611           ],612         )613         print(interaction.output_text)614615   ### JavaScript616617       import { GoogleGenAI } from "@google/genai";618619       const ai = new GoogleGenAI({ auth: auth });620621       async function main() {622           const registeredGcsFiles = await ai.files.registerFiles({623               uris: ["gs://my_bucket/some_object.pdf", "gs://bucket2/object2.txt"]624           });625626           const prompt = "Summarize this file.";627628           for (const file of registeredGcsFiles.files) {629               console.log(file.name);630               const interaction = await ai.interactions.create({631                   model: "gemini-3.8-flash",632                   input: [633                       { type: "text", text: prompt },634                       { type: "document", uri: file.uri, mime_type: file.mimeType }635                   ]636               });637638               console.log(interaction.output_text);639           }640       }641642       main();643644### Java645646    import com.google.auth.oauth2.GoogleCredentials;647    import com.google.genai.Client;648    import com.google.genai.gaos.models.interactions.Content;649    import com.google.genai.gaos.models.interactions.CreateModelInteraction;650    import com.google.genai.gaos.models.interactions.DocumentContent;651    import com.google.genai.gaos.models.interactions.DocumentContentMimeType;652    import com.google.genai.gaos.models.interactions.Interaction;653    import com.google.genai.gaos.models.interactions.InteractionsInput;654    import com.google.genai.gaos.models.interactions.Model;655    import com.google.genai.gaos.models.interactions.TextContent;656    import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;657    import com.google.genai.types.File;658    import com.google.genai.types.RegisterFilesResponse;659    import java.io.FileInputStream;660    import java.util.Arrays;661    import java.util.Collections;662    import java.util.List;663664    GoogleCredentials credentials =665        GoogleCredentials.fromStream(new FileInputStream("service-account.json"))666            .createScoped(667                Arrays.asList(668                    "https://www.googleapis.com/auth/devstorage.read_only",669                    "https://www.googleapis.com/auth/cloud-platform"));670671    Client client = Client.builder().credentials(credentials).build();672673    RegisterFilesResponse registeredGcsFiles =674        client.files.registerFiles(675            credentials,676            Arrays.asList("gs://my_bucket/some_object.pdf", "gs://bucket2/object2.txt"),677            null);678679    String prompt = "Summarize this file.";680681    for (File f : registeredGcsFiles.files().orElse(Collections.emptyList())) {682      System.out.println(f.name().orElse(""));683684      Content textContent = TextContent.builder().text(prompt).build();685      Content docContent =686          DocumentContent.builder()687              .uri(f.uri().orElse(""))688              .mimeType(DocumentContentMimeType.of(f.mimeType().orElse("application/pdf")))689              .build();690691      CreateModelInteraction params =692          CreateModelInteraction.builder()693              .model(Model.of("gemini-3.8-flash"))694              .input(InteractionsInput.ofContent(Arrays.asList(textContent, docContent)))695              .build();696697      Interaction interaction =698          client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();699      System.out.println(interaction.outputText().orElse(""));700    }701702### CLI703704    access_token=$(gcloud auth application-default print-access-token)705    project_id=$(gcloud config get-value project)706    curl -X POST https://generativelanguage.googleapis.com/v1beta/files:register \707        -H 'Content-Type: application/json' \708        -H "Authorization: Bearer ${access_token}" \709        -H "x-goog-user-project: ${project_id}" \710        -d '{"uris": ["gs://bucket/object1", "gs://bucket/object2"]}'711712## External HTTP / Signed URLs713714You can pass publicly accessible HTTPS URLs or pre-signed URLs directly in your715request. The Gemini API will fetch the content securely during processing.716This is ideal for files up to 100MB that you don't want to re-upload.717718> [!NOTE]719> **Note:** Gemini 2.0 family of models are not supported720721### Python722723    from google import genai724725    uri = "https://ontheline.trincoll.edu/images/bookdown/sample-local-pdf.pdf"726    prompt = "Summarize this file"727728    client = genai.Client()729730    interaction = client.interactions.create(731        model="gemini-3.8-flash",732        input=[733            {"type": "document", "uri": uri, "mime_type": "application/pdf"},734            {"type": "text", "text": prompt}735        ]736    )737    print(interaction.output_text)738739### Javascript740741    import { GoogleGenAI } from '@google/genai';742743    const client = new GoogleGenAI({});744745    const uri = "https://ontheline.trincoll.edu/images/bookdown/sample-local-pdf.pdf";746747    async function main() {748      const interaction = await client.interactions.create({749        model: 'gemini-3.8-flash',750        input: [751          { type: "document", uri: uri, mime_type: "application/pdf" },752          { type: "text", text: "summarize this file" }753        ]754      });755756      console.log(interaction.output_text);757    }758759    main();760761### REST762763    curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \764          -H 'x-goog-api-key: $GEMINI_API_KEY' \765          -H 'Content-Type: application/json' \766          -d '{767              "model": "gemini-3.8-flash",768              "input": [769                {"type": "text", "text": "Summarize this pdf"},770                {771                  "type": "document",772                  "uri": "https://ontheline.trincoll.edu/images/bookdown/sample-local-pdf.pdf",773                  "mime_type": "application/pdf"774                }775              ]776            }'777778### Accessibility779780Verify that the URLs you provide don't lead to pages that require a login or781are behind a paywall. For private databases, ensure you create a signed URL782with the correct access permissions and expiry.783784### Safety checks785786The system performs a content moderation check on the URL to confirm they meet787safety and policy standards. If the URL fails this check, you will get an788`url_retrieval_status` of `URL_RETRIEVAL_STATUS_UNSAFE`.789790### Supported content types791792This list of supported file types and limitations is intended as initial793guidance and is not comprehensive. The effective794set of supported types is subject to change and can vary based on the specific795model and tokenizer version in use. Unsupported types will result in an error.796Additionally, content retrieval for these file types797only supports publicly accessible URLs.798799#### Text file types800801- `text/html`802- `text/css`803- `text/plain`804- `text/xml`805- `text/csv`806- `text/rtf`807- `text/javascript`808809#### Application file types810811- `application/json`812- `application/pdf`813814#### Image file types815816- `image/bmp`817- `image/jpeg`818- `image/png`819- `image/webp`820821#### Video file types822823- `video/mp4`824- `video/mpeg`825- `video/quicktime`826- `video/avi`827- `video/x-flv`828- `video/mpg`829- `video/webm`830- `video/wmv`831- `video/3gpp`832833## Best practices834835- **Choose the right method:** Use inline data for small, transient files. Use the File API for larger or frequently used files. Use external URLs for data already hosted online.836- **Specify MIME Types:** Always provide the correct MIME type for the file data to ensure proper processing.837- **Handle Errors:** Implement error handling in your code to manage potential issues like network failures, file access problems, or API errors.838839## Limitations840841- File size limits vary by method (see [comparison table](https://ai.google.dev/gemini-api/docs/file-input-methods#method-comparison)) and file type.842- Inline data increases request payload size.843- File API uploads are temporary and expire after 48 hours.844- External URL fetching is limited to 100MB per payload and supports specific content types.845846## What's next847848- Try writing your own multimodal prompts using [Google AI Studio](http://aistudio.google.com/).849- For information on including files in your prompts, see the [Vision](https://ai.google.dev/gemini-api/docs/vision), [Audio](https://ai.google.dev/gemini-api/docs/audio), and [Document processing](https://ai.google.dev/gemini-api/docs/document-processing) guides.
Files main
Menu

Theme