[Bugfix][Frontend] Apply the stop string limit to Cohere requests (#53750)
Signed-off-by: mhuzaifa3 <mhuzaifa3@outlook.com>
Moe Huzaifa committed
Aug 25, 2026 at 11:41 UTC
217a0b5cefec3f0f4a011cb498afcbf211f8bc25
2 files changed
+24
-2
tests/entrypoints/cohere/test_protocol.py
+19
@@ -16,6 +16,7 @@ types plus a few local additions:
16
import pytest
17
from pydantic import ValidationError
18
19
+import vllm.envs as envs
20
from vllm.entrypoints.cohere.protocol import (
21
AssistantMessageResponse,
22
CitationEndEvent,
@@ -94,6 +95,24 @@ class TestCohereChatV2Request:
95
tool_choice="ANY", # not REQUIRED/NONE
96
)
97
98
+ def test_stop_sequences_bounded_by_env_limit(self):
99
+ messages = [{"role": "user", "content": "hi"}]
100
+ limit = envs.VLLM_MAX_STOP_STRINGS
101
+
102
+ req = CohereChatV2Request(
103
+ model="m",
104
+ messages=messages,
105
+ stop_sequences=[f"s{i}" for i in range(limit)],
106
+ )
107
+ assert len(req.stop_sequences) == limit
108
+
109
+ with pytest.raises(ValidationError):
110
+ CohereChatV2Request(
111
+ model="m",
112
+ messages=messages,
113
+ stop_sequences=[f"s{i}" for i in range(limit + 1)],
114
+ )
115
+
116
117
# ======================================================================
118
# Usage / Logprobs
vllm/entrypoints/cohere/protocol.py
+5
-2
@@ -25,7 +25,7 @@ See https://docs.cohere.com/reference/chat for the upstream spec.
25
26
from __future__ import annotations
27
28
-from typing import Any, Literal
28
+from typing import Annotated, Any, Literal
29
30
from cohere import types as _sdk
31
from cohere.types import (
@@ -46,6 +46,7 @@ from cohere.types import (
46
)
47
from pydantic import BaseModel, Field, field_validator
48
49
+import vllm.envs as envs
50
from vllm.exceptions import VLLMValidationError
51
52
# Re-export the SDK wire-format types alongside our local extensions so
@@ -152,7 +153,9 @@ class CohereChatV2Request(BaseModel):
153
response_format: ResponseFormatV2 | None = None
154
safety_mode: ChatRequestSafetyMode | None = None
155
max_tokens: int | None = None
155
- stop_sequences: list[str] | None = None
156
+ stop_sequences: (
157
+ Annotated[list[str], Field(max_length=envs.VLLM_MAX_STOP_STRINGS)] | None
158
+ ) = None
159
160
# Sampling
161
temperature: float | None = None