Skip to content

Commit 0a17bfa

Browse files
committed
fix(llm): catch JSONDecodeError in LLM retry loop
Handle malformed JSON responses from LLM providers (e.g., OpenRouter returning truncated JSON) by treating json.JSONDecodeError as a retryable error. This allows 3 retry attempts with exponential backoff instead of immediately crashing the run. Previously, JSONDecodeError from the OpenAI SDK's response parsing would escape all error handlers and kill the game run. Now it's treated like APIConnectionError — logged and retried automatically.
1 parent c5692b2 commit 0a17bfa

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

src/balatrollm/llm.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Async OpenAI client wrapper with retry logic."""
22

33
import asyncio
4+
import json
45
import logging
56
from dataclasses import dataclass, field
67
from typing import Any
@@ -113,6 +114,10 @@ async def call(
113114
logger.error(f"LLM content filter error: {e}")
114115
last_exception = e
115116

117+
except json.JSONDecodeError as e:
118+
logger.error(f"LLM response parse error (malformed JSON): {e}")
119+
last_exception = e
120+
116121
if attempt < self.max_retries - 1:
117122
logger.warning(
118123
f"Retrying in {retry_delay}s [{attempt + 1}/{self.max_retries}]"

0 commit comments

Comments
 (0)