vllm.entrypoints.cohere.protocol ¶
Cohere Chat v2 API protocol.
The bulk of the wire types come straight from the official cohere Python SDK so we stay in sync with the upstream specification and avoid re-declaring the schema. We only own three things locally:
- The top-level request body model (the SDK doesn't ship one — its
ClientV2.chattakes the body as kwargs), with vLLM-specific extensions (kv_transfer_params/chat_template_kwargs). - The non-streaming response envelope (the SDK exposes the message shape via :class:
AssistantMessageResponsebut no full response wrapper). - The streaming discriminated union (the SDK exports each event type individually but not as a combined
Annotated[Union[...], discriminator]).
Importing this module pulls in the cohere package. The router that mounts POST /cohere/v2/chat guards on that import succeeding so vLLM still boots without the SDK installed.
See https://docs.cohere.com/reference/chat for the upstream spec.
Classes:
-
CohereChatV2Request–Cohere Chat v2 request body.
-
CohereChatV2Response–Cohere Chat v2 non-streaming response body.
-
CohereError–Top-level error body returned by
/cohere/v2/chaterror responses.
CohereChatV2Request ¶
Bases: BaseModel
Cohere Chat v2 request body.
Mirrors the schema documented at https://docs.cohere.com/reference/chat. All structured fields delegate to the official SDK types so the body schema stays in sync with the upstream spec.
Source code in vllm/entrypoints/cohere/protocol.py
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 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 | |
_normalize_message_roles(v) classmethod ¶
Rewrite OpenAI-style developer roles to system.
Cohere's v2 ChatMessageV2 discriminated union only admits the four literal roles user / assistant / system / tool. OpenAI's developer role is documented as high-priority system instructions, so we alias it onto system before the SDK discriminator runs (otherwise it rejects the message with a literal_error against each union member). Mirrors _role_to_melody in the renderer so the same alias is honoured no matter which surface the message arrives through.
mode="before" is required so the rewrite happens before the list[ChatMessageV2] coercion runs the SDK's discriminated union; a default-mode validator would never see developer because validation would have already failed. On any structural malformation (non-iterable input, items without a dict-shaped role field, etc.) we hand v back unchanged and let Pydantic's normal coercion surface a precise error.
Source code in vllm/entrypoints/cohere/protocol.py
CohereChatV2Response ¶
Bases: BaseModel
Cohere Chat v2 non-streaming response body.
Wraps the SDK :class:AssistantMessageResponse (the message shape) in the documented v2 response envelope (id, finish_reason, usage, logprobs). The single constructor in :class:CohereServingChatV2._chat_completion_to_v2 is responsible for supplying a non-empty id (falling back to a synthesized one if the upstream response is missing it) to this model.
Source code in vllm/entrypoints/cohere/protocol.py
CohereError ¶
Bases: BaseModel
Top-level error body returned by /cohere/v2/chat error responses.
Cohere's documented error schemas are uniform: {message, id}.