Protocol Adapters¶
dashscope.protocols translates chat requests and responses between wire
protocols, so the same client.chat.completions call can speak the DashScope
native protocol, the OpenAI-compatible protocol or the Anthropic-compatible
protocol. Select one with Client(protocol="openai") (or the protocol
setting / DASHSCOPE_PROTOCOL environment variable); valid values are
native, openai and anthropic.
Precedence
An explicit Client(protocol=...) argument wins over the protocol
setting, which wins over DASHSCOPE_PROTOCOL.
Base contract¶
ChatProtocol
¶
ChatProtocol(*, strict: bool = False)
Bases: ABC
Translates canonical chat calls to one wire protocol.
Source code in dashscope/protocols/base.py
59 60 | |
strict
property
¶
strict: bool
Reject (True) or pass through (False) unknown parameters.
Inert on the native adapter: every known parameter maps into
its parameters bag, so nothing is ever "unknown".
build_chat_request
abstractmethod
¶
build_chat_request(
*,
model: str,
messages: List[Dict[str, Any]],
stream: bool = False,
tools: Optional[List[Dict[str, Any]]] = None,
temperature: Optional[float] = None,
top_p: Optional[float] = None,
max_tokens: Optional[int] = None,
stop: Union[str, List[str], None] = None,
seed: Optional[int] = None,
repetition_penalty: Optional[float] = None,
presence_penalty: Optional[float] = None,
enable_thinking: Optional[bool] = None,
response_format: Optional[Dict[str, Any]] = None,
top_k: Optional[int] = None,
tool_choice: Union[str, Dict[str, Any], None] = None,
n: Optional[int] = None,
logprobs: Optional[bool] = None,
top_logprobs: Optional[int] = None,
thinking_budget: Optional[int] = None,
enable_search: Optional[bool] = None,
search_options: Optional[Dict[str, Any]] = None,
parallel_tool_calls: Optional[bool] = None,
extra_parameters: Optional[Dict[str, Any]] = None,
path: Optional[str] = None
) -> ChatRequest
Build the wire request (path, headers, body) for one call.
path overrides the adapter's default endpoint; callers pass
MULTIMODAL_PATH when the messages carry media parts.
Source code in dashscope/protocols/base.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
parse_completion
abstractmethod
¶
parse_completion(
payload: Dict[str, Any],
*,
strict_validation: bool = False,
response: Optional[Response] = None
) -> ChatCompletion
Translate a non-streaming response body.
strict_validation raises on a body that does not fit the
typed model instead of degrading it; it is unrelated to
:attr:strict, which governs request construction.
response is only carried onto that raised error, so the
caller can inspect the status and headers of the body that
failed; websocket payloads have none and leave it None.
Source code in dashscope/protocols/base.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
parse_chunk
abstractmethod
¶
parse_chunk(
payload: Dict[str, Any],
*,
strict_validation: bool = False,
response: Optional[Response] = None
) -> Optional[ChatCompletionChunk]
Translate one streaming event body; None skips the event
(keepalives and control frames carry no user-facing content).
Source code in dashscope/protocols/base.py
124 125 126 127 128 129 130 131 132 133 | |
make_chunk_parser
¶
make_chunk_parser(
*,
strict_validation: bool = False,
response: Optional[Response] = None
) -> Callable[
[Dict[str, Any]], Optional[ChatCompletionChunk]
]
Return the chunk parser for one new stream.
Stateless adapters reuse parse_chunk; stateful adapters
(e.g. anthropic tool-use accumulation) return a fresh parser
per stream so concurrent streams never share state.
The returned callable takes only the payload, so the validation mode and the response a strict failure should carry are bound here rather than passed per event.
Source code in dashscope/protocols/base.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
ChatRequest
dataclass
¶
ChatRequest(
path: str,
body: Dict[str, Any],
headers: Dict[str, str] = dict(),
)
Adapters¶
NativeProtocol
¶
NativeProtocol(*, strict: bool = False)
Bases: ChatProtocol
DashScope native protocol: /services/... + input/parameters body.
Source code in dashscope/protocols/base.py
59 60 | |
MULTIMODAL_PATH
class-attribute
instance-attribute
¶
MULTIMODAL_PATH = (
"/services/aigc/multimodal-generation/generation"
)
build_chat_request
¶
build_chat_request(
*,
model: str,
messages: List[Dict[str, Any]],
stream: bool = False,
tools: Optional[List[Dict[str, Any]]] = None,
temperature: Optional[float] = None,
top_p: Optional[float] = None,
max_tokens: Optional[int] = None,
stop: Union[str, List[str], None] = None,
seed: Optional[int] = None,
repetition_penalty: Optional[float] = None,
presence_penalty: Optional[float] = None,
enable_thinking: Optional[bool] = None,
response_format: Optional[Dict[str, Any]] = None,
top_k: Optional[int] = None,
tool_choice: Union[str, Dict[str, Any], None] = None,
n: Optional[int] = None,
logprobs: Optional[bool] = None,
top_logprobs: Optional[int] = None,
thinking_budget: Optional[int] = None,
enable_search: Optional[bool] = None,
search_options: Optional[Dict[str, Any]] = None,
parallel_tool_calls: Optional[bool] = None,
extra_parameters: Optional[Dict[str, Any]] = None,
path: Optional[str] = None
) -> ChatRequest
Source code in dashscope/protocols/native.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
parse_completion
¶
parse_completion(
payload: Dict[str, Any],
*,
strict_validation: bool = False,
response: Optional[Response] = None
) -> ChatCompletion
Source code in dashscope/protocols/native.py
169 170 171 172 173 174 175 176 177 178 179 180 | |
parse_chunk
¶
parse_chunk(
payload: Dict[str, Any],
*,
strict_validation: bool = False,
response: Optional[Response] = None
) -> ChatCompletionChunk
Source code in dashscope/protocols/native.py
182 183 184 185 186 187 188 189 190 191 192 193 | |
OpenAIProtocol
¶
OpenAIProtocol(*, strict: bool = False)
Bases: ChatProtocol
Adapter for POST /chat/completions (OpenAI-compatible).
Source code in dashscope/protocols/base.py
59 60 | |
build_chat_request
¶
build_chat_request(
*,
model: str,
messages: List[Dict[str, Any]],
stream: bool = False,
tools: Optional[List[Dict[str, Any]]] = None,
temperature: Optional[float] = None,
top_p: Optional[float] = None,
max_tokens: Optional[int] = None,
stop: Union[str, List[str], None] = None,
seed: Optional[int] = None,
repetition_penalty: Optional[float] = None,
presence_penalty: Optional[float] = None,
enable_thinking: Optional[bool] = None,
response_format: Optional[Dict[str, Any]] = None,
top_k: Optional[int] = None,
tool_choice: Union[str, Dict[str, Any], None] = None,
n: Optional[int] = None,
logprobs: Optional[bool] = None,
top_logprobs: Optional[int] = None,
thinking_budget: Optional[int] = None,
enable_search: Optional[bool] = None,
search_options: Optional[Dict[str, Any]] = None,
parallel_tool_calls: Optional[bool] = None,
extra_parameters: Optional[Dict[str, Any]] = None,
path: Optional[str] = None
) -> ChatRequest
Source code in dashscope/protocols/openai.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
parse_completion
¶
parse_completion(
payload: Dict[str, Any],
*,
strict_validation: bool = False,
response: Optional[Response] = None
) -> ChatCompletion
Source code in dashscope/protocols/openai.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
parse_chunk
¶
parse_chunk(
payload: Dict[str, Any],
*,
strict_validation: bool = False,
response: Optional[Response] = None
) -> ChatCompletionChunk
Source code in dashscope/protocols/openai.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
AnthropicProtocol
¶
AnthropicProtocol(*, strict: bool = False)
Bases: ChatProtocol
Adapter for POST /v1/messages (Anthropic Messages API).
Source code in dashscope/protocols/anthropic.py
647 648 649 | |
make_chunk_parser
¶
make_chunk_parser(
*,
strict_validation: bool = False,
response: Optional[Response] = None
) -> Callable[
[Dict[str, Any]], Optional[ChatCompletionChunk]
]
Return a fresh parser so concurrent streams stay isolated.
Source code in dashscope/protocols/anthropic.py
651 652 653 654 655 656 657 658 659 660 661 | |
build_chat_request
¶
build_chat_request(
*,
model: str,
messages: List[Dict[str, Any]],
stream: bool = False,
tools: Optional[List[Dict[str, Any]]] = None,
temperature: Optional[float] = None,
top_p: Optional[float] = None,
max_tokens: Optional[int] = None,
stop: Union[str, List[str], None] = None,
seed: Optional[int] = None,
repetition_penalty: Optional[float] = None,
presence_penalty: Optional[float] = None,
enable_thinking: Optional[bool] = None,
response_format: Optional[Dict[str, Any]] = None,
top_k: Optional[int] = None,
tool_choice: Union[str, Dict[str, Any], None] = None,
n: Optional[int] = None,
logprobs: Optional[bool] = None,
top_logprobs: Optional[int] = None,
thinking_budget: Optional[int] = None,
enable_search: Optional[bool] = None,
search_options: Optional[Dict[str, Any]] = None,
parallel_tool_calls: Optional[bool] = None,
extra_parameters: Optional[Dict[str, Any]] = None,
path: Optional[str] = None
) -> ChatRequest
Source code in dashscope/protocols/anthropic.py
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 | |
parse_completion
¶
parse_completion(
payload: Dict[str, Any],
*,
strict_validation: bool = False,
response: Optional[Response] = None
) -> ChatCompletion
Source code in dashscope/protocols/anthropic.py
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 | |
parse_chunk
¶
parse_chunk(
payload: Dict[str, Any],
*,
strict_validation: bool = False,
response: Optional[Response] = None
) -> Optional[ChatCompletionChunk]
Source code in dashscope/protocols/anthropic.py
854 855 856 857 858 859 860 861 862 863 864 865 866 867 | |
Registry¶
get_protocol
¶
get_protocol(
name: str,
protocol_options: Optional[Dict[str, Any]] = None,
) -> ChatProtocol
Resolve a protocol adapter by name, passing adapter options.
Source code in dashscope/protocols/__init__.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |