Using files

The Gemini API supports uploading media files separately from the prompt input, allowing your media to be reused across multiple requests and multiple prompts. For more details, check out the Prompting with media guide.

Method: media.upload

Uploads data to a ragStore, preprocesses and chunks before storing it in a RagStore Document.

Endpoint

  • Upload URI, for media upload requests:
post https://generativelanguage.googleapis.com/upload/v1beta/{ragStoreName=ragStores/*}:uploadToRagStore
  • Metadata URI, for metadata-only requests:
post https://generativelanguage.googleapis.com/v1beta/{ragStoreName=ragStores/*}:uploadToRagStore

Path parameters

ragStoreName string

Required. Immutable. The name of the RagStore to upload the file into. Example: ragStores/my-rag-store-123 It takes the form ragStores/{ragstore}.

Request body

The request body contains data with the following structure:

Fields
displayName string

Optional. Display name of the created document.

customMetadata[] object (CustomMetadata)

Custom metadata to be associated with the data.

chunkingConfig object (ChunkingConfig)

Optional. Config for telling the service how to chunk the data. If not provided, the service will use default parameters.

mimeType string

Optional. MIME type of the data. If not provided, it will be inferred from the uploaded content.

Response body

This is a copy of google.longrunning.Operation. We need to copy it because for interacting with scotty, we need to add a scotty specific field that can't be added in the top level Operation proto.

If successful, the response body contains data with the following structure:

Fields
name string

The server-assigned name, which is only unique within the same service that originally returns it. If you use the default HTTP mapping, the name should be a resource name ending with operations/{unique_id}.

metadata object

Service-specific metadata associated with the operation. It typically contains progress information and common metadata such as create time. Some services might not provide such metadata. Any method that returns a long-running operation should document the metadata type, if any.

An object containing fields of an arbitrary type. An additional field "@type" contains a URI identifying the type. Example: { "id": 1234, "@type": "types.example.com/standard/id" }.

done boolean

If the value is false, it means the operation is still in progress. If true, the operation is completed, and either error or response is available.

result Union type
The operation result, which can be either an error or a valid response. If done == false, neither error nor response is set. If done == true, exactly one of error or response can be set. Some services might not provide the result. result can be only one of the following:
error object (Status)

The error result of the operation in case of failure or cancellation.

response object

The normal, successful response of the operation. If the original method returns no data on success, such as Delete, the response is google.protobuf.Empty. If the original method is standard Get/Create/Update, the response should be the resource. For other methods, the response should have the type XxxResponse, where Xxx is the original method name. For example, if the original method name is TakeSnapshot(), the inferred response type is TakeSnapshotResponse.

An object containing fields of an arbitrary type. An additional field "@type" contains a URI identifying the type. Example: { "id": 1234, "@type": "types.example.com/standard/id" }.

JSON representation
{
  "name": string,
  "metadata": {
    "@type": string,
    field1: ...,
    ...
  },
  "done": boolean,

  // result
  "error": {
    object (Status)
  },
  "response": {
    "@type": string,
    field1: ...,
    ...
  }
  // Union type
}

ChunkingConfig

Parameters for telling the service how to chunk the file. inspired by google3/cloud/ai/platform/extension/lib/retrieval/config/chunker_config.proto

Fields
config Union type
The chunking configuration to use. config can be only one of the following:
whiteSpaceConfig object (WhiteSpaceConfig)

White space chunking configuration.

JSON representation
{

  // config
  "whiteSpaceConfig": {
    object (WhiteSpaceConfig)
  }
  // Union type
}

WhiteSpaceConfig

Configuration for a white space chunking algorithm [white space delimited].

Fields
maxTokensPerChunk integer

Maximum number of tokens per chunk. Tokens are defined as words for this chunking algorithm. Note: we are defining tokens as words split by whitespace as opposed to the output of a tokenizer. The context window of the latest gemini embedding model as of 2025-04-17 is currently 8192 tokens. We assume that the average word is 5 characters. Therefore, we set the upper limit to 2**9, which is 512 words, or 2560 tokens, assuming worst case a character per token. This is a conservative estimate meant to prevent context window overflow.

maxOverlapTokens integer

Maximum number of overlapping tokens between two adjacent chunks.

JSON representation
{
  "maxTokensPerChunk": integer,
  "maxOverlapTokens": integer
}

Method: files.get

Gets the metadata for the given File.

Endpoint

