mirror of
https://github.com/Aider-AI/aider.git
synced 2025-06-01 18:25:00 +00:00
Improve fix for UnicodeDecodeError in JSON loading
This commit is contained in:
parent
bbad86b733
commit
2582a845dc
2 changed files with 32 additions and 27 deletions
|
@ -58,36 +58,18 @@ class LiteLLMExceptions:
|
||||||
self._load()
|
self._load()
|
||||||
|
|
||||||
def _load(self, strict=False):
|
def _load(self, strict=False):
|
||||||
# Patch litellm's JSON loading to use UTF-8 encoding
|
# Import litellm - json.load is already patched in aider.llm
|
||||||
import json
|
|
||||||
import litellm
|
import litellm
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
# Monkey patch json.load in litellm.utils to handle UTF-8 encoding
|
# Load litellm exceptions
|
||||||
original_json_load = json.load
|
for var in dir(litellm):
|
||||||
|
if var.endswith("Error"):
|
||||||
|
if var not in self.exception_info:
|
||||||
|
raise ValueError(f"{var} is in litellm but not in aider's exceptions list")
|
||||||
|
|
||||||
def patched_json_load(fp, *args, **kwargs):
|
for var in self.exception_info:
|
||||||
# Read the file content with UTF-8 encoding
|
ex = getattr(litellm, var)
|
||||||
content = Path(fp.name).read_text(encoding='utf-8')
|
self.exceptions[ex] = self.exception_info[var]
|
||||||
# Parse the content as JSON
|
|
||||||
return json.loads(content, *args, **kwargs)
|
|
||||||
|
|
||||||
# Apply the monkey patch
|
|
||||||
json.load = patched_json_load
|
|
||||||
|
|
||||||
try:
|
|
||||||
# Now load litellm exceptions
|
|
||||||
for var in dir(litellm):
|
|
||||||
if var.endswith("Error"):
|
|
||||||
if var not in self.exception_info:
|
|
||||||
raise ValueError(f"{var} is in litellm but not in aider's exceptions list")
|
|
||||||
|
|
||||||
for var in self.exception_info:
|
|
||||||
ex = getattr(litellm, var)
|
|
||||||
self.exceptions[ex] = self.exception_info[var]
|
|
||||||
finally:
|
|
||||||
# Restore the original json.load function
|
|
||||||
json.load = original_json_load
|
|
||||||
|
|
||||||
def exceptions_tuple(self):
|
def exceptions_tuple(self):
|
||||||
return tuple(self.exceptions)
|
return tuple(self.exceptions)
|
||||||
|
|
23
aider/llm.py
23
aider/llm.py
|
@ -1,6 +1,8 @@
|
||||||
import importlib
|
import importlib
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import warnings
|
import warnings
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from aider.dump import dump # noqa: F401
|
from aider.dump import dump # noqa: F401
|
||||||
|
|
||||||
|
@ -17,6 +19,27 @@ os.environ["LITELLM_MODE"] = "PRODUCTION"
|
||||||
|
|
||||||
VERBOSE = False
|
VERBOSE = False
|
||||||
|
|
||||||
|
# Patch json.load to handle UTF-8 encoding for litellm
|
||||||
|
original_json_load = json.load
|
||||||
|
|
||||||
|
def patched_json_load(fp, *args, **kwargs):
|
||||||
|
try:
|
||||||
|
# First try the original method
|
||||||
|
return original_json_load(fp, *args, **kwargs)
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
# If it fails with UnicodeDecodeError, try with UTF-8 encoding
|
||||||
|
try:
|
||||||
|
# Read the file content with UTF-8 encoding
|
||||||
|
content = Path(fp.name).read_text(encoding='utf-8')
|
||||||
|
# Parse the content as JSON
|
||||||
|
return json.loads(content, *args, **kwargs)
|
||||||
|
except Exception:
|
||||||
|
# If that also fails, re-raise the original exception
|
||||||
|
raise
|
||||||
|
|
||||||
|
# Apply the monkey patch
|
||||||
|
json.load = patched_json_load
|
||||||
|
|
||||||
|
|
||||||
class LazyLiteLLM:
|
class LazyLiteLLM:
|
||||||
_lazy_module = None
|
_lazy_module = None
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue