Skip to content

Commit 761f8c3

Browse files
authored
openai[patch]: pass through with_structured_output kwargs (#31518)
Support ```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""" pass llm = init_chat_model("openai:gpt-4o-mini") structured_llm = llm.with_structured_output( ResponseSchema, tools=[get_weather], strict=True, include_raw=True, tool_choice="required", parallel_tool_calls=False, ) structured_llm.invoke("whats up?") ```
1 parent 0375848 commit 761f8c3

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

libs/partners/openai/langchain_openai/chat_models/azure.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ def get_weather(location: str) -> str:
854854
"parsed": None,
855855
}
856856
857-
kwargs: Additional keyword args aren't supported.
857+
kwargs: Additional keyword args are passed through to the model.
858858
859859
Returns:
860860
A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`.
@@ -880,6 +880,12 @@ def get_weather(location: str) -> str:
880880
881881
``method`` default changed from "function_calling" to "json_schema".
882882
883+
.. versionchanged:: 0.3.12
884+
Support for ``tools`` added.
885+
886+
.. versionchanged:: 0.3.21
887+
Pass ``kwargs`` through to the model.
888+
883889
.. dropdown:: Example: schema=Pydantic class, method="json_schema", include_raw=False, strict=True
884890
885891
Note, OpenAI has a number of restrictions on what types of schemas can be

libs/partners/openai/langchain_openai/chat_models/base.py

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,7 @@ def get_weather(location: str) -> str:
16311631
"parsed": None,
16321632
}
16331633
1634-
kwargs: Additional keyword args aren't supported.
1634+
kwargs: Additional keyword args are passed through to the model.
16351635
16361636
Returns:
16371637
A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`.
@@ -1655,9 +1655,10 @@ def get_weather(location: str) -> str:
16551655
16561656
.. versionchanged:: 0.3.12
16571657
Support for ``tools`` added.
1658+
1659+
.. versionchanged:: 0.3.21
1660+
Pass ``kwargs`` through to the model.
16581661
""" # noqa: E501
1659-
if kwargs:
1660-
raise ValueError(f"Received unsupported arguments {kwargs}")
16611662
if strict is not None and method == "json_mode":
16621663
raise ValueError(
16631664
"Argument `strict` is not supported with `method`='json_mode'"
@@ -1700,13 +1701,18 @@ def get_weather(location: str) -> str:
17001701
)
17011702
tool_name = convert_to_openai_tool(schema)["function"]["name"]
17021703
bind_kwargs = self._filter_disabled_params(
1703-
tool_choice=tool_name,
1704-
parallel_tool_calls=False,
1705-
strict=strict,
1706-
ls_structured_output_format={
1707-
"kwargs": {"method": method, "strict": strict},
1708-
"schema": schema,
1709-
},
1704+
**{
1705+
**dict(
1706+
tool_choice=tool_name,
1707+
parallel_tool_calls=False,
1708+
strict=strict,
1709+
ls_structured_output_format={
1710+
"kwargs": {"method": method, "strict": strict},
1711+
"schema": schema,
1712+
},
1713+
),
1714+
**kwargs,
1715+
}
17101716
)
17111717

17121718
llm = self.bind_tools([schema], **bind_kwargs)
@@ -1721,11 +1727,16 @@ def get_weather(location: str) -> str:
17211727
)
17221728
elif method == "json_mode":
17231729
llm = self.bind(
1724-
response_format={"type": "json_object"},
1725-
ls_structured_output_format={
1726-
"kwargs": {"method": method},
1727-
"schema": schema,
1728-
},
1730+
**{
1731+
**dict(
1732+
response_format={"type": "json_object"},
1733+
ls_structured_output_format={
1734+
"kwargs": {"method": method},
1735+
"schema": schema,
1736+
},
1737+
),
1738+
**kwargs,
1739+
}
17291740
)
17301741
output_parser = (
17311742
PydanticOutputParser(pydantic_object=schema) # type: ignore[arg-type]
@@ -1739,13 +1750,16 @@ def get_weather(location: str) -> str:
17391750
"Received None."
17401751
)
17411752
response_format = _convert_to_openai_response_format(schema, strict=strict)
1742-
bind_kwargs = dict(
1743-
response_format=response_format,
1744-
ls_structured_output_format={
1745-
"kwargs": {"method": method, "strict": strict},
1746-
"schema": convert_to_openai_tool(schema),
1747-
},
1748-
)
1753+
bind_kwargs = {
1754+
**dict(
1755+
response_format=response_format,
1756+
ls_structured_output_format={
1757+
"kwargs": {"method": method, "strict": strict},
1758+
"schema": convert_to_openai_tool(schema),
1759+
},
1760+
**kwargs,
1761+
)
1762+
}
17491763
if tools:
17501764
bind_kwargs["tools"] = [
17511765
convert_to_openai_tool(t, strict=strict) for t in tools
@@ -2597,7 +2611,7 @@ def get_weather(location: str) -> str:
25972611
"parsed": None,
25982612
}
25992613
2600-
kwargs: Additional keyword args aren't supported.
2614+
kwargs: Additional keyword args are passed through to the model.
26012615
26022616
Returns:
26032617
A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`.
@@ -2623,6 +2637,12 @@ def get_weather(location: str) -> str:
26232637
26242638
``method`` default changed from "function_calling" to "json_schema".
26252639
2640+
.. versionchanged:: 0.3.12
2641+
Support for ``tools`` added.
2642+
2643+
.. versionchanged:: 0.3.21
2644+
Pass ``kwargs`` through to the model.
2645+
26262646
.. dropdown:: Example: schema=Pydantic class, method="json_schema", include_raw=False, strict=True
26272647
26282648
Note, OpenAI has a number of restrictions on what types of schemas can be

0 commit comments

Comments
 (0)