Skip to content

Commit a6e9612

Browse files
jsonbaileyclaude
andcommitted
refactor: Move context before model/provider params, fix resumption token
Reorder LDAIConfigTracker.__init__ to match updated spec: context now comes before model_name and provider_name. Also fix resumption_token to omit variationKey from the JSON when it is empty, and handle the absent key when reconstructing from a token. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent df722f9 commit a6e9612

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ def create_tracker(self, token: str, context: Context) -> LDAIConfigTracker:
9696
ld_client=self._client,
9797
run_id=payload["runId"],
9898
config_key=payload["configKey"],
99-
variation_key=payload.get("variationKey", ""),
99+
variation_key=payload.get("variationKey") or "",
100100
version=payload["version"],
101+
context=context,
101102
model_name="",
102103
provider_name="",
103-
context=context,
104104
)
105105

106106
def _completion_config(
@@ -864,9 +864,9 @@ def tracker_factory() -> LDAIConfigTracker:
864864
config_key=key,
865865
variation_key=variation_key,
866866
version=version,
867+
context=context,
867868
model_name=model_name,
868869
provider_name=provider_name,
869-
context=context,
870870
graph_key=graph_key,
871871
)
872872

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ def __init__(
8080
config_key: str,
8181
variation_key: str,
8282
version: int,
83+
context: Context,
8384
model_name: str,
8485
provider_name: str,
85-
context: Context,
8686
graph_key: Optional[str] = None,
8787
):
8888
"""
@@ -93,9 +93,9 @@ def __init__(
9393
:param config_key: Configuration key for tracking.
9494
:param variation_key: Variation key for tracking.
9595
:param version: Version of the variation.
96+
:param context: Context for evaluation.
9697
:param model_name: Name of the model used.
9798
:param provider_name: Name of the provider used.
98-
:param context: Context for evaluation.
9999
:param graph_key: When set, include ``graphKey`` in all event payloads
100100
(e.g. config-level metrics inside a graph).
101101
"""
@@ -116,15 +116,18 @@ def resumption_token(self) -> str:
116116
A URL-safe Base64-encoded JSON string that can be used to reconstruct
117117
a tracker in a different process (e.g. for deferred feedback).
118118
119-
The token contains ``runId``, ``configKey``, ``variationKey``, and
120-
``version``. ``modelName`` and ``providerName`` are **not** included.
119+
The token contains ``runId``, ``configKey``, ``version``, and
120+
optionally ``variationKey`` (omitted when empty).
121+
``modelName`` and ``providerName`` are **not** included.
121122
"""
122-
payload = json.dumps({
123+
data: dict = {
123124
"runId": self._run_id,
124125
"configKey": self._config_key,
125-
"variationKey": self._variation_key,
126-
"version": self._version,
127-
})
126+
}
127+
if self._variation_key:
128+
data["variationKey"] = self._variation_key
129+
data["version"] = self._version
130+
payload = json.dumps(data)
128131
return base64.urlsafe_b64encode(payload.encode("utf-8")).rstrip(b"=").decode("utf-8")
129132

130133
def __get_track_data(self) -> dict:

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,27 @@ def test_resumption_token_is_url_safe_base64(client: LDClient):
716716
base64.urlsafe_b64decode(padded.encode("utf-8"))
717717

718718

719+
def test_resumption_token_omits_variation_key_when_empty(client: LDClient):
720+
import base64
721+
import json
722+
723+
context = Context.create("user-key")
724+
tracker = LDAIConfigTracker(
725+
ld_client=client, run_id="test-run-id", config_key="cfg-key",
726+
variation_key="", version=1, context=context,
727+
model_name="model", provider_name="provider",
728+
)
729+
730+
token = tracker.resumption_token
731+
padded = token + "=" * (-len(token) % 4)
732+
decoded = json.loads(base64.urlsafe_b64decode(padded.encode("utf-8")).decode("utf-8"))
733+
734+
assert "variationKey" not in decoded
735+
assert decoded["runId"] == "test-run-id"
736+
assert decoded["configKey"] == "cfg-key"
737+
assert decoded["version"] == 1
738+
739+
719740
def test_tracker_with_explicit_run_id(client: LDClient):
720741
context = Context.create("user-key")
721742
tracker = LDAIConfigTracker(

0 commit comments

Comments
 (0)