File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed
libs/genai/langchain_google_genai Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -1216,8 +1216,12 @@ class Joke(BaseModel):
12161216 )
12171217
12181218
1219+ # Default method uses function calling
12191220 structured_llm = llm.with_structured_output(Joke)
1220- structured_llm.invoke("Tell me a joke about cats")
1221+
1222+ # For more reliable output, use json_mode with native responseSchema
1223+ structured_llm_json = llm.with_structured_output(Joke, method="json_mode")
1224+ structured_llm_json.invoke("Tell me a joke about cats")
12211225
12221226 .. code-block:: python
12231227
@@ -1227,6 +1231,17 @@ class Joke(BaseModel):
12271231 rating=None,
12281232 )
12291233
1234+ Two methods are supported for structured output:
1235+
1236+ * ``method="function_calling"`` (default): Uses tool calling to extract
1237+ structured data. Compatible with all models.
1238+ * ``method="json_mode"``: Uses Gemini's native structured output with
1239+ responseSchema. More reliable but requires Gemini 1.5+ models.
1240+
1241+ The ``json_mode`` method is recommended for better reliability as it constrains
1242+ the model's generation process directly rather than relying on post-processing
1243+ tool calls.
1244+
12301245 Image input:
12311246 .. code-block:: python
12321247
You can’t perform that action at this time.
0 commit comments