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.- format: Union[AnyTextFormat, ConstStringFormat, JSONSchemaFormat, GrammarFormat, RegexFormat, QwenXMLParameterFormat, OrFormat, SequenceFormat, TagFormat, TriggeredTagsFormat, TagsWithSeparatorFormat]¶
 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¶
- class xgrammar.structural_tag.ConstStringFormat(*, type: Literal['const_string'] = 'const_string', value: str)[source]¶
 Bases:
BaseModelA format that matches a constant string.
- class xgrammar.structural_tag.JSONSchemaFormat(*, type: Literal['json_schema'] = 'json_schema', json_schema: Union[bool, Dict[str, Any]])[source]¶
 Bases:
BaseModelA format that matches a JSON schema.
- class xgrammar.structural_tag.AnyTextFormat(*, type: Literal['any_text'] = 'any_text')[source]¶
 Bases:
BaseModelA format that matches any text.
- class xgrammar.structural_tag.GrammarFormat(*, type: Literal['grammar'] = 'grammar', grammar: str)[source]¶
 Bases:
BaseModelA format that matches an ebnf grammar.
- class xgrammar.structural_tag.RegexFormat(*, type: Literal['regex'] = 'regex', pattern: str)[source]¶
 Bases:
BaseModelA format that matches a regex pattern.
- class xgrammar.structural_tag.QwenXMLParameterFormat(*, type: Literal['qwen_xml_parameter'] = 'qwen_xml_parameter', json_schema: Union[bool, Dict[str, Any]])[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>
Combinatorial Formats¶
- class xgrammar.structural_tag.SequenceFormat(*, type: Literal['sequence'] = 'sequence', elements: List[Union[AnyTextFormat, ConstStringFormat, JSONSchemaFormat, GrammarFormat, RegexFormat, QwenXMLParameterFormat, OrFormat, SequenceFormat, TagFormat, TriggeredTagsFormat, TagsWithSeparatorFormat]])[source]¶
 Bases:
BaseModelA format that matches a sequence of formats.
- class xgrammar.structural_tag.OrFormat(**data: Any)[source]¶
 Bases:
BaseModelA format that matches one of the formats.
- class xgrammar.structural_tag.TagFormat(*, type: Literal['tag'] = 'tag', begin: str, content: Union[AnyTextFormat, ConstStringFormat, JSONSchemaFormat, GrammarFormat, RegexFormat, QwenXMLParameterFormat, OrFormat, SequenceFormat, TagFormat, TriggeredTagsFormat, TagsWithSeparatorFormat], end: str)[source]¶
 Bases:
BaseModelA format that matches a tag:
begin content end.- content: Format¶
 The content of the tag. It can be any of the formats.
- class xgrammar.structural_tag.TriggeredTagsFormat(*, type: Literal['triggered_tags'] = 'triggered_tags', triggers: List[str], tags: List[TagFormat], at_least_one: bool = False, stop_after_first: bool = False)[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
- class xgrammar.structural_tag.TagsWithSeparatorFormat(*, type: Literal['tags_with_separator'] = 'tags_with_separator', tags: List[TagFormat], separator: str, at_least_one: bool = False, stop_after_first: bool = False)[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>