@@ -1219,8 +1219,8 @@ class Joke(BaseModel):
12191219 # Default method uses function calling
12201220 structured_llm = llm.with_structured_output(Joke)
12211221
1222- # For more reliable output, use json_mode with native responseSchema
1223- structured_llm_json = llm.with_structured_output(Joke, method="json_mode ")
1222+ # For more reliable output, use json_schema with native responseSchema
1223+ structured_llm_json = llm.with_structured_output(Joke, method="json_schema ")
12241224 structured_llm_json.invoke("Tell me a joke about cats")
12251225
12261226 .. code-block:: python
@@ -1235,12 +1235,13 @@ class Joke(BaseModel):
12351235
12361236 * ``method="function_calling"`` (default): Uses tool calling to extract
12371237 structured data. Compatible with all models.
1238- * ``method="json_mode "``: Uses Gemini's native structured output with
1238+ * ``method="json_schema "``: Uses Gemini's native structured output with
12391239 responseSchema. More reliable but requires Gemini 1.5+ models.
1240+ ``method="json_mode"`` also works for backwards compatibility but is a misnomer.
12401241
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.
1242+ The ``json_schema `` method is recommended for better reliability as it
1243+ constrains the model's generation process directly rather than relying on
1244+ post-processing tool calls.
12441245
12451246 Image input:
12461247 .. code-block:: python
@@ -2103,7 +2104,9 @@ def get_num_tokens(self, text: str) -> int:
21032104 def with_structured_output (
21042105 self ,
21052106 schema : Union [Dict , Type [BaseModel ]],
2106- method : Optional [Literal ["function_calling" , "json_mode" ]] = "function_calling" ,
2107+ method : Optional [
2108+ Literal ["function_calling" , "json_mode" , "json_schema" ]
2109+ ] = "function_calling" ,
21072110 * ,
21082111 include_raw : bool = False ,
21092112 ** kwargs : Any ,
@@ -2115,7 +2118,7 @@ def with_structured_output(
21152118
21162119 parser : OutputParserLike
21172120
2118- if method == "json_mode" :
2121+ if method in ( "json_mode" , "json_schema" ): # `json_schema` preferred
21192122 if isinstance (schema , type ) and is_basemodel_subclass (schema ):
21202123 if issubclass (schema , BaseModelV1 ):
21212124 schema_json = schema .schema ()
0 commit comments