docs: ChatOpenAI.with_structured_output nits (#25952)

This commit is contained in:
Bagatur 2024-09-03 01:20:58 -07:00 committed by GitHub
parent 5b99bb2437
commit da113f6363
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1102,18 +1102,15 @@ class BaseChatOpenAI(BaseChatModel):
) -> Runnable[LanguageModelInput, _DictOrPydantic]: ) -> Runnable[LanguageModelInput, _DictOrPydantic]:
"""Model wrapper that returns outputs formatted to match the given schema. """Model wrapper that returns outputs formatted to match the given schema.
.. versionchanged:: 0.1.21
Support for ``strict`` argument added.
Support for ``method`` = "json_schema" added.
Args: Args:
schema: schema:
The output schema. Can be passed in as: The output schema. Can be passed in as:
- an OpenAI function/tool schema,
- a JSON Schema, - an OpenAI function/tool schema,
- a TypedDict class (support added in 0.1.20), - a JSON Schema,
- or a Pydantic class. - a TypedDict class (support added in 0.1.20),
- or a Pydantic class.
If ``schema`` is a Pydantic class then the model output will be a If ``schema`` is a Pydantic class then the model output will be a
Pydantic instance of that class, and the model-generated fields will be Pydantic instance of that class, and the model-generated fields will be
validated by the Pydantic class. Otherwise the model output will be a validated by the Pydantic class. Otherwise the model output will be a
@ -1121,25 +1118,20 @@ class BaseChatOpenAI(BaseChatModel):
for more on how to properly specify types and descriptions of for more on how to properly specify types and descriptions of
schema fields when specifying a Pydantic or TypedDict class. schema fields when specifying a Pydantic or TypedDict class.
.. versionchanged:: 0.1.20 method: The method for steering model generation, one of:
Added support for TypedDict class. - "function_calling":
Uses OpenAI's tool-calling (formerly called function calling)
method: API: https://platform.openai.com/docs/guides/function-calling
The method for steering model generation, one of: - "json_schema":
- "function_calling": Uses OpenAI's Structured Output API: https://platform.openai.com/docs/guides/structured-outputs
Uses OpenAI's tool-calling (formerly called function calling) Supported for "gpt-4o-mini", "gpt-4o-2024-08-06", and later
API: https://platform.openai.com/docs/guides/function-calling models.
- "json_schema": - "json_mode":
Uses OpenAI's Structured Output API: Uses OpenAI's JSON mode. Note that if using JSON mode then you
https://platform.openai.com/docs/guides/structured-outputs must include instructions for formatting the output into the
Supported for "gpt-4o-mini", "gpt-4o-2024-08-06", and later desired schema into the model call:
models. https://platform.openai.com/docs/guides/structured-outputs/json-mode
- "json_mode":
Uses OpenAI's JSON mode. Note that if using JSON mode then you
must include instructions for formatting the output into the
desired schema into the model call:
https://platform.openai.com/docs/guides/structured-outputs/json-mode
Learn more about the differences between the methods and which models Learn more about the differences between the methods and which models
support which methods here: support which methods here:
@ -1147,14 +1139,6 @@ class BaseChatOpenAI(BaseChatModel):
- https://platform.openai.com/docs/guides/structured-outputs/structured-outputs-vs-json-mode - https://platform.openai.com/docs/guides/structured-outputs/structured-outputs-vs-json-mode
- https://platform.openai.com/docs/guides/structured-outputs/function-calling-vs-response-format - https://platform.openai.com/docs/guides/structured-outputs/function-calling-vs-response-format
.. versionchanged:: 0.1.21
Added support for "json_schema".
.. note:: Planned breaking change in version `0.2.0`
``method`` default will be changed to "json_schema" from
"function_calling".
include_raw: include_raw:
If False then only the parsed structured output is returned. If If False then only the parsed structured output is returned. If
an error occurs during model output parsing it will be raised. If True an error occurs during model output parsing it will be raised. If True
@ -1163,6 +1147,7 @@ class BaseChatOpenAI(BaseChatModel):
will be caught and returned as well. The final output is always a dict will be caught and returned as well. The final output is always a dict
with keys "raw", "parsed", and "parsing_error". with keys "raw", "parsed", and "parsing_error".
strict: strict:
- True: - True:
Model output is guaranteed to exactly match the schema. Model output is guaranteed to exactly match the schema.
The input schema will also be validated according to The input schema will also be validated according to
@ -1177,12 +1162,6 @@ class BaseChatOpenAI(BaseChatModel):
"function_calling" or "json_mode" defaults to None. Can only be "function_calling" or "json_mode" defaults to None. Can only be
non-null if ``method`` is "function_calling" or "json_schema". non-null if ``method`` is "function_calling" or "json_schema".
.. versionadded:: 0.1.21
.. note:: Planned breaking change in version `0.2.0`
``strict`` will default to True when ``method`` is
"function_calling" as of version `0.2.0`.
kwargs: Additional keyword args aren't supported. kwargs: Additional keyword args aren't supported.
Returns: Returns:
@ -1196,6 +1175,23 @@ class BaseChatOpenAI(BaseChatModel):
- "parsed": None if there was a parsing error, otherwise the type depends on the ``schema`` as described above. - "parsed": None if there was a parsing error, otherwise the type depends on the ``schema`` as described above.
- "parsing_error": Optional[BaseException] - "parsing_error": Optional[BaseException]
.. versionchanged:: 0.1.20
Added support for TypedDict class ``schema``.
.. versionchanged:: 0.1.21
Support for ``strict`` argument added.
Support for ``method`` = "json_schema" added.
.. note:: Planned breaking changes in version `0.2.0`
- ``method`` default will be changed to "json_schema" from
"function_calling".
- ``strict`` will default to True when ``method`` is
"function_calling" as of version `0.2.0`.
.. dropdown:: Example: schema=Pydantic class, method="function_calling", include_raw=False, strict=True .. dropdown:: Example: schema=Pydantic class, method="function_calling", include_raw=False, strict=True
Note, OpenAI has a number of restrictions on what types of schemas can be Note, OpenAI has a number of restrictions on what types of schemas can be