Intro to Gemini 3.1 Flash-Lite

Source notebook

Repo path: gemini/getting-started/intro_gemini_3_1_flash_lite.ipynb · Open on GitHub · intermediate

Quickstart for Gemini 3.1 Flash-Lite with Google Gen AI SDK on Google Cloud.

Summary

This notebook teaches how to initialize the Google Gen AI SDK with an enterprise Google Cloud client and call Gemini 3.1 Flash-Lite. It demonstrates generation, streaming, chat, safety settings, multimodal inputs, structured output, tools, function calling, grounding metadata, URL context, and token counting. It also highlights Gemini 3 API features such as thinking levels, media resolution, thought signatures, multimodal function responses, and inline citations.

Key code patterns

Enterprise client

PROJECT_ID = str(os.environ.get("GOOGLE_CLOUD_PROJECT"))
LOCATION = "global"
client = genai.Client(
    enterprise=True,
    project=PROJECT_ID,
    location=LOCATION,
)

Creates the Google Gen AI SDK client for Google Cloud project-based access.

Thinking level

response = client.models.generate_content(
    model=MODEL_ID,
    contents="How does AI work?",
    config=types.GenerateContentConfig(
        thinking_config=types.ThinkingConfig(
            thinking_level=types.ThinkingLevel.MINIMAL
        )
    ),
)

Controls reasoning effort to trade response quality against latency and cost.

Media resolution per part

types.Part(
    file_data=types.FileData(
        file_uri="gs://cloud-samples-data/generative-ai/image/a-man-and-a-dog.png",
        mime_type="image/jpeg",
    ),
    media_resolution=types.PartMediaResolution(
        level=types.PartMediaResolutionLevel.MEDIA_RESOLUTION_ULTRA_HIGH
    ),
)

Sets multimodal processing resolution for an individual image or video input.

Manual function calling history

history = [
    types.Content(role="user", parts=[types.Part(text=prompt)]),
    response.candidates[0].content,
    types.Content(
        role="user",
        parts=[types.Part.from_function_response(
            name=function_call.name,
            response=function_response_data,
        )],
    ),
]

Preserves the model response content so thought signatures remain attached.

Search grounding

response = client.models.generate_content(
    model=MODEL_ID,
    contents="Where will the next FIFA World Cup be held?",
    config=types.GenerateContentConfig(
        tools=[types.Tool(google_search=types.GoogleSearch())],
    ),
)
print(response.candidates[0].grounding_metadata.grounding_chunks)

Uses Google Search as a tool and reads grounding metadata for citations.

Structured output

response = client.models.generate_content(
    model=MODEL_ID,
    contents="Give me information for the United States.",
    config=types.GenerateContentConfig(
        response_mime_type="application/json",
        response_schema=CountryInfo,
    ),
)
print(response.parsed)

Requests JSON output validated against a Pydantic schema.

Models & APIs used

  • Models: gemini-3.1-flash-lite
  • APIs / services: Vertex AI, Agent Platform API, Google Cloud Storage, Google Search
  • SDKs / libraries: google-genai, pydantic, IPython

When to use this

Use this pattern to prototype Gemini 3.1 Flash-Lite features on Google Cloud with the Google Gen AI SDK.

Gotchas & caveats

  • The notebook requires installing or upgrading google-genai and says to ignore pip dependency errors.
  • Colab users must authenticate with google.colab.auth.authenticate_user().
  • A Google Cloud project is used for authentication, and the Agent Platform API must be enabled.
  • LOCATION is set to global.
  • If thinking_level is not specified, the model defaults to HIGH.
  • thinking_level and legacy thinking_budget cannot be used in the same request or a 400 error is returned.
  • Media resolution affects token usage and latency; long videos or documents may require lower resolution.
  • ULTRA_HIGH media resolution is only supported for individual parts.
  • Raw API or JSON history handling must return thought_signature in the exact part received, or a 400 error can occur.
  • For Gemini 3, the notebook recommends keeping temperature at the default value of 1.0.

Best practices

  • Use thinking_level MINIMAL for low-complexity, lower-latency responses.
  • Use higher thinking levels for tasks requiring deeper reasoning.
  • Use Google Gen AI SDK chat history or append the full model response so thought signatures are handled automatically.
  • Append response.candidates[0].content when doing manual function calling across turns.
  • Set media_resolution based on detail needs and input length.
  • Use response_schema with response_mime_type application/json for structured output.
  • Inspect grounding_metadata when using search or URL context tools.
  • Check finish_reason and safety_ratings when safety filters may block a response.