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:
BaseModelDescribes a complete structural tag structure. It corresponds to
"response_format": {"type": "structural_tag", "format": {...}}in API.- 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.
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:
BaseModelA 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" ] }
- pydantic model xgrammar.structural_tag.JSONSchemaFormat[source]¶
Bases:
BaseModelA 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" ] }
- pydantic model xgrammar.structural_tag.AnyTextFormat[source]¶
Bases:
BaseModelA 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" } } }
- pydantic model xgrammar.structural_tag.GrammarFormat[source]¶
Bases:
BaseModelA 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" ] }
- pydantic model xgrammar.structural_tag.RegexFormat[source]¶
Bases:
BaseModelA 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" ] }
- pydantic model xgrammar.structural_tag.QwenXMLParameterFormat[source]¶
Bases:
BaseModelA 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<"</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<\"</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" ] }
Combinatorial Formats¶
- pydantic model xgrammar.structural_tag.SequenceFormat[source]¶
Bases:
BaseModelA 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" } }, "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<\"</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``.", "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": { "title": "End", "type": "string" } }, "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" } }, "required": [ "triggers", "tags" ], "title": "TriggeredTagsFormat", "type": "object" } }, "$ref": "#/$defs/SequenceFormat" }
- pydantic model xgrammar.structural_tag.OrFormat[source]¶
Bases:
BaseModelA 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" } }, "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<\"</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``.", "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": { "title": "End", "type": "string" } }, "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" } }, "required": [ "triggers", "tags" ], "title": "TriggeredTagsFormat", "type": "object" } }, "$ref": "#/$defs/OrFormat" }
- pydantic model xgrammar.structural_tag.TagFormat[source]¶
Bases:
BaseModelA format that matches a tag:
begin content end.Show JSON schema
{ "$defs": { "AnyTextFormat": { "description": "A format that matches any text.", "properties": { "type": { "const": "any_text", "default": "any_text", "title": "Type", "type": "string" } }, "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<\"</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``.", "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": { "title": "End", "type": "string" } }, "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" } }, "required": [ "triggers", "tags" ], "title": "TriggeredTagsFormat", "type": "object" } }, "$ref": "#/$defs/TagFormat" }
- field content: Format [Required]¶
The content of the tag. It can be any of the formats.
- pydantic model xgrammar.structural_tag.TriggeredTagsFormat[source]¶
Bases:
BaseModelA 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" } }, "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<\"</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``.", "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": { "title": "End", "type": "string" } }, "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" } }, "required": [ "triggers", "tags" ], "title": "TriggeredTagsFormat", "type": "object" } }, "$ref": "#/$defs/TriggeredTagsFormat" }
- pydantic model xgrammar.structural_tag.TagsWithSeparatorFormat[source]¶
Bases:
BaseModelA 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" } }, "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<\"</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``.", "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": { "title": "End", "type": "string" } }, "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" } }, "required": [ "triggers", "tags" ], "title": "TriggeredTagsFormat", "type": "object" } }, "$ref": "#/$defs/TagsWithSeparatorFormat" }