When the API returns an error body that is not shaped as {"error": {...}} - for example a FastAPI-style {"detail": "Not Found"} body, a plain string error, or a non-dict JSON body - the SDK raises a raw AssertionError instead of together.error.JSONError.
APIRequestor.handle_error_response validates the payload with asserts, but the surrounding except only catches KeyError/TypeError:
try:
assert isinstance(resp.data, dict)
error_resp = resp.data.get("error")
assert isinstance(error_resp, dict), f"Unexpected error response {error_resp}"
error_data = TogetherErrorResponse(**(error_resp))
except (KeyError, TypeError):
raise error.JSONError(...)
The asserts bypass the handler, so the JSONError fallback is dead code. (Asserts are also stripped under python -O, in which case this path raises AttributeError instead.)
Repro
from together.abstract.api_requestor import APIRequestor
from together.together_response import TogetherResponse
APIRequestor.handle_error_response(TogetherResponse({"detail": "Not Found"}, {}), 404)
# AssertionError: Unexpected error response None
APIRequestor.handle_error_response(TogetherResponse({"error": "bad request"}, {}), 400)
# AssertionError: Unexpected error response bad request
APIRequestor.handle_error_response(TogetherResponse(["some", "list"], {}), 500)
# AssertionError
Output is identical across repeated runs. Expected: together.error.JSONError in all three cases, as the except clause intends.
Note: the 2.0 SDK (together-py) handles these bodies without leaking AssertionError; this only affects the V1 SDK.
Happy to send a PR with the fix and regression tests.
When the API returns an error body that is not shaped as
{"error": {...}}- for example a FastAPI-style{"detail": "Not Found"}body, a plain string error, or a non-dict JSON body - the SDK raises a rawAssertionErrorinstead oftogether.error.JSONError.APIRequestor.handle_error_responsevalidates the payload with asserts, but the surroundingexceptonly catchesKeyError/TypeError:The asserts bypass the handler, so the
JSONErrorfallback is dead code. (Asserts are also stripped underpython -O, in which case this path raisesAttributeErrorinstead.)Repro
Output is identical across repeated runs. Expected:
together.error.JSONErrorin all three cases, as theexceptclause intends.Note: the 2.0 SDK (
together-py) handles these bodies without leakingAssertionError; this only affects the V1 SDK.Happy to send a PR with the fix and regression tests.