Structural Tag

This page contains the API reference for the structural tag. For its usage, see Structural Tag Usage.

Top Level Classes

class xgrammar.StructuralTag(*, type: Literal['structural_tag'] = 'structural_tag', format: Union[AnyTextFormat, ConstStringFormat, JSONSchemaFormat, GrammarFormat, RegexFormat, QwenXMLParameterFormat, OrFormat, SequenceFormat, TagFormat, TriggeredTagsFormat, TagsWithSeparatorFormat])[source]

Bases: BaseModel

Describes a complete structural tag structure. It corresponds to "response_format": {"type": "structural_tag", "format": {...}} in API.

field type: Literal['structural_tag'] = 'structural_tag'

The type must be “structural_tag”.

field format: Union[AnyTextFormat, ConstStringFormat, JSONSchemaFormat, GrammarFormat, RegexFormat, QwenXMLParameterFormat, OrFormat, SequenceFormat, TagFormat, TriggeredTagsFormat, TagsWithSeparatorFormat] [Required]

The format of the structural tag. Could be any of the structural tag formats.

static from_legacy_structural_tag(tags: List[StructuralTagItem], triggers: List[str]) StructuralTag[source]

Convert a legacy structural tag item to a structural tag.

static from_json(json_str: Union[str, Dict[str, Any]]) StructuralTag[source]

Convert a JSON string to a structural tag.

class xgrammar.structural_tag.StructuralTagItem(*, begin: str, schema: Union[str, Type[BaseModel], Dict[str, Any]], end: str)[source]

Bases: BaseModel

Deprecated. Definition of a structural tag item.

See xgrammar.Grammar.from_structural_tag() for more details.

field begin: str [Required]

The begin tag.

field schema_: Union[str, Type[BaseModel], Dict[str, Any]] [Required] (alias 'schema')

The schema.

field end: str [Required]

The end tag.

Format Union

xgrammar.structural_tag.Format(*args, **kwargs)

Union of all structural tag formats.

alias of Annotated[Union[AnyTextFormat, ConstStringFormat, JSONSchemaFormat, GrammarFormat, RegexFormat, QwenXMLParameterFormat, OrFormat, SequenceFormat, TagFormat, TriggeredTagsFormat, TagsWithSeparatorFormat]]

Basic Formats

pydantic model xgrammar.structural_tag.ConstStringFormat[source]

Bases: BaseModel

A format that matches a constant string.

Show JSON schema
{
   "title": "ConstStringFormat",
   "description": "A format that matches a constant string.",
   "type": "object",
   "properties": {
      "type": {
         "const": "const_string",
         "default": "const_string",
         "title": "Type",
         "type": "string"
      },
      "value": {
         "title": "Value",
         "type": "string"
      }
   },
   "required": [
      "value"
   ]
}

field type: Literal['const_string'] = 'const_string'

The type of the format.

field value: str [Required]

The constant string.

pydantic model xgrammar.structural_tag.JSONSchemaFormat[source]

Bases: BaseModel

A format that matches a JSON schema.

Show JSON schema
{
   "title": "JSONSchemaFormat",
   "description": "A format that matches a JSON schema.",
   "type": "object",
   "properties": {
      "type": {
         "const": "json_schema",
         "default": "json_schema",
         "title": "Type",
         "type": "string"
      },
      "json_schema": {
         "anyOf": [
            {
               "type": "boolean"
            },
            {
               "additionalProperties": true,
               "type": "object"
            }
         ],
         "title": "Json Schema"
      }
   },
   "required": [
      "json_schema"
   ]
}

field type: Literal['json_schema'] = 'json_schema'

The type of the format.

field json_schema: Union[bool, Dict[str, Any]] [Required]

The JSON schema.

pydantic model xgrammar.structural_tag.AnyTextFormat[source]

Bases: BaseModel

A format that matches any text.

Show JSON schema
{
   "title": "AnyTextFormat",
   "description": "A format that matches any text.",
   "type": "object",
   "properties": {
      "type": {
         "const": "any_text",
         "default": "any_text",
         "title": "Type",
         "type": "string"
      },
      "excludes": {
         "default": [],
         "items": {
            "type": "string"
         },
         "title": "Excludes",
         "type": "array"
      }
   }
}

field type: Literal['any_text'] = 'any_text'

The type of the format.

field excludes: List[str] = []

List of strings that should not appear in the matched text.

pydantic model xgrammar.structural_tag.GrammarFormat[source]

Bases: BaseModel

A format that matches an ebnf grammar.

Show JSON schema
{
   "title": "GrammarFormat",
   "description": "A format that matches an ebnf grammar.",
   "type": "object",
   "properties": {
      "type": {
         "const": "grammar",
         "default": "grammar",
         "title": "Type",
         "type": "string"
      },
      "grammar": {
         "title": "Grammar",
         "type": "string"
      }
   },
   "required": [
      "grammar"
   ]
}

field type: Literal['grammar'] = 'grammar'

The type of the format.

field grammar: str [Required]

The ebnf grammar.

pydantic model xgrammar.structural_tag.RegexFormat[source]

Bases: BaseModel

A format that matches a regex pattern.

Show JSON schema
{
   "title": "RegexFormat",
   "description": "A format that matches a regex pattern.",
   "type": "object",
   "properties": {
      "type": {
         "const": "regex",
         "default": "regex",
         "title": "Type",
         "type": "string"
      },
      "pattern": {
         "title": "Pattern",
         "type": "string"
      }
   },
   "required": [
      "pattern"
   ]
}

field type: Literal['regex'] = 'regex'

The type of the format.

field pattern: str [Required]

The regex pattern.

pydantic model xgrammar.structural_tag.QwenXMLParameterFormat[source]

Bases: BaseModel

A format that matches Qwen XML function calls.

Examples

structural_tag = QwenXMLParameterFormat(
    json_schema={
        "type": "qwen_xml_parameter",
        "json_schema": {
            "type": "object",
            "properties": {"name": {"type": "string"}, "age": {"type": "integer"}},
            "required": ["name", "age"],
        },
    }
)

The above structural tag can accept the following outputs:

<parameter=name>Bob</parameter><parameter=age>100</parameter>
<parameter=name>"Bob&lt;"</parameter><parameter=age>100</parameter>

Show JSON schema
{
   "title": "QwenXMLParameterFormat",
   "description": "A format that matches Qwen XML function calls.\n\nExamples\n--------\n.. code-block:: python\n\n    structural_tag = QwenXMLParameterFormat(\n        json_schema={\n            \"type\": \"qwen_xml_parameter\",\n            \"json_schema\": {\n                \"type\": \"object\",\n                \"properties\": {\"name\": {\"type\": \"string\"}, \"age\": {\"type\": \"integer\"}},\n                \"required\": [\"name\", \"age\"],\n            },\n        }\n    )\n\nThe above structural tag can accept the following outputs::\n\n    <parameter=name>Bob</parameter><parameter=age>100</parameter>\n    <parameter=name>\"Bob&lt;\"</parameter><parameter=age>100</parameter>",
   "type": "object",
   "properties": {
      "type": {
         "const": "qwen_xml_parameter",
         "default": "qwen_xml_parameter",
         "title": "Type",
         "type": "string"
      },
      "json_schema": {
         "anyOf": [
            {
               "type": "boolean"
            },
            {
               "additionalProperties": true,
               "type": "object"
            }
         ],
         "title": "Json Schema"
      }
   },
   "required": [
      "json_schema"
   ]
}

field type: Literal['qwen_xml_parameter'] = 'qwen_xml_parameter'

The type of the format.

field json_schema: Union[bool, Dict[str, Any]] [Required]

The JSON schema for the parameters of the function calling.

Combinatorial Formats

pydantic model xgrammar.structural_tag.SequenceFormat[source]

Bases: BaseModel

A format that matches a sequence of formats.

Show JSON schema
{
   "$defs": {
      "AnyTextFormat": {
         "description": "A format that matches any text.",
         "properties": {
            "type": {
               "const": "any_text",
               "default": "any_text",
               "title": "Type",
               "type": "string"
            },
            "excludes": {
               "default": [],
               "items": {
                  "type": "string"
               },
               "title": "Excludes",
               "type": "array"
            }
         },
         "title": "AnyTextFormat",
         "type": "object"
      },
      "ConstStringFormat": {
         "description": "A format that matches a constant string.",
         "properties": {
            "type": {
               "const": "const_string",
               "default": "const_string",
               "title": "Type",
               "type": "string"
            },
            "value": {
               "title": "Value",
               "type": "string"
            }
         },
         "required": [
            "value"
         ],
         "title": "ConstStringFormat",
         "type": "object"
      },
      "GrammarFormat": {
         "description": "A format that matches an ebnf grammar.",
         "properties": {
            "type": {
               "const": "grammar",
               "default": "grammar",
               "title": "Type",
               "type": "string"
            },
            "grammar": {
               "title": "Grammar",
               "type": "string"
            }
         },
         "required": [
            "grammar"
         ],
         "title": "GrammarFormat",
         "type": "object"
      },
      "JSONSchemaFormat": {
         "description": "A format that matches a JSON schema.",
         "properties": {
            "type": {
               "const": "json_schema",
               "default": "json_schema",
               "title": "Type",
               "type": "string"
            },
            "json_schema": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "additionalProperties": true,
                     "type": "object"
                  }
               ],
               "title": "Json Schema"
            }
         },
         "required": [
            "json_schema"
         ],
         "title": "JSONSchemaFormat",
         "type": "object"
      },
      "OrFormat": {
         "description": "A format that matches one of the formats.",
         "properties": {
            "type": {
               "const": "or",
               "default": "or",
               "title": "Type",
               "type": "string"
            },
            "elements": {
               "items": {
                  "discriminator": {
                     "mapping": {
                        "any_text": "#/$defs/AnyTextFormat",
                        "const_string": "#/$defs/ConstStringFormat",
                        "grammar": "#/$defs/GrammarFormat",
                        "json_schema": "#/$defs/JSONSchemaFormat",
                        "or": "#/$defs/OrFormat",
                        "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                        "regex": "#/$defs/RegexFormat",
                        "sequence": "#/$defs/SequenceFormat",
                        "tag": "#/$defs/TagFormat",
                        "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                        "triggered_tags": "#/$defs/TriggeredTagsFormat"
                     },
                     "propertyName": "type"
                  },
                  "oneOf": [
                     {
                        "$ref": "#/$defs/AnyTextFormat"
                     },
                     {
                        "$ref": "#/$defs/ConstStringFormat"
                     },
                     {
                        "$ref": "#/$defs/JSONSchemaFormat"
                     },
                     {
                        "$ref": "#/$defs/GrammarFormat"
                     },
                     {
                        "$ref": "#/$defs/RegexFormat"
                     },
                     {
                        "$ref": "#/$defs/QwenXMLParameterFormat"
                     },
                     {
                        "$ref": "#/$defs/OrFormat"
                     },
                     {
                        "$ref": "#/$defs/SequenceFormat"
                     },
                     {
                        "$ref": "#/$defs/TagFormat"
                     },
                     {
                        "$ref": "#/$defs/TriggeredTagsFormat"
                     },
                     {
                        "$ref": "#/$defs/TagsWithSeparatorFormat"
                     }
                  ]
               },
               "title": "Elements",
               "type": "array"
            }
         },
         "required": [
            "elements"
         ],
         "title": "OrFormat",
         "type": "object"
      },
      "QwenXMLParameterFormat": {
         "description": "A format that matches Qwen XML function calls.\n\nExamples\n--------\n.. code-block:: python\n\n    structural_tag = QwenXMLParameterFormat(\n        json_schema={\n            \"type\": \"qwen_xml_parameter\",\n            \"json_schema\": {\n                \"type\": \"object\",\n                \"properties\": {\"name\": {\"type\": \"string\"}, \"age\": {\"type\": \"integer\"}},\n                \"required\": [\"name\", \"age\"],\n            },\n        }\n    )\n\nThe above structural tag can accept the following outputs::\n\n    <parameter=name>Bob</parameter><parameter=age>100</parameter>\n    <parameter=name>\"Bob&lt;\"</parameter><parameter=age>100</parameter>",
         "properties": {
            "type": {
               "const": "qwen_xml_parameter",
               "default": "qwen_xml_parameter",
               "title": "Type",
               "type": "string"
            },
            "json_schema": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "additionalProperties": true,
                     "type": "object"
                  }
               ],
               "title": "Json Schema"
            }
         },
         "required": [
            "json_schema"
         ],
         "title": "QwenXMLParameterFormat",
         "type": "object"
      },
      "RegexFormat": {
         "description": "A format that matches a regex pattern.",
         "properties": {
            "type": {
               "const": "regex",
               "default": "regex",
               "title": "Type",
               "type": "string"
            },
            "pattern": {
               "title": "Pattern",
               "type": "string"
            }
         },
         "required": [
            "pattern"
         ],
         "title": "RegexFormat",
         "type": "object"
      },
      "SequenceFormat": {
         "description": "A format that matches a sequence of formats.",
         "properties": {
            "type": {
               "const": "sequence",
               "default": "sequence",
               "title": "Type",
               "type": "string"
            },
            "elements": {
               "items": {
                  "discriminator": {
                     "mapping": {
                        "any_text": "#/$defs/AnyTextFormat",
                        "const_string": "#/$defs/ConstStringFormat",
                        "grammar": "#/$defs/GrammarFormat",
                        "json_schema": "#/$defs/JSONSchemaFormat",
                        "or": "#/$defs/OrFormat",
                        "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                        "regex": "#/$defs/RegexFormat",
                        "sequence": "#/$defs/SequenceFormat",
                        "tag": "#/$defs/TagFormat",
                        "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                        "triggered_tags": "#/$defs/TriggeredTagsFormat"
                     },
                     "propertyName": "type"
                  },
                  "oneOf": [
                     {
                        "$ref": "#/$defs/AnyTextFormat"
                     },
                     {
                        "$ref": "#/$defs/ConstStringFormat"
                     },
                     {
                        "$ref": "#/$defs/JSONSchemaFormat"
                     },
                     {
                        "$ref": "#/$defs/GrammarFormat"
                     },
                     {
                        "$ref": "#/$defs/RegexFormat"
                     },
                     {
                        "$ref": "#/$defs/QwenXMLParameterFormat"
                     },
                     {
                        "$ref": "#/$defs/OrFormat"
                     },
                     {
                        "$ref": "#/$defs/SequenceFormat"
                     },
                     {
                        "$ref": "#/$defs/TagFormat"
                     },
                     {
                        "$ref": "#/$defs/TriggeredTagsFormat"
                     },
                     {
                        "$ref": "#/$defs/TagsWithSeparatorFormat"
                     }
                  ]
               },
               "title": "Elements",
               "type": "array"
            }
         },
         "required": [
            "elements"
         ],
         "title": "SequenceFormat",
         "type": "object"
      },
      "TagFormat": {
         "description": "A format that matches a tag: ``begin content end``.\n\nThe ``end`` field can be a single string or a list of possible end strings.\nWhen multiple end strings are provided, any of them will be accepted as a valid\nending for the tag.\n\nExamples\n--------\n\nSingle end string:\n\n.. code-block:: python\n\n    TagFormat(begin=\"<response>\", content=..., end=\"</response>\")\n\nMultiple end strings:\n\n.. code-block:: python\n\n    TagFormat(begin=\"<response>\", content=..., end=[\"</response>\", \"</answer>\"])",
         "properties": {
            "type": {
               "const": "tag",
               "default": "tag",
               "title": "Type",
               "type": "string"
            },
            "begin": {
               "title": "Begin",
               "type": "string"
            },
            "content": {
               "discriminator": {
                  "mapping": {
                     "any_text": "#/$defs/AnyTextFormat",
                     "const_string": "#/$defs/ConstStringFormat",
                     "grammar": "#/$defs/GrammarFormat",
                     "json_schema": "#/$defs/JSONSchemaFormat",
                     "or": "#/$defs/OrFormat",
                     "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                     "regex": "#/$defs/RegexFormat",
                     "sequence": "#/$defs/SequenceFormat",
                     "tag": "#/$defs/TagFormat",
                     "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                     "triggered_tags": "#/$defs/TriggeredTagsFormat"
                  },
                  "propertyName": "type"
               },
               "oneOf": [
                  {
                     "$ref": "#/$defs/AnyTextFormat"
                  },
                  {
                     "$ref": "#/$defs/ConstStringFormat"
                  },
                  {
                     "$ref": "#/$defs/JSONSchemaFormat"
                  },
                  {
                     "$ref": "#/$defs/GrammarFormat"
                  },
                  {
                     "$ref": "#/$defs/RegexFormat"
                  },
                  {
                     "$ref": "#/$defs/QwenXMLParameterFormat"
                  },
                  {
                     "$ref": "#/$defs/OrFormat"
                  },
                  {
                     "$ref": "#/$defs/SequenceFormat"
                  },
                  {
                     "$ref": "#/$defs/TagFormat"
                  },
                  {
                     "$ref": "#/$defs/TriggeredTagsFormat"
                  },
                  {
                     "$ref": "#/$defs/TagsWithSeparatorFormat"
                  }
               ],
               "title": "Content"
            },
            "end": {
               "anyOf": [
                  {
                     "type": "string"
                  },
                  {
                     "items": {
                        "type": "string"
                     },
                     "type": "array"
                  }
               ],
               "title": "End"
            }
         },
         "required": [
            "begin",
            "content",
            "end"
         ],
         "title": "TagFormat",
         "type": "object"
      },
      "TagsWithSeparatorFormat": {
         "description": "A format that matches a tags with separator. It can match zero, one, or more tags, separated\nby the separator, with no other text allowed.\n\nExamples\n--------\n\n.. code-block:: python\n\n    structural_tag = TagsWithSeparatorFormat(\n        tags=[\n            TagFormat(begin=\"<function=func1>\", content=JSONSchemaFormat(json_schema=...), end=\"</function>\"),\n            TagFormat(begin=\"<function=func2>\", content=JSONSchemaFormat(json_schema=...), end=\"</function>\"),\n        ],\n        separator=\",\",\n        at_least_one=False,\n        stop_after_first=False,\n    )\n\nThe above structural tag can accept an empty string, or the following outputs::\n\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>,<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>,<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>,<function=func1>{\"name\": \"John\", \"age\": 30}</function>",
         "properties": {
            "type": {
               "const": "tags_with_separator",
               "default": "tags_with_separator",
               "title": "Type",
               "type": "string"
            },
            "tags": {
               "items": {
                  "$ref": "#/$defs/TagFormat"
               },
               "title": "Tags",
               "type": "array"
            },
            "separator": {
               "title": "Separator",
               "type": "string"
            },
            "at_least_one": {
               "default": false,
               "title": "At Least One",
               "type": "boolean"
            },
            "stop_after_first": {
               "default": false,
               "title": "Stop After First",
               "type": "boolean"
            }
         },
         "required": [
            "tags",
            "separator"
         ],
         "title": "TagsWithSeparatorFormat",
         "type": "object"
      },
      "TriggeredTagsFormat": {
         "description": "A format that matches triggered tags. It can allow any output until a trigger is\nencountered, then dispatch to the corresponding tag; when the end tag is encountered, the\ngrammar will allow any following output, until the next trigger is encountered.\n\nEach tag should be matched by exactly one trigger. \"matching\" means the trigger should be a\nprefix of the begin tag.\n\nExamples\n--------\n\n.. code-block:: python\n\n    structural_tag = TriggeredTagsFormat(\n        triggers=[\"<function=\"],\n        tags=[\n            TagFormat(\n                begin=\"<function=func1>\",\n                content=JSONSchemaFormat(json_schema=...),\n                end=\"</function>\",\n            ),\n            TagFormat(\n                begin=\"<function=func2>\",\n                content=JSONSchemaFormat(json_schema=...),\n                end=\"</function>\",\n            ),\n        ],\n        at_least_one=False,\n        stop_after_first=False,\n    )\n\nThe above structural tag can accept the following outputs::\n\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>\n    <function=func2>{\"name\": \"Jane\", \"age\": 25}</function>\n    any_text<function=func1>{\"name\": \"John\", \"age\": 30}</function>any_text1<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>any_text2",
         "properties": {
            "type": {
               "const": "triggered_tags",
               "default": "triggered_tags",
               "title": "Type",
               "type": "string"
            },
            "triggers": {
               "items": {
                  "type": "string"
               },
               "title": "Triggers",
               "type": "array"
            },
            "tags": {
               "items": {
                  "$ref": "#/$defs/TagFormat"
               },
               "title": "Tags",
               "type": "array"
            },
            "at_least_one": {
               "default": false,
               "title": "At Least One",
               "type": "boolean"
            },
            "stop_after_first": {
               "default": false,
               "title": "Stop After First",
               "type": "boolean"
            },
            "excludes": {
               "default": [],
               "items": {
                  "type": "string"
               },
               "title": "Excludes",
               "type": "array"
            }
         },
         "required": [
            "triggers",
            "tags"
         ],
         "title": "TriggeredTagsFormat",
         "type": "object"
      }
   },
   "$ref": "#/$defs/SequenceFormat"
}

field type: Literal['sequence'] = 'sequence'

The type of the format.

field elements: List[Format] [Required]

The elements of the sequence.

pydantic model xgrammar.structural_tag.OrFormat[source]

Bases: BaseModel

A format that matches one of the formats.

Show JSON schema
{
   "$defs": {
      "AnyTextFormat": {
         "description": "A format that matches any text.",
         "properties": {
            "type": {
               "const": "any_text",
               "default": "any_text",
               "title": "Type",
               "type": "string"
            },
            "excludes": {
               "default": [],
               "items": {
                  "type": "string"
               },
               "title": "Excludes",
               "type": "array"
            }
         },
         "title": "AnyTextFormat",
         "type": "object"
      },
      "ConstStringFormat": {
         "description": "A format that matches a constant string.",
         "properties": {
            "type": {
               "const": "const_string",
               "default": "const_string",
               "title": "Type",
               "type": "string"
            },
            "value": {
               "title": "Value",
               "type": "string"
            }
         },
         "required": [
            "value"
         ],
         "title": "ConstStringFormat",
         "type": "object"
      },
      "GrammarFormat": {
         "description": "A format that matches an ebnf grammar.",
         "properties": {
            "type": {
               "const": "grammar",
               "default": "grammar",
               "title": "Type",
               "type": "string"
            },
            "grammar": {
               "title": "Grammar",
               "type": "string"
            }
         },
         "required": [
            "grammar"
         ],
         "title": "GrammarFormat",
         "type": "object"
      },
      "JSONSchemaFormat": {
         "description": "A format that matches a JSON schema.",
         "properties": {
            "type": {
               "const": "json_schema",
               "default": "json_schema",
               "title": "Type",
               "type": "string"
            },
            "json_schema": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "additionalProperties": true,
                     "type": "object"
                  }
               ],
               "title": "Json Schema"
            }
         },
         "required": [
            "json_schema"
         ],
         "title": "JSONSchemaFormat",
         "type": "object"
      },
      "OrFormat": {
         "description": "A format that matches one of the formats.",
         "properties": {
            "type": {
               "const": "or",
               "default": "or",
               "title": "Type",
               "type": "string"
            },
            "elements": {
               "items": {
                  "discriminator": {
                     "mapping": {
                        "any_text": "#/$defs/AnyTextFormat",
                        "const_string": "#/$defs/ConstStringFormat",
                        "grammar": "#/$defs/GrammarFormat",
                        "json_schema": "#/$defs/JSONSchemaFormat",
                        "or": "#/$defs/OrFormat",
                        "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                        "regex": "#/$defs/RegexFormat",
                        "sequence": "#/$defs/SequenceFormat",
                        "tag": "#/$defs/TagFormat",
                        "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                        "triggered_tags": "#/$defs/TriggeredTagsFormat"
                     },
                     "propertyName": "type"
                  },
                  "oneOf": [
                     {
                        "$ref": "#/$defs/AnyTextFormat"
                     },
                     {
                        "$ref": "#/$defs/ConstStringFormat"
                     },
                     {
                        "$ref": "#/$defs/JSONSchemaFormat"
                     },
                     {
                        "$ref": "#/$defs/GrammarFormat"
                     },
                     {
                        "$ref": "#/$defs/RegexFormat"
                     },
                     {
                        "$ref": "#/$defs/QwenXMLParameterFormat"
                     },
                     {
                        "$ref": "#/$defs/OrFormat"
                     },
                     {
                        "$ref": "#/$defs/SequenceFormat"
                     },
                     {
                        "$ref": "#/$defs/TagFormat"
                     },
                     {
                        "$ref": "#/$defs/TriggeredTagsFormat"
                     },
                     {
                        "$ref": "#/$defs/TagsWithSeparatorFormat"
                     }
                  ]
               },
               "title": "Elements",
               "type": "array"
            }
         },
         "required": [
            "elements"
         ],
         "title": "OrFormat",
         "type": "object"
      },
      "QwenXMLParameterFormat": {
         "description": "A format that matches Qwen XML function calls.\n\nExamples\n--------\n.. code-block:: python\n\n    structural_tag = QwenXMLParameterFormat(\n        json_schema={\n            \"type\": \"qwen_xml_parameter\",\n            \"json_schema\": {\n                \"type\": \"object\",\n                \"properties\": {\"name\": {\"type\": \"string\"}, \"age\": {\"type\": \"integer\"}},\n                \"required\": [\"name\", \"age\"],\n            },\n        }\n    )\n\nThe above structural tag can accept the following outputs::\n\n    <parameter=name>Bob</parameter><parameter=age>100</parameter>\n    <parameter=name>\"Bob&lt;\"</parameter><parameter=age>100</parameter>",
         "properties": {
            "type": {
               "const": "qwen_xml_parameter",
               "default": "qwen_xml_parameter",
               "title": "Type",
               "type": "string"
            },
            "json_schema": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "additionalProperties": true,
                     "type": "object"
                  }
               ],
               "title": "Json Schema"
            }
         },
         "required": [
            "json_schema"
         ],
         "title": "QwenXMLParameterFormat",
         "type": "object"
      },
      "RegexFormat": {
         "description": "A format that matches a regex pattern.",
         "properties": {
            "type": {
               "const": "regex",
               "default": "regex",
               "title": "Type",
               "type": "string"
            },
            "pattern": {
               "title": "Pattern",
               "type": "string"
            }
         },
         "required": [
            "pattern"
         ],
         "title": "RegexFormat",
         "type": "object"
      },
      "SequenceFormat": {
         "description": "A format that matches a sequence of formats.",
         "properties": {
            "type": {
               "const": "sequence",
               "default": "sequence",
               "title": "Type",
               "type": "string"
            },
            "elements": {
               "items": {
                  "discriminator": {
                     "mapping": {
                        "any_text": "#/$defs/AnyTextFormat",
                        "const_string": "#/$defs/ConstStringFormat",
                        "grammar": "#/$defs/GrammarFormat",
                        "json_schema": "#/$defs/JSONSchemaFormat",
                        "or": "#/$defs/OrFormat",
                        "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                        "regex": "#/$defs/RegexFormat",
                        "sequence": "#/$defs/SequenceFormat",
                        "tag": "#/$defs/TagFormat",
                        "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                        "triggered_tags": "#/$defs/TriggeredTagsFormat"
                     },
                     "propertyName": "type"
                  },
                  "oneOf": [
                     {
                        "$ref": "#/$defs/AnyTextFormat"
                     },
                     {
                        "$ref": "#/$defs/ConstStringFormat"
                     },
                     {
                        "$ref": "#/$defs/JSONSchemaFormat"
                     },
                     {
                        "$ref": "#/$defs/GrammarFormat"
                     },
                     {
                        "$ref": "#/$defs/RegexFormat"
                     },
                     {
                        "$ref": "#/$defs/QwenXMLParameterFormat"
                     },
                     {
                        "$ref": "#/$defs/OrFormat"
                     },
                     {
                        "$ref": "#/$defs/SequenceFormat"
                     },
                     {
                        "$ref": "#/$defs/TagFormat"
                     },
                     {
                        "$ref": "#/$defs/TriggeredTagsFormat"
                     },
                     {
                        "$ref": "#/$defs/TagsWithSeparatorFormat"
                     }
                  ]
               },
               "title": "Elements",
               "type": "array"
            }
         },
         "required": [
            "elements"
         ],
         "title": "SequenceFormat",
         "type": "object"
      },
      "TagFormat": {
         "description": "A format that matches a tag: ``begin content end``.\n\nThe ``end`` field can be a single string or a list of possible end strings.\nWhen multiple end strings are provided, any of them will be accepted as a valid\nending for the tag.\n\nExamples\n--------\n\nSingle end string:\n\n.. code-block:: python\n\n    TagFormat(begin=\"<response>\", content=..., end=\"</response>\")\n\nMultiple end strings:\n\n.. code-block:: python\n\n    TagFormat(begin=\"<response>\", content=..., end=[\"</response>\", \"</answer>\"])",
         "properties": {
            "type": {
               "const": "tag",
               "default": "tag",
               "title": "Type",
               "type": "string"
            },
            "begin": {
               "title": "Begin",
               "type": "string"
            },
            "content": {
               "discriminator": {
                  "mapping": {
                     "any_text": "#/$defs/AnyTextFormat",
                     "const_string": "#/$defs/ConstStringFormat",
                     "grammar": "#/$defs/GrammarFormat",
                     "json_schema": "#/$defs/JSONSchemaFormat",
                     "or": "#/$defs/OrFormat",
                     "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                     "regex": "#/$defs/RegexFormat",
                     "sequence": "#/$defs/SequenceFormat",
                     "tag": "#/$defs/TagFormat",
                     "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                     "triggered_tags": "#/$defs/TriggeredTagsFormat"
                  },
                  "propertyName": "type"
               },
               "oneOf": [
                  {
                     "$ref": "#/$defs/AnyTextFormat"
                  },
                  {
                     "$ref": "#/$defs/ConstStringFormat"
                  },
                  {
                     "$ref": "#/$defs/JSONSchemaFormat"
                  },
                  {
                     "$ref": "#/$defs/GrammarFormat"
                  },
                  {
                     "$ref": "#/$defs/RegexFormat"
                  },
                  {
                     "$ref": "#/$defs/QwenXMLParameterFormat"
                  },
                  {
                     "$ref": "#/$defs/OrFormat"
                  },
                  {
                     "$ref": "#/$defs/SequenceFormat"
                  },
                  {
                     "$ref": "#/$defs/TagFormat"
                  },
                  {
                     "$ref": "#/$defs/TriggeredTagsFormat"
                  },
                  {
                     "$ref": "#/$defs/TagsWithSeparatorFormat"
                  }
               ],
               "title": "Content"
            },
            "end": {
               "anyOf": [
                  {
                     "type": "string"
                  },
                  {
                     "items": {
                        "type": "string"
                     },
                     "type": "array"
                  }
               ],
               "title": "End"
            }
         },
         "required": [
            "begin",
            "content",
            "end"
         ],
         "title": "TagFormat",
         "type": "object"
      },
      "TagsWithSeparatorFormat": {
         "description": "A format that matches a tags with separator. It can match zero, one, or more tags, separated\nby the separator, with no other text allowed.\n\nExamples\n--------\n\n.. code-block:: python\n\n    structural_tag = TagsWithSeparatorFormat(\n        tags=[\n            TagFormat(begin=\"<function=func1>\", content=JSONSchemaFormat(json_schema=...), end=\"</function>\"),\n            TagFormat(begin=\"<function=func2>\", content=JSONSchemaFormat(json_schema=...), end=\"</function>\"),\n        ],\n        separator=\",\",\n        at_least_one=False,\n        stop_after_first=False,\n    )\n\nThe above structural tag can accept an empty string, or the following outputs::\n\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>,<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>,<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>,<function=func1>{\"name\": \"John\", \"age\": 30}</function>",
         "properties": {
            "type": {
               "const": "tags_with_separator",
               "default": "tags_with_separator",
               "title": "Type",
               "type": "string"
            },
            "tags": {
               "items": {
                  "$ref": "#/$defs/TagFormat"
               },
               "title": "Tags",
               "type": "array"
            },
            "separator": {
               "title": "Separator",
               "type": "string"
            },
            "at_least_one": {
               "default": false,
               "title": "At Least One",
               "type": "boolean"
            },
            "stop_after_first": {
               "default": false,
               "title": "Stop After First",
               "type": "boolean"
            }
         },
         "required": [
            "tags",
            "separator"
         ],
         "title": "TagsWithSeparatorFormat",
         "type": "object"
      },
      "TriggeredTagsFormat": {
         "description": "A format that matches triggered tags. It can allow any output until a trigger is\nencountered, then dispatch to the corresponding tag; when the end tag is encountered, the\ngrammar will allow any following output, until the next trigger is encountered.\n\nEach tag should be matched by exactly one trigger. \"matching\" means the trigger should be a\nprefix of the begin tag.\n\nExamples\n--------\n\n.. code-block:: python\n\n    structural_tag = TriggeredTagsFormat(\n        triggers=[\"<function=\"],\n        tags=[\n            TagFormat(\n                begin=\"<function=func1>\",\n                content=JSONSchemaFormat(json_schema=...),\n                end=\"</function>\",\n            ),\n            TagFormat(\n                begin=\"<function=func2>\",\n                content=JSONSchemaFormat(json_schema=...),\n                end=\"</function>\",\n            ),\n        ],\n        at_least_one=False,\n        stop_after_first=False,\n    )\n\nThe above structural tag can accept the following outputs::\n\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>\n    <function=func2>{\"name\": \"Jane\", \"age\": 25}</function>\n    any_text<function=func1>{\"name\": \"John\", \"age\": 30}</function>any_text1<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>any_text2",
         "properties": {
            "type": {
               "const": "triggered_tags",
               "default": "triggered_tags",
               "title": "Type",
               "type": "string"
            },
            "triggers": {
               "items": {
                  "type": "string"
               },
               "title": "Triggers",
               "type": "array"
            },
            "tags": {
               "items": {
                  "$ref": "#/$defs/TagFormat"
               },
               "title": "Tags",
               "type": "array"
            },
            "at_least_one": {
               "default": false,
               "title": "At Least One",
               "type": "boolean"
            },
            "stop_after_first": {
               "default": false,
               "title": "Stop After First",
               "type": "boolean"
            },
            "excludes": {
               "default": [],
               "items": {
                  "type": "string"
               },
               "title": "Excludes",
               "type": "array"
            }
         },
         "required": [
            "triggers",
            "tags"
         ],
         "title": "TriggeredTagsFormat",
         "type": "object"
      }
   },
   "$ref": "#/$defs/OrFormat"
}

