Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
43d0542
Initial cohere chatv2 support
andrewbcohere Jun 30, 2026
141648e
tests fix
andrewbcohere Jun 30, 2026
b91221f
Use lighter model in integration test
andrewbcohere Jun 30, 2026
b022606
Style and lint fixes
andrewbcohere Jun 30, 2026
b007c99
Merge remote-tracking branch 'upstream/main' into andrewbcohere/chat-…
andrewbcohere Jun 30, 2026
14ec0bf
Address automated comments
andrewbcohere Jul 9, 2026
15f7e27
fix to throw an error if template_jinja is passed
andrewbcohere Jul 9, 2026
fa775db
fix missing import
andrewbcohere Jul 9, 2026
f63c708
Refactor citations to cohere specific classes
andrewbcohere Jul 15, 2026
5d84d90
Merge remote-tracking branch 'origin/main' into andrewbcohere/chat-v2…
andrewbcohere Jul 15, 2026
7439357
Remove trivial tests
andrewbcohere Jul 15, 2026
87bedd6
Fix sanitize message not being called for streaming errors
andrewbcohere Jul 16, 2026
355b392
Update to use _create_chat_message constructor method instead of _cha…
andrewbcohere Jul 16, 2026
318053f
fixes for comments
andrewbcohere Jul 21, 2026
96433d0
Remove unused import
andrewbcohere Jul 21, 2026
b70045f
Merge remote-tracking branch 'origin/main' into andrewbcohere/chat-v2…
andrewbcohere Jul 21, 2026
c4a6339
Minor improvements with match statements
andrewbcohere Jul 22, 2026
ccd09ea
Add guard to cohere prefix, other minor fixes
andrewbcohere Jul 22, 2026
e887fda
match code default with request default to cmd4
andrewbcohere Jul 27, 2026
24cb385
fix citations passthrough in request
andrewbcohere Jul 28, 2026
8614d4b
Fixes to citation handling
andrewbcohere Jul 28, 2026
81cd0d8
Fix test requirements
andrewbcohere Jul 28, 2026
40b1e9f
Fixes to citation handling
andrewbcohere Jul 28, 2026
9ceb76c
Merge remote-tracking branch 'origin/main' into andrewbcohere/chat-v2…
andrewbcohere Jul 28, 2026
8a02c99
add error handling middleware to make sure all errors are cohere format
andrewbcohere Jul 29, 2026
6083d03
Merge remote-tracking branch 'origin/main' into andrewbcohere/chat-v2…
andrewbcohere Jul 29, 2026
860afa5
minor cleanup
andrewbcohere Jul 30, 2026
b59d160
added env var to make cohere api explicit opt in only
andrewbcohere Jul 30, 2026
990561b
Merge remote-tracking branch 'origin/main' into andrewbcohere/chat-v2…
andrewbcohere Jul 30, 2026
ac58d6a
fmt fix
andrewbcohere Jul 30, 2026
30c6c76
Merge branch 'main' into andrewbcohere/chat-v2-api-rfc
DarkLight1337 Jul 31, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
186 changes: 186 additions & 0 deletions tests/entrypoints/cohere/test_api_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Unit tests for ``vllm/entrypoints/cohere/api_router.py``.

Covers:

* The optional-import guard: ``attach_router`` is a no-op when the
``cohere`` SDK isn't installed.
* The router wiring: response shapes (JSON + SSE), error translation,
and the ``cohere_serving_chat_v2 is None`` fallback (501 Not
Implemented).
"""

import json
from collections.abc import AsyncGenerator
from http import HTTPStatus

from fastapi import FastAPI
from fastapi.testclient import TestClient

from vllm.entrypoints.cohere import api_router as api_router_mod
from vllm.entrypoints.cohere.api_router import attach_router
from vllm.entrypoints.cohere.protocol import (
AssistantMessageResponse,
CohereChatV2Response,
)
from vllm.entrypoints.openai.engine.protocol import ErrorInfo, ErrorResponse

# ----------------------------------------------------------------------
# Fakes
# ----------------------------------------------------------------------


class _Handler:
"""Minimal stand-in for :class:`CohereServingChatV2` used by the
router. Each test sets ``self.result`` to either:

* a :class:`CohereChatV2Response` (non-streaming JSON path);
* an async generator yielding SSE frames (streaming path);
* an :class:`ErrorResponse` (error envelope path); or
* an exception (router-level 500 path).
"""

def __init__(self, result):
self.result = result

async def create_chat_v2(self, request, raw_request):
if isinstance(self.result, Exception):
raise self.result
return self.result


def _build_app(handler: _Handler | None) -> FastAPI:
app = FastAPI()
attach_router(app)
app.state.cohere_serving_chat_v2 = handler
return app


def _minimal_request_body() -> dict:
return {
"model": "m",
"messages": [{"role": "user", "content": "hi"}],
}


# ----------------------------------------------------------------------
# Optional-import guard
# ----------------------------------------------------------------------


class TestOptionalCohereImport:
def test_attach_router_noop_when_cohere_missing(self, monkeypatch, caplog):
# ``api_router`` probes for the SDK once at module load (because
# the route handler closes over types imported from ``cohere``)
# and stashes the result in ``_SDK_AVAILABLE``. We can't undo the
# original import, so simulate the "SDK missing" state by
# flipping that flag for the duration of the test. This is what
# ``attach_router`` actually checks at call time.
monkeypatch.setattr(api_router_mod, "_SDK_AVAILABLE", False)

with caplog.at_level("INFO", logger="vllm.entrypoints.cohere.api_router"):
app = FastAPI()
attach_router(app)

paths = [getattr(r, "path", None) for r in app.routes]
assert "/cohere/v2/chat" not in paths
assert any("cohere SDK not installed" in rec.message for rec in caplog.records)

def test_attach_router_registers_route_when_cohere_present(self):
app = _build_app(handler=None)
paths = [getattr(r, "path", None) for r in app.routes]
assert "/cohere/v2/chat" in paths


# ----------------------------------------------------------------------
# Endpoint behavior
# ----------------------------------------------------------------------


class TestEndpoint:
def test_501_when_handler_missing(self):
app = _build_app(handler=None)
with TestClient(app) as client:
r = client.post("/cohere/v2/chat", json=_minimal_request_body())
assert r.status_code == HTTPStatus.NOT_IMPLEMENTED
body = r.json()
assert "does not support" in body["message"]
assert "id" not in body # excluded by ``exclude_none=True``

def test_non_streaming_response_is_json(self):
msg = AssistantMessageResponse(content=[{"type": "text", "text": "hello"}])
result = CohereChatV2Response(id="r1", finish_reason="COMPLETE", message=msg)
app = _build_app(handler=_Handler(result))
with TestClient(app) as client:
r = client.post("/cohere/v2/chat", json=_minimal_request_body())
assert r.status_code == HTTPStatus.OK
assert r.headers["content-type"].startswith("application/json")
body = r.json()
assert body["id"] == "r1"
assert body["finish_reason"] == "COMPLETE"
assert body["message"]["content"][0]["text"] == "hello"

def test_streaming_response_is_sse(self):
async def _gen() -> AsyncGenerator[str, None]:
yield 'data: {"type":"message-start"}\n\n'
yield "data: [DONE]\n\n"

app = _build_app(handler=_Handler(_gen()))
with TestClient(app) as client:
r = client.post(
"/cohere/v2/chat",
json={**_minimal_request_body(), "stream": True},
)
assert r.status_code == HTTPStatus.OK
assert r.headers["content-type"].startswith("text/event-stream")
body = r.text
assert "message-start" in body
assert body.rstrip().endswith("[DONE]")

def test_error_response_translated_to_cohere_envelope(self):
err = ErrorResponse(
error=ErrorInfo(
message="bad request",
type="bad_request",
code=400,
)
)
app = _build_app(handler=_Handler(err))
with TestClient(app) as client:
r = client.post("/cohere/v2/chat", json=_minimal_request_body())
assert r.status_code == HTTPStatus.BAD_REQUEST
body = r.json()
assert body == {"message": "bad request"}

def test_handler_exception_returns_500_envelope(self):
app = _build_app(handler=_Handler(RuntimeError("kaboom")))
with TestClient(app) as client:
r = client.post("/cohere/v2/chat", json=_minimal_request_body())
assert r.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
body = r.json()
assert body == {"message": "kaboom"}

def test_non_json_content_type_rejected(self):
"""The ``validate_json_request`` dependency raises
``RequestValidationError`` (HTTP 422) for non-JSON content
types, matching the behavior of the other vLLM API routers.
"""
app = _build_app(handler=None)
with TestClient(app) as client:
r = client.post(
"/cohere/v2/chat",
content=json.dumps(_minimal_request_body()),
headers={"content-type": "text/plain"},
)
assert r.status_code == HTTPStatus.UNPROCESSABLE_ENTITY

def test_invalid_body_returns_422(self):
# ``model`` is required; omit it to trip Pydantic validation.
app = _build_app(handler=None)
with TestClient(app) as client:
r = client.post(
"/cohere/v2/chat",
json={"messages": [{"role": "user", "content": "hi"}]},
)
assert r.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
Loading
Loading