Skip to content

_handle_error_response raises UnicodeDecodeError on binary error bodies #170

Description

@Kamilbenkirane

Bug

When a provider returns a non-200 HTTP response with a binary (non-UTF-8) body, _handle_error_response raises UnicodeDecodeError instead of the expected httpx.HTTPStatusError.

Root cause

In client.py, _handle_error_response:

except (JSONDecodeError, KeyError, TypeError, IndexError):
    error_msg = response.text or f"HTTP {response.status_code}"

response.text is httpx's UTF-8-decoded property. When the error body contains binary bytes (e.g. 0xff), it raises UnicodeDecodeError — which is not in the caught exception list — so httpx.HTTPStatusError is never raised.

Observed

ElevenLabs occasionally returns a binary error body, causing:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 45

Fix

except (JSONDecodeError, KeyError, TypeError, IndexError):
    error_msg = response.content.decode("utf-8", errors="replace") or f"HTTP {response.status_code}"

response.content is the raw bytes. errors="replace" substitutes U+FFFD for undecodable bytes instead of raising.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions