Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/together/abstract/api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,9 @@ async def binary_stream_generator() -> (
except (aiohttp.ServerTimeoutError, asyncio.TimeoutError) as e:
raise error.Timeout("Request timed out") from e
except aiohttp.ClientError as e:
utils.log_warn(e, body=result.content)
raise error.APIConnectionError(
"Error communicating with Together"
) from e

if content_type in ["application/octet-stream", "audio/wav", "audio/mpeg"]:
# Binary content - keep as bytes
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/test_async_response_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from unittest.mock import patch

import aiohttp
import pytest

from together.abstract.api_requestor import APIRequestor
from together.error import APIConnectionError, Timeout
from together.types import TogetherClient


class _FailingReadResponse:
"""Minimal stand-in for aiohttp.ClientResponse whose read() fails."""

status = 200
headers = {"Content-Type": "application/json"}
content = None

def __init__(self, exc):
self._exc = exc

async def read(self):
raise self._exc

def release(self):
pass


class TestAsyncResponseReadErrors:
@pytest.fixture
def requestor(self):
with patch.dict("os.environ", {"TOGETHER_API_KEY": "fake_api_key"}):
return APIRequestor(client=TogetherClient(api_key="fake_api_key"))

@pytest.mark.asyncio
async def test_client_error_reading_body_raises_connection_error(self, requestor):
"""
A connection failure while reading a non-streaming response body must
surface as APIConnectionError, not UnboundLocalError.
"""
resp = _FailingReadResponse(aiohttp.ClientError("connection reset"))

with pytest.raises(APIConnectionError):
await requestor._interpret_async_response(resp, stream=False)

@pytest.mark.asyncio
async def test_timeout_reading_body_raises_timeout(self, requestor):
resp = _FailingReadResponse(aiohttp.ServerTimeoutError("read timeout"))

with pytest.raises(Timeout):
await requestor._interpret_async_response(resp, stream=False)