Skip to content

SDK raises raw AssertionError for API error bodies not shaped as {"error": {...}} #460

Description

@Harsh23Kashyap

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions