Skip to content

The agent engineering platform.

openai[patch]: update with_structured_outputs docstring (#31517)

Bagatur0375848f6c9b
diff --git a/libs/partners/openai/langchain_openai/chat_models/azure.py b/libs/partners/openai/langchain_openai/chat_models/azure.py
index d8bcf99d0b..6d9a356a68 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 561ccd669d..73bc4a806c 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.