From 0375848f6c9b71606777087e717e12aec9469400 Mon Sep 17 00:00:00 2001 From: Bagatur <22008038+baskaryan@users.noreply.github.com> Date: Fri, 6 Jun 2025 10:03:47 -0400 Subject: [PATCH] openai[patch]: update with_structured_outputs docstring (#31517) Update docstrings --- .../langchain_openai/chat_models/azure.py | 44 +++++++++++++++++++ .../langchain_openai/chat_models/base.py | 44 +++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/libs/partners/openai/langchain_openai/chat_models/azure.py b/libs/partners/openai/langchain_openai/chat_models/azure.py index d8bcf99d0b7..6d9a356a685 100644 --- a/libs/partners/openai/langchain_openai/chat_models/azure.py +++ b/libs/partners/openai/langchain_openai/chat_models/azure.py @@ -809,6 +809,50 @@ class AzureChatOpenAI(BaseChatOpenAI): Note: ``strict`` can only be non-null if ``method`` is ``"json_schema"`` or ``"function_calling"``. + tools: + A list of tool-like objects to bind to the chat model. Requires that: + + - ``method`` is ``"json_schema"`` (default). + - ``strict=True`` + - ``include_raw=True`` + + If a model elects to call a + tool, the resulting ``AIMessage`` in ``"raw"`` will include tool calls. + + .. dropdown:: Example + + .. code-block:: python + + from langchain.chat_models import init_chat_model + from pydantic import BaseModel + + + class ResponseSchema(BaseModel): + response: str + + + def get_weather(location: str) -> str: + \"\"\"Get weather at a location.\"\"\" + pass + + llm = init_chat_model("openai:gpt-4o-mini") + + structured_llm = llm.with_structured_output( + ResponseSchema, + tools=[get_weather], + strict=True, + include_raw=True, + ) + + structured_llm.invoke("What's the weather in Boston?") + + .. code-block:: python + + { + "raw": AIMessage(content="", tool_calls=[...], ...), + "parsing_error": None, + "parsed": None, + } kwargs: Additional keyword args aren't supported. diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py index 561ccd669dc..73bc4a806cf 100644 --- a/libs/partners/openai/langchain_openai/chat_models/base.py +++ b/libs/partners/openai/langchain_openai/chat_models/base.py @@ -2552,6 +2552,50 @@ class ChatOpenAI(BaseChatOpenAI): # type: ignore[override] Note: ``strict`` can only be non-null if ``method`` is ``"json_schema"`` or ``"function_calling"``. + tools: + A list of tool-like objects to bind to the chat model. Requires that: + + - ``method`` is ``"json_schema"`` (default). + - ``strict=True`` + - ``include_raw=True`` + + If a model elects to call a + tool, the resulting ``AIMessage`` in ``"raw"`` will include tool calls. + + .. dropdown:: Example + + .. code-block:: python + + from langchain.chat_models import init_chat_model + from pydantic import BaseModel + + + class ResponseSchema(BaseModel): + response: str + + + def get_weather(location: str) -> str: + \"\"\"Get weather at a location.\"\"\" + pass + + llm = init_chat_model("openai:gpt-4o-mini") + + structured_llm = llm.with_structured_output( + ResponseSchema, + tools=[get_weather], + strict=True, + include_raw=True, + ) + + structured_llm.invoke("What's the weather in Boston?") + + .. code-block:: python + + { + "raw": AIMessage(content="", tool_calls=[...], ...), + "parsing_error": None, + "parsed": None, + } kwargs: Additional keyword args aren't supported.