Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(evals): write redirected output as utf-8

plox-sumitb481ed3a141f
diff --git a/laya/evals_cli.py b/laya/evals_cli.py
index 48d88797..174eb220 100644
--- a/laya/evals_cli.py
+++ b/laya/evals_cli.py
@@ -490,6 +490,12 @@ def _cmd_compare(args) -> int:
def main(argv: Optional[Sequence[str]] = None) -> int:
args = _build_parser().parse_args(argv)
+ # Same reason as `laya.cli.main`: redirected output is encoded with the locale's codec by
+ # default (cp1252 on Windows), which cannot hold a non-Latin question id, tag or language, so
+ # `laya-evals validate data.jsonl > out.txt` died with a traceback and exit 1 -- the code kept
+ # for a quality failure. `laya eval` lands here before `cli.main` reaches its own copy.
+ if hasattr(sys.stdout, "reconfigure"):
+ sys.stdout.reconfigure(encoding="utf-8")
try:
if args.command == "validate":
return _cmd_validate(args)
diff --git a/tests/test_evals.py b/tests/test_evals.py
index efce7cae..12ed6555 100644
--- a/tests/test_evals.py
+++ b/tests/test_evals.py
@@ -2,8 +2,10 @@
Run: python -m pytest tests/test_evals.py -q
"""
+import io
import json
import re
+import sys
import warnings
import numpy as np
@@ -1826,6 +1828,30 @@ def test_cli_validate_and_dispatch(tmp_path):
assert evals_cli.main(["validate", str(bad)]) == 2
+def test_cli_writes_redirected_output_as_utf8(tmp_path, monkeypatch):
+ """`validate` prints question ids and `run --slice` prints tags: the caller's own text.
+
+ Redirected stdout is encoded with the locale's codec by default (cp1252 on Windows), so
+ `laya-evals validate data.jsonl > out.txt` died with a UnicodeEncodeError traceback on a
+ dataset whose question id is not Latin -- and with exit 1, the code docs/evals.md keeps for
+ a quality failure. The latin-1 wrapper stands in for that codec, so this fails on any OS
+ without the fix. Both entry points are checked because `laya eval` returns before the line
+ that sets stdout in `cli.main`.
+ """
+ from laya import cli, evals_cli
+
+ qid = "श्रेणी"
+ dataset = _write_dataset(tmp_path, [{"state": "s", "questions": {qid: Q["intent"]},
+ "expected": {qid: "a"}}])
+ for entry, argv in ((evals_cli.main, ["validate", dataset]),
+ (cli.main, ["eval", "validate", dataset])):
+ raw = io.BytesIO()
+ monkeypatch.setattr(sys, "stdout", io.TextIOWrapper(raw, encoding="latin-1"))
+ assert entry(argv) == 0
+ sys.stdout.flush()
+ assert qid in raw.getvalue().decode("utf-8")
+
+
def test_cli_compare_exit_codes(tmp_path):
from laya import evals_cli