vllm.parser.cohere_command ¶
Cohere Command parser.
Uses cohere_melody as a stateful parser filter.
Classes:
-
CohereCommandParser– -
CohereNormalizedTool–A tool definition normalized to the shape
collect_tool_schemaexpects. -
CohereTagRegistry–A single
structural_tagtrigger / end pair (beginusestrigger). -
CohereTagStyle–The structural tags style for a given model architecture.
Functions:
-
collect_tool_schema–Build an xgrammar EBNF grammar that matches a JSON array of tool calls.
-
convert_schema_to_structural_tags–Returns a response_format string accepted by xgrammar's structural tag format.
CohereCommandParser ¶
Bases: DelegatingParser
Source code in vllm/parser/cohere_command.py
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 | |
_apply_structural_tags(request) ¶
Fold tools and any JSON schema into a Cohere structural_tag.
Source code in vllm/parser/cohere_command.py
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.
Source code in vllm/parser/cohere_command.py
CohereTagRegistry ¶
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.
Source code in vllm/parser/cohere_command.py
_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.
Source code in vllm/parser/cohere_command.py
_maybe_parse_json_dict(value) ¶
If value is a JSON string, parse to dict; otherwise require dict.
Source code in vllm/parser/cohere_command.py
_melody_citations_to_vllm(citations, position_to_source) ¶
Convert melody FilterCitation objects into :class: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.
Source code in vllm/parser/cohere_command.py
_melody_sources_to_vllm(sources, position_to_source) ¶
Resolve melody Source addresses into :class:CitationSource objects.
Melody reports each source as (tool_call_index, tool_result_indices). position_to_source is built by :meth: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.
Source code in vllm/parser/cohere_command.py
_schema_dict_from_chat_response_format(rf) ¶
JSON schema dict from Chat Completions request.response_format only.
Source code in vllm/parser/cohere_command.py
_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.
Source code in vllm/parser/cohere_command.py
_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
Source code in vllm/parser/cohere_command.py
_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()
Source code in vllm/parser/cohere_command.py
_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.
Source code in vllm/parser/cohere_command.py
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).
Source code in vllm/parser/cohere_command.py
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.