Skip to content

vllm.parser.cohere_command

Cohere Command parser.

Uses cohere_melody as a stateful parser filter.

Classes:

Functions:

CohereCommandParser

Bases: DelegatingParser

_apply_structural_tags(request)

Fold tools and any JSON schema into a Cohere structural_tag.

CohereNormalizedTool

Bases: TypedDict

A tool definition normalized to the shape collect_tool_schema expects.

parameters is a JSON Schema object (possibly empty) describing the tool's call signature.

CohereTagRegistry

Bases: NamedTuple

A single structural_tag trigger / end pair (begin uses trigger).

CohereTagStyle

Bases: NamedTuple

The structural tags style for a given model architecture.

json_tags lists every JSON-schema wrapper the model may emit (MOE uses both response and text delimiters). tools is the tool-call wrapper.

_has_effective_tools(tools)

True when tools contains at least one tool definition to convert.

ResponsesRequest defaults tools to []; ChatCompletionRequest uses None. Both mean "no tools" here. Strings (e.g. a JSON blob) are treated as effective only when non-blank.

_maybe_parse_json_dict(value)

If value is a JSON string, parse to dict; otherwise require dict.

_melody_citations_to_vllm(citations, position_to_source)

Convert melody FilterCitation objects into Citation.

Citations whose sources all fail to resolve are still returned with sources=[]; the serving layer drops them so the fail-closed policy lives in one place.

_melody_sources_to_vllm(sources, position_to_source)

Resolve melody Source addresses into CitationSource objects.

Melody reports each source as (tool_call_index, tool_result_indices). position_to_source is built by vllm.entrypoints.cohere.serving.CohereServingChatV2._build_position_to_source and forwarded through chat_template_kwargs; each index fans out to one resolved source, and unresolved positions are skipped. Without a map (a request that did not come through the Cohere v2 handler) every source is dropped and the serving layer discards the citation at wire-coercion time.

_schema_dict_from_chat_response_format(rf)

JSON schema dict from Chat Completions request.response_format only.

_schema_dict_from_structured_outputs(so)

Schema dict from structured_outputs (json / json_object).

Same unwrapping as json_schema. json is expected to be str or dict (enforced by StructuredOutputsParams / request models); other types raise ValueError only if a caller bypasses that validation.

_schema_from_json_schema_field(js_wr)

Extract the JSON Schema object from Chat Completions json_schema payload.

Accepts: - JsonSchemaResponseFormat (Pydantic) with schema / json_schema field - dict in OpenAI shape {"name": ..., "schema": {...}} - dict with json_schema key holding either the schema or a nested wrapper - dict that is already a JSON Schema document (some clients omit the wrapper) - JSON strings for any of the above

_tool_definitions_to_schema_list(tools)

Build the list of CohereNormalizedTool dicts expected by collect_tool_schema.

Accepts: - JSON string - list of dicts with top-level name / parameters - list of Chat Completions-style {"type": "function", "function": {...}} - list of Pydantic models with model_dump()

_unwrap_nested_schema(candidate)

Return candidate as a dict, unwrapping a nested schema if present.

Returns None if candidate is not (and cannot be parsed into) a dict.

collect_tool_schema(tool_schema)

Build an xgrammar EBNF grammar that matches a JSON array of tool calls.

The grammar shape is architecture-independent; callers are responsible for wrapping it in the correct structural tag (see CohereTagStyle.tools).

convert_schema_to_structural_tags(schema=None, tools=None, model_architecture=None)

Returns a response_format string accepted by xgrammar's structural tag format. Uses the canonical shape: {"type": "structural_tag", "format": {...}} with format.type "triggered_tags" and tag content type "json_schema" or "grammar".

Callers that are not on an engine path (e.g. the reasoning parser) must pass model_architecture explicitly.