Skip to content

Commit 2189b81

Browse files
authored
fix: Add runtime DeprecationWarnings to deprecated methods (#145)
1 parent 53db736 commit 2189b81

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

packages/sdk/server-ai/src/ldai/client.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import uuid
2+
import warnings
23
from typing import Any, Callable, Dict, List, Optional, Tuple
34

45
import chevron
@@ -169,6 +170,7 @@ def config(
169170
:param variables: Additional variables for the model configuration.
170171
:return: The value of the model configuration along with a tracker used for gathering metrics.
171172
"""
173+
warnings.warn("config() is deprecated, use completion_config() instead", DeprecationWarning, stacklevel=2)
172174
return self.completion_config(key, context, default, variables)
173175

174176
def _judge_config(
@@ -417,12 +419,9 @@ async def create_chat(
417419
default_ai_provider: Optional[str] = None,
418420
) -> Optional[ManagedModel]:
419421
"""
420-
.. deprecated:: Use :meth:`create_model` instead.
421-
422-
Creates and returns a ManagedModel for AI conversations.
423-
This method is a deprecated alias for :meth:`create_model`.
422+
.. deprecated:: Use :meth:`create_model` instead. This method will be removed in a future version.
424423
"""
425-
log.warning('create_chat() is deprecated, use create_model() instead')
424+
warnings.warn("create_chat() is deprecated, use create_model() instead", DeprecationWarning, stacklevel=2)
426425
return await self.create_model(key, context, default, variables, default_ai_provider)
427426

428427
async def create_agent(
@@ -545,6 +544,7 @@ def agent(
545544
:param context: The context to evaluate the agent configuration in.
546545
:return: Configured AIAgentConfig instance.
547546
"""
547+
warnings.warn("agent() is deprecated, use agent_config() instead", DeprecationWarning, stacklevel=2)
548548
return self.agent_config(config.key, context, config.default, config.variables)
549549

550550
def agent_configs(
@@ -791,6 +791,7 @@ def agents(
791791
:param context: The context to evaluate the agent configurations in.
792792
:return: Dictionary mapping agent keys to their AIAgentConfig configurations.
793793
"""
794+
warnings.warn("agents() is deprecated, use agent_configs() instead", DeprecationWarning, stacklevel=2)
794795
return self.agent_configs(agent_configs, context)
795796

796797
def __evaluate(

packages/sdk/server-ai/src/ldai/tracker.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import base64
22
import json
33
import time
4+
import warnings
45
from dataclasses import dataclass
56
from enum import Enum
67
from typing import Any, Callable, Dict, Iterable, List, Optional
@@ -391,6 +392,9 @@ def track_openai_metrics(self, func):
391392
"""
392393
Track OpenAI-specific operations.
393394
395+
.. deprecated:: Use :meth:`track_metrics_of` with ``get_ai_metrics_from_response``
396+
from ``ldai_openai`` instead. This method will be removed in a future version.
397+
394398
This function will track the duration of the operation, the token
395399
usage, and the success or error status.
396400
@@ -404,6 +408,12 @@ def track_openai_metrics(self, func):
404408
:param func: Function to track.
405409
:return: Result of the tracked function.
406410
"""
411+
warnings.warn(
412+
"track_openai_metrics is deprecated. Use track_metrics_of with "
413+
"get_ai_metrics_from_response from ldai_openai instead.",
414+
DeprecationWarning,
415+
stacklevel=2,
416+
)
407417
start_ns = time.perf_counter_ns()
408418
try:
409419
result = func()

packages/sdk/server-ai/tests/test_tracker.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ def to_dict(self):
294294
def get_result():
295295
return Result()
296296

297-
tracker.track_openai_metrics(get_result)
297+
with pytest.warns(DeprecationWarning, match="track_openai_metrics is deprecated"):
298+
tracker.track_openai_metrics(get_result)
298299

299300
calls = [
300301
call(
@@ -335,11 +336,12 @@ def test_tracks_openai_metrics_with_exception(client: LDClient):
335336
def raise_exception():
336337
raise ValueError("Something went wrong")
337338

338-
try:
339-
tracker.track_openai_metrics(raise_exception)
340-
assert False, "Should have thrown an exception"
341-
except ValueError:
342-
pass
339+
with pytest.warns(DeprecationWarning, match="track_openai_metrics is deprecated"):
340+
try:
341+
tracker.track_openai_metrics(raise_exception)
342+
assert False, "Should have thrown an exception"
343+
except ValueError:
344+
pass
343345

344346
calls = [
345347
call(

0 commit comments

Comments
 (0)