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(mcp): type-check a batch item's lang_guess like the single-request path

AlKor130a51bfd24cc4
diff --git a/laya/mcp/tools.py b/laya/mcp/tools.py
index 761a27f9..4c6631e8 100644
--- a/laya/mcp/tools.py
+++ b/laya/mcp/tools.py
@@ -918,7 +918,11 @@ def _validate_batch_item(request: Any, i: int) -> dict:
if key in request:
item[key] = _validate_batch_str(request[key], "%s[%r]" % (where, key))
if "lang_guess" in request:
- item["lang_guess"] = request["lang_guess"]
+ # Type-check it the way the single-request path does (`validate_lang_guess`), instead of
+ # passing it through raw: a non-string (e.g. a JSON object) otherwise reached
+ # `_english_from_code(dict)`, stringified to something that matches no English subtag, and
+ # silently routed that item to the multilingual checkpoint instead of raising cleanly.
+ item["lang_guess"] = validate_lang_guess(request["lang_guess"])
# `Router.predict_batch` reads both off the request dict and splits requests that ask for
# different budgets into separate forward passes, so an item that names one must keep it.
for key in ("max_len", "head_max_len"):
diff --git a/tests/test_mcp.py b/tests/test_mcp.py
index af5a4dd6..494946df 100644
--- a/tests/test_mcp.py
+++ b/tests/test_mcp.py
@@ -943,7 +943,7 @@ def test_batch_validation():
# The message that lists the accepted keys must list the ones the validator keeps --
# a client reads that list as the schema. Both directions, derived from the validator.
probe = {"state": STATE, "questions": QUESTIONS, "model": "english", "task": "massive",
- "lang": "en", "lang_guess": False, "max_len": 5, "head_max_len": 5}
+ "lang": "en", "lang_guess": "de", "max_len": 5, "head_max_len": 5}
kept = set(validate_batch_requests([probe])[0])
ok("batch/validation_covers_every_override",
kept == {"state", "questions"} | set(BATCH_ITEM_OVERRIDES), repr(sorted(kept)))
@@ -977,6 +977,20 @@ def test_batch_predict():
"invalid_questions")
ok("batch/predict_not_called_on_bad_input", router.predict_batch_calls == [])
+ # A batch item's lang_guess is type-checked like the single-request path, not passed through
+ # raw -- a non-string otherwise reached core and silently misrouted the item.
+ router = BatchRouter()
+ expect_tool_error("batch/predict_item_lang_guess_type",
+ lambda: laya_predict_batch([{"state": STATE, "questions": QUESTIONS,
+ "lang_guess": {"not": "a string"}}], router=router),
+ "invalid_lang_guess")
+ ok("batch/predict_bad_lang_guess_not_called", router.predict_batch_calls == [])
+ router = BatchRouter()
+ laya_predict_batch([{"state": STATE, "questions": QUESTIONS, "lang_guess": "de"}], router=router)
+ ok("batch/predict_item_lang_guess_forwarded",
+ router.predict_batch_calls[0][0][0].get("lang_guess") == "de",
+ repr(router.predict_batch_calls[0][0][0]))
+
router = BatchRouter()
out = laya_predict_batch(BATCH_REQUESTS, batch_size=8, router=router)
ok("batch/predict_one_call", len(router.predict_batch_calls) == 1,