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(integrations): reject a non-object remote decision response in LangChain, CrewAI and LlamaIndex

Aashishfb401ebf8244
diff --git a/laya/integrations/crewai.py b/laya/integrations/crewai.py
index 73ac94b8..b5d703e4 100644
--- a/laya/integrations/crewai.py
+++ b/laya/integrations/crewai.py
@@ -196,12 +196,24 @@ def _call_remote(
opener = urllib.request.build_opener(_SameOriginRedirectHandler())
try:
with opener.open(req, timeout=timeout) as resp:
- return json.loads(resp.read().decode("utf-8"))
+ parsed = json.loads(resp.read().decode("utf-8"))
except urllib.error.HTTPError as e:
body = e.read().decode("utf-8", errors="replace")
raise RuntimeError(f"Laya server error {e.code}: {body}") from e
except Exception as e:
raise RuntimeError(f"Failed to connect to Laya server at {url}: {e}") from e
+ # A remote laya-serve answers with a JSON object, but `json.loads` accepts any top-level value:
+ # a gateway, a mis-pointed `base_url`, or a proxy error page can reply with a bare array, string,
+ # number or `true`/`null`. Consumers read the result with `res.get("answers", …)` (the delegation
+ # lookup and the guardrail screen), so a non-object leaks ``AttributeError: 'list' object has no
+ # attribute 'get'`` instead of the ``RuntimeError`` every other remote failure here already
+ # raises. The check sits after the ``try`` so it is not swallowed by the catch-all above.
+ if not isinstance(parsed, dict):
+ raise RuntimeError(
+ f"Malformed decision response from Laya server at {url}: "
+ f"expected a JSON object, got {type(parsed).__name__}"
+ )
+ return parsed
_DEFAULT_ROUTER = None
diff --git a/laya/integrations/langchain.py b/laya/integrations/langchain.py
index 52fd9548..4ef37f31 100644
--- a/laya/integrations/langchain.py
+++ b/laya/integrations/langchain.py
@@ -206,12 +206,24 @@ def _call_remote(
opener = urllib.request.build_opener(_SameOriginRedirectHandler())
try:
with opener.open(req, timeout=timeout) as resp:
- return json.loads(resp.read().decode("utf-8"))
+ parsed = json.loads(resp.read().decode("utf-8"))
except urllib.error.HTTPError as e:
body = e.read().decode("utf-8", errors="replace")
raise RuntimeError(f"Laya server error {e.code}: {body}") from e
except Exception as e:
raise RuntimeError(f"Failed to connect to Laya server at {url}: {e}") from e
+ # A remote laya-serve answers with a JSON object, but `json.loads` accepts any top-level value:
+ # a gateway, a mis-pointed `base_url`, or a proxy error page can reply with a bare array, string,
+ # number or `true`/`null`. Consumers read the result with `result.get("answers", …)`, so a
+ # non-object leaks ``AttributeError: 'list' object has no attribute 'get'`` instead of the
+ # ``RuntimeError`` every other remote failure here already raises. The check sits after the
+ # ``try`` so it is not swallowed by the catch-all connect handler above.
+ if not isinstance(parsed, dict):
+ raise RuntimeError(
+ f"Malformed decision response from Laya server at {url}: "
+ f"expected a JSON object, got {type(parsed).__name__}"
+ )
+ return parsed
_DEFAULT_ROUTER = None
diff --git a/laya/integrations/llamaindex.py b/laya/integrations/llamaindex.py
index de423407..90a71527 100644
--- a/laya/integrations/llamaindex.py
+++ b/laya/integrations/llamaindex.py
@@ -192,12 +192,24 @@ def _call_remote(
opener = urllib.request.build_opener(_SameOriginRedirectHandler())
try:
with opener.open(req, timeout=timeout) as resp:
- return json.loads(resp.read().decode("utf-8"))
+ parsed = json.loads(resp.read().decode("utf-8"))
except urllib.error.HTTPError as e:
body = e.read().decode("utf-8", errors="replace")
raise RuntimeError(f"Laya server error {e.code}: {body}") from e
except Exception as e:
raise RuntimeError(f"Failed to connect to Laya server at {url}: {e}") from e
+ # A remote laya-serve answers with a JSON object, but `json.loads` accepts any top-level value:
+ # a gateway, a mis-pointed `base_url`, or a proxy error page can reply with a bare array, string,
+ # number or `true`/`null`. Consumers read the result with `res.get("answers", …)` (the selector
+ # and route lookups), so a non-object leaks ``AttributeError: 'list' object has no attribute
+ # 'get'`` instead of the ``RuntimeError`` every other remote failure here already raises. The
+ # check sits after the ``try`` so it is not swallowed by the catch-all connect handler above.
+ if not isinstance(parsed, dict):
+ raise RuntimeError(
+ f"Malformed decision response from Laya server at {url}: "
+ f"expected a JSON object, got {type(parsed).__name__}"
+ )
+ return parsed
_DEFAULT_ROUTER = None
diff --git a/tests/test_crewai.py b/tests/test_crewai.py
index b2626c40..da25050e 100644
--- a/tests/test_crewai.py
+++ b/tests/test_crewai.py
@@ -327,6 +327,54 @@ with patch("urllib.request.build_opener") as mock_build_opener:
check("remote/auth", req.headers.get("Authorization"), "Bearer sk-crew-key")
+# --------------------------------------------------------------- 4b. Non-Object Remote Response
+#
+# A remote laya-serve body is arbitrary JSON: a gateway, a mis-pointed `base_url`, or a proxy error
+# page can answer with a top-level array, string, number or `true`/`null`. `json.loads` accepts each
+# of those and both consumers read the result with `res.get("answers", …)`, so before the guard a
+# non-object leaked `AttributeError: 'list' object has no attribute 'get'` past the fail-closed path
+# instead of the `RuntimeError` every other remote failure raises here. `_call_remote` and the public
+# `route()` are both driven so a regression at either layer is caught.
+from laya.integrations.crewai import _call_remote as _crewai_call_remote
+
+_CREWAI_NON_OBJECT_BODIES = [
+ ([], "list"),
+ ([{"answers": {"delegation": {"choice": "agent_1"}}}], "list-of-objects"),
+ ("unauthorized", "str"),
+ (None, "null"),
+ (123, "int"),
+ (True, "bool"),
+]
+for _body, _label in _CREWAI_NON_OBJECT_BODIES:
+ with patch("urllib.request.build_opener") as _m:
+ _m.return_value.open.return_value = DummyHTTPResponse(_body)
+ try:
+ _crewai_call_remote("http://localhost:8000", "s", {"q": {}})
+ check_true("nonobj/%s/_call_remote raises" % _label, False, "no error raised")
+ except RuntimeError as exc:
+ check_true("nonobj/%s/malformed" % _label, "Malformed decision response" in str(exc))
+ check_true("nonobj/%s/names object" % _label, "expected a JSON object" in str(exc))
+ except Exception as exc:
+ check_true("nonobj/%s/_call_remote raises" % _label, False, type(exc).__name__)
+
+ try:
+ LayaCrewRouter(base_url="http://localhost:8000", api_key="k").route(
+ "Analyze codebase", agents)
+ check_true("nonobj/%s/route raises" % _label, False, "no error raised")
+ except RuntimeError as exc:
+ check_true("nonobj/%s/route malformed" % _label,
+ "Malformed decision response" in str(exc))
+ except Exception as exc:
+ check_true("nonobj/%s/route surfaces RuntimeError" % _label, False, type(exc).__name__)
+
+# Positive control: a well-formed object body still routes -- the guard does not over-reject.
+with patch("urllib.request.build_opener") as _m:
+ _m.return_value.open.return_value = DummyHTTPResponse(remote_response)
+ check("nonobj/object still routes",
+ LayaCrewRouter(base_url="http://localhost:8000").route(
+ "Analyze codebase", agents).agent_index, 1)
+
+
# --------------------------------------------------------------- 5. Per-call decision controls
#
# `laya.integrations.langchain` has forwarded `max_len` / `head_max_len` and the five hook
diff --git a/tests/test_langchain.py b/tests/test_langchain.py
index 906ec2e4..5c187d01 100644
--- a/tests/test_langchain.py
+++ b/tests/test_langchain.py
@@ -1776,6 +1776,69 @@ check("routing/with the de hint no German query is read as English",
_on_english(_DE_ALL, lang_guess="de"), [])
+# --------------------------------------------------------------- Non-Object Remote Response
+#
+# A remote laya-serve body is arbitrary JSON: a gateway, a mis-pointed `base_url`, or a proxy error
+# page can answer with a top-level array, string, number or `true`/`null`. `json.loads` accepts each
+# of those and the consumers read the result with `result.get("answers", …)`, so before the guard a
+# non-object leaked `AttributeError: 'list' object has no attribute 'get'` past the fail-closed path
+# instead of the `RuntimeError` every other remote failure raises here. `_call_remote` is driven
+# directly so the check is independent of whether langchain-core is installed in this run.
+class _RawBodyResponse:
+ def __init__(self, raw):
+ self._data = raw.encode("utf-8")
+
+ def read(self):
+ return self._data
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ pass
+
+
+class _RawBodyOpener:
+ def __init__(self, raw):
+ self._raw = raw
+
+ def open(self, req, timeout=10.0):
+ return _RawBodyResponse(self._raw)
+
+
+_NON_OBJECT_BODIES_LC = [
+ "[]",
+ '[{"answers": {"q": {"choice": "a"}}}]',
+ '"unauthorized"',
+ "null",
+ "123",
+ "true",
+]
+_orig_build_opener_lc = langchain_module.urllib.request.build_opener
+try:
+ for _raw in _NON_OBJECT_BODIES_LC:
+ langchain_module.urllib.request.build_opener = (lambda *a, _r=_raw: _RawBodyOpener(_r))
+ try:
+ langchain_module._call_remote("http://localhost:8000", "s", {"q": {}})
+ check_true("nonobj/%s/raises" % _raw, False, "no error raised")
+ except RuntimeError as _e:
+ check_true("nonobj/%s/malformed" % _raw,
+ "Malformed decision response" in str(_e))
+ check_true("nonobj/%s/names object" % _raw,
+ "expected a JSON object" in str(_e))
+ except Exception as _e:
+ check_true("nonobj/%s/raises" % _raw, False, type(_e).__name__)
+ # Positive control: a well-formed object body passes straight through -- the guard does not
+ # over-reject.
+ _obj_body = json.dumps({"answers": {"q": {"choice": "a"}}})
+ langchain_module.urllib.request.build_opener = lambda *a: _RawBodyOpener(_obj_body)
+ check("nonobj/object passes through",
+ langchain_module._call_remote("http://localhost:8000", "s", {"q": {}}),
+ {"answers": {"q": {"choice": "a"}}})
+finally:
+ langchain_module.urllib.request.build_opener = _orig_build_opener_lc
+
+
# --------------------------------------------------------------- Summary
print(f"PASS: {len(PASS)}")
print(f"FAIL: {len(FAIL)}")
diff --git a/tests/test_llamaindex.py b/tests/test_llamaindex.py
index 9df1b6b2..7615507b 100644
--- a/tests/test_llamaindex.py
+++ b/tests/test_llamaindex.py
@@ -393,6 +393,55 @@ with patch("urllib.request.build_opener") as mock_build_opener:
check("remote/auth", req.headers.get("Authorization"), "Bearer sk-test-token")
+# --------------------------------------------------------------- 5b. Non-Object Remote Response
+#
+# A remote laya-serve body is arbitrary JSON: a gateway, a mis-pointed `base_url`, or a proxy error
+# page can answer with a top-level array, string, number or `true`/`null`. `json.loads` accepts each
+# of those and the consumers read the result with `res.get("answers", …)` (the selector and route
+# lookups), so before the guard a non-object leaked `AttributeError: 'list' object has no attribute
+# 'get'` past the fail-closed path instead of the `RuntimeError` every other remote failure raises
+# here. `_call_remote` and the public `select()` are both driven so a regression at either layer is
+# caught.
+from laya.integrations.llamaindex import _call_remote as _llama_call_remote
+
+_LLAMA_NON_OBJECT_BODIES = [
+ ([], "list"),
+ ([{"answers": {"selector": {"choice": "choice_1"}}}], "list-of-objects"),
+ ("unauthorized", "str"),
+ (None, "null"),
+ (123, "int"),
+ (True, "bool"),
+]
+for _body, _label in _LLAMA_NON_OBJECT_BODIES:
+ with patch("urllib.request.build_opener") as _m:
+ _m.return_value.open.return_value = DummyHTTPResponse(_body)
+ try:
+ _llama_call_remote("http://localhost:8000", "s", {"q": {}})
+ check_true("nonobj/%s/_call_remote raises" % _label, False, "no error raised")
+ except RuntimeError as exc:
+ check_true("nonobj/%s/malformed" % _label, "Malformed decision response" in str(exc))
+ check_true("nonobj/%s/names object" % _label, "expected a JSON object" in str(exc))
+ except Exception as exc:
+ check_true("nonobj/%s/_call_remote raises" % _label, False, type(exc).__name__)
+
+ try:
+ LayaSingleSelector(base_url="http://localhost:8000", api_key="k").select(
+ tools, "Query via remote server")
+ check_true("nonobj/%s/select raises" % _label, False, "no error raised")
+ except RuntimeError as exc:
+ check_true("nonobj/%s/select malformed" % _label,
+ "Malformed decision response" in str(exc))
+ except Exception as exc:
+ check_true("nonobj/%s/select surfaces RuntimeError" % _label, False, type(exc).__name__)
+
+# Positive control: a well-formed object body still selects -- the guard does not over-reject.
+with patch("urllib.request.build_opener") as _m:
+ _m.return_value.open.return_value = DummyHTTPResponse(remote_response)
+ check("nonobj/object still selects",
+ LayaSingleSelector(base_url="http://localhost:8000").select(
+ tools, "Query via remote server").selections[0].index, 1)
+
+
# --------------------------------------------------------------- 6. Per-call decision controls
#
# `laya.integrations.langchain` has forwarded `max_len` / `head_max_len` and the five hook