From 6409498f6c4cc3f6498ec4c548759c5305df0449 Mon Sep 17 00:00:00 2001 From: ccurme Date: Tue, 17 Jun 2025 16:04:38 -0400 Subject: [PATCH] openai[patch]: route to Responses API if relevant attributes are set (#31645) Following https://github.com/langchain-ai/langchain/pull/30329. --- .../langchain_openai/chat_models/base.py | 18 +++++++++++++++--- .../chat_models/test_responses_api.py | 6 ++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py index aa38b4a9338..4ebda394ab1 100644 --- a/libs/partners/openai/langchain_openai/chat_models/base.py +++ b/libs/partners/openai/langchain_openai/chat_models/base.py @@ -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)) diff --git a/libs/partners/openai/tests/integration_tests/chat_models/test_responses_api.py b/libs/partners/openai/tests/integration_tests/chat_models/test_responses_api.py index 7224a7e2958..622c83f0f7f 100644 --- a/libs/partners/openai/tests/integration_tests/chat_models/test_responses_api.py +++ b/libs/partners/openai/tests/integration_tests/chat_models/test_responses_api.py @@ -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?"}