fix(cohere): map string stop parameter to list of strings for stop_sequences

Fixes #46977: Cohere chat API reference expects stop_sequences to be a list of strings. Convert string stop parameter to a single-element list for CohereChatConfig and CohereV2ChatConfig while preserving lists.
This commit is contained in:
Praising Harris Ratnam 2026-10-17 12:48:23 +05:30
parent 3ade8288da
commit 9ce5dede2c
3 changed files with 42 additions and 2 deletions

View file

@ -205,7 +205,7 @@ class CohereChatConfig(BaseConfig):
if param == "presence_penalty":
optional_params["presence_penalty"] = value
if param == "stop":
optional_params["stop_sequences"] = value
optional_params["stop_sequences"] = [value] if isinstance(value, str) else value
if param == "tools":
optional_params["tools"] = value
if param == "seed":

View file

@ -159,7 +159,7 @@ class CohereV2ChatConfig(OpenAIGPTConfig):
if param == "presence_penalty":
optional_params["presence_penalty"] = value
if param == "stop":
optional_params["stop_sequences"] = value
optional_params["stop_sequences"] = [value] if isinstance(value, str) else value
if param == "tools":
optional_params["tools"] = value
if param == "seed":

View file

@ -77,6 +77,26 @@ class TestCohereTransform:
# The function should properly map max_tokens if max_completion_tokens is not provided
assert result == {"temperature": 0.7, "max_tokens": 200}
def test_cohere_stop_string_to_list(self):
"""Test that a string stop parameter is converted to a list of strings for stop_sequences"""
result = self.config.map_openai_params(
non_default_params={"stop": "END"},
optional_params={},
model=self.model,
drop_params=False,
)
assert result == {"stop_sequences": ["END"]}
def test_cohere_stop_list_preserved(self):
"""Test that a list stop parameter is preserved as a list for stop_sequences"""
result = self.config.map_openai_params(
non_default_params={"stop": ["END", "STOP"]},
optional_params={},
model=self.model,
drop_params=False,
)
assert result == {"stop_sequences": ["END", "STOP"]}
class TestCohereV2Transform:
def setup_method(self):
@ -141,6 +161,26 @@ class TestCohereV2Transform:
assert optional_params["max_tokens"] == 256
def test_v2_stop_string_to_list(self):
"""Test that a string stop parameter is converted to a list of strings for stop_sequences in v2"""
result = self.config.map_openai_params(
non_default_params={"stop": "END"},
optional_params={},
model=self.model,
drop_params=False,
)
assert result == {"stop_sequences": ["END"]}
def test_v2_stop_list_preserved(self):
"""Test that a list stop parameter is preserved as a list for stop_sequences in v2"""
result = self.config.map_openai_params(
non_default_params={"stop": ["END", "STOP"]},
optional_params={},
model=self.model,
drop_params=False,
)
assert result == {"stop_sequences": ["END", "STOP"]}
@pytest.mark.asyncio
async def test_cohere_request_body_with_allowed_params():