field type: Literal['or'] = 'or'

The type of the format.

field elements: List[Format] [Required]

The elements of the or.

pydantic model xgrammar.structural_tag.TagFormat[source]

Bases: BaseModel

A format that matches a tag: begin content end.

The end field can be a single string or a list of possible end strings. When multiple end strings are provided, any of them will be accepted as a valid ending for the tag.

Examples

Single end string:

TagFormat(begin="<response>", content=..., end="</response>")

Multiple end strings:

TagFormat(begin="<response>", content=..., end=["</response>", "</answer>"])

Show JSON schema
{
   "$defs": {
      "AnyTextFormat": {
         "description": "A format that matches any text.",
         "properties": {
            "type": {
               "const": "any_text",
               "default": "any_text",
               "title": "Type",
               "type": "string"
            },
            "excludes": {
               "default": [],
               "items": {
                  "type": "string"
               },
               "title": "Excludes",
               "type": "array"
            }
         },
         "title": "AnyTextFormat",
         "type": "object"
      },
      "ConstStringFormat": {
         "description": "A format that matches a constant string.",
         "properties": {
            "type": {
               "const": "const_string",
               "default": "const_string",
               "title": "Type",
               "type": "string"
            },
            "value": {
               "title": "Value",
               "type": "string"
            }
         },
         "required": [
            "value"
         ],
         "title": "ConstStringFormat",
         "type": "object"
      },
      "GrammarFormat": {
         "description": "A format that matches an ebnf grammar.",
         "properties": {
            "type": {
               "const": "grammar",
               "default": "grammar",
               "title": "Type",
               "type": "string"
            },
            "grammar": {
               "title": "Grammar",
               "type": "string"
            }
         },
         "required": [
            "grammar"
         ],
         "title": "GrammarFormat",
         "type": "object"
      },
      "JSONSchemaFormat": {
         "description": "A format that matches a JSON schema.",
         "properties": {
            "type": {
               "const": "json_schema",
               "default": "json_schema",
               "title": "Type",
               "type": "string"
            },
            "json_schema": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "additionalProperties": true,
                     "type": "object"
                  }
               ],
               "title": "Json Schema"
            }
         },
         "required": [
            "json_schema"
         ],
         "title": "JSONSchemaFormat",
         "type": "object"
      },
      "OrFormat": {
         "description": "A format that matches one of the formats.",
         "properties": {
            "type": {
               "const": "or",
               "default": "or",
               "title": "Type",
               "type": "string"
            },
            "elements": {
               "items": {
                  "discriminator": {
                     "mapping": {
                        "any_text": "#/$defs/AnyTextFormat",
                        "const_string": "#/$defs/ConstStringFormat",
                        "grammar": "#/$defs/GrammarFormat",
                        "json_schema": "#/$defs/JSONSchemaFormat",
                        "or": "#/$defs/OrFormat",
                        "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                        "regex": "#/$defs/RegexFormat",
                        "sequence": "#/$defs/SequenceFormat",
                        "tag": "#/$defs/TagFormat",
                        "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                        "triggered_tags": "#/$defs/TriggeredTagsFormat"
                     },
                     "propertyName": "type"
                  },
                  "oneOf": [
                     {
                        "$ref": "#/$defs/AnyTextFormat"
                     },
                     {
                        "$ref": "#/$defs/ConstStringFormat"
                     },
                     {
                        "$ref": "#/$defs/JSONSchemaFormat"
                     },
                     {
                        "$ref": "#/$defs/GrammarFormat"
                     },
                     {
                        "$ref": "#/$defs/RegexFormat"
                     },
                     {
                        "$ref": "#/$defs/QwenXMLParameterFormat"
                     },
                     {
                        "$ref": "#/$defs/OrFormat"
                     },
                     {
                        "$ref": "#/$defs/SequenceFormat"
                     },
                     {
                        "$ref": "#/$defs/TagFormat"
                     },
                     {
                        "$ref": "#/$defs/TriggeredTagsFormat"
                     },
                     {
                        "$ref": "#/$defs/TagsWithSeparatorFormat"
                     }
                  ]
               },
               "title": "Elements",
               "type": "array"
            }
         },
         "required": [
            "elements"
         ],
         "title": "OrFormat",
         "type": "object"
      },
      "QwenXMLParameterFormat": {
         "description": "A format that matches Qwen XML function calls.\n\nExamples\n--------\n.. code-block:: python\n\n    structural_tag = QwenXMLParameterFormat(\n        json_schema={\n            \"type\": \"qwen_xml_parameter\",\n            \"json_schema\": {\n                \"type\": \"object\",\n                \"properties\": {\"name\": {\"type\": \"string\"}, \"age\": {\"type\": \"integer\"}},\n                \"required\": [\"name\", \"age\"],\n            },\n        }\n    )\n\nThe above structural tag can accept the following outputs::\n\n    <parameter=name>Bob</parameter><parameter=age>100</parameter>\n    <parameter=name>\"Bob&lt;\"</parameter><parameter=age>100</parameter>",
         "properties": {
            "type": {
               "const": "qwen_xml_parameter",
               "default": "qwen_xml_parameter",
               "title": "Type",
               "type": "string"
            },
            "json_schema": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "additionalProperties": true,
                     "type": "object"
                  }
               ],
               "title": "Json Schema"
            }
         },
         "required": [
            "json_schema"
         ],
         "title": "QwenXMLParameterFormat",
         "type": "object"
      },
      "RegexFormat": {
         "description": "A format that matches a regex pattern.",
         "properties": {
            "type": {
               "const": "regex",
               "default": "regex",
               "title": "Type",
               "type": "string"
            },
            "pattern": {
               "title": "Pattern",
               "type": "string"
            }
         },
         "required": [
            "pattern"
         ],
         "title": "RegexFormat",
         "type": "object"
      },
      "SequenceFormat": {
         "description": "A format that matches a sequence of formats.",
         "properties": {
            "type": {
               "const": "sequence",
               "default": "sequence",
               "title": "Type",
               "type": "string"
            },
            "elements": {
               "items": {
                  "discriminator": {
                     "mapping": {
                        "any_text": "#/$defs/AnyTextFormat",
                        "const_string": "#/$defs/ConstStringFormat",
                        "grammar": "#/$defs/GrammarFormat",
                        "json_schema": "#/$defs/JSONSchemaFormat",
                        "or": "#/$defs/OrFormat",
                        "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                        "regex": "#/$defs/RegexFormat",
                        "sequence": "#/$defs/SequenceFormat",
                        "tag": "#/$defs/TagFormat",
                        "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                        "triggered_tags": "#/$defs/TriggeredTagsFormat"
                     },
                     "propertyName": "type"
                  },
                  "oneOf": [
                     {
                        "$ref": "#/$defs/AnyTextFormat"
                     },
                     {
                        "$ref": "#/$defs/ConstStringFormat"
                     },
                     {
                        "$ref": "#/$defs/JSONSchemaFormat"
                     },
                     {
                        "$ref": "#/$defs/GrammarFormat"
                     },
                     {
                        "$ref": "#/$defs/RegexFormat"
                     },
                     {
                        "$ref": "#/$defs/QwenXMLParameterFormat"
                     },
                     {
                        "$ref": "#/$defs/OrFormat"
                     },
                     {
                        "$ref": "#/$defs/SequenceFormat"
                     },
                     {
                        "$ref": "#/$defs/TagFormat"
                     },
                     {
                        "$ref": "#/$defs/TriggeredTagsFormat"
                     },
                     {
                        "$ref": "#/$defs/TagsWithSeparatorFormat"
                     }
                  ]
               },
               "title": "Elements",
               "type": "array"
            }
         },
         "required": [
            "elements"
         ],
         "title": "SequenceFormat",
         "type": "object"
      },
      "TagFormat": {
         "description": "A format that matches a tag: ``begin content end``.\n\nThe ``end`` field can be a single string or a list of possible end strings.\nWhen multiple end strings are provided, any of them will be accepted as a valid\nending for the tag.\n\nExamples\n--------\n\nSingle end string:\n\n.. code-block:: python\n\n    TagFormat(begin=\"<response>\", content=..., end=\"</response>\")\n\nMultiple end strings:\n\n.. code-block:: python\n\n    TagFormat(begin=\"<response>\", content=..., end=[\"</response>\", \"</answer>\"])",
         "properties": {
            "type": {
               "const": "tag",
               "default": "tag",
               "title": "Type",
               "type": "string"
            },
            "begin": {
               "title": "Begin",
               "type": "string"
            },
            "content": {
               "discriminator": {
                  "mapping": {
                     "any_text": "#/$defs/AnyTextFormat",
                     "const_string": "#/$defs/ConstStringFormat",
                     "grammar": "#/$defs/GrammarFormat",
                     "json_schema": "#/$defs/JSONSchemaFormat",
                     "or": "#/$defs/OrFormat",
                     "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                     "regex": "#/$defs/RegexFormat",
                     "sequence": "#/$defs/SequenceFormat",
                     "tag": "#/$defs/TagFormat",
                     "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                     "triggered_tags": "#/$defs/TriggeredTagsFormat"
                  },
                  "propertyName": "type"
               },
               "oneOf": [
                  {
                     "$ref": "#/$defs/AnyTextFormat"
                  },
                  {
                     "$ref": "#/$defs/ConstStringFormat"
                  },
                  {
                     "$ref": "#/$defs/JSONSchemaFormat"
                  },
                  {
                     "$ref": "#/$defs/GrammarFormat"
                  },
                  {
                     "$ref": "#/$defs/RegexFormat"
                  },
                  {
                     "$ref": "#/$defs/QwenXMLParameterFormat"
                  },
                  {
                     "$ref": "#/$defs/OrFormat"
                  },
                  {
                     "$ref": "#/$defs/SequenceFormat"
                  },
                  {
                     "$ref": "#/$defs/TagFormat"
                  },
                  {
                     "$ref": "#/$defs/TriggeredTagsFormat"
                  },
                  {
                     "$ref": "#/$defs/TagsWithSeparatorFormat"
                  }
               ],
               "title": "Content"
            },
            "end": {
               "anyOf": [
                  {
                     "type": "string"
                  },
                  {
                     "items": {
                        "type": "string"
                     },
                     "type": "array"
                  }
               ],
               "title": "End"
            }
         },
         "required": [
            "begin",
            "content",
            "end"
         ],
         "title": "TagFormat",
         "type": "object"
      },
      "TagsWithSeparatorFormat": {
         "description": "A format that matches a tags with separator. It can match zero, one, or more tags, separated\nby the separator, with no other text allowed.\n\nExamples\n--------\n\n.. code-block:: python\n\n    structural_tag = TagsWithSeparatorFormat(\n        tags=[\n            TagFormat(begin=\"<function=func1>\", content=JSONSchemaFormat(json_schema=...), end=\"</function>\"),\n            TagFormat(begin=\"<function=func2>\", content=JSONSchemaFormat(json_schema=...), end=\"</function>\"),\n        ],\n        separator=\",\",\n        at_least_one=False,\n        stop_after_first=False,\n    )\n\nThe above structural tag can accept an empty string, or the following outputs::\n\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>,<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>,<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>,<function=func1>{\"name\": \"John\", \"age\": 30}</function>",
         "properties": {
            "type": {
               "const": "tags_with_separator",
               "default": "tags_with_separator",
               "title": "Type",
               "type": "string"
            },
            "tags": {
               "items": {
                  "$ref": "#/$defs/TagFormat"
               },
               "title": "Tags",
               "type": "array"
            },
            "separator": {
               "title": "Separator",
               "type": "string"
            },
            "at_least_one": {
               "default": false,
               "title": "At Least One",
               "type": "boolean"
            },
            "stop_after_first": {
               "default": false,
               "title": "Stop After First",
               "type": "boolean"
            }
         },
         "required": [
            "tags",
            "separator"
         ],
         "title": "TagsWithSeparatorFormat",
         "type": "object"
      },
      "TriggeredTagsFormat": {
         "description": "A format that matches triggered tags. It can allow any output until a trigger is\nencountered, then dispatch to the corresponding tag; when the end tag is encountered, the\ngrammar will allow any following output, until the next trigger is encountered.\n\nEach tag should be matched by exactly one trigger. \"matching\" means the trigger should be a\nprefix of the begin tag.\n\nExamples\n--------\n\n.. code-block:: python\n\n    structural_tag = TriggeredTagsFormat(\n        triggers=[\"<function=\"],\n        tags=[\n            TagFormat(\n                begin=\"<function=func1>\",\n                content=JSONSchemaFormat(json_schema=...),\n                end=\"</function>\",\n            ),\n            TagFormat(\n                begin=\"<function=func2>\",\n                content=JSONSchemaFormat(json_schema=...),\n                end=\"</function>\",\n            ),\n        ],\n        at_least_one=False,\n        stop_after_first=False,\n    )\n\nThe above structural tag can accept the following outputs::\n\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>\n    <function=func2>{\"name\": \"Jane\", \"age\": 25}</function>\n    any_text<function=func1>{\"name\": \"John\", \"age\": 30}</function>any_text1<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>any_text2",
         "properties": {
            "type": {
               "const": "triggered_tags",
               "default": "triggered_tags",
               "title": "Type",
               "type": "string"
            },
            "triggers": {
               "items": {
                  "type": "string"
               },
               "title": "Triggers",
               "type": "array"
            },
            "tags": {
               "items": {
                  "$ref": "#/$defs/TagFormat"
               },
               "title": "Tags",
               "type": "array"
            },
            "at_least_one": {
               "default": false,
               "title": "At Least One",
               "type": "boolean"
            },
            "stop_after_first": {
               "default": false,
               "title": "Stop After First",
               "type": "boolean"
            },
            "excludes": {
               "default": [],
               "items": {
                  "type": "string"
               },
               "title": "Excludes",
               "type": "array"
            }
         },
         "required": [
            "triggers",
            "tags"
         ],
         "title": "TriggeredTagsFormat",
         "type": "object"
      }
   },
   "$ref": "#/$defs/TagFormat"
}

