Skip to content

Conversation

@zxzinn
Copy link

@zxzinn zxzinn commented Dec 24, 2025

Problem

When the Cohere API returns an HTTP error (e.g., 500 Internal Server Error) with an empty response body, the SDK crashes with a JSONDecodeError instead of properly handling the error. This occurs because the error handling code attempts to parse the empty response body as JSON without checking if it's parseable first.

Error Flow

# Current problematic code structure in raw_client.py
try:
    if _response.status_code == 500:
        raise InternalServerError(
            body=construct_type(
                object_=_response.json(),  # ⚠️ Throws JSONDecodeError if body is empty
            )
        )
    ...
    _response_json = _response.json()
except JSONDecodeError:
    # This catch block is too late - it can't catch the error from line above
    raise ApiError(...)

Production Error Example

From Sentry issue:

ApiError: status_code: 500, body:
  at cohere/v2/client.py:971

During handling of the above exception, another exception occurred:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  at cohere/v2/client.py:935 in _response.json()

Solution

Introduced a _safe_json_parse() helper function that gracefully handles JSON parsing failures:

def _safe_json_parse(response: typing.Any) -> typing.Any:
    """
    Safely parse JSON from HTTP response.
    Returns parsed JSON or response text if parsing fails.
    """
    try:
        return response.json()
    except JSONDecodeError:
        return response.text

Add _safe_json_parse helper function to gracefully handle
malformed JSON responses by returning response text instead
of raising JSONDecodeError. This prevents crashes when servers
return empty or invalid JSON bodies, particularly in error
scenarios like HTTP 500 responses.

Replace all direct _response.json() calls with _safe_json_parse
throughout raw_client.py to ensure consistent error handling
across all API endpoints.

Add comprehensive unit tests covering valid JSON, empty
responses, malformed JSON, and production error cases.

Signed-off-by: zxzinn <92992703+zxzinn@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant