Gemini integration design
Check capabilities before a request
Gemini is a model family, not a single fixed set of input and output types. A multimodal request combines different kinds of data, such as a question and an image of a chart. Before a request, verify the exact model ID, API mode, accepted modalities, limits and output features. In this example a synthetic text question is 24 bytes and fictional image metadata says 120 bytes. These are invented sizes, not real files or provider limits. The local preflight accepts the combined 144 bytes under the application limit of 200, but rejects unsupported images or a smaller limit. A real gateway must additionally measure actual bytes, decode files safely, check MIME and authorization, and enforce token and spend budgets.
def preflight(parts, supported, byte_limit): # Normalized metadata only; no file reading or upload. if not parts or type(byte_limit) is not int or byte_limit < 0: return False total = 0 for modality, size in parts: if modality not in supported or type(size) is not int or size <= 0: return False total += size return total <= byte_limit
chart = [("text", 24), ("image", 120)]print(preflight(chart, {"text", "image"}, 200))print(preflight(chart, {"text"}, 200))print(preflight(chart, {"text", "image"}, 100))Select the interface and SDK
Google recommends the Interactions API for new Gemini Developer API projects as of June 2026; the older generateContent interface remains supported. The Python package is google-genai and its import is from google import genai. Interactions uses client.interactions.create(model=..., input=...), with typed steps and an output-text convenience accessor. In contrast, client.models.generate_content(model=..., contents=...) uses contents and candidate/part responses. Do not mix the input shapes, configuration parameters or response parsing of the two modes. Pin a reviewed SDK release and check its actual signatures. The official image guide demonstrates a model ID gemini-3.8-flash; it is a documentation example, not a guarantee of current access, image support on every platform, or a recommendation to use that model.
Build a request without credentials
The following mock mimics only the basic create call and a text output accessor. It returns a dictionary with an application fixture marker, not a real SDK object. The printed month is prewritten, not inferred by a model. In an authorized integration, genai.Client(api_key=...) creates a developer client, and a call can use store=False to opt out of server-side interaction state. This is not a blanket promise of no retention: check product terms, abuse monitoring and account settings. Stateful continuation uses previous_interaction_id; never share another user’s interaction ID. For multimodal input, Interactions accepts typed text and image objects; the image guide shows an image URI plus MIME type and also inline data. Use only approved synthetic or consented files; avoid arbitrary URL fetching. A declared MIME type is not proof of actual content.
class MockInteractions: def create(self, *, model, input, store): assert model == "offline-chart-demo" and store is False assert input == "Read the fictional chart label: July." return {"output_text": "July", "fixture": True}
client = MockInteractions()reply = client.create(model="offline-chart-demo", input="Read the fictional chart label: July.", store=False)print(reply["output_text"])Choose the deployment boundary
Google AI Studio is a developer prototyping environment connected to the Gemini Developer API. Google Cloud’s enterprise Gemini platform (including Vertex AI terminology in SDKs and existing integrations) has separate project, identity, region, permissions and billing controls. Google’s consumer Gemini app is a different product; its subscription does not establish API quotas or credits. The shared SDK does not make these environments interchangeable. Select developer API credentials or the appropriate Cloud identity explicitly, verify service availability and model/feature compatibility, and keep keys server-side. Do not assume a free tier, regional access, or a specific rate. An upload or file reference can create an external data lifecycle; no upload occurs here.
Practice: reject unsupported input
Try text only, an audio entry on a text/image-only allowlist, a zero-byte entry, and exactly 200 bytes. Predict the result before running the preflight function. Explain why a byte budget is not a token budget, why a matching MIME string is insufficient, and why supported input modalities do not guarantee supported output modalities. Read the migration and image guides in source-evidence.json. Next, build an adapter that keeps a blocked response separate from a valid but empty reply.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Gemini integration design quiz
Apply the Gemini integration contract.
Which input interface is recommended for new Gemini Developer API projects in the reviewed documentation?
A text part is 24 bytes and an image is 120 bytes. Both modalities are supported, but the local limit is 100. What happens?
What does store=False establish?
Which SDK package is used in the reviewed Python documentation?
What must be checked before using a Cloud deployment rather than the Developer API?