openai[patch]: route to Responses API if relevant attributes are set (#31645)

Following https://github.com/langchain-ai/langchain/pull/30329.
This commit is contained in:
ccurme 2025-06-17 16:04:38 -04:00 committed by GitHub
parent 3044bd37a9
commit 6409498f6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 5 deletions

View File

@ -593,8 +593,8 @@ class BaseChatOpenAI(BaseChatModel):
"""
store: Optional[bool] = None
"""If True, the Responses API may store response data for future use. Defaults to
True.
"""If True, OpenAI may store response data for future use. Defaults to True
for the Responses API and False for the Chat Completions API.
.. versionadded:: 0.3.24
"""
@ -1074,6 +1074,12 @@ class BaseChatOpenAI(BaseChatModel):
def _use_responses_api(self, payload: dict) -> bool:
if isinstance(self.use_responses_api, bool):
return self.use_responses_api
elif self.include is not None:
return True
elif self.reasoning is not None:
return True
elif self.truncation is not None:
return True
else:
return _use_responses_api(payload)
@ -3173,7 +3179,13 @@ def _use_responses_api(payload: dict) -> bool:
uses_builtin_tools = "tools" in payload and any(
_is_builtin_tool(tool) for tool in payload["tools"]
)
responses_only_args = {"previous_response_id", "text", "truncation", "include"}
responses_only_args = {
"include",
"previous_response_id",
"reasoning",
"text",
"truncation",
}
return bool(uses_builtin_tools or responses_only_args.intersection(payload))

View File

@ -317,7 +317,9 @@ def test_stateful_api() -> None:
def test_route_from_model_kwargs() -> None:
llm = ChatOpenAI(model=MODEL_NAME, model_kwargs={"truncation": "auto"})
llm = ChatOpenAI(
model=MODEL_NAME, model_kwargs={"text": {"format": {"type": "text"}}}
)
_ = next(llm.stream("Hello"))
@ -356,7 +358,7 @@ def test_file_search() -> None:
def test_stream_reasoning_summary() -> None:
llm = ChatOpenAI(
model="o4-mini",
use_responses_api=True,
# Routes to Responses API if `reasoning` is set.
reasoning={"effort": "medium", "summary": "auto"},
)
message_1 = {"role": "user", "content": "What is 3^3?"}