From 664a99a55f6cf3da00e02c38937bd9aeec149356 Mon Sep 17 00:00:00 2001 From: "mose-x.zm" Date: Fri, 22 May 2026 09:49:19 +0800 Subject: [PATCH 1/2] Add headers to the response. --- dashscope/api_entities/dashscope_response.py | 55 ++++++++++++++++++++ dashscope/api_entities/http_request.py | 11 ++++ dashscope/common/utils.py | 29 +++++++++-- 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/dashscope/api_entities/dashscope_response.py b/dashscope/api_entities/dashscope_response.py index a49cd23..5a1d0a5 100644 --- a/dashscope/api_entities/dashscope_response.py +++ b/dashscope/api_entities/dashscope_response.py @@ -91,6 +91,7 @@ class DashScopeAPIResponse(DictMixin): message: str output: Any usage: Any + headers: dict def __init__( self, @@ -100,6 +101,7 @@ def __init__( message: str = "", output: Any = None, usage: Any = None, + headers: dict = None, **kwargs, ): super().__init__( @@ -109,9 +111,32 @@ def __init__( message=message, output=output, usage=usage, + headers=headers, **kwargs, ) + def __repr__(self): + data = { + "status_code": self.status_code, + "request_id": self.request_id, + "code": self.code, + "message": self.message, + "output": self.output, + "usage": self.usage, + } + return f"{type(self).__name__}({data})" + + def __str__(self): + data = { + "status_code": self.status_code, + "request_id": self.request_id, + "code": self.code, + "message": self.message, + "output": self.output, + "usage": self.usage, + } + return json.dumps(data, ensure_ascii=False) + class Role: USER = "user" @@ -235,6 +260,7 @@ def __init__( class GenerationResponse(DashScopeAPIResponse): output: GenerationOutput usage: GenerationUsage + headers: dict @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -250,6 +276,7 @@ def from_api_response(api_response: DashScopeAPIResponse): message=api_response.message, output=GenerationOutput(**api_response.output), usage=GenerationUsage(**usage), + headers=api_response.headers, ) else: return GenerationResponse( @@ -257,6 +284,7 @@ def from_api_response(api_response: DashScopeAPIResponse): request_id=api_response.request_id, code=api_response.code, message=api_response.message, + headers=api_response.headers, ) @@ -316,6 +344,7 @@ def __init__( class MultiModalConversationResponse(DashScopeAPIResponse): output: MultiModalConversationOutput usage: MultiModalConversationUsage + headers: dict @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -331,6 +360,7 @@ def from_api_response(api_response: DashScopeAPIResponse): message=api_response.message, output=MultiModalConversationOutput(**api_response.output), usage=MultiModalConversationUsage(**usage), + headers=api_response.headers, ) else: return MultiModalConversationResponse( @@ -338,6 +368,7 @@ def from_api_response(api_response: DashScopeAPIResponse): request_id=api_response.request_id, code=api_response.code, message=api_response.message, + headers=api_response.headers, ) @@ -365,6 +396,7 @@ def __init__(self, **kwargs): class TranscriptionResponse(DashScopeAPIResponse): output: TranscriptionOutput usage: TranscriptionUsage + headers: dict @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -383,6 +415,7 @@ def from_api_response(api_response: DashScopeAPIResponse): message=api_response.message, output=output, usage=usage, + headers=api_response.headers, ) else: @@ -391,6 +424,7 @@ def from_api_response(api_response: DashScopeAPIResponse): request_id=api_response.request_id, code=api_response.code, message=api_response.message, + headers=api_response.headers, ) @@ -414,6 +448,7 @@ def __init__(self, duration: int = 0, **kwargs): class RecognitionResponse(DashScopeAPIResponse): output: RecognitionOutput usage: RecognitionUsage + headers: dict @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -433,6 +468,7 @@ def from_api_response(api_response: DashScopeAPIResponse): message=api_response.message, output=output, usage=usage, + headers=api_response.headers, ) else: @@ -441,6 +477,7 @@ def from_api_response(api_response: DashScopeAPIResponse): request_id=api_response.request_id, code=api_response.code, message=api_response.message, + headers=api_response.headers, ) @staticmethod @@ -478,6 +515,7 @@ def __init__(self, characters: int = 0, **kwargs): class SpeechSynthesisResponse(DashScopeAPIResponse): output: SpeechSynthesisOutput usage: SpeechSynthesisUsage + headers: dict @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -496,6 +534,7 @@ def from_api_response(api_response: DashScopeAPIResponse): message=api_response.message, output=output, usage=usage, + headers=api_response.headers, ) else: @@ -504,6 +543,7 @@ def from_api_response(api_response: DashScopeAPIResponse): request_id=api_response.request_id, code=api_response.code, message=api_response.message, + headers=api_response.headers, ) @@ -597,6 +637,7 @@ def __init__( class ImageSynthesisResponse(DashScopeAPIResponse): output: ImageSynthesisOutput usage: ImageSynthesisUsage + headers: dict @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -615,6 +656,7 @@ def from_api_response(api_response: DashScopeAPIResponse): message=api_response.message, output=output, usage=usage, + headers=api_response.headers, ) else: @@ -623,6 +665,7 @@ def from_api_response(api_response: DashScopeAPIResponse): request_id=api_response.request_id, code=api_response.code, message=api_response.message, + headers=api_response.headers, ) @@ -630,6 +673,7 @@ def from_api_response(api_response: DashScopeAPIResponse): class VideoSynthesisResponse(DashScopeAPIResponse): output: VideoSynthesisOutput usage: VideoSynthesisUsage + headers: dict @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -648,6 +692,7 @@ def from_api_response(api_response: DashScopeAPIResponse): message=api_response.message, output=output, usage=usage, + headers=api_response.headers, ) else: @@ -656,6 +701,7 @@ def from_api_response(api_response: DashScopeAPIResponse): request_id=api_response.request_id, code=api_response.code, message=api_response.message, + headers=api_response.headers, ) @@ -705,6 +751,7 @@ def __init__(self, total_tokens=None, **kwargs): class ReRankResponse(DashScopeAPIResponse): output: ReRankOutput usage: GenerationUsage + headers: dict @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -720,6 +767,7 @@ def from_api_response(api_response: DashScopeAPIResponse): message=api_response.message, output=ReRankOutput(**api_response.output), usage=ReRankUsage(**usage), + headers=api_response.headers, ) else: return ReRankResponse( @@ -727,6 +775,7 @@ def from_api_response(api_response: DashScopeAPIResponse): request_id=api_response.request_id, code=api_response.code, message=api_response.message, + headers=api_response.headers, ) @@ -777,6 +826,7 @@ def __init__( class TextToSpeechResponse(DashScopeAPIResponse): output: TextToSpeechOutput usage: MultiModalConversationUsage + headers: dict @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -792,6 +842,7 @@ def from_api_response(api_response: DashScopeAPIResponse): message=api_response.message, output=TextToSpeechOutput(**api_response.output), usage=MultiModalConversationUsage(**usage), + headers=api_response.headers, ) else: return TextToSpeechResponse( @@ -799,6 +850,7 @@ def from_api_response(api_response: DashScopeAPIResponse): request_id=api_response.request_id, code=api_response.code, message=api_response.message, + headers=api_response.headers, ) @@ -858,6 +910,7 @@ def __init__( class ImageGenerationResponse(DashScopeAPIResponse): output: ImageGenerationOutput usage: ImageGenerationUsage + headers: dict @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -873,6 +926,7 @@ def from_api_response(api_response: DashScopeAPIResponse): message=api_response.message, output=ImageGenerationOutput(**api_response.output), usage=ImageGenerationUsage(**usage), + headers=api_response.headers, ) else: return ImageGenerationResponse( @@ -880,4 +934,5 @@ def from_api_response(api_response: DashScopeAPIResponse): request_id=api_response.request_id, code=api_response.code, message=api_response.message, + headers=api_response.headers, ) diff --git a/dashscope/api_entities/http_request.py b/dashscope/api_entities/http_request.py index 052ca83..85ee959 100644 --- a/dashscope/api_entities/http_request.py +++ b/dashscope/api_entities/http_request.py @@ -266,6 +266,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s response: aiohttp.ClientResponse, ): request_id = "" + headers = dict(response.headers) if ( response.status == HTTPStatus.OK and self.stream @@ -291,6 +292,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s status_code=HTTPStatus.INTERNAL_SERVER_ERROR, code="Unknown", message=data, + headers=headers, ) continue if is_error: @@ -299,6 +301,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s status_code=status_code, code=msg["code"], message=msg["message"], + headers=headers, ) else: if self.encryption and self.encryption.is_valid(): @@ -308,6 +311,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s status_code=HTTPStatus.OK, output=output, usage=usage, + headers=headers, ) elif ( response.status == HTTPStatus.OK @@ -329,6 +333,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s request_id=request_id, status_code=HTTPStatus.OK, output=output, + headers=headers, ) elif response.status == HTTPStatus.OK: json_content = await response.json() @@ -356,6 +361,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s status_code=HTTPStatus.OK, output=output, usage=usage, + headers=headers, ) else: yield await _handle_aiohttp_failed_response(response) @@ -365,6 +371,7 @@ def _handle_response( # pylint: disable=too-many-branches response: requests.Response, ): request_id = "" + headers = dict(response.headers) if ( response.status_code == HTTPStatus.OK and self.stream @@ -395,6 +402,7 @@ def _handle_response( # pylint: disable=too-many-branches output=None, code="Unknown", message=data, + headers=headers, ) continue if is_error: @@ -406,6 +414,7 @@ def _handle_response( # pylint: disable=too-many-branches if "code" in msg else None, # noqa E501 message=msg["message"] if "message" in msg else None, + headers=headers, ) # noqa E501 else: if self.flattened_output: @@ -418,6 +427,7 @@ def _handle_response( # pylint: disable=too-many-branches status_code=HTTPStatus.OK, output=output, usage=usage, + headers=headers, ) elif response.status_code == HTTPStatus.OK: json_content = response.json() @@ -442,6 +452,7 @@ def _handle_response( # pylint: disable=too-many-branches status_code=HTTPStatus.OK, output=output, usage=usage, + headers=headers, ) else: yield _handle_http_failed_response(response) diff --git a/dashscope/common/utils.py b/dashscope/common/utils.py index 5893b8b..d9be4d3 100644 --- a/dashscope/common/utils.py +++ b/dashscope/common/utils.py @@ -261,7 +261,7 @@ def _handle_stream(response: requests.Response): continue # ignore heartbeat... -def _handle_error_message(error, status_code, flattened_output): +def _handle_error_message(error, status_code, flattened_output, headers): code = None msg = "" request_id = "" @@ -281,6 +281,7 @@ def _handle_error_message(error, status_code, flattened_output): status_code=status_code, code=code, message=msg, + headers=headers, ) @@ -289,12 +290,14 @@ def _handle_http_failed_response( flattened_output: bool = False, ) -> DashScopeAPIResponse: request_id = "" + headers = dict(response.headers) if "application/json" in response.headers.get("content-type", ""): error = response.json() return _handle_error_message( error, response.status_code, flattened_output, + headers, ) elif SSE_CONTENT_TYPE in response.headers.get("content-type", ""): msgs = response.content.decode("utf-8").split("\n") @@ -305,12 +308,14 @@ def _handle_http_failed_response( error, response.status_code, flattened_output, + headers, ) return DashScopeAPIResponse( request_id=request_id, status_code=response.status_code, code="Unknown", message=msgs, + headers=headers, ) else: msg = response.content.decode("utf-8") @@ -321,6 +326,7 @@ def _handle_http_failed_response( status_code=response.status_code, code="Unknown", message=msg, + headers=headers, ) @@ -351,13 +357,24 @@ async def _handle_aiohttp_failed_response( flattened_output: bool = False, ) -> DashScopeAPIResponse: request_id = "" + headers = dict(response.headers) if "application/json" in response.content_type: error = await response.json() - return _handle_error_message(error, response.status, flattened_output) + return _handle_error_message( + error, + response.status, + flattened_output, + headers, + ) elif SSE_CONTENT_TYPE in response.content_type: async for _, _, data in _handle_aio_stream(response): error = json.loads(data) - return _handle_error_message(error, response.status, flattened_output) + return _handle_error_message( + error, + response.status, + flattened_output, + headers, + ) else: msg = response.content.decode("utf-8") if flattened_output: @@ -367,6 +384,7 @@ async def _handle_aiohttp_failed_response( status_code=response.status, code="Unknown", message=msg, + headers=headers, ) @@ -389,6 +407,7 @@ def _handle_http_stream_response( flattened_output: bool = False, ): request_id = "" + headers = dict(response.headers) if ( response.status_code == HTTPStatus.OK and SSE_CONTENT_TYPE in response.headers.get("content-type", "") @@ -416,6 +435,7 @@ def _handle_http_stream_response( status_code=HTTPStatus.OK, output=output, usage=usage, + headers=headers, ) except json.JSONDecodeError as e: if flattened_output: @@ -430,6 +450,7 @@ def _handle_http_stream_response( output=None, code="Unknown", message=event.data, + headers=headers, ) continue else: @@ -448,6 +469,7 @@ def _handle_http_stream_response( if "code" in msg else None, # noqa E501 message=msg["message"] if "message" in msg else None, + headers=headers, ) # noqa E501 # pylint: disable=consider-using-in elif ( @@ -487,6 +509,7 @@ def _handle_http_stream_response( output=output, usage=usage, message=msg, + headers=headers, ) else: yield None, _handle_http_failed_response(response, flattened_output) From f389b9040721768e124e8d19cfe96a261af95c48 Mon Sep 17 00:00:00 2001 From: "mose-x.zm" Date: Fri, 22 May 2026 11:21:52 +0800 Subject: [PATCH 2/2] change dict to Dict --- dashscope/api_entities/dashscope_response.py | 24 ++++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/dashscope/api_entities/dashscope_response.py b/dashscope/api_entities/dashscope_response.py index 5a1d0a5..3c2d9a4 100644 --- a/dashscope/api_entities/dashscope_response.py +++ b/dashscope/api_entities/dashscope_response.py @@ -91,7 +91,7 @@ class DashScopeAPIResponse(DictMixin): message: str output: Any usage: Any - headers: dict + headers: Dict[Any, Any] def __init__( self, @@ -101,7 +101,7 @@ def __init__( message: str = "", output: Any = None, usage: Any = None, - headers: dict = None, + headers: Dict[Any, Any] = None, **kwargs, ): super().__init__( @@ -260,7 +260,7 @@ def __init__( class GenerationResponse(DashScopeAPIResponse): output: GenerationOutput usage: GenerationUsage - headers: dict + headers: Dict[Any, Any] @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -344,7 +344,7 @@ def __init__( class MultiModalConversationResponse(DashScopeAPIResponse): output: MultiModalConversationOutput usage: MultiModalConversationUsage - headers: dict + headers: Dict[Any, Any] @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -396,7 +396,7 @@ def __init__(self, **kwargs): class TranscriptionResponse(DashScopeAPIResponse): output: TranscriptionOutput usage: TranscriptionUsage - headers: dict + headers: Dict[Any, Any] @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -448,7 +448,7 @@ def __init__(self, duration: int = 0, **kwargs): class RecognitionResponse(DashScopeAPIResponse): output: RecognitionOutput usage: RecognitionUsage - headers: dict + headers: Dict[Any, Any] @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -515,7 +515,7 @@ def __init__(self, characters: int = 0, **kwargs): class SpeechSynthesisResponse(DashScopeAPIResponse): output: SpeechSynthesisOutput usage: SpeechSynthesisUsage - headers: dict + headers: Dict[Any, Any] @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -637,7 +637,7 @@ def __init__( class ImageSynthesisResponse(DashScopeAPIResponse): output: ImageSynthesisOutput usage: ImageSynthesisUsage - headers: dict + headers: Dict[Any, Any] @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -673,7 +673,7 @@ def from_api_response(api_response: DashScopeAPIResponse): class VideoSynthesisResponse(DashScopeAPIResponse): output: VideoSynthesisOutput usage: VideoSynthesisUsage - headers: dict + headers: Dict[Any, Any] @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -751,7 +751,7 @@ def __init__(self, total_tokens=None, **kwargs): class ReRankResponse(DashScopeAPIResponse): output: ReRankOutput usage: GenerationUsage - headers: dict + headers: Dict[Any, Any] @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -826,7 +826,7 @@ def __init__( class TextToSpeechResponse(DashScopeAPIResponse): output: TextToSpeechOutput usage: MultiModalConversationUsage - headers: dict + headers: Dict[Any, Any] @staticmethod def from_api_response(api_response: DashScopeAPIResponse): @@ -910,7 +910,7 @@ def __init__( class ImageGenerationResponse(DashScopeAPIResponse): output: ImageGenerationOutput usage: ImageGenerationUsage - headers: dict + headers: Dict[Any, Any] @staticmethod def from_api_response(api_response: DashScopeAPIResponse):