field type: Literal['tag'] = 'tag'

The type of the format.

field begin: str [Required]

The begin tag.

field content: Format [Required]

The content of the tag. It can be any of the formats.

field end: Union[str, List[str]] [Required]

The end tag(s). Can be a single string or a list of possible end strings.

pydantic model xgrammar.structural_tag.TriggeredTagsFormat[source]

Bases: BaseModel

A format that matches triggered tags. It can allow any output until a trigger is encountered, then dispatch to the corresponding tag; when the end tag is encountered, the grammar will allow any following output, until the next trigger is encountered.

Each tag should be matched by exactly one trigger. “matching” means the trigger should be a prefix of the begin tag.

Examples

structural_tag = TriggeredTagsFormat(
    triggers=["<function="],
    tags=[
        TagFormat(
            begin="<function=func1>",
            content=JSONSchemaFormat(json_schema=...),
            end="</function>",
        ),
        TagFormat(
            begin="<function=func2>",
            content=JSONSchemaFormat(json_schema=...),
            end="</function>",
        ),
    ],
    at_least_one=False,
    stop_after_first=False,
)

The above structural tag can accept the following outputs:

<function=func1>{"name": "John", "age": 30}</function>
<function=func2>{"name": "Jane", "age": 25}</function>
any_text<function=func1>{"name": "John", "age": 30}</function>any_text1<function=func2>{"name": "Jane", "age": 25}</function>any_text2

