vllm.entrypoints.cohere.serving
¶
Cohere Chat v2 API serving handler.
Implements POST /cohere/v2/chat by translating the incoming Cohere v2 request
into a standard ChatCompletionRequest and delegating to
OpenAIServingChat. The actual prompt rendering is handled by
vLLM's renderer pipeline (vllm.renderers):
- For Cohere Command-family models, set
--tokenizer-mode cohereand thevllm.renderers.cohere.CohereRendererwill template the request via thecohere_melodylibrary (render_cmd3/render_cmd4) and surface citations through the Cohere-scopedCohereChatMessage/CohereDeltaMessagesubclasses (seevllm.entrypoints.cohere.cohere_chat_message). - For any other model the default Jinja-based
HfRendereris used, and the endpoint behaves as a plain v2-shaped wrapper around chat completions.
Classes:
-
CohereServingChatV2–Handler for the Cohere Chat v2 API (
POST /cohere/v2/chat). -
ContentBlockType–Wire-format / internal discriminator for chat content blocks.
CohereServingChatV2
¶
Bases: OpenAIServingChat
Handler for the Cohere Chat v2 API (POST /cohere/v2/chat).
The handler is intentionally thin: it converts the v2 request into a
ChatCompletionRequest (preserving Cohere-specific fields such
as documents, safety_mode and citation_options via
chat_template_kwargs) and delegates to the underlying chat
completion machinery. All Cohere-specific templating happens in the
renderer (vllm.renderers.cohere.CohereRenderer) when the
engine is started with --tokenizer-mode cohere.
Citations flow through CohereChatMessage / CohereDeltaMessage
subclasses (see vllm.entrypoints.cohere.cohere_chat_message) so
the OpenAI-shared ChatMessage / DeltaMessage keep
their declared schemas unchanged. Non-streaming responses pick up
the subclass by overriding _create_chat_message; streaming
deltas are emitted directly by the Cohere reasoning parser.
Methods:
-
create_chat_v2–Implements
POST /cohere/v2/chat. -
to_chat_completion_request–Convert a Cohere v2 request into its
ChatCompletionRequest.
_apply_cohere_template_kwargs(chat_req, request)
classmethod
¶
Forward Cohere-specific request fields into chat_template_kwargs.
The vllm.renderers.cohere.CohereRenderer consumes these
kwargs to drive cohere_melody.render_cmd3 / render_cmd4.
Other renderers ignore unknown kwargs, so this is also a no-op for
non-cohere --tokenizer-mode settings.
_build_doc_id_to_prompt_position(messages, documents)
classmethod
¶
Map every citable document id to its (bucket, result_index).
Inbound helper: converts request-side citation source.id
refs into melody's numeric FilterCitation addressing (see
_sdk_citation_to_melody). First-seen-id wins so an
explicit id on a top-level document takes precedence over the
doc_{idx} fallback that _walk_citable_positions
also emits for the same slot.
Thin wrapper over _walk_citable_positions; the
numbering rule lives there.
_build_position_to_source(request)
classmethod
¶
Invert the request-side numbering into (bucket, idx) -> source.
Outbound helper: forwarded to the reasoning parser via
chat_template_kwargs[POSITION_TO_SOURCE_KEY] so
_melody_sources_to_vllm can attach real document ids /
types / payloads to each melody source it emits. Each entry
carries the fully-resolved wire-shape info: type
("document" for the reserved top-level bucket 0, "tool"
for any tool-call bucket), id (explicit doc id, the
doc_{idx} fallback, or None for id-less sources), and
document / tool_output payload -- matching the cohere
api's Source shape.
Thin wrapper over _walk_citable_positions; the
numbering rule lives there.
_chat_completion_stream_to_v2(generator)
async
¶
Translate raw OpenAI chat completion chunks into Cohere's v2 stream-event format.
Cohere's v2 stream lifecycle is:
message-start
[content-start, content-delta..., content-end]*
[tool-plan-delta]*
[tool-call-start, tool-call-delta..., tool-call-end]*
message-end
_close_open_blocks(state)
¶
Emit content-end / tool-call-end for the currently open
block (if any) and reset the corresponding _StreamState slots.
_collect_message_citations(request)
classmethod
¶
Extract AssistantChatMessageV2.citations keyed by message index.
Returns an empty dict when no assistant message in the request
carries any resolvable citations, so callers can use truthiness
as the "should I emit the MESSAGES_CITATIONS_KEY entry?"
test. Citations whose sources can't be resolved are dropped
individually; messages that end up with zero surviving
citations are omitted from the result.
_collect_v2_tool_message_content(request)
classmethod
¶
Collect the original v2 tool-message content blocks so the renderer can apply its v2 → melody mapping.
Builds the map that
_apply_cohere_template_kwargs stashes under
TOOL_MESSAGE_V2_CONTENT_KEY for the renderer to
consume (see the constant definition in
vllm.renderers.cohere for why the map exists). Walks
request.messages -- not the OpenAI-shape messages built by
_convert_messages -- because that flattening step
already threw away the structural distinction between text and
document blocks. Each surviving block is
model_dump'd to a plain dict so the
forwarded map carries no Pydantic instances (msgpack- /
general-serialization-safe on principle even though internal
underscore-prefixed keys are filtered before crossing the
engine-core boundary).
Only emits an entry for a tool message whose content is a
non-empty list of blocks; a bare-string tool message renders
fine through the default text-block path in
vllm.renderers.cohere._content_blocks, so no forwarded
blocks are needed there. Returns an empty dict when there is
nothing to forward, so the caller can use truthiness to decide
whether to stash the key at all.
_create_chat_message(*args, **kwargs)
¶
Route response construction through the citation-carrying subclass.
Overrides OpenAIServingChat._create_chat_message so every
non-streaming construction site in the base full-generator
produces a CohereChatMessage. That lets
_finalize_response_message below set citations on
message.citations directly, without relying on
extra="allow" attribute injection.
_engine_chat_template_kwargs(chat_template_kwargs)
¶
Keep this handler's internal entries out of the engine hop.
Overrides OpenAIServingChat._engine_chat_template_kwargs.
The reserved keys in _API_SERVER_ONLY_TEMPLATE_KWARGS
exist only to hand request-side state to the API-server-side
parser and renderer; the engine-core-side reasoning parser (used
for structured-output reasoning-end gating) never reads them. They
also can't survive the trip: POSITION_TO_SOURCE_KEY holds
CitationSource models, and msgpack -- which encodes
reasoning_parser_kwargs on the way to the engine core -- has
no native encoding for Pydantic models, so forwarding them fails
the request with a TypeError.
_extract_citations_if_any(msg)
¶
Coerce CohereChatMessage.citations into the Cohere v2 wire shape.
CohereChatMessage carries a
citations: list[vllm...Citation] | None field populated by
_finalize_response_message (which reads it off the
unified parser's last_unary_citations cache). Sources are
already fully resolved by the parser (see
_melody_sources_to_vllm in
vllm.parser.cohere_command) using
the position map forwarded via chat_template_kwargs. This
method's remaining job is:
- Drop citations whose sources all failed to resolve (empty
sourceslist) -- fail-closed policy matching the inbound_sdk_citation_to_melody. - Rewrite
THINKING_CONTENT->PLANfor non-reasoning models, since_chat_completion_to_v2surfacesmsg.reasoningastool_planon the sameis_reasoning_modelflag. - Coerce to
cohere.types.Citationfor the wire.
_finalize_response_message(message, *, parser)
¶
Copy grounding citations off the unified parser onto the message.
vllm.parser.cohere_command.CohereCommandParser caches the
citations produced by its most recent unary parse call on
last_unary_citations. We surface them here on
CohereChatMessage so downstream response conversion can
pick them up without the base OpenAIServingChat having to
know about citations.
_handle_citation_deltas(state, citations)
¶
Emit citation-start / citation-end events for a delta.
The reasoning parser populates
CohereDeltaMessage.citations (see
vllm.entrypoints.cohere.cohere_chat_message) with
sources whose id / type / document / tool_output
are already resolved against the request's document context
(see _melody_sources_to_vllm in the parser). This
method only coerces to the SDK wire shape, applies the
THINKING_CONTENT -> PLAN rewrite for non-reasoning
models, and drops citations whose sources didn't resolve --
the shared _to_wire_citation handles all three.
_sdk_citation_to_melody(citation, doc_positions)
staticmethod
¶
Convert a Cohere SDK Citation to melody's FilterCitation.
Melody's Source uses a two-level numeric address:
tool_call_index picks a bucket (0 = top-level
documents when present, 1..N = tool results in history
order) and tool_result_indices picks positions inside that
bucket. The Cohere Citation.sources schema is per-document:
each entry's id is the id of the specific document that
grounds the citation (type distinguishes payload shape, not
id semantics). We resolve each source id against
doc_positions and aggregate hits sharing a bucket into one
melody Source so the renderer emits compact
<co>text</co: N:[i,j]> markers.
Returns None when any source id fails to resolve or when
the input has no sources at all: emitting a citation with an
empty sources=[] would render a malformed
<co>text</co: :> marker, and silently attributing a
citation to the wrong bucket is worse than dropping the
<co> markup. This matches the cohere api's behavior.
Source.document_ids is intentionally NOT populated: it is a
parser-output lookup table (see PromptRenderIds in
melody), and melody's deserializer ignores it on input.
The Cohere type literal (TEXT_CONTENT / THINKING_CONTENT
/ PLAN) collapses to melody's boolean is_thinking.
PLAN is treated as a thinking-style citation because cmd3 /
cmd4 templates render both inline with the same <co>...</co>
markup.
_text_to_tool_output_payload(text)
staticmethod
¶
Wrap a bare tool-content text into a tool_output payload.
Matches the cohere api's handling of text-only tool content:
attempt to interpret text as a JSON object first; fall
back to {"content": <text>} so a text-only tool result
still has a structured payload we can attach to a citation
source.
_to_wire_citation(citation)
¶
Coerce one parser-produced citation into the SDK wire shape.
Accepts both InternalCitation instances (what the
unary path receives directly from the parser via
last_unary_citations) and plain dict payloads (what
the streaming path sees, because DeltaMessage.citations is
an untyped extras field on the OpenAI wire protocol and dict
shapes survive model_validate_json unchanged). Both flows
end up carrying the same key names, so a small _get helper
handles both without a shape-specific code path.
_tool_content_block_to_source(block)
classmethod
¶
Extract the wire-shape source for one tool-message content block.
Handles both cohere DocumentToolContent and
TextToolContent blocks:
documentblocks with an explicit id produce atoolsource keyed by that id (payload is the document'sdatadict, or{"text": ...}for scalar data).documentblocks without an id andtextblocks produce an id-lesstoolsource: the cohere api emits the payload with an empty id in this case, and cohere's SDK treatsToolSource.idas optional -- so we simply omitidon the wire instead of inventing a synthetic one.
Returns None only for shapes we don't recognise at all;
that slot still gets counted (callers advance the bucket
cursor over the block regardless) so numbering stays aligned
with melody.
_walk_citable_positions(messages, documents)
classmethod
¶
Single-pass implementation of melody's citation numbering rule.
Mirrors PromptRenderIds::from_messages in
melody/src/templating/util.rs. Emits one
_CitablePosition per document-bearing slot melody
assigns while rendering the prompt:
- Bucket numbering. Bucket
0is reserved for the top-leveldocumentsarray when present, and each uniquetool_call_idclaims the next integer on first-seen basis -- registered from whichever of the assistant'stool_calls[].idor the tool message'stool_call_idappears first in the history. - Per-bucket result-index slots. Consumed by every
content item, not just documents -- a text block occupies a
slot too, and the source at that slot carries an id-less
tool_outputpayload synthesized via_text_to_tool_output_payload, matching the cohere api's behavior for text-only tool content. Strings and empty contents each consume exactly one slot because the cohere renderer wraps them into a single text block before handing the message off to melody. Multiple tool messages sharing atool_call_idaccumulate into the same bucket, so later docs' slots are offset by the sum of previous messages' content lengths. - Doc ids attached to each position. Top-level docs
contribute both the explicit
id(if set) and thedoc_{idx}fallback used for inbound addressing; tool document blocks contribute their singledocument.idwhen present. Theidstuple is empty for slots that are only reachable positionally (text-only tool content, docs without a client-provided id) -- those slots still have a fully-populatedInternalCitationSource, just no client-visible id to cite them by.
Ids alone key the inbound helper
(_build_doc_id_to_prompt_position, which drives
request-side citation resolution); the resolved
InternalCitationSource (with type / id /
payload) keys the outbound helper
(_build_position_to_source, whose result is forwarded
to the reasoning parser via
chat_template_kwargs[POSITION_TO_SOURCE_KEY]).
Cohere's on-wire semantics -- ToolSource.id and
DocumentSource.id both carry a document id, with type
acting only as a payload-shape hint -- are the invariant this
walk is mirroring.
create_chat_v2(request, raw_request=None)
async
¶
Implements POST /cohere/v2/chat.
to_chat_completion_request(request)
¶
Convert a Cohere v2 request into its ChatCompletionRequest.
Exposed for callers that need the converted request without
running generation - notably the render endpoint
(POST /cohere/v2/chat/render), which hands the result to
vllm.entrypoints.scale_out.render.serving.ServingRender.render_chat_request
so that Cohere requests tokenize through exactly the same path
as /v1/chat/completions/render.
ContentBlockType
¶
Bases: StrEnum
Wire-format / internal discriminator for chat content blocks.
THINKING and TEXT are the documented Cohere v2 content-block
type discriminators on the wire. TOOL_CALL is reserved for the
internal stream state machine (see _StreamState) when a
tool call is the currently open block; it is never serialized.
_CitablePosition
¶
Bases: NamedTuple
One entry in melody's citation numbering scheme, resolved to wire shape.
Populated by CohereServingChatV2._walk_citable_positions
(the single source of truth for melody's numbering rule) and
consumed by both the inbound
(CohereServingChatV2._build_doc_id_to_prompt_position,
which drives citation resolution on the request side) and outbound
(CohereServingChatV2._build_position_to_source, whose
result is forwarded to the reasoning parser via
chat_template_kwargs[POSITION_TO_SOURCE_KEY]) helpers -- so
the numbering rule only lives in one place.
ids lists every id under which this position is addressable:
top-level documents contribute both their explicit id (if set) and
the doc_{idx} synthetic fallback, in that order so
setdefault-style resolution prefers the explicit id;
tool-message documents contribute their single document.id.
source is the fully-resolved wire-shape source (type /
id / document or tool_output payload) for the
outbound direction.
_StreamState
¶
Tracks which Cohere v2 stream block (if any) is currently open.
_emit(event)
¶
Serialize a typed stream event into an SSE frame.
The typed event classes in vllm.entrypoints.cohere.protocol
(MessageStartEvent, ContentStartEvent, CitationStartEvent,
...) bake the wire-format type field into the model definition,
so a plain model_dump_json() already carries the discriminator.
_sse(data)
¶
Wrap a JSON payload in a Server-Sent Event frame.
Cohere's stream uses bare data: lines (no event: prefix); each
JSON object's type field carries the event discriminator.