#gemini api
2 prompts found

Gemini API Structured Output Schema Builder: response_mime_type JSON, response_schema from Pydantic, Enums, propertyOrdering, Nullable Fields, and Validation Retries
Design a Gemini API structured output setup for an extraction or classification job: the schema in the subset Gemini accepts, the google-genai Python config, enum and nullable handling, field order that keeps reasoning before answers, and a validation and retry plan for bad responses.
Act as an LLM application engineer who builds production extraction pipelines on the Gemini API, knows which JSON schema features Gemini structured output supports, and ships schemas that parse on the first try. Inputs: - The task: what goes in (document type, length, language) and what must come out: [ExtractionTask] - Fields you need, with type, allowed values, required or optional, and an example value: [FieldList] - Model and SDK: Gemini model name, google-genai Python or JS SDK version, Vertex AI or Gemini Developer API: [ModelAndSDK] - Sample documents or snippets, including a messy or incomplete one: [SampleDocs] - Downstream consumer: database table, Pydantic model, or API that reads the JSON: [Downstream] - Output format: [Format] Generate: 1. A schema design table from FieldList: field, Gemini schema type, enum values, nullable, required, and description text that tells the model how to fill it. 2. A Pydantic model (or TypeScript type if ModelAndSDK uses JS) that maps to Downstream, kept to schema features Gemini structured output accepts, with unsupported features (for example complex conditional keywords) replaced by simpler patterns and marked [confirm in current Gemini docs]. 3. The google-genai request config: response_mime_type set to application/json and response_schema set to the model, plus the system instruction. 4. Field ordering: put evidence or source quote fields before the answer fields and set propertyOrdering where the SDK needs it, so the model writes support before conclusions. 5. Null and enum rules: when to return null instead of guessing, an "unknown" enum value where a category may not apply, and a single text/x.enum option if the job is pure classification. 6. A test run plan over SampleDocs: expected JSON for each, including the messy one. 7. Validation and retry: parse with the Pydantic model, what to do on a validation error (retry once with the error message, then route to human review), and what to log. 8. A short cost and size note: keep descriptions short, cap list lengths, and avoid deep nesting. Constraints: - Do not invent SDK parameters; when unsure of a name or limit in ModelAndSDK, write [confirm in current Gemini docs]. - Never make up field values in examples beyond SampleDocs. - Code first, short explanations. No em dashes.

Gemini API Context Caching Planner: Implicit vs Explicit Caching on Gemini 2.5 Models, google-genai caches.create with TTL, Minimum Cacheable Tokens, Stable Prefix Prompt Layout, Storage vs Input Cost Math, and Cache Hit Monitoring
Decide whether and how to cache a large repeated context in the Gemini API: compare implicit caching with explicit caches, lay out prompts so the stable part comes first, create and refresh a cache with the google-genai SDK and a TTL that matches your traffic, check the cost against storage charges using the rates you paste, and confirm hits in usage metadata.
Act as a backend engineer who runs production apps on the Gemini API, has cut monthly token bills by caching large shared contexts, and has debugged the error you get when a request sends a system instruction alongside a cached context. Inputs: - The workload: requests per day, hours when traffic arrives, shared context size in tokens, and typical question size: [Workload] - What repeats across requests (system instruction, manuals, codebase, a long video or PDF) and how often it changes: [SharedContext] - Model and SDK in use: [ModelChoice] - Rates copied from the current Gemini pricing page for input, cached input, and cache storage: [PricingNotes] - Current prompt structure or code snippet: [CurrentCode] - Output format: [Format] Generate: 1. An implicit versus explicit decision: implicit caching is on by default for Gemini 2.5 models and gives a discount only when a request shares a prefix with a recent one, with no guarantee; explicit caching stores the context for a TTL you pay storage for, and gives a predictable discount. Pick one for this Workload and say why. 2. Prompt layout rules for either mode: put the large stable content first and the per request question last, keep the stable part byte identical, and send similar requests close together in time. 3. A minimum size check: explicit caches need a minimum number of input tokens that differs by model; tell the user to measure with client.models.count_tokens and compare against the current minimum for ModelChoice from the docs. 4. Python code with the google-genai SDK: client.caches.create with model, a CreateCachedContentConfig holding display_name, system_instruction, contents, and ttl; then client.models.generate_content with GenerateContentConfig(cached_content=cache.name). Put system_instruction and tools in the cache, not in the request, because the request cannot set them when it uses cached content. 5. TTL and refresh: choose a TTL from the traffic hours in Workload, extend it with client.caches.update when traffic continues, and delete it with client.caches.delete when the day ends. 6. Versioning: caches are not edited in place; create a new cache when SharedContext changes, and put the content version in display_name. 7. Cost math using only PricingNotes: daily cost without caching, with explicit caching (cached tokens at the cached rate plus storage tokens times hours), and the break even request count. 8. Monitoring: log usage_metadata.cached_content_token_count and prompt_token_count per request, alert when the cached share drops, and recreate the cache on a not found error after expiry. Constraints: - Never invent prices or token minimums; use PricingNotes and mark anything else "confirm in the current Gemini docs". - The cache model must match the request model exactly. No em dashes.