|
31 | 31 | "\n",
|
32 | 32 | "| [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",
|
33 | 33 | "| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
|
34 |
| - "| ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |\n", |
| 34 | + "| ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |\n", |
35 | 35 | "\n",
|
36 | 36 | "### Setup\n",
|
37 | 37 | "\n",
|
|
653 | 653 | "\n",
|
654 | 654 | "# Initialize the model\n",
|
655 | 655 | "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", |
657 | 662 | "\n",
|
658 | 663 | "# Invoke the model with a query asking for structured information\n",
|
659 |
| - "result = structured_llm.invoke(\n", |
| 664 | + "result = structured_llm_json.invoke(\n", |
660 | 665 | " \"Who was the 16th president of the USA, and how tall was he in meters?\"\n",
|
661 | 666 | ")\n",
|
662 | 667 | "print(result)"
|
663 | 668 | ]
|
664 | 669 | },
|
| 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 | + }, |
665 | 685 | {
|
666 | 686 | "cell_type": "markdown",
|
667 | 687 | "id": "90d4725e",
|
|
0 commit comments