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:
2026-05-15 15:14:27 +02:00
parent 783c5c2b87
commit e4e47c4649

View File

@@ -7,6 +7,7 @@ from pathlib import Path
import tempfile import tempfile
from typing import TYPE_CHECKING, Any, Awaitable, Callable from typing import TYPE_CHECKING, Any, Awaitable, Callable
import httpx
from openai import AsyncOpenAI from openai import AsyncOpenAI
from app.ai.image_understand import ( from app.ai.image_understand import (
@@ -28,6 +29,15 @@ log = logging.getLogger(__name__)
ProgressCallback = Callable[[str], Awaitable[None] | None] 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) @dataclass(slots=True)
class AIResponse: class AIResponse:
text: str text: str
@@ -103,6 +113,9 @@ class AIService:
api_key=provider.api_key, api_key=provider.api_key,
base_url=provider.base_url, base_url=provider.base_url,
timeout=provider.timeout, timeout=provider.timeout,
http_client=httpx.AsyncClient(
event_hooks={"response": [_sanitize_response_encoding]},
),
) )
self._clients[provider.name] = client self._clients[provider.name] = client
return client return client