Show JSON schema
{
   "$defs": {
      "AnyTextFormat": {
         "description": "A format that matches any text.",
         "properties": {
            "type": {
               "const": "any_text",
               "default": "any_text",
               "title": "Type",
               "type": "string"
            },
            "excludes": {
               "default": [],
               "items": {
                  "type": "string"
               },
               "title": "Excludes",
               "type": "array"
            }
         },
         "title": "AnyTextFormat",
         "type": "object"
      },
      "ConstStringFormat": {
         "description": "A format that matches a constant string.",
         "properties": {
            "type": {
               "const": "const_string",
               "default": "const_string",
               "title": "Type",
               "type": "string"
            },
            "value": {
               "title": "Value",
               "type": "string"
            }
         },
         "required": [
            "value"
         ],
         "title": "ConstStringFormat",
         "type": "object"
      },
      "GrammarFormat": {
         "description": "A format that matches an ebnf grammar.",
         "properties": {
            "type": {
               "const": "grammar",
               "default": "grammar",
               "title": "Type",
               "type": "string"
            },
            "grammar": {
               "title": "Grammar",
               "type": "string"
            }
         },
         "required": [
            "grammar"
         ],
         "title": "GrammarFormat",
         "type": "object"
      },
      "JSONSchemaFormat": {
         "description": "A format that matches a JSON schema.",
         "properties": {
            "type": {
               "const": "json_schema",
               "default": "json_schema",
               "title": "Type",
               "type": "string"
            },
            "json_schema": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "additionalProperties": true,
                     "type": "object"
                  }
               ],
               "title": "Json Schema"
            }
         },
         "required": [
            "json_schema"
         ],
         "title": "JSONSchemaFormat",
         "type": "object"
      },
      "OrFormat": {
         "description": "A format that matches one of the formats.",
         "properties": {
            "type": {
               "const": "or",
               "default": "or",
               "title": "Type",
               "type": "string"
            },
            "elements": {
               "items": {
                  "discriminator": {
                     "mapping": {
                        "any_text": "#/$defs/AnyTextFormat",
                        "const_string": "#/$defs/ConstStringFormat",
                        "grammar": "#/$defs/GrammarFormat",
                        "json_schema": "#/$defs/JSONSchemaFormat",
                        "or": "#/$defs/OrFormat",
                        "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                        "regex": "#/$defs/RegexFormat",
                        "sequence": "#/$defs/SequenceFormat",
                        "tag": "#/$defs/TagFormat",
                        "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                        "triggered_tags": "#/$defs/TriggeredTagsFormat"
                     },
                     "propertyName": "type"
                  },
                  "oneOf": [
                     {
                        "$ref": "#/$defs/AnyTextFormat"
                     },
                     {
                        "$ref": "#/$defs/ConstStringFormat"
                     },
                     {
                        "$ref": "#/$defs/JSONSchemaFormat"
                     },
                     {
                        "$ref": "#/$defs/GrammarFormat"
                     },
                     {
                        "$ref": "#/$defs/RegexFormat"
                     },
                     {
                        "$ref": "#/$defs/QwenXMLParameterFormat"
                     },
                     {
                        "$ref": "#/$defs/OrFormat"
                     },
                     {
                        "$ref": "#/$defs/SequenceFormat"
                     },
                     {
                        "$ref": "#/$defs/TagFormat"
                     },
                     {
                        "$ref": "#/$defs/TriggeredTagsFormat"
                     },
                     {
                        "$ref": "#/$defs/TagsWithSeparatorFormat"
                     }
                  ]
               },
               "title": "Elements",
               "type": "array"
            }
         },
         "required": [
            "elements"
         ],
         "title": "OrFormat",
         "type": "object"
      },
      "QwenXMLParameterFormat": {
         "description": "A format that matches Qwen XML function calls.\n\nExamples\n--------\n.. code-block:: python\n\n    structural_tag = QwenXMLParameterFormat(\n        json_schema={\n            \"type\": \"qwen_xml_parameter\",\n            \"json_schema\": {\n                \"type\": \"object\",\n                \"properties\": {\"name\": {\"type\": \"string\"}, \"age\": {\"type\": \"integer\"}},\n                \"required\": [\"name\", \"age\"],\n            },\n        }\n    )\n\nThe above structural tag can accept the following outputs::\n\n    <parameter=name>Bob</parameter><parameter=age>100</parameter>\n    <parameter=name>\"Bob&lt;\"</parameter><parameter=age>100</parameter>",
         "properties": {
            "type": {
               "const": "qwen_xml_parameter",
               "default": "qwen_xml_parameter",
               "title": "Type",
               "type": "string"
            },
            "json_schema": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "additionalProperties": true,
                     "type": "object"
                  }
               ],
               "title": "Json Schema"
            }
         },
         "required": [
            "json_schema"
         ],
         "title": "QwenXMLParameterFormat",
         "type": "object"
      },
      "RegexFormat": {
         "description": "A format that matches a regex pattern.",
         "properties": {
            "type": {
               "const": "regex",
               "default": "regex",
               "title": "Type",
               "type": "string"
            },
            "pattern": {
               "title": "Pattern",
               "type": "string"
            }
         },
         "required": [
            "pattern"
         ],
         "title": "RegexFormat",
         "type": "object"
      },
      "SequenceFormat": {
         "description": "A format that matches a sequence of formats.",
         "properties": {
            "type": {
               "const": "sequence",
               "default": "sequence",
               "title": "Type",
               "type": "string"
            },
            "elements": {
               "items": {
                  "discriminator": {
                     "mapping": {
                        "any_text": "#/$defs/AnyTextFormat",
                        "const_string": "#/$defs/ConstStringFormat",
                        "grammar": "#/$defs/GrammarFormat",
                        "json_schema": "#/$defs/JSONSchemaFormat",
                        "or": "#/$defs/OrFormat",
                        "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                        "regex": "#/$defs/RegexFormat",
                        "sequence": "#/$defs/SequenceFormat",
                        "tag": "#/$defs/TagFormat",
                        "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                        "triggered_tags": "#/$defs/TriggeredTagsFormat"
                     },
                     "propertyName": "type"
                  },
                  "oneOf": [
                     {
                        "$ref": "#/$defs/AnyTextFormat"
                     },
                     {
                        "$ref": "#/$defs/ConstStringFormat"
                     },
                     {
                        "$ref": "#/$defs/JSONSchemaFormat"
                     },
                     {
                        "$ref": "#/$defs/GrammarFormat"
                     },
                     {
                        "$ref": "#/$defs/RegexFormat"
                     },
                     {
                        "$ref": "#/$defs/QwenXMLParameterFormat"
                     },
                     {
                        "$ref": "#/$defs/OrFormat"
                     },
                     {
                        "$ref": "#/$defs/SequenceFormat"
                     },
                     {
                        "$ref": "#/$defs/TagFormat"
                     },
                     {
                        "$ref": "#/$defs/TriggeredTagsFormat"
                     },
                     {
                        "$ref": "#/$defs/TagsWithSeparatorFormat"
                     }
                  ]
               },
               "title": "Elements",
               "type": "array"
            }
         },
         "required": [
            "elements"
         ],
         "title": "SequenceFormat",
         "type": "object"
      },
      "TagFormat": {
         "description": "A format that matches a tag: ``begin content end``.\n\nThe ``end`` field can be a single string or a list of possible end strings.\nWhen multiple end strings are provided, any of them will be accepted as a valid\nending for the tag.\n\nExamples\n--------\n\nSingle end string:\n\n.. code-block:: python\n\n    TagFormat(begin=\"<response>\", content=..., end=\"</response>\")\n\nMultiple end strings:\n\n.. code-block:: python\n\n    TagFormat(begin=\"<response>\", content=..., end=[\"</response>\", \"</answer>\"])",
         "properties": {
            "type": {
               "const": "tag",
               "default": "tag",
               "title": "Type",
               "type": "string"
            },
            "begin": {
               "title": "Begin",
               "type": "string"
            },
            "content": {
               "discriminator": {
                  "mapping": {
                     "any_text": "#/$defs/AnyTextFormat",
                     "const_string": "#/$defs/ConstStringFormat",
                     "grammar": "#/$defs/GrammarFormat",
                     "json_schema": "#/$defs/JSONSchemaFormat",
                     "or": "#/$defs/OrFormat",
                     "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                     "regex": "#/$defs/RegexFormat",
                     "sequence": "#/$defs/SequenceFormat",
                     "tag": "#/$defs/TagFormat",
                     "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                     "triggered_tags": "#/$defs/TriggeredTagsFormat"
                  },
                  "propertyName": "type"
               },
               "oneOf": [
                  {
                     "$ref": "#/$defs/AnyTextFormat"
                  },
                  {
                     "$ref": "#/$defs/ConstStringFormat"
                  },
                  {
                     "$ref": "#/$defs/JSONSchemaFormat"
                  },
                  {
                     "$ref": "#/$defs/GrammarFormat"
                  },
                  {
                     "$ref": "#/$defs/RegexFormat"
                  },
                  {
                     "$ref": "#/$defs/QwenXMLParameterFormat"
                  },
                  {
                     "$ref": "#/$defs/OrFormat"
                  },
                  {
                     "$ref": "#/$defs/SequenceFormat"
                  },
                  {
                     "$ref": "#/$defs/TagFormat"
                  },
                  {
                     "$ref": "#/$defs/TriggeredTagsFormat"
                  },
                  {
                     "$ref": "#/$defs/TagsWithSeparatorFormat"
                  }
               ],
               "title": "Content"
            },
            "end": {
               "anyOf": [
                  {
                     "type": "string"
                  },
                  {
                     "items": {
                        "type": "string"
                     },
                     "type": "array"
                  }
               ],
               "title": "End"
            }
         },
         "required": [
            "begin",
            "content",
            "end"
         ],
         "title": "TagFormat",
         "type": "object"
      },
      "TagsWithSeparatorFormat": {
         "description": "A format that matches a tags with separator. It can match zero, one, or more tags, separated\nby the separator, with no other text allowed.\n\nExamples\n--------\n\n.. code-block:: python\n\n    structural_tag = TagsWithSeparatorFormat(\n        tags=[\n            TagFormat(begin=\"<function=func1>\", content=JSONSchemaFormat(json_schema=...), end=\"</function>\"),\n            TagFormat(begin=\"<function=func2>\", content=JSONSchemaFormat(json_schema=...), end=\"</function>\"),\n        ],\n        separator=\",\",\n        at_least_one=False,\n        stop_after_first=False,\n    )\n\nThe above structural tag can accept an empty string, or the following outputs::\n\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>,<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>,<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>,<function=func1>{\"name\": \"John\", \"age\": 30}</function>",
         "properties": {
            "type": {
               "const": "tags_with_separator",
               "default": "tags_with_separator",
               "title": "Type",
               "type": "string"
            },
            "tags": {
               "items": {
                  "$ref": "#/$defs/TagFormat"
               },
               "title": "Tags",
               "type": "array"
            },
            "separator": {
               "title": "Separator",
               "type": "string"
            },
            "at_least_one": {
               "default": false,
               "title": "At Least One",
               "type": "boolean"
            },
            "stop_after_first": {
               "default": false,
               "title": "Stop After First",
               "type": "boolean"
            }
         },
         "required": [
            "tags",
            "separator"
         ],
         "title": "TagsWithSeparatorFormat",
         "type": "object"
      },
      "TriggeredTagsFormat": {
         "description": "A format that matches triggered tags. It can allow any output until a trigger is\nencountered, then dispatch to the corresponding tag; when the end tag is encountered, the\ngrammar will allow any following output, until the next trigger is encountered.\n\nEach tag should be matched by exactly one trigger. \"matching\" means the trigger should be a\nprefix of the begin tag.\n\nExamples\n--------\n\n.. code-block:: python\n\n    structural_tag = TriggeredTagsFormat(\n        triggers=[\"<function=\"],\n        tags=[\n            TagFormat(\n                begin=\"<function=func1>\",\n                content=JSONSchemaFormat(json_schema=...),\n                end=\"</function>\",\n            ),\n            TagFormat(\n                begin=\"<function=func2>\",\n                content=JSONSchemaFormat(json_schema=...),\n                end=\"</function>\",\n            ),\n        ],\n        at_least_one=False,\n        stop_after_first=False,\n    )\n\nThe above structural tag can accept the following outputs::\n\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>\n    <function=func2>{\"name\": \"Jane\", \"age\": 25}</function>\n    any_text<function=func1>{\"name\": \"John\", \"age\": 30}</function>any_text1<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>any_text2",
         "properties": {
            "type": {
               "const": "triggered_tags",
               "default": "triggered_tags",
               "title": "Type",
               "type": "string"
            },
            "triggers": {
               "items": {
                  "type": "string"
               },
               "title": "Triggers",
               "type": "array"
            },
            "tags": {
               "items": {
                  "$ref": "#/$defs/TagFormat"
               },
               "title": "Tags",
               "type": "array"
            },
            "at_least_one": {
               "default": false,
               "title": "At Least One",
               "type": "boolean"
            },
            "stop_after_first": {
               "default": false,
               "title": "Stop After First",
               "type": "boolean"
            },
            "excludes": {
               "default": [],
               "items": {
                  "type": "string"
               },
               "title": "Excludes",
               "type": "array"
            }
         },
         "required": [
            "triggers",
            "tags"
         ],
         "title": "TriggeredTagsFormat",
         "type": "object"
      }
   },
   "$ref": "#/$defs/TriggeredTagsFormat"
}

field type: Literal['triggered_tags'] = 'triggered_tags'

The type of the format.

field triggers: List[str] [Required]

The triggers of the triggered tags.

field tags: List[TagFormat] [Required]

The tags of the triggered tags.

field at_least_one: bool = False

Whether at least one of the tags must be generated.

field stop_after_first: bool = False

Whether to stop after the first tag is generated.

field excludes: List[str] = []

List of strings that should not appear in the matched text.

pydantic model xgrammar.structural_tag.TagsWithSeparatorFormat[source]

Bases: BaseModel

A format that matches a tags with separator. It can match zero, one, or more tags, separated by the separator, with no other text allowed.

Examples

structural_tag = TagsWithSeparatorFormat(
    tags=[
        TagFormat(begin="<function=func1>", content=JSONSchemaFormat(json_schema=...), end="</function>"),
        TagFormat(begin="<function=func2>", content=JSONSchemaFormat(json_schema=...), end="</function>"),
    ],
    separator=",",
    at_least_one=False,
    stop_after_first=False,
)

The above structural tag can accept an empty string, or the following outputs:

<function=func1>{"name": "John", "age": 30}</function>
<function=func1>{"name": "John", "age": 30}</function>,<function=func2>{"name": "Jane", "age": 25}</function>
<function=func1>{"name": "John", "age": 30}</function>,<function=func2>{"name": "Jane", "age": 25}</function>,<function=func1>{"name": "John", "age": 30}</function>

Show JSON schema
{
   "$defs": {
      "AnyTextFormat": {
         "description": "A format that matches any text.",
         "properties": {
            "type": {
               "const": "any_text",
               "default": "any_text",
               "title": "Type",
               "type": "string"
            },
            "excludes": {
               "default": [],
               "items": {
                  "type": "string"
               },
               "title": "Excludes",
               "type": "array"
            }
         },
         "title": "AnyTextFormat",
         "type": "object"
      },
      "ConstStringFormat": {
         "description": "A format that matches a constant string.",
         "properties": {
            "type": {
               "const": "const_string",
               "default": "const_string",
               "title": "Type",
               "type": "string"
            },
            "value": {
               "title": "Value",
               "type": "string"
            }
         },
         "required": [
            "value"
         ],
         "title": "ConstStringFormat",
         "type": "object"
      },
      "GrammarFormat": {
         "description": "A format that matches an ebnf grammar.",
         "properties": {
            "type": {
               "const": "grammar",
               "default": "grammar",
               "title": "Type",
               "type": "string"
            },
            "grammar": {
               "title": "Grammar",
               "type": "string"
            }
         },
         "required": [
            "grammar"
         ],
         "title": "GrammarFormat",
         "type": "object"
      },
      "JSONSchemaFormat": {
         "description": "A format that matches a JSON schema.",
         "properties": {
            "type": {
               "const": "json_schema",
               "default": "json_schema",
               "title": "Type",
               "type": "string"
            },
            "json_schema": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "additionalProperties": true,
                     "type": "object"
                  }
               ],
               "title": "Json Schema"
            }
         },
         "required": [
            "json_schema"
         ],
         "title": "JSONSchemaFormat",
         "type": "object"
      },
      "OrFormat": {
         "description": "A format that matches one of the formats.",
         "properties": {
            "type": {
               "const": "or",
               "default": "or",
               "title": "Type",
               "type": "string"
            },
            "elements": {
               "items": {
                  "discriminator": {
                     "mapping": {
                        "any_text": "#/$defs/AnyTextFormat",
                        "const_string": "#/$defs/ConstStringFormat",
                        "grammar": "#/$defs/GrammarFormat",
                        "json_schema": "#/$defs/JSONSchemaFormat",
                        "or": "#/$defs/OrFormat",
                        "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                        "regex": "#/$defs/RegexFormat",
                        "sequence": "#/$defs/SequenceFormat",
                        "tag": "#/$defs/TagFormat",
                        "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                        "triggered_tags": "#/$defs/TriggeredTagsFormat"
                     },
                     "propertyName": "type"
                  },
                  "oneOf": [
                     {
                        "$ref": "#/$defs/AnyTextFormat"
                     },
                     {
                        "$ref": "#/$defs/ConstStringFormat"
                     },
                     {
                        "$ref": "#/$defs/JSONSchemaFormat"
                     },
                     {
                        "$ref": "#/$defs/GrammarFormat"
                     },
                     {
                        "$ref": "#/$defs/RegexFormat"
                     },
                     {
                        "$ref": "#/$defs/QwenXMLParameterFormat"
                     },
                     {
                        "$ref": "#/$defs/OrFormat"
                     },
                     {
                        "$ref": "#/$defs/SequenceFormat"
                     },
                     {
                        "$ref": "#/$defs/TagFormat"
                     },
                     {
                        "$ref": "#/$defs/TriggeredTagsFormat"
                     },
                     {
                        "$ref": "#/$defs/TagsWithSeparatorFormat"
                     }
                  ]
               },
               "title": "Elements",
               "type": "array"
            }
         },
         "required": [
            "elements"
         ],
         "title": "OrFormat",
         "type": "object"
      },
      "QwenXMLParameterFormat": {
         "description": "A format that matches Qwen XML function calls.\n\nExamples\n--------\n.. code-block:: python\n\n    structural_tag = QwenXMLParameterFormat(\n        json_schema={\n            \"type\": \"qwen_xml_parameter\",\n            \"json_schema\": {\n                \"type\": \"object\",\n                \"properties\": {\"name\": {\"type\": \"string\"}, \"age\": {\"type\": \"integer\"}},\n                \"required\": [\"name\", \"age\"],\n            },\n        }\n    )\n\nThe above structural tag can accept the following outputs::\n\n    <parameter=name>Bob</parameter><parameter=age>100</parameter>\n    <parameter=name>\"Bob&lt;\"</parameter><parameter=age>100</parameter>",
         "properties": {
            "type": {
               "const": "qwen_xml_parameter",
               "default": "qwen_xml_parameter",
               "title": "Type",
               "type": "string"
            },
            "json_schema": {
               "anyOf": [
                  {
                     "type": "boolean"
                  },
                  {
                     "additionalProperties": true,
                     "type": "object"
                  }
               ],
               "title": "Json Schema"
            }
         },
         "required": [
            "json_schema"
         ],
         "title": "QwenXMLParameterFormat",
         "type": "object"
      },
      "RegexFormat": {
         "description": "A format that matches a regex pattern.",
         "properties": {
            "type": {
               "const": "regex",
               "default": "regex",
               "title": "Type",
               "type": "string"
            },
            "pattern": {
               "title": "Pattern",
               "type": "string"
            }
         },
         "required": [
            "pattern"
         ],
         "title": "RegexFormat",
         "type": "object"
      },
      "SequenceFormat": {
         "description": "A format that matches a sequence of formats.",
         "properties": {
            "type": {
               "const": "sequence",
               "default": "sequence",
               "title": "Type",
               "type": "string"
            },
            "elements": {
               "items": {
                  "discriminator": {
                     "mapping": {
                        "any_text": "#/$defs/AnyTextFormat",
                        "const_string": "#/$defs/ConstStringFormat",
                        "grammar": "#/$defs/GrammarFormat",
                        "json_schema": "#/$defs/JSONSchemaFormat",
                        "or": "#/$defs/OrFormat",
                        "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                        "regex": "#/$defs/RegexFormat",
                        "sequence": "#/$defs/SequenceFormat",
                        "tag": "#/$defs/TagFormat",
                        "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                        "triggered_tags": "#/$defs/TriggeredTagsFormat"
                     },
                     "propertyName": "type"
                  },
                  "oneOf": [
                     {
                        "$ref": "#/$defs/AnyTextFormat"
                     },
                     {
                        "$ref": "#/$defs/ConstStringFormat"
                     },
                     {
                        "$ref": "#/$defs/JSONSchemaFormat"
                     },
                     {
                        "$ref": "#/$defs/GrammarFormat"
                     },
                     {
                        "$ref": "#/$defs/RegexFormat"
                     },
                     {
                        "$ref": "#/$defs/QwenXMLParameterFormat"
                     },
                     {
                        "$ref": "#/$defs/OrFormat"
                     },
                     {
                        "$ref": "#/$defs/SequenceFormat"
                     },
                     {
                        "$ref": "#/$defs/TagFormat"
                     },
                     {
                        "$ref": "#/$defs/TriggeredTagsFormat"
                     },
                     {
                        "$ref": "#/$defs/TagsWithSeparatorFormat"
                     }
                  ]
               },
               "title": "Elements",
               "type": "array"
            }
         },
         "required": [
            "elements"
         ],
         "title": "SequenceFormat",
         "type": "object"
      },
      "TagFormat": {
         "description": "A format that matches a tag: ``begin content end``.\n\nThe ``end`` field can be a single string or a list of possible end strings.\nWhen multiple end strings are provided, any of them will be accepted as a valid\nending for the tag.\n\nExamples\n--------\n\nSingle end string:\n\n.. code-block:: python\n\n    TagFormat(begin=\"<response>\", content=..., end=\"</response>\")\n\nMultiple end strings:\n\n.. code-block:: python\n\n    TagFormat(begin=\"<response>\", content=..., end=[\"</response>\", \"</answer>\"])",
         "properties": {
            "type": {
               "const": "tag",
               "default": "tag",
               "title": "Type",
               "type": "string"
            },
            "begin": {
               "title": "Begin",
               "type": "string"
            },
            "content": {
               "discriminator": {
                  "mapping": {
                     "any_text": "#/$defs/AnyTextFormat",
                     "const_string": "#/$defs/ConstStringFormat",
                     "grammar": "#/$defs/GrammarFormat",
                     "json_schema": "#/$defs/JSONSchemaFormat",
                     "or": "#/$defs/OrFormat",
                     "qwen_xml_parameter": "#/$defs/QwenXMLParameterFormat",
                     "regex": "#/$defs/RegexFormat",
                     "sequence": "#/$defs/SequenceFormat",
                     "tag": "#/$defs/TagFormat",
                     "tags_with_separator": "#/$defs/TagsWithSeparatorFormat",
                     "triggered_tags": "#/$defs/TriggeredTagsFormat"
                  },
                  "propertyName": "type"
               },
               "oneOf": [
                  {
                     "$ref": "#/$defs/AnyTextFormat"
                  },
                  {
                     "$ref": "#/$defs/ConstStringFormat"
                  },
                  {
                     "$ref": "#/$defs/JSONSchemaFormat"
                  },
                  {
                     "$ref": "#/$defs/GrammarFormat"
                  },
                  {
                     "$ref": "#/$defs/RegexFormat"
                  },
                  {
                     "$ref": "#/$defs/QwenXMLParameterFormat"
                  },
                  {
                     "$ref": "#/$defs/OrFormat"
                  },
                  {
                     "$ref": "#/$defs/SequenceFormat"
                  },
                  {
                     "$ref": "#/$defs/TagFormat"
                  },
                  {
                     "$ref": "#/$defs/TriggeredTagsFormat"
                  },
                  {
                     "$ref": "#/$defs/TagsWithSeparatorFormat"
                  }
               ],
               "title": "Content"
            },
            "end": {
               "anyOf": [
                  {
                     "type": "string"
                  },
                  {
                     "items": {
                        "type": "string"
                     },
                     "type": "array"
                  }
               ],
               "title": "End"
            }
         },
         "required": [
            "begin",
            "content",
            "end"
         ],
         "title": "TagFormat",
         "type": "object"
      },
      "TagsWithSeparatorFormat": {
         "description": "A format that matches a tags with separator. It can match zero, one, or more tags, separated\nby the separator, with no other text allowed.\n\nExamples\n--------\n\n.. code-block:: python\n\n    structural_tag = TagsWithSeparatorFormat(\n        tags=[\n            TagFormat(begin=\"<function=func1>\", content=JSONSchemaFormat(json_schema=...), end=\"</function>\"),\n            TagFormat(begin=\"<function=func2>\", content=JSONSchemaFormat(json_schema=...), end=\"</function>\"),\n        ],\n        separator=\",\",\n        at_least_one=False,\n        stop_after_first=False,\n    )\n\nThe above structural tag can accept an empty string, or the following outputs::\n\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>,<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>,<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>,<function=func1>{\"name\": \"John\", \"age\": 30}</function>",
         "properties": {
            "type": {
               "const": "tags_with_separator",
               "default": "tags_with_separator",
               "title": "Type",
               "type": "string"
            },
            "tags": {
               "items": {
                  "$ref": "#/$defs/TagFormat"
               },
               "title": "Tags",
               "type": "array"
            },
            "separator": {
               "title": "Separator",
               "type": "string"
            },
            "at_least_one": {
               "default": false,
               "title": "At Least One",
               "type": "boolean"
            },
            "stop_after_first": {
               "default": false,
               "title": "Stop After First",
               "type": "boolean"
            }
         },
         "required": [
            "tags",
            "separator"
         ],
         "title": "TagsWithSeparatorFormat",
         "type": "object"
      },
      "TriggeredTagsFormat": {
         "description": "A format that matches triggered tags. It can allow any output until a trigger is\nencountered, then dispatch to the corresponding tag; when the end tag is encountered, the\ngrammar will allow any following output, until the next trigger is encountered.\n\nEach tag should be matched by exactly one trigger. \"matching\" means the trigger should be a\nprefix of the begin tag.\n\nExamples\n--------\n\n.. code-block:: python\n\n    structural_tag = TriggeredTagsFormat(\n        triggers=[\"<function=\"],\n        tags=[\n            TagFormat(\n                begin=\"<function=func1>\",\n                content=JSONSchemaFormat(json_schema=...),\n                end=\"</function>\",\n            ),\n            TagFormat(\n                begin=\"<function=func2>\",\n                content=JSONSchemaFormat(json_schema=...),\n                end=\"</function>\",\n            ),\n        ],\n        at_least_one=False,\n        stop_after_first=False,\n    )\n\nThe above structural tag can accept the following outputs::\n\n    <function=func1>{\"name\": \"John\", \"age\": 30}</function>\n    <function=func2>{\"name\": \"Jane\", \"age\": 25}</function>\n    any_text<function=func1>{\"name\": \"John\", \"age\": 30}</function>any_text1<function=func2>{\"name\": \"Jane\", \"age\": 25}</function>any_text2",
         "properties": {
            "type": {
               "const": "triggered_tags",
               "default": "triggered_tags",
               "title": "Type",
               "type": "string"
            },
            "triggers": {
               "items": {
                  "type": "string"
               },
               "title": "Triggers",
               "type": "array"
            },
            "tags": {
               "items": {
                  "$ref": "#/$defs/TagFormat"
               },
               "title": "Tags",
               "type": "array"
            },
            "at_least_one": {
               "default": false,
               "title": "At Least One",
               "type": "boolean"
            },
            "stop_after_first": {
               "default": false,
               "title": "Stop After First",
               "type": "boolean"
            },
            "excludes": {
               "default": [],
               "items": {
                  "type": "string"
               },
               "title": "Excludes",
               "type": "array"
            }
         },
         "required": [
            "triggers",
            "tags"
         ],
         "title": "TriggeredTagsFormat",
         "type": "object"
      }
   },
   "$ref": "#/$defs/TagsWithSeparatorFormat"
}

field type: Literal['tags_with_separator'] = 'tags_with_separator'

The type of the format.

field tags: List[TagFormat] [Required]

The tags of the tags with separator.

field separator: str [Required]

The separator of the tags with separator.

field at_least_one: bool = False

Whether at least one of the tags must be matched.

field stop_after_first: bool = False

Whether to stop after the first tag is matched.