vllm.entrypoints.cohere.api_router ¶
FastAPI router for the Cohere Chat v2 API (POST /cohere/v2/chat).
The Cohere v2 protocol models are sourced from the official cohere Python SDK (pip install cohere). To keep that an optional dependency for vLLM, the SDK-dependent imports - and the route handler itself - are gated on a one-shot probe at module load. If the SDK isn't installed, :func:attach_router becomes a no-op (with an info log) and vLLM continues to boot normally.
Even when the SDK is installed, :func:attach_router also requires VLLM_ENABLE_COHERE_API=1 in the environment before it will expose the route. This keeps non-Cohere deployments that pull in the SDK for unrelated reasons (e.g. test dependencies) from accidentally exposing the api.
Note: the handler must live at module scope (not inside attach_router) so that FastAPI's typing.get_type_hints resolves the CohereChatV2Request body annotation against the module's globals. Defining it locally inside attach_router would hide the type from get_type_hints, causing FastAPI to silently degrade the body parameter into a query parameter and reject every request with 422.
Classes:
-
CohereErrorEnvelopeMiddleware–Rewrite vLLM error bodies into the Cohere
{message, id}shape.
Functions:
-
attach_router–Register
POST /cohere/v2/chatonapp.
CohereErrorEnvelopeMiddleware ¶
Bases: BaseHTTPMiddleware
Rewrite vLLM error bodies into the Cohere {message, id} shape.
The endpoint handler above already returns :class:CohereError for errors it owns, but globally-registered exception handlers (e.g. :func:validation_exception_handler for pydantic body errors, :func:http_exception_handler, engine error handlers) fire before the handler runs and produce vLLM's internal ErrorResponse shape ({"error": {"message": ...}}). That shape doesn't match the CohereError schema advertised on the route's OpenAPI responses, so clients (and schema-conformance tests like test_openai_schema.py) would see a mismatch on those paths. This middleware normalises any error body on /cohere/* responses to :class:CohereError.
Source code in vllm/entrypoints/cohere/api_router.py
_error_response(error, raw_request, *, fallback_status=HTTPStatus.BAD_REQUEST) ¶
Translate vLLM's internal error envelope into Cohere's shape.
Source code in vllm/entrypoints/cohere/api_router.py
_request_id(raw_request) ¶
Best-effort lookup of the active request id.
Prefers the id the underlying chat handler stamped onto raw_request.state.request_metadata (if it got that far before failing), falling back to the X-Request-Id HTTP header. May return None if neither is available, in which case the field is omitted from the response.
Source code in vllm/entrypoints/cohere/api_router.py
_translate_vllm_error_body(raw, request) ¶
Translate a vLLM ErrorResponse body to a CohereError body.
Returns None if raw does not match the vLLM error envelope (which signals the middleware to pass the body through unchanged).
Source code in vllm/entrypoints/cohere/api_router.py
attach_router(app) ¶
Register POST /cohere/v2/chat on app.
No-op when either:
- the
VLLM_ENABLE_COHERE_APIenv var isn't set to1. The Cohere v2 endpoint is opt-in because it carries Cohere-specific request/response semantics (grounding citations, tool_plan, PLAN/THINKING_CONTENT blocks) that are only meaningful when serving a Cohere Command-family model. - the optional
cohereSDK isn't installed (the v2 protocol models live there)
The two skip paths log at different levels: an operator who set VLLM_ENABLE_COHERE_API=1 but forgot to install cohere sees a WARNING (they explicitly asked for the endpoint and it's silently absent), whereas the default-off skip logs at debug.