get https://generativelanguage.googleapis.com/v1beta/{name=files/*}

Path parameters

name string

Required. The name of the File to get. Example: files/abc-123 It takes the form files/{file}.

Request body

The request body must be empty.

Example request

Python

from google import genai

client = genai.Client()
myfile = client.files.upload(file=media / "poem.txt")
file_name = myfile.name
print(file_name)  # "files/*"

myfile = client.files.get(name=file_name)
print(myfile)

Node.js

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const myfile = await ai.files.upload({
  file: path.join(media, "poem.txt"),
});
const fileName = myfile.name;
console.log(fileName);

const fetchedFile = await ai.files.get({ name: fileName });
console.log(fetchedFile);

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)
}
myfile, err := client.Files.UploadFromPath(
	ctx,
	filepath.Join(getMedia(), "poem.txt"), 
	&genai.UploadFileConfig{
		MIMEType: "text/plain",
	},
)
if err != nil {
	log.Fatal(err)
}
fileName := myfile.Name
fmt.Println(fileName)
file, err := client.Files.Get(ctx, fileName, nil)
if err != nil {
	log.Fatal(err)
}
fmt.Println(file)

Shell

name=$(jq ".file.name" file_info.json)
# Get the file of interest to check state
curl https://generativelanguage.googleapis.com/v1beta/files/$name > file_info.json
# Print some information about the file you got
name=$(jq ".file.name" file_info.json)
echo name=$name
file_uri=$(jq ".file.uri" file_info.json)
echo file_uri=$file_uri

Response body

If successful, the response body contains an instance of File.

Method: files.list

Lists the metadata for Files owned by the requesting project.

Endpoint

get https://generativelanguage.googleapis.com/v1beta/files

Query parameters

pageSize integer

Optional. Maximum number of Files to return per page. If unspecified, defaults to 10. Maximum pageSize is 100.

pageToken string

Optional. A page token from a previous files.list call.

Request body

The request body must be empty.

Example request

Python

from google import genai

client = genai.Client()
print("My files:")
for f in client.files.list():
    print("  ", f.name)

Node.js

// Make sure to include the following import:
// import {GoogleGenAI} from '@google/genai';
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
console.log("My files:");
// Using the pager style to list files
const pager = await ai.files.list({ config: { pageSize: 10 } });
let page = pager.page;
const names = [];
while (true) {
  for (const f of page) {
    console.log("  ", f.name);
    names.push(f.name);
  }
  if (!pager.hasNextPage()) break;
  page = await pager.nextPage();
}

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)
}
fmt.Println("My files:")
page, err := client.Files.List(ctx, nil)
if err != nil {
	log.Fatal(err)
}
for _, f := range page.Items {
	fmt.Println("  ", f.Name)
}

Shell

echo "My files: "

curl "https://generativelanguage.googleapis.com/v1beta/files?key=$GEMINI_API_KEY"

Response body

Response for files.list.

If successful, the response body contains data with the following structure:

Fields
files[] object (File)

The list of Files.

nextPageToken string

A token that can be sent as a pageToken into a subsequent files.list call.

JSON representation
{
  "files": [
    {
      object (File)
    }
  ],
  "nextPageToken": string
}

Method: files.delete

Deletes the File.

Endpoint

delete https://generativelanguage.googleapis.com/v1beta/{name=files/*}

Path parameters

name string

Required. The name of the File to delete. Example: files/abc-123 It takes the form files/{file}.

Request body

The request body must be empty.

Example request

Python

from google import genai

client = genai.Client()
myfile = client.files.upload(file=media / "poem.txt")

client.files.delete(name=myfile.name)

try:
    result = client.models.generate_content(
        model="gemini-2.0-flash", contents=[myfile, "Describe this file."]
    )
    print(result)
except genai.errors.ClientError:
    pass

Node.js

// The Gen AI SDK for TypeScript and JavaScript is in preview.
// Some features have not been implemented.

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)
}
myfile, err := client.Files.UploadFromPath(
	ctx, 
	filepath.Join(getMedia(), "poem.txt"), 
	&genai.UploadFileConfig{
		MIMEType: "text/plain",
	},
)
if err != nil {
	log.Fatal(err)
}
// Delete the file.
_, err = client.Files.Delete(ctx, myfile.Name, nil)
if err != nil {
	log.Fatal(err)
}
// Attempt to use the deleted file.
parts := []*genai.Part{
	genai.NewPartFromURI(myfile.URI, myfile.MIMEType,),
	genai.NewPartFromText("Describe this file."),
}

