From 531f4265577fca20867e86445eee9902b4d9228c Mon Sep 17 00:00:00 2001 From: Giuseppe Date: Thu, 4 Jun 2026 19:38:45 +0200 Subject: [PATCH] fix: KeyError on missing 'content' key in system messages (#2362) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A system message that arrives without a 'content' key — possible via malformed tool results — raised a KeyError in the hot path of llm_call, llm_call_async, and stream_llm. Replace m["content"] with m.get("content") or "" in all three functions so a missing key degrades to an empty string instead of crashing. Also removes a redundant .rstrip() after .strip() in _model_activity_key. Co-authored-by: Claude Sonnet 4.6 --- src/llm_core.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/llm_core.py b/src/llm_core.py index a155530..1995982 100644 --- a/src/llm_core.py +++ b/src/llm_core.py @@ -67,7 +67,7 @@ _host_health_lock = threading.Lock() _model_activity: Dict[str, float] = {} def _model_activity_key(url: str, model: str) -> str: - return f"{(url or '').strip().rstrip()}|{(model or '').strip()}" + return f"{(url or '').strip()}|{(model or '').strip()}" def note_model_activity(url: str, model: str): """Record that a real upstream request used this endpoint/model.""" @@ -884,7 +884,7 @@ def llm_call(url: str, model: str, messages: List[Dict], temperature: float = LL non_sys = [] for m in messages_copy: if m.get("role") == "system": - sys_parts.append(m["content"]) + sys_parts.append(m.get('content') or '') else: non_sys.append(m) if sys_parts: @@ -1028,7 +1028,7 @@ async def llm_call_async( non_sys = [] for m in messages_copy: if m.get("role") == "system": - sys_parts.append(m["content"]) + sys_parts.append(m.get('content') or '') else: non_sys.append(m) if sys_parts: @@ -1143,7 +1143,7 @@ async def stream_llm(url: str, model: str, messages: List[Dict], temperature: fl non_sys = [] for m in messages_copy: if m.get("role") == "system": - sys_parts.append(m["content"]) + sys_parts.append(m.get('content') or '') else: non_sys.append(m) if sys_parts: