Skip to content

Commit 8b3f740

Browse files
authored
docs: update GenAI structured output section to include JSON mode details (#32992)
1 parent 54a9556 commit 8b3f740

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

docs/docs/integrations/chat/google_generative_ai.ipynb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"\n",
3232
"| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n",
3333
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
34-
"| ✅ | ✅ | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |\n",
34+
"| ✅ | ✅ | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |\n",
3535
"\n",
3636
"### Setup\n",
3737
"\n",
@@ -653,15 +653,35 @@
653653
"\n",
654654
"# Initialize the model\n",
655655
"llm = ChatGoogleGenerativeAI(model=\"gemini-2.0-flash\", temperature=0)\n",
656-
"structured_llm = llm.with_structured_output(Person)\n",
656+
"\n",
657+
"# Method 1: Default function calling approach\n",
658+
"structured_llm_default = llm.with_structured_output(Person)\n",
659+
"\n",
660+
"# Method 2: Native JSON mode\n",
661+
"structured_llm_json = llm.with_structured_output(Person, method=\"json_mode\")\n",
657662
"\n",
658663
"# Invoke the model with a query asking for structured information\n",
659-
"result = structured_llm.invoke(\n",
664+
"result = structured_llm_json.invoke(\n",
660665
" \"Who was the 16th president of the USA, and how tall was he in meters?\"\n",
661666
")\n",
662667
"print(result)"
663668
]
664669
},
670+
{
671+
"cell_type": "markdown",
672+
"id": "g9w06ld1ggq",
673+
"metadata": {},
674+
"source": [
675+
"### Structured Output Methods\n",
676+
"\n",
677+
"Two methods are supported for structured output:\n",
678+
"\n",
679+
"- **`method=\"function_calling\"` (default)**: Uses tool calling to extract structured data. Compatible with all Gemini models.\n",
680+
"- **`method=\"json_mode\"`**: Uses Gemini's native structured output with `responseSchema`. More reliable but requires Gemini 1.5+ models.\n",
681+
"\n",
682+
"The `json_mode` method is **recommended for better reliability** as it constrains the model's generation process directly rather than relying on post-processing tool calls."
683+
]
684+
},
665685
{
666686
"cell_type": "markdown",
667687
"id": "90d4725e",

0 commit comments

Comments
 (0)