File inputs and file slicing
Last updated: 2026-11-25T06:38:11-03:00 (UTC-3)
Gemini reads documents, spreadsheets, images, audio and video as part of a request, and bills only the part of a file it is told to read: a page range of a PDF, one sheet of a workbook, a lower resolution. Sending pages 3 and 4 of a six-page PDF instead of the whole file cut the prompt from 3,133 to 1,053 tokens in a measured call through the gateway (gemini-3.5-flash-lite, 2026-11-25). This page explains how to send a file to a Gemini target on each route, how to give an alias a default slice so no caller pays for a whole file by accident, how such requests are priced and reserved, and how to use the Gemini Interactions API.
Slicing is done by the provider; the gateway carries the fields, checks them before any call and accounts for the result. It works on Gemini 3 and later models.
Which route to use
Section titled “Which route to use”| You send | Route | What you need |
|---|---|---|
| Gemini’s own request | POST /v1beta/models/{alias}:generateContent (and :streamGenerateContent) |
gemini_generate_content (on by default). The body passes through, slice included |
| An OpenAI-style request | POST /v1/chat/completions, POST /v1/responses |
gemini_file_inputs (it also prices and reserves file requests on GenerateContent) |
| Gemini’s Interactions request | POST /v1beta/interactions, POST /v1/interactions |
gemini_interactions |
| Nothing: the alias decides | any of the above | gemini_file_defaults and models[].file_defaults |
All three new flags are off by default. Turn on only what you use:
features: gemini_file_inputs: true # file parts on Chat Completions and Responses gemini_file_defaults: true # apply models[].file_defaults gemini_interactions: true # the Interactions API routesSend a file on Chat Completions
Section titled “Send a file on Chat Completions”With gemini_file_inputs, a Gemini target accepts a file content part in a user message:
{ "model": "docs", "messages": [{ "role": "user", "content": [ {"type": "text", "text": "Summarize the chart on pages 3 and 4."}, {"type": "file", "file": { "file_data": "data:application/pdf;base64,JVBERi0xLjQK...", "document_metadata": {"start_page": 3, "end_page": 4}, "resolution": "low" }} ] }]}The members of file:
| Member | Meaning |
|---|---|
file_data |
The file inline: base64, or a base64 data URL that names its type |
file_id or file_url |
A reference the provider reads itself: a gs:// object or a Files API URI. See references. Exactly one of file_data, file_id and file_url is required |
format |
The MIME type. Required when nothing else names it: a data URL, or the extension of filename or of a gs:// path, names it too |
filename |
Used only to find the type |
document_metadata |
For PDF, Word, PowerPoint and OpenDocument files: start_page and end_page (1-indexed, inclusive, 1 to 2,000; one alone is valid) and content_layers, a list of page_image and page_text. A sliced document is read as page images unless you list page_text |
spreadsheet_metadata |
For Excel and OpenDocument spreadsheets: sheet_index (1-indexed, in tab order), visible_cells (an A1-style cell or range such as A1:G40) and content_layers, a list of data, formulas and visual |
video_metadata |
For video: start_offset and end_offset (seconds, as "90s") and fps (above 0, up to 24) |
resolution |
low, medium or high; ultra_high for images only. A page or an image costs about 280, 560 or 1,120 tokens |
The gateway sends Gemini’s own part: inlineData or fileData with documentMetadata, spreadsheetMetadata, videoMetadata and mediaResolution. A file part that is malformed, of a type Gemini does not read, sliced with the wrong kind of metadata, or referenced without approval is refused with 422 unsupported_on_provider and a message naming the member, before any provider call.
A tool can return a file too. Put the file part in the tool message’s content, beside its text; it reaches Gemini inside the function response:
{"role": "tool", "tool_call_id": "call_1", "content": [ {"type": "text", "text": "attached"}, {"type": "file", "file": {"file_data": "data:application/pdf;base64,...", "document_metadata": {"start_page": 5, "end_page": 5}}}]}Files are accepted in user and tool messages only. Streaming works the same way.
On Responses
Section titled “On Responses”On POST /v1/responses routed to a Gemini target, an input_file part carries the same members, flat on the part, in a user message or a function_call_output:
{"type": "input_file", "file_data": "data:application/pdf;base64,...", "document_metadata": {"start_page": 3, "end_page": 4}}On a target that is not Gemini
Section titled “On a target that is not Gemini”No other provider slices a file. With gemini_file_inputs on, a request that names a slice and reaches another target follows the alias’s unsupported_parameters:
refuse(the default):422 unsupported_on_provider, naming the first slice field (file.document_metadata is not supported by this provider). The whole file is never billed for a request that asked for a few pages.drop: the slice fields are removed, the file is sent whole, andX-Gateway-Dropped-Parameterslists what was removed (file.document_metadata,file.resolution).
References
Section titled “References”A reference is a file the provider fetches itself. It is accepted only under a prefix you approve, because the provider reads it with the gateway’s own credentials or from the gateway’s own provider project:
file_inputs: reference_prefixes: - gs://acme-docs/reports/ # a storage prefix, ending in / - https://generativelanguage.googleapis.com/v1beta/files/ # files uploaded to the Files API page_tokens: 560| Setting | Default | Meaning |
|---|---|---|
file_inputs.reference_prefixes |
empty | Up to 64 entries: gs://bucket/ or gs://bucket/path/ prefixes, and the Files API prefix (…/v1beta/files/ or …/v1/files/; either one allows both spellings). Empty means every file is sent inline |
file_inputs.page_tokens |
560 | The tokens a budget reserves per page at medium resolution (64 to 8,192). See budgets |
Both are read on every request, so a configuration change applies without a restart. The console shows them, read-only, under Routing status → File inputs, and GET /api/v1/admin/file-inputs returns them with the flags and the aliases that set defaults (permission providers:read).
A gs:// reference needs a type: format, or a path with a known extension. A path with a .. segment is refused. Storage objects are read by Vertex AI targets; the Gemini API reads Files API URIs.
On GenerateContent, a file search tool (tools[].fileSearch) reads a store of the provider project in the same way: with organizations it is refused with 422 provider_state_not_isolated. There is no per-organization allow-list of stores; send the content in the request.
With organizations. When organizations share the gateway, they share the provider project too, and a Files API URI names a file any of them could have uploaded. Such a URI is refused with 422, whatever the prefixes say, on every route (provider_state_not_isolated; unsupported_on_provider when it comes in file_url); send the file inline instead. In that mode file_id is refused for every value, so name an approved storage object in file_url.
Give an alias a default slice
Section titled “Give an alias a default slice”models[].file_defaults is the slice and resolution a file part gets when the request names none. It is a cost control: a caller that attaches a 300-page PDF without thinking pays for the first 20 pages at low resolution, not for all of it.
models: - id: docs file_defaults: resolution: low start_page: 1 end_page: 20 targets: - provider: gemini model: gemini-3.5-flash-lite| Field | Bounds | Applies to |
|---|---|---|
resolution |
low, medium, high |
Documents, spreadsheets, images and video that name no resolution |
start_page, end_page |
1 to 2,000, end_page not before start_page |
Documents that name no document_metadata |
document_layers |
page_image, page_text |
Documents that name no document_metadata |
sheet_index |
1 to 10,000 | Spreadsheets that name no spreadsheet_metadata |
visible_cells |
An A1-style cell or range | Spreadsheets that name no spreadsheet_metadata |
spreadsheet_layers |
data, formulas, visual |
Spreadsheets that name no spreadsheet_metadata |
How it behaves:
- It applies only while
gemini_file_defaultsis on. The value is validated either way, so turning the flag on never activates an invalid one. - What the request names always wins. A part with its own page range keeps it; the default resolution is still added if the part names none.
- It applies on every route to a Gemini target: GenerateContent, Chat Completions and Responses (with
gemini_file_inputs), and Interactions. - When a default changed a part, the answer carries
X-Gateway-File-Defaults: applied, so a slice the caller did not ask for is never silent. - On GenerateContent a request that sets
generationConfig.mediaResolutionkeeps that choice for every part. A GenerateContent body that repeats a member name is refused with400on an alias with defaults. - A file part must declare its type on an alias with defaults, since the default is chosen by type: one that does not is refused with
422(file_type_requiredon GenerateContent and Interactions,unsupported_on_provideron the translated routes). - It is set in YAML, through
PUT /api/v1/admin/models/{id}and in the console’s model editor (File defaults). It is not allowed on anauto_routeoralias_ofentry, and needs at least one field.
What a file request costs
Section titled “What a file request costs”With usage valuation and gemini_file_inputs on, a request with a file is priced from the usage the provider reports by modality, on GenerateContent and on the translated routes; an Interactions request is priced the same way under gemini_interactions. Without the flag a GenerateContent request with a file part keeps the cost unknown, as before.
| The file is read as | Priced at |
|---|---|
Text: text files, a document’s page_text layer alone, a spreadsheet’s cells |
The input price |
Images: an image, a document’s pages, a spreadsheet’s visual layer |
The input price on the Gemini API and Vertex AI, where images bill as input; elsewhere the rate’s image input price |
| Audio, video | The rate’s audio or video input price |
The cost stays unknown, never a guess, when a file part declares no type (non_text_or_stateful_profile), when the rate publishes no price for a modality the request sends (media_token_unpriced), or when the answer’s usage does not count a modality the request sent (media_usage_unreported). To get a cost, declare the type: format on the OpenAI-style routes, mimeType on GenerateContent, mime_type on Interactions.
The request span records how many file parts named a slice (file_slices in the structure, gateway.structure.file_slices over OTLP), with no content.
Budgets and token ceilings
Section titled “Budgets and token ceilings”A budget or token ceiling reserves a request’s cost before the call, so it needs a bound the gateway can check. A file has one in a single case: an inline PDF with a closed page range whose layers name page_image alone (document_metadata: {start_page, end_page, content_layers: ["page_image"]}). The gateway holds the bytes and checks they are a PDF; the provider clamps the range to the pages that exist; and a page image costs a fixed number of tokens at its resolution. The layer must be written out, so the bound rests on the request and not on a provider default.
Each such page is reserved like an image, at the larger of the rate’s per-image ceiling (image_input_tokens) and file_inputs.page_tokens for its resolution: half of it at low, twice at high. The charge settles on the usage the answer reports. The rate must approve an image ceiling, as for any image input.
Every other file keeps the request out of the protected profile and is refused with 422 quota_profile_unsupported (reason media_input_not_bounded): a reference, whose real type and size cannot be checked before the provider reads it; an open range; a range that names no layer, or the page_text layer, whose tokens follow the page’s own text; office documents, spreadsheets, audio and video.
An alias default makes requests bounded without the caller’s help: with file_defaults: {start_page: 1, end_page: 20, document_layers: [page_image]}, an inline PDF sent with no slice is reserved as 20 pages. On Chat Completions and GenerateContent this needs gemini_file_inputs; the Interactions API has its own profile.
The Interactions API
Section titled “The Interactions API”The Interactions API is Gemini’s current generation interface. With gemini_interactions, the gateway serves it natively: the body passes through with only model replaced, so everything works as the provider documents it, the file slice included.
features: gemini_interactions: truemodels: - id: docs capabilities: [interactions] # alone on the alias targets: - provider: gemini # the Gemini API; not Vertex AI model: gemini-3.5-flash-litekeys: - id: app scopes: [route:interactions]curl https://gateway.example.com/v1beta/interactions \ -H "x-goog-api-key: $GATEWAY_KEY" -H "Content-Type: application/json" \ -d '{ "model": "docs", "input": [ {"type": "text", "text": "Summarize the chart on pages 3 and 4."}, {"type": "document", "data": "JVBERi0xLjQK...", "mime_type": "application/pdf", "resolution": "low", "processing": {"type": "static", "page_options": {"start_page": 3, "end_page": 4}}} ] }'The key goes in x-goog-api-key or Authorization: Bearer, never in ?key=. /v1/interactions is the same route. With "stream": true the answer is the provider’s event stream: interaction.created, step.start, step.delta, step.stop, interaction.completed with the usage, then done.
Stored interactions
Section titled “Stored interactions”The provider stores an interaction unless the request says "store": false, and its ID alone reads, continues, cancels or deletes it. Every key of the gateway shares one provider project, so the gateway never accepts the provider’s ID from a caller it cannot attribute it to. The id in an answer is the gateway’s own (int_gw1.…), signed to the key that created it. It is signed, not encrypted: it proves which key may use it and is not a secret.
| Call | Behavior |
|---|---|
"previous_interaction_id": "int_gw1.…" |
Continues the interaction at the provider that holds it. Another key’s ID, or a tampered one, answers 404 interaction_not_found without a provider call |
GET /v1beta/interactions/{id} |
Reads it. No tokens are billed |
POST /v1beta/interactions/{id}/cancel |
Cancels it |
DELETE /v1beta/interactions/{id} |
Deletes it |
The same three exist under /v1/interactions/{id}. The provider’s own ID is accepted in previous_interaction_id only when the gateway has one tenant; with organizations it is refused like a foreign ID. The IDs are signed with the console session secret, as stored responses are, so they survive a restart and work on every replica; without a configured secret a per-process key is used, and IDs last until the process restarts.
A request with store: false and no previous_interaction_id keeps nothing at the provider; it falls back to the alias’s next target on a 429 or 5xx, like any stateless call (a retry policy is not available on an interactions alias). Any other request is one send to one provider, and an alias with several targets refuses it with 422 stateless_fallback_needed unless it continues an interaction.
Background interactions
Section titled “Background interactions”With "background": true the provider answers at once with the interaction in_progress and runs it afterwards. The answer carries the signed ID; read it with GET /v1beta/interactions/{id} until its status is no longer in_progress, or stop it with POST …/cancel. A background interaction must be stored (store: false is refused) and is not streamed.
webhook_config is refused with 422 webhook_unsupported. The provider would post the result straight to the caller’s URL, around the gateway, with its own interaction ID: nothing on that path can be verified, attributed to a tenant or kept from naming provider state. Poll the interaction instead.
Agents and remote environments
Section titled “Agents and remote environments”An agent interaction (agent, agent_config) and a remote environment (environment, environment_id) run tools and code at the provider. The gateway’s guardrails, tool policies and egress controls see none of it, so both are refused with 422 agents_disabled unless you turn on gemini_interactions_agents (off by default, an operator control; it needs gemini_interactions). Read the threat note first.
With the flag on, the request names the agent where it would name a model: {"agent": "research", "input": "…", "background": true}. research is a gateway alias with the interactions capability whose target model is the provider’s agent ID. A request names a model or an agent, not both. The cost of an agent interaction is unknown (its tools bill per use) and it is not admitted under a budget. With organizations, environment and environment_id are refused whatever the flag says (a remote environment is state in the shared project), and so is an agent_config that names a store or a file.
Guardrails
Section titled “Guardrails”Guardrails apply to this route as to the others.
- Input. The system instruction and every text of the input are checked in order: text blocks, the content of turns, and tool results (
function_result), whether the result is a string, a list or a structured value (every string in it is checked). A mask rewrites the text the provider receives; a block refuses the request. Media blocks and function call arguments are not inspected. An input the guardrails cannot read (an item that is not an object, nesting deeper than the documented shapes) is refused with400 invalid_requestinstead of sent on unchecked. - Output. The text of every
model_outputstep is checked on the whole answer. A blocked answer is the guardrail’s refusal as a completed interaction. - Streaming. With an output guardrail on the alias, a request with
"stream": trueis sent to the provider without streaming, checked whole, and then delivered as the stream’s events (interaction.created, each step with its text in one delta,interaction.completed,done). The text arrives at once instead of token by token; that is the price of checking all of it first. - An enforcing output guardrail needs the answer to exist nowhere else. The gateway sends such a request with
store: false, and refusesstore: true,previous_interaction_idandbackground: truewith400 guardrail_output_unsupported: a stored interaction keeps the original answer at the provider, readable by its ID. Send the history ininputinstead. A guardrail that only monitors never changes the answer or what may be stored; a streamed answer is still read whole before it is delivered, so the monitor sees all of it.
Under a budget or token ceiling
Section titled “Under a budget or token ceiling”With quota_native_protocols, an interactions alias is admitted under a budget or token ceiling when the request is bounded:
| Part | Rule |
|---|---|
| Input | Text: a string, text blocks, turns and function results made of text, function calls. An inline PDF with a closed page range that names page_image alone adds its pages, as above |
| Output | generation_config.max_output_tokens is required. It includes thought tokens: a limit of 24 with high thinking ended at 19 thought tokens and 1 output token |
| Tools | Function tools only |
| Tier | service_tier absent, standard, flex, priority or batch |
| Stream | Needs quota_streaming and stream_usage, as on the other routes |
The reservation settles on the usage of the answer: the JSON object, or the stream’s interaction.completed event. An answer that stopped at its output limit (status: incomplete) or ended in a function call for the client to run (status: requires_action) is measured like a completed one. The policy’s item, tool and content-part limits apply to the input list, the tools and the widest turn.
Refused with 422 quota_profile_unsupported: a continuation (stored_history_not_bounded: the provider adds the stored history to the input), an agent (agent_not_bounded), hosted tools, media other than the bounded PDF, and any member outside the profile.
Background. With quota_background_responses on, a background interaction is admitted too. Its reservation stays open until the interaction ends: the gateway settles it from the stored interaction when the caller reads or cancels it, and a worker polls the ones nobody read (charge_accounting.background_poll_seconds); past background_max_age_seconds the reservation is held as unknown for reconciliation. A completed or incomplete interaction settles on its usage; a failed or cancelled one settles on the usage it reports, and is held as unknown without one. DELETE on an interaction whose reservation is still open answers 409 background_interaction_pending. Without that flag a background interaction under a budget is refused (background_tracking_disabled). Tracking needs the PostgreSQL store, and rows are bound to the key and team that created the interaction.
Without a budget, a background create is recorded with no usage and an unknown cost: the tokens exist only in the stored interaction.
What is refused
Section titled “What is refused”Before any provider call:
| Request | Answer |
|---|---|
agent, agent_config, environment, environment_id without gemini_interactions_agents |
422 agents_disabled |
webhook_config |
422 webhook_unsupported |
background: true with store: false or stream: true |
422 background_unsupported |
A member spelled in lowerCamel (previousInteractionId) |
400 member_spelling: the API’s members are snake_case, and a twin could bypass the checks above |
An input that is a single object |
400 invalid_interaction: send a string or a list |
With organizations: a Files API file anywhere in input (a uri on the API’s host or a files/<id> name), a file_search tool or a tool that names a store, a continuation_token, an environment_id |
422 provider_state_not_isolated |
| A provider answer the gateway cannot read to sign its ID | 502 invalid_provider_response: withheld, never relayed as it came |
| A body that repeats a member name | 400 invalid_request |
| A stored, continued or background interaction on an alias with an enforcing output guardrail | 400 guardrail_output_unsupported |
An interaction is valued from its usage: total_input_tokens as input, total_output_tokens plus total_thought_tokens as output, total_cached_tokens as cache reads, and input_tokens_by_modality by modality, with the rules of the table above. Hosted tools (search, code execution and the like), tool-use tokens and grounding leave the cost unknown. The request log names the operation interactions.
Limits
Section titled “Limits”- Files in system and assistant messages are refused on the translated routes.
- On Chat Completions and Responses a reference must be a
gs://object or a Files API URI; any other URL is refused, and the gateway does not download files. - The provider’s own limits apply: 50 MB per file, 1,000 pages per PDF, 2,000 pages or slides per office document, 50,000 cells per sheet. Page limits apply to the whole file even when you slice it.
- A
start_pagepast the last page returns an empty slice; anend_pagepast it is clamped.