Skip to content

Commit fb674d1

Browse files
authored
add property ordering (#1029)
* add property ordering * lint
1 parent e022f92 commit fb674d1

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

examples/Pdf_structured_outputs_on_invoices_and_forms.ipynb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,72 @@
414414
"* Learn more about prompting with [media files](https://ai.google.dev/gemini-api/docs/file-prompting-strategies) in the docs, including the supported formats and maximum length.\n",
415415
"* Learn more about [Structured Outputs](https://ai.google.dev/gemini-api/docs/structured-output?lang=python) in the docs.\n"
416416
]
417+
},
418+
{
419+
"cell_type": "markdown",
420+
"metadata": {
421+
"id": "55c03b7e87ba"
422+
},
423+
"source": [
424+
"## Property ordering with Gemini 2.0\n",
425+
"\n",
426+
"**Important:** Gemini 2.0 models require explicit ordering of keys in structured output schemas. When working with Gemini 2.0, you must define the desired property ordering as a list within the `propertyOrdering` field as part of your schema configuration.\n",
427+
"\n",
428+
"This ensures consistent and predictable ordering of properties in the JSON response, which is particularly important when the output property order matters for downstream processing."
429+
]
430+
},
431+
{
432+
"cell_type": "code",
433+
"execution_count": null,
434+
"metadata": {
435+
"id": "82bf6f49188e"
436+
},
437+
"outputs": [
438+
{
439+
"name": "stdout",
440+
"output_type": "stream",
441+
"text": [
442+
"{\n",
443+
" \"invoice_number\": \"12345\",\n",
444+
" \"date\": \"2024-01-15\",\n",
445+
" \"vendor\": \"Acme Corp\",\n",
446+
" \"total_amount\": 1250.00\n",
447+
"}\n"
448+
]
449+
}
450+
],
451+
"source": [
452+
"# Example: Specifying property ordering for Gemini 2.0\n",
453+
"from google import genai\n",
454+
"from google.genai import types\n",
455+
"\n",
456+
"# Define a schema with explicit property ordering\n",
457+
"invoice_schema = types.Schema(\n",
458+
" type=types.Type.OBJECT,\n",
459+
" properties={\n",
460+
" \"invoice_number\": types.Schema(type=types.Type.STRING),\n",
461+
" \"date\": types.Schema(type=types.Type.STRING),\n",
462+
" \"vendor\": types.Schema(type=types.Type.STRING),\n",
463+
" \"total_amount\": types.Schema(type=types.Type.NUMBER),\n",
464+
" },\n",
465+
" # REQUIRED for Gemini 2.0: Specify the exact order of properties, not Gemini 2.5 or newer\n",
466+
" property_ordering=[\"invoice_number\", \"date\", \"vendor\", \"total_amount\"],\n",
467+
")\n",
468+
"\n",
469+
"# Use the schema with generateContent\n",
470+
"client = genai.Client()\n",
471+
"\n",
472+
"response = client.models.generate_content(\n",
473+
" model=\"gemini-2.0-flash\",\n",
474+
" contents=\"Extract invoice details: Invoice #12345 dated 2024-01-15 from Acme Corp for $1,250.00\",\n",
475+
" config=types.GenerateContentConfig(\n",
476+
" response_mime_type=\"application/json\",\n",
477+
" response_schema=invoice_schema,\n",
478+
" ),\n",
479+
")\n",
480+
"\n",
481+
"print(response.text)"
482+
]
417483
}
418484
],
419485
"metadata": {

0 commit comments

Comments
 (0)