@@ -1,4 +1,5 @@ import json +import re from enum import StrEnum import logging import time @@ -13,6 +14,7 @@ from ml_model.exceptions import ( NSFWDetectedException, RealPersonDetectedError, RequestBlocked, + PromptLengthExceeded, ) from poller.models import Proxy @@ -111,6 +113,26 @@ class BytedanceModelArkAdapter: return 'video_url' return 'image_url' + @classmethod + def _handle_error_response(cls, resp: httpx.Response) -> None: + if resp.status_code == 413: + raise PromptLengthExceeded(max_length=202752) + + if 'Input length' in resp.text and 'exceeds the maximum length' in resp.text: + match = re.search(r"Input length (\d+) exceeds the maximum length (\d+)", resp.text) + if match: + max_length = int(match.group(2)) + raise PromptLengthExceeded(max_length=max_length) + raise PromptLengthExceeded(max_length=202752) + + logger.error( + 'Bytedance request failed status=%s route=%s body=%s', + resp.status_code, + cls.CONTENT_TYPE_TO_ENDPOINT.get(BytedanceContentType.CHAT), + resp.text, + ) + raise GenerationException + @classmethod def _raise_by_error_payload(cls, data: dict[str, Any], choices: list[dict[str, Any]]) -> None: choice_reasons = {str(choice.get('finish_reason', '')).lower() for choice in choices} @@ -207,12 +229,7 @@ class BytedanceModelArkAdapter: ) as client: resp = client.post(cls.CONTENT_TYPE_TO_ENDPOINT[BytedanceContentType.CHAT], json=payload) if resp.status_code >= 400: - logger.error( - 'Bytedance request failed status=%s route=%s body=%s', - resp.status_code, - cls.CONTENT_TYPE_TO_ENDPOINT[BytedanceContentType.CHAT], - resp.text, - ) + cls._handle_error_response(resp) try: data: BytedanceChatResponse = resp.json() except Exception as exc: @@ -278,13 +295,7 @@ class BytedanceModelArkAdapter: 'POST', cls.CONTENT_TYPE_TO_ENDPOINT[BytedanceContentType.CHAT], json=payload ) as resp: if resp.status_code >= 400: - logger.error( - 'Bytedance request failed status=%s route=%s body=%s', - resp.status_code, - cls.CONTENT_TYPE_TO_ENDPOINT[BytedanceContentType.CHAT], - resp.text, - ) - raise GenerationException + cls._handle_error_response(resp) usage: BytedanceUsage = {} reasoning_started = False content_started = False