Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

fix(examples): reject malformed latency reply members

svonavae068b1d8fa77
diff --git a/examples/agent-stage-latency/run.py b/examples/agent-stage-latency/run.py
index b11e0d7c5..401afbcc8 100644
--- a/examples/agent-stage-latency/run.py
+++ b/examples/agent-stage-latency/run.py
@@ -261,21 +261,34 @@ def project_reply(stage: str, arm: str, raw: Any, data: dict[str, Any]) -> dict[
reply["item_error"] = {"code": "ITEM_ERROR"}
if stage == "G" or (stage == "M" and arm == "rival"):
if arm == "sie":
- choices = raw.get("choices") or []
- message = choices[0].get("message", {}) if choices and isinstance(choices[0], dict) else {}
+ choices = raw.get("choices")
+ if not isinstance(choices, list):
+ reply["malformed_reply"] = True
+ choices = []
+ malformed = [index for index, choice in enumerate(choices) if not isinstance(choice, dict)]
+ if malformed:
+ reply.update(malformed_reply=True, malformed_choice_indices=malformed, choice_count=len(choices))
+ first = choices[0] if choices and isinstance(choices[0], dict) else {}
+ message = first.get("message", {})
+ if not isinstance(message, dict):
+ reply["malformed_reply"] = True
+ message = {}
reply.update(
{
"text": message.get("content"),
"refusal": message.get("refusal"),
- "finish_reason": public_id(choices[0].get("finish_reason")) if choices else None,
+ "finish_reason": public_id(first.get("finish_reason")),
}
)
else:
blocks = raw.get("content")
if isinstance(blocks, list):
reply["content"] = [
- {"type": public_id(b.get("type")), "text": b.get("text")} for b in blocks if isinstance(b, dict)
+ {"type": public_id(b.get("type")), "text": b.get("text")} if isinstance(b, dict) else None
+ for b in blocks
]
+ if any(not isinstance(b, dict) for b in blocks):
+ reply["malformed_reply"] = True
reply["text"] = "".join(
b.get("text", "")
for b in blocks
@@ -297,10 +310,17 @@ def project_reply(stage: str, arm: str, raw: Any, data: dict[str, Any]) -> dict[
reply["dense"] = raw.get("dense")
else:
rows = raw.get("data")
- if isinstance(rows, list) and len(rows) == 1 and isinstance(rows[0], dict) and rows[0].get("index") == 0:
+ if (
+ isinstance(rows, list)
+ and len(rows) == 1
+ and isinstance(rows[0], dict)
+ and type(rows[0].get("index")) is int
+ and rows[0]["index"] == 0
+ ):
reply["dense"] = rows[0].get("embedding")
else:
reply["dense"] = None
+ reply["malformed_reply"] = True
elif arm == "sie":
scores = raw.get("scores")
reply["scores"] = (
diff --git a/examples/agent-stage-latency/tests/test_example.py b/examples/agent-stage-latency/tests/test_example.py
index 93c191123..c84cb280f 100644
--- a/examples/agent-stage-latency/tests/test_example.py
+++ b/examples/agent-stage-latency/tests/test_example.py
@@ -1049,6 +1049,42 @@ def test_malformed_cohere_reply_is_durable_failed_call(tmp_path: Path) -> None:
assert all(len(row["reply"]["results"]) == 21 and row["reply"]["results"][-1] is None for row in replies)
[email protected]("index", [False, True, 0.0, "0", None])
+def test_openai_embedding_projection_requires_integer_zero_index(index: Any) -> None:
+ data = population(1)["E"][0]["data"]
+ reply = run.project_reply("E", "rival", {"data": [{"index": index, "embedding": [0.0] * 3072}]}, data)
+ assert reply["dense"] is None and reply["malformed_reply"]
+ call = protocol.requests("E", data, "rival")[0]
+ with pytest.raises(ValueError, match="malformed reply"):
+ protocol.validate_reply("E", "rival", reply, data, call)
+
+
[email protected]("stage", ["G", "M"])
[email protected]("member", [None, 17])
+def test_anthropic_non_object_content_is_preserved_as_malformed(stage: str, member: Any) -> None:
+ data = population(1)[stage][0]["data"]
+ text = "unharmful" if stage == "G" else '{"entities":[]}'
+ reply = run.project_reply(stage, "rival", {"content": [{"type": "text", "text": text}, member]}, data)
+ assert reply["text"] == text and reply["content"][1] is None and reply["malformed_reply"]
+ call = protocol.requests(stage, data, "rival")[0]
+ with pytest.raises(ValueError, match="malformed reply"):
+ protocol.validate_reply(stage, "rival", reply, data, call)
+
+
[email protected]("member,first", [(None, False), (17, False), (None, True)])
+def test_non_object_guard_choices_keep_stable_failure_evidence(member: Any, first: bool) -> None:
+ data = population(1)["G"][0]["data"]
+ choices = [{"message": {"content": "Safety: Safe"}, "finish_reason": "stop"}]
+ choices.insert(0 if first else 1, member)
+ reply = run.project_reply("G", "sie", {"choices": choices}, data)
+ assert reply["malformed_reply"] and reply["choice_count"] == 2
+ assert reply["malformed_choice_indices"] == [0 if first else 1]
+ assert reply["text"] == (None if first else "Safety: Safe")
+ call = protocol.requests("G", data, "sie")[0]
+ with pytest.raises(ValueError, match="malformed reply"):
+ protocol.validate_reply("G", "sie", reply, data, call)
+
+
def test_paired_cluster_bootstrap_known_statistics() -> None:
settings = config()["bootstrap"]
clusters = [[(1.0, 4.0), (3.0, 6.0)], [(2.0, 5.0), (4.0, 7.0)]]