Fix UTF-8 decode error for providers returning invalid encoding (Ollama)
Some providers (e.g. Ollama) may return response bytes that aren't valid UTF-8. json.loads() raises UnicodeDecodeError in that case. Add an httpx response event hook that replaces invalid bytes before the SDK parses the response body. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,7 @@ from pathlib import Path
|
||||
import tempfile
|
||||
from typing import TYPE_CHECKING, Any, Awaitable, Callable
|
||||
|
||||
import httpx
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from app.ai.image_understand import (
|
||||
@@ -28,6 +29,15 @@ log = logging.getLogger(__name__)
|
||||
ProgressCallback = Callable[[str], Awaitable[None] | None]
|
||||
|
||||
|
||||
async def _sanitize_response_encoding(response: httpx.Response) -> None:
|
||||
await response.aread()
|
||||
raw = response.content
|
||||
try:
|
||||
raw.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
response._content = raw.decode("utf-8", errors="replace").encode("utf-8")
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class AIResponse:
|
||||
text: str
|
||||
@@ -103,6 +113,9 @@ class AIService:
|
||||
api_key=provider.api_key,
|
||||
base_url=provider.base_url,
|
||||
timeout=provider.timeout,
|
||||
http_client=httpx.AsyncClient(
|
||||
event_hooks={"response": [_sanitize_response_encoding]},
|
||||
),
|
||||
)
|
||||
self._clients[provider.name] = client
|
||||
return client
|
||||
|
||||
Reference in New Issue
Block a user