Skip to content

Gemini 3 image models return 404 NOT_FOUND if too many input tokens #2397

@ascillitoe

Description

@ascillitoe

Since early this week, both gemini-3.1-flash-image-preview and gemini-3-pro-image-preview models return the following error if there are over ~15k input tokens (well below the apparent maximums listed on their model cards):

google.genai.errors.ClientError: 404 NOT_FOUND. {'error': {'code': 404, 'message': 'Requested entity was not found.', 'status': 'NOT_FOUND'}}

I assume this is a server side issue, but opening here because a ClientError with a presumably totally incorrect error message is raised. This happens with api_version='v1' and 'v1beta1'.

Repro script:

from google import genai
from google.genai import types

PROJECT_ID = "<redacted>"
LOCATION = "global"
#MODEL = "gemini-3-pro-image-preview"
MODEL = "gemini-3.1-flash-image-preview"
API_VERSION = "v1beta1"

INPUT_TOKENS = 15_000 # fails
#INPUT_TOKENS = 10_000 # passes

# ~4 chars per token, so 15k tokens ~ 60k chars. Build a long descriptive block.
PARAGRAPH = """\
In a vibrant coastal city at sunset, the sky is painted in deep layers of amber, magenta, and violet, \
reflecting off the calm harbour waters. Tall glass skyscrapers line the waterfront, their facades \
glowing orange as the last light catches each pane. A wide promenade runs along the shore, \
dotted with palm trees and old iron lampposts just beginning to flicker on. Cyclists weave between \
pedestrians strolling with coffee cups and dogs on leads. A small wooden jetty extends into the harbour \
where a single red sailboat bobs gently, its mast swaying. Seagulls circle lazily overhead. \
In the middle distance, a historic lighthouse stands on a rocky promontory, its white-and-red \
striped tower contrasting with the darkening cliffs behind it. Fishing trawlers chug slowly back \
toward the docks, their deck lights reflected in long rippling streaks across the water. \
On the promenade, a street musician plays an acoustic guitar outside a café whose windows \
are steaming from the espresso machines inside. Children chase pigeons near a mosaic fountain \
that commemorates the city's founding. The cobblestones are warm from the day's heat. \
Across the bay, the hills are covered in terracotta-roofed houses cascading down to the water, \
each with a small garden bursting with bougainvillea in pink and white. A funicular railway \
climbs the steepest hill, its yellow carriage visible between the rooftops. The air smells \
of salt, grilled fish, and jasmine. A street-food vendor ladles steaming soup into paper cups \
for a queue of office workers heading home. Neon signs in three languages begin to glow \
above the narrow side streets leading back into the old town. A tabby cat sits on a warm \
stone wall, watching everything with calm authority. The tide is coming in.\
"""

# Repeat until we exceed 60 000 characters (safely above 15k tokens)
filler = ""
while len(filler) < (INPUT_TOKENS*4):
    filler += PARAGRAPH + "\n\n"

prompt_text = "Make an image based on the following:\n\n" + filler

print(f"Prompt length: {len(prompt_text):,} chars (~{len(prompt_text)//4:,} tokens)")

client = genai.Client(
    vertexai=True,
    project=PROJECT_ID,
    location=LOCATION,
    http_options=types.HttpOptions(api_version=API_VERSION),
)

response = client.models.generate_content(
    model=MODEL,
    contents=[
        types.Content(
            role="user",
            parts=[types.Part(text=prompt_text)],
        )
    ],
    config=types.GenerateContentConfig(
        response_modalities=["TEXT", "IMAGE"],
        image_config=types.ImageConfig(aspect_ratio="1:1"),
    ),
)

print(response)

for i, part in enumerate(response.candidates[0].content.parts):
    if part.inline_data and part.inline_data.mime_type.startswith("image/"):
        ext = part.inline_data.mime_type.split("/")[-1]
        filename = f"long_prompt_output_{i}.{ext}"
        with open(filename, "wb") as f:
            f.write(part.inline_data.data)
        print(f"Saved image to {filename}")
    elif part.text:
        print(f"Text: {part.text}")

Note: Repetition of PARAGRAPH in the prompt is not realistic. In our real case we have a set text of instructions, and then provide numerous example files for style guidance, and this amounts to >15k tokens which causes the error.

Metadata

Metadata

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions