Skip to content

Commit 536d45c

Browse files
smurchingclaude
andauthored
Deprecate WorkspaceClient().serving_endpoints.get_open_ai_client() and WorkspaceClient().serving_endpoints.get_langchain_chat_open_ai_client() methods (#1238)
# Deprecate get_open_ai_client and get_langchain_chat_open_ai_client methods These methods are now deprecated in favor of the databricks-openai and databricks-langchain package. Instead of ``` pip install databricks-sdk[openai] from databricks.sdk import WorkspaceClient w = WorkspaceClient() client = w.serving_endpoints.get_open_ai_client() response = client.chat.completions.create(model="databricks-claude-sonnet-4-5", ...) ``` We now recommend ``` pip install databricks-openai from databricks_openai import DatabricksOpenAI # or AsyncDatabricksOpenAI client = DatabricksOpenAI() response = client.chat.completions.create(model="databricks-claude-sonnet-4-5", ...) ``` ## What changes are proposed in this pull request? **WHAT:** This PR deprecates two OpenAI client helper methods in the Databricks SDK: 1. `ServingEndpointsExt.get_open_ai_client()` 2. `ServingEndpointsExt.get_langchain_chat_open_ai_client()` The changes include: - Adding `DeprecationWarning` messages that are raised when either method is called, with clear instructions to migrate to the `databricks-openai` package - Updating docstrings with `.. deprecated::` directives pointing users to the new package - Adding test coverage to verify the deprecation warnings are properly raised - The methods remain functional - this is a soft deprecation that warns users but doesn't break existing code **WHY:** These helper methods were originally added to the Databricks SDK to make it easier to use OpenAI-compatible clients with Databricks Model Serving. However, maintaining OpenAI client integration directly in the SDK creates several issues: 1. **Tight coupling**: The SDK shouldn't be tightly coupled to specific client libraries (OpenAI, LangChain) which have their own release cycles and breaking changes 2. **Feature richness**: A dedicated package (`databricks-openai`) can provide more comprehensive OpenAI compatibility features without bloating the core SDK 3. **Maintenance burden**: Separating concerns allows the OpenAI integration to evolve independently from the core SDK functionality 4. **Better user experience**: The `databricks-openai` package provides a more standard OpenAI-compatible interface that users familiar with the OpenAI SDK will find more intuitive By deprecating these methods now, we give users a clear migration path while maintaining backward compatibility. The deprecation warnings will appear in users' logs, guiding them to adopt the better-maintained `databricks-openai` package. ## How is this tested? **Unit tests:** - Added `test_get_open_ai_client_deprecation_warning()` which verifies that calling `get_open_ai_client()` raises a `DeprecationWarning` with the correct message pointing to `databricks-openai` and `DatabricksOpenAI` - Added `test_get_langchain_chat_open_ai_client_deprecation_warning()` which verifies that calling `get_langchain_chat_open_ai_client()` raises a `DeprecationWarning` with the correct message pointing to `AsyncDatabricksOpenAI`. This test handles both cases where `langchain_openai` is installed and not installed. - Verified all existing tests still pass (with expected deprecation warnings now appearing in test output) **Manual verification:** All new tests pass locally: pytest tests/test_open_ai_mixin.py::test_get_open_ai_client_deprecation_warning -v # PASSED pytest tests/test_open_ai_mixin.py::test_get_langchain_chat_open_ai_client_deprecation_warning -v # PASSED The deprecation warnings are correctly raised in all existing tests that use these methods, confirming the warnings will be visible to end users. --------- Signed-off-by: Siddharth Murching <smurching@gmail.com> Signed-off-by: Sid Murching <sid.murching@databricks.com> Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 50a5b40 commit 536d45c

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
### Internal Changes
1818

1919
### API Changes
20+
* Deprecated `WorkspaceClient.serving_endpoints.get_open_ai_client()` and `WorkspaceClient.serving_endpoints.get_langchain_chat_open_ai_client()` methods in favor of dedicated packages. Users should migrate to `databricks-openai` (using `DatabricksOpenAI`) and `databricks-langchain` (using `ChatDatabricks`) respectively ([#1238](https://github.com/databricks/databricks-sdk-py/pull/1238)).

databricks/sdk/mixins/open_ai_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json as js
2+
import warnings
23
from typing import Dict, Optional
34

45
from requests import Response
@@ -34,6 +35,11 @@ def auth_flow(self, request: httpx.Request) -> httpx.Request:
3435
def get_open_ai_client(self, **kwargs):
3536
"""Create an OpenAI client configured for Databricks Model Serving.
3637
38+
.. deprecated::
39+
This method is deprecated. Please install the `databricks-openai` package
40+
and use `from databricks_openai import DatabricksOpenAI` instead.
41+
See https://api-docs.databricks.com/python/databricks-ai-bridge/latest/databricks_openai.html for more information.
42+
3743
Returns an OpenAI client instance that is pre-configured to send requests to
3844
Databricks Model Serving endpoints. The client uses Databricks authentication
3945
to query endpoints within the workspace associated with the current WorkspaceClient
@@ -66,6 +72,13 @@ def get_open_ai_client(self, **kwargs):
6672
... max_retries=5
6773
... )
6874
"""
75+
warnings.warn(
76+
"get_open_ai_client() is deprecated. Please install the databricks-openai package "
77+
"and use 'from databricks_openai import DatabricksOpenAI' instead. "
78+
"See https://api-docs.databricks.com/python/databricks-ai-bridge/latest/databricks_openai.html for more information.",
79+
DeprecationWarning,
80+
stacklevel=2,
81+
)
6982
try:
7083
from openai import OpenAI
7184
except Exception:
@@ -95,6 +108,20 @@ def get_open_ai_client(self, **kwargs):
95108
return OpenAI(**client_params)
96109

97110
def get_langchain_chat_open_ai_client(self, model):
111+
"""Create a LangChain ChatOpenAI client configured for Databricks Model Serving.
112+
113+
.. deprecated::
114+
This method is deprecated. Please install the `databricks-langchain` package
115+
and use `from databricks_langchain import ChatDatabricks` instead.
116+
See https://api-docs.databricks.com/python/databricks-ai-bridge/latest/databricks_langchain.html for more information.
117+
"""
118+
warnings.warn(
119+
"get_langchain_chat_open_ai_client() is deprecated. Please install the databricks-langchain package "
120+
"and use 'from databricks_langchain import ChatDatabricks' instead. "
121+
"See https://pypi.org/project/databricks-langchain/ for more information.",
122+
DeprecationWarning,
123+
stacklevel=2,
124+
)
98125
try:
99126
from langchain_openai import ChatOpenAI
100127
except Exception:

tests/test_open_ai_mixin.py

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import warnings
23
from io import BytesIO
34

45
import pytest
@@ -81,15 +82,36 @@ def test_open_ai_client_prevents_reserved_param_override(monkeypatch):
8182

8283
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Requires Python > 3.7")
8384
def test_langchain_open_ai_client(monkeypatch):
85+
from unittest.mock import MagicMock, Mock
86+
8487
from databricks.sdk import WorkspaceClient
8588

8689
monkeypatch.setenv("DATABRICKS_HOST", "test_host")
8790
monkeypatch.setenv("DATABRICKS_TOKEN", "test_token")
88-
w = WorkspaceClient(config=Config())
89-
client = w.serving_endpoints.get_langchain_chat_open_ai_client("databricks-meta-llama-3-1-70b-instruct")
9091

91-
assert client.openai_api_base == "https://test_host/serving-endpoints"
92-
assert client.model_name == "databricks-meta-llama-3-1-70b-instruct"
92+
# Mock the langchain_openai import
93+
mock_chat_openai = Mock()
94+
mock_chat_openai.return_value = MagicMock(
95+
openai_api_base="https://test_host/serving-endpoints", model_name="databricks-meta-llama-3-1-70b-instruct"
96+
)
97+
98+
# Mock the module import
99+
import sys
100+
101+
mock_module = MagicMock()
102+
mock_module.ChatOpenAI = mock_chat_openai
103+
sys.modules["langchain_openai"] = mock_module
104+
105+
try:
106+
w = WorkspaceClient(config=Config())
107+
client = w.serving_endpoints.get_langchain_chat_open_ai_client("databricks-meta-llama-3-1-70b-instruct")
108+
109+
assert client.openai_api_base == "https://test_host/serving-endpoints"
110+
assert client.model_name == "databricks-meta-llama-3-1-70b-instruct"
111+
finally:
112+
# Clean up the mock module
113+
if "langchain_openai" in sys.modules:
114+
del sys.modules["langchain_openai"]
93115

94116

95117
def test_http_request(w, requests_mock):
@@ -115,3 +137,73 @@ def test_http_request(w, requests_mock):
115137
assert requests_mock.called
116138
assert response.status_code == 200 # Verify the response status
117139
assert response.text == "The request was successful" # Ensure the response body matches the mocked data
140+
141+
142+
def test_get_open_ai_client_deprecation_warning(monkeypatch):
143+
"""Test that get_open_ai_client raises a DeprecationWarning."""
144+
from databricks.sdk import WorkspaceClient
145+
146+
monkeypatch.setenv("DATABRICKS_HOST", "test_host")
147+
monkeypatch.setenv("DATABRICKS_TOKEN", "test_token")
148+
w = WorkspaceClient(config=Config())
149+
150+
with warnings.catch_warnings(record=True) as warning_list:
151+
warnings.simplefilter("always")
152+
client = w.serving_endpoints.get_open_ai_client()
153+
154+
# Verify a DeprecationWarning was raised
155+
assert len(warning_list) == 1
156+
assert issubclass(warning_list[0].category, DeprecationWarning)
157+
assert "get_open_ai_client() is deprecated" in str(warning_list[0].message)
158+
assert "databricks-openai" in str(warning_list[0].message)
159+
assert "DatabricksOpenAI" in str(warning_list[0].message)
160+
161+
# Verify the client still works
162+
assert client.base_url == "https://test_host/serving-endpoints/"
163+
assert client.api_key == "no-token"
164+
165+
166+
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Requires Python > 3.7")
167+
def test_get_langchain_chat_open_ai_client_deprecation_warning(monkeypatch):
168+
"""Test that get_langchain_chat_open_ai_client raises a DeprecationWarning."""
169+
from unittest.mock import MagicMock, Mock
170+
171+
from databricks.sdk import WorkspaceClient
172+
173+
monkeypatch.setenv("DATABRICKS_HOST", "test_host")
174+
monkeypatch.setenv("DATABRICKS_TOKEN", "test_token")
175+
176+
# Mock the langchain_openai import
177+
mock_chat_openai = Mock()
178+
mock_chat_openai.return_value = MagicMock(
179+
openai_api_base="https://test_host/serving-endpoints", model_name="databricks-meta-llama-3-1-70b-instruct"
180+
)
181+
182+
# Mock the module import
183+
import sys
184+
185+
mock_module = MagicMock()
186+
mock_module.ChatOpenAI = mock_chat_openai
187+
sys.modules["langchain_openai"] = mock_module
188+
189+
try:
190+
w = WorkspaceClient(config=Config())
191+
192+
with warnings.catch_warnings(record=True) as warning_list:
193+
warnings.simplefilter("always")
194+
client = w.serving_endpoints.get_langchain_chat_open_ai_client("databricks-meta-llama-3-1-70b-instruct")
195+
196+
# Verify a DeprecationWarning was raised
197+
assert len(warning_list) == 1
198+
assert issubclass(warning_list[0].category, DeprecationWarning)
199+
assert "get_langchain_chat_open_ai_client() is deprecated" in str(warning_list[0].message)
200+
assert "databricks-langchain" in str(warning_list[0].message)
201+
assert "ChatDatabricks" in str(warning_list[0].message)
202+
203+
# Verify the client still works
204+
assert client.openai_api_base == "https://test_host/serving-endpoints"
205+
assert client.model_name == "databricks-meta-llama-3-1-70b-instruct"
206+
finally:
207+
# Clean up the mock module
208+
if "langchain_openai" in sys.modules:
209+
del sys.modules["langchain_openai"]

0 commit comments

Comments
 (0)