Back to Discover

#structured output

2 prompts found

Gemini API Structured Output Schema Builder: response_mime_type JSON, response_schema from Pydantic, Enums, propertyOrdering, Nullable Fields, and Validation Retries
🤖 AI Tools

Gemini API Structured Output Schema Builder: response_mime_type JSON, response_schema from Pydantic, Enums, propertyOrdering, Nullable Fields, and Validation Retries

Ppromptstudio·Oct 9, 2026
No rating

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.

llama.cpp GBNF Grammar Writer: Force a Local Model to Return Valid Invoice Extraction JSON
🤖 AI Tools

llama.cpp GBNF Grammar Writer: Force a Local Model to Return Valid Invoice Extraction JSON

Ppromptstudio·Oct 5, 2026
No rating

Write a GBNF grammar for llama.cpp that forces a local GGUF model to emit JSON matching your invoice extraction schema: fixed key order, bounded strings and whitespace, enums, dates, nullable fields, and the commands to run it with llama-cli or llama-server, plus the validation that still has to happen in code.

Act as a machine learning engineer who runs local GGUF models with llama.cpp in document extraction pipelines and writes GBNF grammars by hand. You know a grammar controls the shape of the output, not whether the values are true, and you design both halves. Inputs: - Target JSON schema or a sample of the exact JSON wanted: [JsonSchema] - Field rules (required, nullable, enums, date format, max lengths, max array items): [FieldRules] - How llama.cpp is run (llama-cli, llama-server /completion, or the OpenAI compatible endpoint) and the build date if known: [LlamaCppSetup] - Two short sample documents the model will read: [SampleDocs] - Downstream code that consumes the JSON (language, validator): [Consumer] - Output format: [Format] - Language: [Lang] Generate: 1. Grammar design notes: fixed key order from JsonSchema, which fields are nullable, how enums become literal alternatives, and why strings, arrays, and whitespace get upper bounds (an unbounded ws rule lets the model pad forever). 2. The .gbnf file. Start with root ::= and define rules for objects, arrays, string, number, integer, date, enums, and ws. Write literal quotes as "\"" and use a character class that excludes quote, backslash, and control characters for string content. Use {m,n} repetition for bounds and note that older builds may not support it. 3. Run commands for LlamaCppSetup: llama-cli with --grammar-file, or the grammar field in a llama-server /completion request body. Mention --json-schema and the json_schema_to_grammar.py script as the alternative when the schema changes often, and say to confirm paths in the current repo. 4. Prompt text that goes with the grammar: a short instruction plus the document, telling the model to use null when a field is not on the page. 5. Validation in Consumer: JSON parse, schema validation, and business checks the grammar cannot do (line items times price against total, date is a real calendar date). 6. Test plan against SampleDocs: what to look for, including a document with a missing field to confirm null is produced instead of a guess. Constraints: - The grammar must accept every valid output and nothing else; walk through one sample output against the rules. - Do not claim the grammar improves accuracy; it only guarantees parseable structure. - No em dashes.