contents := []*genai.Content{
	genai.NewContentFromParts(parts, genai.RoleUser),
}

_, err = client.Models.GenerateContent(ctx, "gemini-2.0-flash", contents, nil)
// Expect an error when using a deleted file.
if err != nil {
	return nil
}
return fmt.Errorf("expected an error when using deleted file")

Shell

curl --request "DELETE" https://generativelanguage.googleapis.com/v1beta/files/$name?key=$GEMINI_API_KEY

Response body

If successful, the response body is an empty JSON object.

REST Resource: files

Resource: File

A file uploaded to the API. Next ID: 15

Fields
name string

Immutable. Identifier. The File resource name. The ID (name excluding the "files/" prefix) can contain up to 40 characters that are lowercase alphanumeric or dashes (-). The ID cannot start or end with a dash. If the name is empty on create, a unique name will be generated. Example: files/123-456

displayName string

Optional. The human-readable display name for the File. The display name must be no more than 512 characters in length, including spaces. Example: "Welcome Image"

mimeType string

Output only. MIME type of the file.

sizeBytes string (int64 format)

Output only. Size of the file in bytes.

createTime string (Timestamp format)

Output only. The timestamp of when the File was created.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z", "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30".

updateTime string (Timestamp format)

Output only. The timestamp of when the File was last updated.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z", "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30".

expirationTime string (Timestamp format)

Output only. The timestamp of when the File will be deleted. Only set if the File is scheduled to expire.

Uses RFC 3339, where generated output will always be Z-normalized and use 0, 3, 6 or 9 fractional digits. Offsets other than "Z" are also accepted. Examples: "2014-10-02T15:01:23Z", "2014-10-02T15:01:23.045123456Z" or "2014-10-02T15:01:23+05:30".

sha256Hash string (bytes format)

Output only. SHA-256 hash of the uploaded bytes.

A base64-encoded string.

uri string

Output only. The uri of the File.

downloadUri string

Output only. The download uri of the File.

state enum (State)

Output only. Processing state of the File.

source enum (Source)

Source of the File.

error object (Status)

Output only. Error status if File processing failed.

metadata Union type
Metadata for the File. metadata can be only one of the following:
videoMetadata object (VideoFileMetadata)

Output only. Metadata for a video.

JSON representation
{
  "name": string,
  "displayName": string,
  "mimeType": string,
  "sizeBytes": string,
  "createTime": string,
  "updateTime": string,
  "expirationTime": string,
  "sha256Hash": string,
  "uri": string,
  "downloadUri": string,
  "state": enum (State),
  "source": enum (Source),
  "error": {
    object (Status)
  },

  // metadata
  "videoMetadata": {
    object (VideoFileMetadata)
  }
  // Union type
}

VideoFileMetadata

Metadata for a video File.

Fields
videoDuration string (Duration format)

Duration of the video.

A duration in seconds with up to nine fractional digits, ending with 's'. Example: "3.5s".

JSON representation
{
  "videoDuration": string
}

State

States for the lifecycle of a File.

Enums
STATE_UNSPECIFIED The default value. This value is used if the state is omitted.
PROCESSING File is being processed and cannot be used for inference yet.
ACTIVE File is processed and available for inference.
FAILED File failed processing.

Source

Enums
SOURCE_UNSPECIFIED Used if source is not specified.
UPLOADED Indicates the file is uploaded by the user.
GENERATED Indicates the file is generated by Google.
REGISTERED Indicates the file is a registered, i.e. a Google Cloud Storage file.

Status

The Status type defines a logical error model that is suitable for different programming environments, including REST APIs and RPC APIs. It is used by gRPC. Each Status message contains three pieces of data: error code, error message, and error details.

You can find out more about this error model and how to work with it in the API Design Guide.

Fields
code integer

The status code, which should be an enum value of google.rpc.Code.

message string

A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the google.rpc.Status.details field, or localized by the client.

details[] object

A list of messages that carry the error details. There is a common set of message types for APIs to use.

An object containing fields of an arbitrary type. An additional field "@type" contains a URI identifying the type. Example: { "id": 1234, "@type": "types.example.com/standard/id" }.

JSON representation
{
  "code": integer,
  "message": string,
  "details": [
    {
      "@type": string,
      field1: ...,
      ...
    }
  ]
}