Skip to content

Commit 0392d8c

Browse files
jsonbaileyclaude
andcommitted
fix: swap track_metrics_of parameter order to match spec
Spec requirement 1.1.14.1 defines metrics_extractor as the first parameter and the operation callable as the second. The previous implementation had them reversed. This is a breaking change for any callers passing positional arguments. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f17c535 commit 0392d8c

5 files changed

Lines changed: 9 additions & 9 deletions

File tree

packages/sdk/server-ai/src/ldai/judge/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ async def evaluate(
7575
assert self._evaluation_response_structure is not None
7676

7777
response = await tracker.track_metrics_of_async(
78-
lambda: self._model_runner.invoke_structured_model(messages, self._evaluation_response_structure),
7978
lambda result: result.metrics,
79+
lambda: self._model_runner.invoke_structured_model(messages, self._evaluation_response_structure),
8080
)
8181

8282
parsed = self._parse_evaluation_response(response.data)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ async def run(self, input: str) -> AgentResult:
2929
"""
3030
tracker = self._ai_config.create_tracker()
3131
return await tracker.track_metrics_of_async(
32-
lambda: self._agent_runner.run(input),
3332
lambda result: result.metrics,
33+
lambda: self._agent_runner.run(input),
3434
)
3535

3636
def get_agent_runner(self) -> AgentRunner:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ async def invoke(self, prompt: str) -> ModelResponse:
4949
all_messages = config_messages + self._messages
5050

5151
response = await tracker.track_metrics_of_async(
52-
lambda: self._model_runner.invoke_model(all_messages),
5352
lambda result: result.metrics,
53+
lambda: self._model_runner.invoke_model(all_messages),
5454
)
5555

5656
if (

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ def _track_from_metrics_extractor(
262262

263263
def track_metrics_of(
264264
self,
265-
func: Callable[[], Any],
266265
metrics_extractor: Callable[[Any], Any],
266+
func: Callable[[], Any],
267267
) -> Any:
268268
"""
269269
Track metrics for a synchronous AI operation.
@@ -277,8 +277,8 @@ def track_metrics_of(
277277
278278
For async operations, use :meth:`track_metrics_of_async`.
279279
280-
:param func: Synchronous callable that runs the operation
281280
:param metrics_extractor: Function that extracts LDAIMetrics from the operation result
281+
:param func: Synchronous callable that runs the operation
282282
:return: The result of the operation
283283
"""
284284
start_ns = time.perf_counter_ns()
@@ -294,14 +294,14 @@ def track_metrics_of(
294294
self.track_duration(duration)
295295
return self._track_from_metrics_extractor(result, metrics_extractor)
296296

297-
async def track_metrics_of_async(self, func, metrics_extractor):
297+
async def track_metrics_of_async(self, metrics_extractor, func):
298298
"""
299299
Track metrics for an async AI operation (``func`` is awaited).
300300
301301
Same event semantics as :meth:`track_metrics_of`.
302302
303-
:param func: Async callable or zero-arg callable that returns an awaitable when called
304303
:param metrics_extractor: Function that extracts LDAIMetrics from the operation result
304+
:param func: Async callable or zero-arg callable that returns an awaitable when called
305305
:return: The result of the operation
306306
"""
307307
start_ns = time.perf_counter_ns()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ def fn():
531531
def extract(r):
532532
return LDAIMetrics(success=True, usage=TokenUsage(5, 2, 3))
533533

534-
out = tracker.track_metrics_of(fn, extract)
534+
out = tracker.track_metrics_of(extract, fn)
535535
assert out == "done"
536536
calls = client.track.mock_calls # type: ignore
537537
assert any(c.args[0] == "$ld:ai:generation:success" for c in calls)
@@ -551,7 +551,7 @@ async def fn():
551551
def extract(r):
552552
return LDAIMetrics(success=True, usage=TokenUsage(5, 2, 3))
553553

554-
await tracker.track_metrics_of_async(fn, extract)
554+
await tracker.track_metrics_of_async(extract, fn)
555555
gk_td = {**_base_td(), "graphKey": "gg"}
556556
calls = client.track.mock_calls # type: ignore
557557
assert any(

0 commit comments

Comments
 (0)