Repository navigation
feat(evaluations): normalize text behaviors #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
29d09f0
Use RFC 8785 json canonicalization and normalize text behaviors
aknight-ld 4d24d34
fix test
aknight-ld 7d2b7bb
empty objects must still omit message history
aknight-ld 850f353
fix(client): give judges JSON.stringify text for non-string values
jeffdupont eed4743
Merge origin/main into AIC-3642-match-spec-content-hash
jeffdupont eaa4028
fix(client): give online judges the same response text as offline judges
jeffdupont File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
packages/client/src/launchdarkly_ai_server/canonical_json.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """RFC 8785 JSON Canonicalization Scheme (JCS), for hashes every SDK must agree on. | ||
|
|
||
| JCS spells JSON the way ECMAScript's ``JSON.stringify`` does: no whitespace, | ||
| object keys sorted by UTF-16 code unit, non-ASCII written as is, and numbers in | ||
| ECMAScript's shortest round-trip form. That is the native form in JavaScript | ||
| and close to it in Go, so the only language that needs this module is Python, | ||
| whose ``json.dumps`` writes ``1.0`` where JCS writes ``1``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import re | ||
| from decimal import Decimal | ||
| from math import isfinite | ||
| from typing import Any | ||
|
|
||
| # Above this an int has no exact IEEE double, so it is written as the double | ||
| # nearest to it, as a JavaScript JSON.parse reads it. | ||
| _MAX_SAFE_INTEGER = 2**53 - 1 | ||
| _SURROGATE = re.compile("[\ud800-\udfff]") | ||
|
|
||
|
|
||
| def canonical_json(value: Any, *, sort_keys: bool = True) -> str: | ||
| """The JCS text of a JSON-like value. | ||
|
|
||
| With ``sort_keys=False`` object keys keep their insertion order, which is | ||
| ``JSON.stringify``'s output exactly: the form judge text uses, so a judge | ||
| reads the same text in every SDK. It is not JCS, so never hash it. | ||
|
|
||
| The value is first normalized through ``json.dumps(..., default=str)``, so | ||
| a tuple is a list, a non-string key is the string ``json.dumps`` writes for | ||
| it, and a value JSON cannot hold is its ``str()``. A non-finite number is | ||
| written ``null``, as ``JSON.stringify`` writes it. | ||
| """ | ||
| return _encode(json.loads(json.dumps(value, default=str)), sort_keys) | ||
|
|
||
|
|
||
| def _encode(value: Any, sort_keys: bool = True) -> str: | ||
| if value is None or value is True or value is False: | ||
| return json.dumps(value) | ||
| if isinstance(value, int): | ||
| if abs(value) <= _MAX_SAFE_INTEGER: | ||
| return str(value) | ||
| try: | ||
| return _number(float(value)) | ||
| except OverflowError: | ||
| return "null" | ||
| if isinstance(value, float): | ||
| return _number(value) | ||
| if isinstance(value, str): | ||
| return _string(value) | ||
| if isinstance(value, list): | ||
| return "[" + ",".join(_encode(item, sort_keys) for item in value) + "]" | ||
| keys = sorted(value, key=_utf16) if sort_keys else list(value) | ||
| return ( | ||
| "{" | ||
| + ",".join(f"{_string(key)}:{_encode(value[key], sort_keys)}" for key in keys) | ||
| + "}" | ||
| ) | ||
|
|
||
|
|
||
| def _number(value: float) -> str: | ||
| """ECMAScript's Number::toString, from the shortest round-trip digits ``repr`` gives.""" | ||
| if not isfinite(value): | ||
| return "null" | ||
| if value == 0: | ||
| return "0" | ||
| _, digit_tuple, exponent = Decimal(repr(abs(value))).as_tuple() | ||
| # The value is 0.<digits> x 10^n, the form Number::toString starts from. | ||
| n = len(digit_tuple) + int(exponent) | ||
| digits = "".join(map(str, digit_tuple)).rstrip("0") | ||
| k = len(digits) | ||
| if k <= n <= 21: | ||
| text = digits + "0" * (n - k) | ||
| elif 0 < n <= 21: | ||
| text = f"{digits[:n]}.{digits[n:]}" | ||
| elif -6 < n <= 0: | ||
| text = f"0.{'0' * -n}{digits}" | ||
| else: | ||
| fraction = f".{digits[1:]}" if k > 1 else "" | ||
| text = f"{digits[0]}{fraction}e{'+' if n - 1 >= 0 else '-'}{abs(n - 1)}" | ||
| return ("-" if value < 0 else "") + text | ||
|
|
||
|
|
||
| def _string(text: str) -> str: | ||
| # A surrogate pair held as two code points is joined into one character, as | ||
| # it is in a JavaScript string; a lone surrogate is escaped, as | ||
| # JSON.stringify escapes it, so the text always has a UTF-8 encoding. | ||
| joined = text.encode("utf-16", "surrogatepass").decode("utf-16", "surrogatepass") | ||
| return _SURROGATE.sub( | ||
| lambda match: f"\\u{ord(match.group()):04x}", | ||
| json.dumps(joined, ensure_ascii=False), | ||
| ) | ||
|
|
||
|
|
||
| def _utf16(key: str) -> bytes: | ||
| return key.encode("utf-16-be", "surrogatepass") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Online judges miss new stringify
Medium Severity
judge_textreturns a string unchanged, so online invoke still shows a judgejson.dumpsof a structured response (spaces,1.0, ASCII escapes). Offline now renders the same object as compact JSON.stringify text, so a rubric can see different conversation text on the two paths.Additional Locations (1)
packages/client/src/launchdarkly_ai_server/evaluations/runner.py#L835-L840Reviewed by Cursor Bugbot for commit eed4743. Configure here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in the commit above: config().invoke() now builds the judge's llm_response with judge_text, so online and offline judges read the same compact text. Test: test_online_judge_reads_a_structured_response_as_json_stringify_text.