about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-06-22 17:52:46 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-06-22 17:52:46 +0300
commit7e515ef3a86cb63ef467bdfcbe3c52393d3a67e6 (patch)
treeec7a4522b37e80b88835a6d44ad84a84f4e2f3b9
parentb8685086ad3b85b2e42cf3d0a9e4ff1017f2bcbe (diff)
downloadthe-epube-7e515ef3a86cb63ef467bdfcbe3c52393d3a67e6.tar.gz
the-epube-7e515ef3a86cb63ef467bdfcbe3c52393d3a67e6.tar.xz
add json schema
-rw-r--r--review_mr.py127
1 files changed, 89 insertions, 38 deletions
diff --git a/review_mr.py b/review_mr.py
index 770c79c..901d6b8 100644
--- a/review_mr.py
+++ b/review_mr.py
@@ -3,7 +3,6 @@
 
 import json
 import os
-import re
 import sys
 from dataclasses import dataclass
 from typing import Optional
@@ -88,44 +87,91 @@ def fetch_diff(project_id: str, mr_iid: str, token: str, server_url: str):
 
 SYSTEM_PROMPT = (
     "You are a code review assistant. Review the diff below and return "
-    "a JSON array of line annotations. Each annotation must have:\n"
-    "  - \"file\": the file path (as shown above)\n"
-    "  - \"line\": the line number in the new version\n"
-    "  - \"message\": a concise review comment (1-2 sentences)\n\n"
+    "a JSON object with an \"annotations\" key containing an array of "
+    "line annotations.\n\n"
+    "JSON Schema:\n"
+    "```json\n"
+    "{\n"
+    '  "type": "object",\n'
+    '  "properties": {\n'
+    '    "annotations": {\n'
+    '      "type": "array",\n'
+    '      "items": {\n'
+    '        "type": "object",\n'
+    '        "properties": {\n'
+    '          "file": {"type": "string"},\n'
+    '          "line": {"type": "integer"},\n'
+    '          "message": {"type": "string"}\n'
+    '        },\n'
+    '        "required": ["file", "line", "message"]\n'
+    '      }\n'
+    '    }\n'
+    '  },\n'
+    '  "required": ["annotations"],\n'
+    '  "additionalProperties": false\n'
+    '}\n'
+    "```\n\n"
     "Rules:\n"
     "- Only annotate lines that actually exist in the diff.\n"
     "- Be specific — reference the code you're commenting on.\n"
     "- Skip unchanged lines.\n"
-    "- Return ONLY valid JSON, no markdown wrapping, no explanation."
+    "- Return ONLY the JSON object, no markdown wrapping, no explanation."
 )
 
-USER_PROMPT_TEMPLATE = (
-    "Review the following code diff for potential bugs, security issues, "
-    "or improvements:\n\n{diff}"
-)
-
-LLM_PAYLOAD_TEMPLATE = {
-    "messages": [
-        {"role": "system", "content": SYSTEM_PROMPT},
-        {"role": "user", "content": USER_PROMPT_TEMPLATE},
-    ],
-    "max_tokens": 2048,
-    "chat_template_kwargs": {"enable_thinking": False},
+ANNOTATIONS_SCHEMA = {
+    "type": "object",
+    "properties": {
+        "annotations": {
+            "type": "array",
+            "items": {
+                "type": "object",
+                "properties": {
+                    "file": {"type": "string"},
+                    "line": {"type": "integer"},
+                    "message": {"type": "string"},
+                },
+                "required": ["file", "line", "message"],
+                "additionalProperties": False,
+            },
+        },
+    },
+    "required": ["annotations"],
+    "additionalProperties": False,
 }
 
 
-def send_to_llm(diff_text: str, endpoint: str, apikey: str) -> dict:
-    """Send diff to LLM and return parsed annotations (list of LineAnnotation).
+def _build_llm_payload(diff_text: str) -> dict:
+    """Build the LLM request payload with JSON schema enforcement."""
+    prompt = (
+        "Review the following code diff for potential bugs, security issues, "
+        "or improvements:\n\n"
+        f"{diff_text}"
+    )
+    return {
+        "messages": [
+            {"role": "system", "content": SYSTEM_PROMPT},
+            {"role": "user", "content": prompt},
+        ],
+        "max_tokens": 2048,
+        "response_format": {
+            "type": "json_schema",
+            "json_schema": {
+                "name": "code_review_annotations",
+                "schema": ANNOTATIONS_SCHEMA,
+            },
+        },
+        "chat_template_kwargs": {"enable_thinking": False},
+    }
+
+
+def send_to_llm(diff_text: str, endpoint: str, apikey: str) -> list[LineAnnotation] | None:
+    """Send diff to LLM and return parsed annotations.
 
-    If the LLM response is not valid JSON, returns None (caller falls back
-    to a plain comment).
+    Uses JSON schema enforcement so the LLM is constrained to return
+    {"annotations": [{"file": ..., "line": ..., "message": ...}, ...]}.
+    Falls back to None on any parse error.
     """
-    prompt = USER_PROMPT_TEMPLATE.format(diff=diff_text)
-    payload = LLM_PAYLOAD_TEMPLATE.copy()
-    payload["messages"] = [
-        {"role": "system", "content": SYSTEM_PROMPT},
-        {"role": "user", "content": prompt},
-    ]
+    payload = _build_llm_payload(diff_text)
 
     resp = requests.post(
         f"{endpoint}/chat/completions",
@@ -138,27 +184,32 @@ def send_to_llm(diff_text: str, endpoint: str, apikey: str) -> dict:
     resp.raise_for_status()
     raw = resp.json()["choices"][0]["message"]["content"].strip()
 
-    # Try to extract JSON from the response (handle stray markdown)
-    json_match = re.search(r"\[.*\]", raw, re.DOTALL)
-    if not json_match:
-        print(f"LLM did not return JSON. Raw response: {raw[:200]}", file=sys.stderr)
-        return None
-
+    # The LLM should return {"annotations": [...]}
     try:
-        items = json.loads(json_match.group())
+        data = json.loads(raw)
     except json.JSONDecodeError as exc:
         print(f"LLM JSON parse error: {exc}", file=sys.stderr)
+        print(f"  Raw response: {raw[:300]}", file=sys.stderr)
+        return None
+
+    if not isinstance(data, dict) or "annotations" not in data:
+        print(f"LLM response missing 'annotations' key. Raw: {raw[:200]}", file=sys.stderr)
         return None
 
     annotations = []
-    for item in items:
-        if "file" in item and "line" in item and "message" in item:
+    for item in data["annotations"]:
+        if not isinstance(item, dict):
+            continue
+        try:
             annotations.append(LineAnnotation(
                 file=str(item["file"]),
                 line=int(item["line"]),
                 message=str(item["message"]),
             ))
-    return annotations
+        except (KeyError, ValueError, TypeError):
+            continue
+
+    return annotations if annotations else None
 
 
 # ---------------------------------------------------------------------------