fix: KeyError on missing 'content' key in system messages (#2362)

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 <noreply@anthropic.com>
This commit is contained in:
Giuseppe
2026-06-04 19:38:45 +02:00
committed by GitHub
parent ff8f9f2188
commit 531f426557

View File

@@ -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: