huggingface/transformers · #50619
[Chat Parsing] Add `python-call-list` content parser
docs/source/en/chat_response_parsing.md95 + / 1 −
@@ -413,6 +413,37 @@ input = "<meta>name: alice\nage: 30</meta>" Note `age` keeps `"30"` as a string; add a `value_parser` of `{"name": "int"}` to parse it to `30`. +#### python-call-list++The `python-call-list` parser handles tool calls written as a list of function calls with keyword arguments,+such as `[get_weather(city="Paris", days=2)]`. Several model families use this format, including LFM2 and Llama 4.+It returns a list of `{"name": ..., "arguments": {...}}` dicts, so it is usually combined with `transform_each`.+It has no `content_args`.++- Argument values are strings (single or double quoted), numbers, lists, dicts with string keys, and+ `True`/`False`/`None` or their JSON spellings `true`/`false`/`null`.+- Function and argument names are ASCII and may contain `-` and `.`, like `get-weather`. Python keywords such as+ `from` are valid names.+- The format is strict. Anything else raises a `ValueError`, including positional arguments such as+ `add_numbers(2, 3.5)`.++The model output is never evaluated as code. The exact grammar is in+[The python-call-list format](#framework-developers-the-python-call-list-format).++```python+field = {+ "tool_calls": {+ "open": "<|tool_call_start|>",+ "close": "<|tool_call_end|>",+ "content": "python-call-list",+ "transform_each": True,+ "transform": {"type": "function", "function": {"name": "{name}", "arguments": "{arguments}"}},+ }+}+input = '<|tool_call_start|>[get_weather(city="Paris", days=2)]<|tool_call_end|>'+# Returns: {"tool_calls": [{"type": "function", "function": {"name": "get_weather", "arguments": {"city": "Paris", "days": 2}}}]}+```+ ### Typing tool-call arguments Sometimes, the response parser may parse model outputs with the wrong type. For example,@@ -587,4 +618,67 @@ follow the simple guidelines below, then response templates should be much less extract the lookarounds and enforce them in your code when the regex engine finds a possible match. - Other advanced features like backreferences, atomic groups, possessive quantifiers, recursion and so on are generally not used in response templates. We'll try to dissuade model authors from using them, so you can hopefully safely - ignore them.\ No newline at end of file+ ignore them.++## Framework developers: The python-call-list format++The `python-call-list` content parser does not accept Python syntax in general. It accepts the small grammar below,+so that every implementation can use the same hand-written parser and return the same result for the same input.+The grammar is deliberately strict: it covers what chat templates render and what models are documented to emit,+and everything else is an error.++```+call-list = ws "[" ws [ call *( sep call ) ] ws "]" ws+call = name ws "(" ws [ argument *( sep argument ) ] ws ")"+argument = name ws "=" ws value+sep = ws "," ws++value = string / number / list / dict / constant+list = "[" ws [ value *( sep value ) ] ws "]"+dict = "{" ws [ pair *( sep pair ) ] ws "}"+pair = string ws ":" ws value+constant = "True" / "False" / "None" / "true" / "false" / "null"++name = name-start *name-char *( ( "-" / "." ) 1*name-char )+name-start = ALPHA / "_"+name-char = ALPHA / DIGIT / "_"+number = [ "-" ] int [ "." 1*DIGIT ] [ ( "e" / "E" ) [ "+" / "-" ] 1*DIGIT ]+int = "0" / ( %x31-39 *DIGIT )+string = "'" *( char / escape ) "'" / DQUOTE *( char / escape ) DQUOTE+char = any character except backslash, the closing quote, LF and CR+escape = "\" ( "\" / "'" / DQUOTE / "/" / "b" / "f" / "n" / "r" / "t" / "u" 4HEXDIG )+ws = *( SP / TAB / LF / CR )+```++- **Result.** A list of `{"name": ..., "arguments": {...}}` in source order. `[]` gives an empty list.+- **Names.** ASCII letters, digits and `_`, starting with a letter or `_`. `-` and `.` are allowed between those, so+ `get-weather` and `github.search` are names but `get-` and `a..b` are not. There are no reserved words: `from`,+ `class`, `None` and `true` are valid function and argument names. Names and dict keys are plain data, so+ `__proto__` or `constructor` must end up as ordinary keys.+- **Constants.** An argument always starts with a name followed by `=`. Constants only exist in value position,+ where a bare word must be exactly one of the six constants. `True`/`true`, `False`/`false` and `None`/`null` map+ to the same three values.+- **Numbers.** This is the JSON number grammar. A number with no fraction and no exponent is an integer, anything+ else is a float. An integer outside -(2^53 - 1) to 2^53 - 1 is an error in every language, so that no+ implementation returns a rounded value. A float that overflows to infinity (`1e999`) is an error, and a float that+ underflows (`1e-999`) is `0.0`.+- **Strings.** Both quote styles behave the same. The escapes are JSON's escapes plus `\'`. A `\uXXXX` surrogate+ pair becomes one character, and a lone surrogate, escaped or raw, is an error. Any other backslash sequence is an+ error, including `\d`, `\x41`, and a `\u` that is not followed by four hex digits. A string cannot contain a raw+ line break, but other raw characters such as a tab are allowed.+- **Whitespace.** Only space, tab, LF and CR. Any other whitespace character outside a string is an error.+- **Commas.** Only between items. A trailing comma is an error.+- **Duplicates.** A repeated argument name or a repeated dict key is an error.+- **Nesting.** Lists and dicts nest up to 100 levels deep, not counting the call list itself. A 101st level is an+ ordinary error, even when it is empty.+- **Errors.** Anything outside the grammar is an error for the whole region. There are no partial results.++Languages with a single number type cannot tell `1.0` from `1`, so compare numbers by value when testing.++Positional arguments, such as `[add_numbers(2, 3.5)]`, are not supported yet and raise an error. Some models do emit+them. Supporting them means naming the values from the called tool's parameters, which needs `tools`.++The test cases in+[`tests/fixtures/chat_parsing/python_call_list.json`](https://github.com/huggingface/transformers/blob/main/tests/fixtures/chat_parsing/python_call_list.json)+are shared between implementations. Each accepted input must parse to exactly the given output, and each rejected+input must raise an error.src/transformers/utils/chat_parsing/content_parsers.py152 + / 2 −
@@ -17,7 +17,9 @@ from __future__ import annotations import json-from typing import Any+import math+from collections.abc import Callable+from typing import Any, NoReturn import regex as re @@ -137,6 +139,153 @@ def _kv_lines(text: str, args: dict) -> dict: return out +_CALL_LIST_WHITESPACE = re.compile(r"[ \t\n\r]*")+_CALL_LIST_NAME = re.compile(r"[A-Za-z_][A-Za-z0-9_]*(?:[-.][A-Za-z0-9_]+)*")+_CALL_LIST_NUMBER = re.compile(r"-?(?:0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?")+_CALL_LIST_STRING = re.compile(r"""'((?:[^'\\\n\r]|\\.)*)'|"((?:[^"\\\n\r]|\\.)*)\"""", flags=re.DOTALL)+_CALL_LIST_UNICODE_ESCAPE = re.compile(r"u([0-9a-fA-F]{4})")+_CALL_LIST_SURROGATE = re.compile(r"[\ud800-\udfff]")+_CALL_LIST_LOW_SURROGATE = re.compile(r"\\u([dD][c-fC-F][0-9a-fA-F]{2})")+_CALL_LIST_ESCAPES = {"\\": "\\", "'": "'", '"': '"', "/": "/", "b": "\b", "f": "\f", "n": "\n", "r": "\r", "t": "\t"}+_CALL_LIST_CONSTANTS = {"True": True, "true": True, "False": False, "false": False, "None": None, "null": None}+_CALL_LIST_MAX_INTEGER = 2**53 - 1+_CALL_LIST_MAX_DEPTH = 100+++def _python_call_list(text: str, args: dict) -> list[dict]:+ """Parse a list of keyword-argument calls, e.g. `[get_weather(city="Paris")]`, into+ `[{"name": ..., "arguments": {...}}]`. The grammar is specified in `docs/source/en/chat_response_parsing.md`+ and is deliberately strict, so that implementations in other languages can match it exactly."""+ position = 0++ def fail(message: str) -> NoReturn:+ raise ValueError(f"python-call-list: {message} at position {position}.\nContent: {text!r}")++ def skip_whitespace() -> None:+ nonlocal position+ position = _CALL_LIST_WHITESPACE.match(text, position).end()++ def at(char: str) -> bool:+ skip_whitespace()+ return text.startswith(char, position)++ def expect(char: str) -> None:+ nonlocal position+ if not at(char):+ fail(f"expected {char!r}")+ position += 1++ def items(close: str, item: Callable[[], Any]) -> list:+ nonlocal position+ out = []+ if at(close):+ position += 1+ return out+ while True:+ out.append(item())+ if at(close):+ position += 1+ return out+ expect(",")++ def unique(pairs: list[tuple[str, Any]]) -> dict:+ out = {}+ for key, value in pairs:+ if key in out:+ fail(f"repeated name {key!r}")+ out[key] = value+ return out++ def string() -> str:+ nonlocal position+ match = _CALL_LIST_STRING.match(text, position)+ if match is None:+ fail("invalid string")+ body = match[1] if match[1] is not None else match[2]+ if _CALL_LIST_SURROGATE.search(body) is not None:+ fail("lone surrogate in the string starting")+ out, start = [], 0+ while (backslash := body.find("\\", start)) != -1:+ out.append(body[start:backslash])+ start = backslash + 2+ if body[backslash + 1] in _CALL_LIST_ESCAPES:+ out.append(_CALL_LIST_ESCAPES[body[backslash + 1]])+ continue+ escape = _CALL_LIST_UNICODE_ESCAPE.match(body, backslash + 1)+ if escape is None:+ fail(f"unsupported escape {body[backslash : backslash + 2]!r} in the string starting")+ code, start = int(escape[1], 16), escape.end()+ if 0xD800 <= code <= 0xDBFF and (low := _CALL_LIST_LOW_SURROGATE.match(body, start)) is not None:+ code, start = 0x10000 + ((code - 0xD800) << 10) + (int(low[1], 16) - 0xDC00), low.end()+ if 0xD800 <= code <= 0xDFFF:+ fail("lone surrogate escape in the string starting")+ out.append(chr(code))+ position = match.end()+ return "".join(out) + body[start:]++ def value(depth: int) -> Any:+ # `depth` is the number of lists and dicts that enclose this value.+ nonlocal position+ if at("'") or at('"'):+ return string()+ if at("[") or at("{"):+ if depth >= _CALL_LIST_MAX_DEPTH:+ fail(f"lists and dicts nested more than {_CALL_LIST_MAX_DEPTH} levels deep")+ position += 1+ if text[position - 1] == "[":+ return items("]", lambda: value(depth + 1))+ return unique(items("}", lambda: pair(depth + 1)))+ if (match := _CALL_LIST_NUMBER.match(text, position)) is not None:+ is_integer = match[1] is None and match[2] is None+ # Check the digit count first so that huge literals never reach `int()`.+ if is_integer and (len(match[0]) > 17 or abs(int(match[0])) > _CALL_LIST_MAX_INTEGER):+ fail("integer outside the range of -(2^53 - 1) to 2^53 - 1")+ if not is_integer and math.isinf(float(match[0])):+ fail("number too large")+ position = match.end()+ return int(match[0]) if is_integer else float(match[0])+ if (match := _CALL_LIST_NAME.match(text, position)) is not None and match[0] in _CALL_LIST_CONSTANTS:+ position = match.end()+ return _CALL_LIST_CONSTANTS[match[0]]+ fail("expected a string, number, list, dict, True, False or None")++ def pair(depth: int) -> tuple[str, Any]:+ if not (at("'") or at('"')):+ fail("dict keys must be strings")+ key = string()+ expect(":")+ return key, value(depth)++ def name(what: str) -> str:+ nonlocal position+ skip_whitespace()+ match = _CALL_LIST_NAME.match(text, position)+ if match is None:+ fail(f"expected {what}")+ position = match.end()+ return match[0]++ def argument() -> tuple[str, Any]:+ nonlocal position+ key = name("a keyword argument (positional arguments and trailing commas are not supported)")+ if not at("="):+ fail("expected '=' (positional arguments are not supported)")+ position += 1+ return key, value(0)++ def call() -> dict:+ function_name = name("a function name")+ expect("(")+ return {"name": function_name, "arguments": unique(items(")", argument))}++ expect("[")+ calls = items("]", call)+ skip_whitespace()+ if position != len(text):+ fail("unexpected content after the closing ']'")+ return calls++ CONTENT_PARSERS = { "text": _text, "int": _int,@@ -145,11 +294,12 @@ def _kv_lines(text: str, args: dict) -> dict: "json": _json, "xml-inline": _xml_inline, "kv-lines": _kv_lines,+ "python-call-list": _python_call_list, } # Parsers whose output is the verbatim body text (modulo whitespace) — chunks # from these fields stream with `dirty=False` because each chunk is part of-# the final value. Structured parsers (`json`, `xml-inline`, `kv-lines`) only+# the final value. Structured parsers (`json`, `xml-inline`, `kv-lines`, `python-call-list`) only # produce a meaningful value on close, so their chunks stream raw bytes # flagged `dirty=True` while the parsed value is delivered in `region_close`. STREAMABLE_PARSERS = frozenset({"text", "int", "float", "bool"})src/transformers/utils/chat_parsing/response_parser.py1 + / 1 −
@@ -338,7 +338,7 @@ def _accumulate(self, events: list[dict], text: str) -> None: region is the null sink (no implicit declared, no explicit open), we silently discard. Every routed chunk emits a `region_chunk` event so consumers can render live; `dirty=True` flags chunks from structured- parsers (json, xml-inline, kv-lines) whose raw bytes will only be+ parsers (json, xml-inline, kv-lines, python-call-list) whose raw bytes will only be parsed into the final value on close.""" if not text or self._current is None: returntests/fixtures/chat_parsing/python_call_list.jsonadded110 + / 0 −
@@ -0,0 +1,110 @@+{+ "description": "Shared test cases for the `python-call-list` content parser. Every implementation of the format (e.g. transformers and Transformers.js) loads this file. Each accepted input must parse to exactly `output`, with numbers compared by value. Each rejected input must raise an error.",+ "accepted": [+ {"input": "[]", "output": []},+ {"input": " [ ] ", "output": []},+ {"input": "[f()]", "output": [{"name": "f", "arguments": {}}]},+ {"input": "[f(city='Paris', days=2)]", "output": [{"name": "f", "arguments": {"city": "Paris", "days": 2}}]},+ {"input": "[get_weather(location=\"Paris\", unit=\"celsius\"), add_numbers(a=2, b=3.5)]", "output": [{"name": "get_weather", "arguments": {"location": "Paris", "unit": "celsius"}}, {"name": "add_numbers", "arguments": {"a": 2, "b": 3.5}}]},+ {"input": "[convert(amount=5, from='USD', to='EUR', class='a', in=1)]", "output": [{"name": "convert", "arguments": {"amount": 5, "from": "USD", "to": "EUR", "class": "a", "in": 1}}]},+ {"input": "[f(None=1, true=2)]", "output": [{"name": "f", "arguments": {"None": 1, "true": 2}}]},+ {"input": "[f(a=True, b=false, c=None, d=null)]", "output": [{"name": "f", "arguments": {"a": true, "b": false, "c": null, "d": null}}]},+ {"input": "[f(l=[1, \"x\", true, {\"k\": [1.5]}], d={}, e=[])]", "output": [{"name": "f", "arguments": {"l": [1, "x", true, {"k": [1.5]}], "d": {}, "e": []}}]},+ {"input": "[f(s='it\\'s', t=\"say \\\"hi\\\"\", u='a\"b', v=\"a'b\")]", "output": [{"name": "f", "arguments": {"s": "it's", "t": "say \"hi\"", "u": "a\"b", "v": "a'b"}}]},+ {"input": "[f(u=\"é🚀\", e=\"\\u00e9\\ud83d\\ude80\")]", "output": [{"name": "f", "arguments": {"u": "é🚀", "e": "é🚀"}}]},+ {"input": "[f(w=\"a\\\\b\\/c\\td\\ne\\rf\\bg\\fh\")]", "output": [{"name": "f", "arguments": {"w": "a\\b/c\td\ne\rf\bg\fh"}}]},+ {"input": "[f(s=\"tab\there\")]", "output": [{"name": "f", "arguments": {"s": "tab\there"}}]},+ {"input": "[f(s=\"\")]", "output": [{"name": "f", "arguments": {"s": ""}}]},+ {"input": "[f(n=-3, x=0.5, e=1e-05, g=1E+20, z=0)]", "output": [{"name": "f", "arguments": {"n": -3, "x": 0.5, "e": 1e-05, "g": 1e+20, "z": 0}}]},+ {"input": "[f(x=1e-999)]", "output": [{"name": "f", "arguments": {"x": 0.0}}]},+ {"input": "[f(x=9007199254740991, y=-9007199254740991)]", "output": [{"name": "f", "arguments": {"x": 9007199254740991, "y": -9007199254740991}}]},+ {"input": "[get-weather(max-results=5)]", "output": [{"name": "get-weather", "arguments": {"max-results": 5}}]},+ {"input": "[github.search(q=\"a\")]", "output": [{"name": "github.search", "arguments": {"q": "a"}}]},+ {"input": "[a.b-c_d(x-y.z=1)]", "output": [{"name": "a.b-c_d", "arguments": {"x-y.z": 1}}]},+ {"input": "[f(__proto__=1, constructor=2, d={\"__proto__\": 3})]", "output": [{"name": "f", "arguments": {"__proto__": 1, "constructor": 2, "d": {"__proto__": 3}}}]},+ {"input": "[ f( x = 1 , y = [ 1 , 2 ] ) ]", "output": [{"name": "f", "arguments": {"x": 1, "y": [1, 2]}}]},+ {"input": "[f(\n\tx=1,\r\n\ty=\"a\"\n)]", "output": [{"name": "f", "arguments": {"x": 1, "y": "a"}}]},+ {"input": "[f(x=[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]])]", "output": [{"name": "f", "arguments": {"x": [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]}}]},+ {"input": "[f(x=[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[1]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]])]", "output": [{"name": "f", "arguments": {"x": [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[1]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]}}]},+ {"input": "[f(x={\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}})]", "output": [{"name": "f", "arguments": {"x": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}]},+ {"input": "[f(x={\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": 1}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}})]", "output": [{"name": "f", "arguments": {"x": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": {"k": 1}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}]},+ {"input": "[f(x=[{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}])]", "output": [{"name": "f", "arguments": {"x": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}}]},+ {"input": "[f(x=[{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": 1}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}])]", "output": [{"name": "f", "arguments": {"x": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": [{"k": 1}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}}]},+ {"input": "[f(s=\"🚀\", d={\"🚀\": \"a🚀b\"})]", "output": [{"name": "f", "arguments": {"s": "🚀", "d": {"🚀": "a🚀b"}}}]},+ {"input": "[f(s=\"\\ud83d\\ude80\", d={\"\\ud83d\\ude80\": \"a\\uD83D\\uDE80b\"})]", "output": [{"name": "f", "arguments": {"s": "🚀", "d": {"🚀": "a🚀b"}}}]}+ ],+ "rejected": [+ {"input": "[add_numbers(2, 3.5)]", "reason": "positional arguments"},+ {"input": "[f(2, x=1)]", "reason": "positional arguments"},+ {"input": "[f(x=1, 2)]", "reason": "positional arguments"},+ {"input": "[f(x)]", "reason": "positional arguments"},+ {"input": "f(x=1)", "reason": "no surrounding list"},+ {"input": "", "reason": "empty input"},+ {"input": "[f(", "reason": "truncated"},+ {"input": "[f(x=\"a", "reason": "truncated"},+ {"input": "[f(x=1)", "reason": "truncated"},+ {"input": "[f(x=1)] extra", "reason": "trailing content"},+ {"input": "[[f()]]", "reason": "not a call"},+ {"input": "[f(x=1),]", "reason": "trailing comma"},+ {"input": "[f(x=1,)]", "reason": "trailing comma"},+ {"input": "[f(l=[1,])]", "reason": "trailing comma"},+ {"input": "[f(d={\"a\": 1,})]", "reason": "trailing comma"},+ {"input": "[,]", "reason": "stray comma"},+ {"input": "[f(x=1 y=2)]", "reason": "missing comma"},+ {"input": "[f(x=1, x=2)]", "reason": "repeated argument name"},+ {"input": "[f(d={\"a\": 1, \"a\": 2})]", "reason": "repeated dict key"},+ {"input": "[f(q=\"\\d+\")]", "reason": "unknown escape"},+ {"input": "[f(q=\"\\x41\")]", "reason": "unknown escape"},+ {"input": "[f(p=\"C:\\users\")]", "reason": "unknown escape"},+ {"input": "[f(s=\"a\nb\")]", "reason": "raw line break in a string"},+ {"input": "[f(s=\"a\rb\")]", "reason": "raw line break in a string"},+ {"input": "[f(s=\"\\ud83d\")]", "reason": "lone surrogate"},+ {"input": "[f(s=\"\\ude80\")]", "reason": "lone surrogate"},+ {"input": "[f(s=\"\\ud83d\\u0041\")]", "reason": "lone surrogate"},+ {"input": "[f(x=9007199254740992)]", "reason": "integer above 2^53 - 1"},+ {"input": "[f(x=-9007199254740992)]", "reason": "integer below -(2^53 - 1)"},+ {"input": "[f(x=12345678901234567890)]", "reason": "integer above 2^53 - 1"},+ {"input": "[f(x=1e999)]", "reason": "float overflow"},+ {"input": "[f(x=inf)]", "reason": "not a number literal"},+ {"input": "[f(x=nan)]", "reason": "not a number literal"},+ {"input": "[f(month=07)]", "reason": "leading zero"},+ {"input": "[f(x=0x1F)]", "reason": "not a JSON number"},+ {"input": "[f(x=1_000)]", "reason": "not a JSON number"},+ {"input": "[f(x=.5)]", "reason": "not a JSON number"},+ {"input": "[f(x=1.)]", "reason": "not a JSON number"},+ {"input": "[f(x=+1)]", "reason": "not a JSON number"},+ {"input": "[f(größe=1)]", "reason": "non-ASCII name"},+ {"input": "[2f(x=1)]", "reason": "name starts with a digit"},+ {"input": "[-f(x=1)]", "reason": "name starts with a dash"},+ {"input": "[get-(x=1)]", "reason": "name ends with a dash"},+ {"input": "[a..b()]", "reason": "empty name segment"},+ {"input": "[f(x=(1, 2))]", "reason": "tuple"},+ {"input": "[f(d={k: 1})]", "reason": "dict key is not a string"},+ {"input": "[f(d={1: 2})]", "reason": "dict key is not a string"},+ {"input": "[f(s=\"\"\"a\"\"\")]", "reason": "triple quotes"},+ {"input": "[f(s=r\"a\")]", "reason": "string prefix"},+ {"input": "[f(s=\"a\" \"b\")]", "reason": "string concatenation"},+ {"input": "[f(x=g())]", "reason": "value is not a literal"},+ {"input": "[f(x=TRUE)]", "reason": "value is not a literal"},+ {"input": "[f(x=True1)]", "reason": "value is not a literal"},+ {"input": "[f(x=1) # note]", "reason": "comment"},+ {"input": "[f(x=1) ]", "reason": "non-breaking space is not whitespace"},+ {"input": "[f(x=\f1)]", "reason": "form feed is not whitespace"},+ {"input": "[f(x=[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]])]", "reason": "nested deeper than 100"},+ {"input": "[f(x=[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[1]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]])]", "reason": "101 nested lists, scalar innermost"},+ {"input": "[f(x={\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}})]", "reason": "101 nested dicts, empty innermost"},+ {"input": "[f(x={\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": {\"k\": 1}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}})]", "reason": "101 nested dicts, scalar innermost"},+ {"input": "[f(x=[{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": []}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}])]", "reason": "101 nested mixed lists and dicts, empty innermost"},+ {"input": "[f(x=[{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [{\"k\": [1]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}])]", "reason": "101 nested mixed lists and dicts, scalar innermost"},+ {"input": "[f(x=[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[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]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]])]", "reason": "5000 nested lists"},+ {"input": "[f(s=\"a\ud83db\")]", "reason": "raw lone high surrogate in a value"},+ {"input": "[f(s='\ud83d')]", "reason": "raw lone high surrogate in a value"},+ {"input": "[f(d={\"\ud83d\": 1})]", "reason": "raw lone high surrogate in a dict key"},+ {"input": "[f(s=\"a\ude80b\")]", "reason": "raw lone low surrogate in a value"},+ {"input": "[f(s='\ude80')]", "reason": "raw lone low surrogate in a value"},+ {"input": "[f(d={\"\ude80\": 1})]", "reason": "raw lone low surrogate in a dict key"},+ {"input": "[f(s=\"\ude80\ud83d\")]", "reason": "raw surrogates in the wrong order"},+ {"input": "[f(d={\"\\ud83d\": 1})]", "reason": "escaped lone surrogate in a dict key"},+ {"input": "[f(d={\"\\ude80\": 1})]", "reason": "escaped lone surrogate in a dict key"}+ ]+}tests/utils/test_chat_parsing.py73 + / 5 −
@@ -18,13 +18,15 @@ output dicts. Any divergence indicates a regression in the new executor.""" import copy+import json import random import tempfile import unittest from transformers import AutoTokenizer-from transformers.testing_utils import require_torch+from transformers.testing_utils import get_tests_dir, require_torch from transformers.utils.chat_parsing import ResponseParser, parse_response+from transformers.utils.chat_parsing.content_parsers import parse_content from transformers.utils.chat_parsing.response_parser import _coerce, _schema_types from transformers.utils.chat_parsing.response_templates import load_response_template @@ -239,6 +241,23 @@ } +lfm2_template = {+ "defaults": {"role": "assistant"},+ "start_anchor": "<|im_start|>assistant\n",+ "fields": {+ "reasoning_content": {"open": "<think>", "close": "</think>", "content": "text"},+ "tool_calls": {+ "open": "<|tool_call_start|>",+ "close": "<|tool_call_end|>",+ "content": "python-call-list",+ "transform_each": True,+ "transform": {"type": "function", "function": {"name": "{name}", "arguments": "{arguments}"}},+ },+ "content": {"close": "<|im_end|>", "content": "text"},+ },+}++ class ChatResponseTemplateParserTest(unittest.TestCase): def test_response_template_save_load(self): tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2")@@ -585,6 +604,28 @@ def test_qwen3_tool_calls(self): }, ) + def test_lfm2_tool_calls(self):+ model_out = (+ "<think>Two lookups.</think>"+ "<|tool_call_start|>[get_weather(city=\"Paris\", days=2), get_time(zone='Europe/Paris')]<|tool_call_end|>"+ "Checking.<|im_end|>"+ )+ self.assertEqual(+ parse_response(model_out, lfm2_template, prefix=""),+ {+ "role": "assistant",+ "reasoning_content": "Two lookups.",+ "tool_calls": [+ {+ "type": "function",+ "function": {"name": "get_weather", "arguments": {"city": "Paris", "days": 2}},+ },+ {"type": "function", "function": {"name": "get_time", "arguments": {"zone": "Europe/Paris"}}},+ ],+ "content": "Checking.",+ },+ )+ def test_gemma4_tool_call(self): model_out = ( "<|channel>thought\nThe user is asking for the current temperature in Paris. I should check "@@ -901,6 +942,23 @@ def test_kv_lines_parser(self): {"role": "assistant", "metadata": {"name": "alice", "age": "30"}}, ) + def test_python_call_list_parser(self):+ # The fixture is the shared test suite for every implementation of the format (e.g. Transformers.js).+ with open(get_tests_dir("fixtures/chat_parsing/python_call_list.json"), encoding="utf-8") as f:+ cases = json.load(f)+ for case in cases["accepted"]:+ with self.subTest(accepted=case["input"]):+ self.assertEqual(parse_content(case["input"], "python-call-list", {}), case["output"])+ for case in cases["rejected"]:+ with self.subTest(rejected=case["input"], reason=case["reason"]):+ with self.assertRaisesRegex(ValueError, "python-call-list"):+ parse_content(case["input"], "python-call-list", {})++ def test_python_call_list_positional_arguments_error(self):+ for text in ("[add_numbers(2, 3.5)]", "[add_numbers(a=2, 3.5)]", "[f(x)]"):+ with self.subTest(text=text), self.assertRaisesRegex(ValueError, "positional arguments"):+ parse_content(text, "python-call-list", {})+ def test_unknown_content_parser_rejected(self): bad_template = { "defaults": {"role": "assistant"},@@ -1129,6 +1187,11 @@ def test_field_without_close_runs_to_end_of_stream(self): "<|content_model_end_sampling|>" ), ),+ (+ "lfm2",+ lfm2_template,+ '<think>Look it up.</think><|tool_call_start|>[get_weather(city="Paris")]<|tool_call_end|>Checking.<|im_end|>',+ ), ( "beam", beam_template,@@ -1275,8 +1338,8 @@ def test_region_chunks_reconstruct_text_regions(self): def test_dirty_flag_marks_structured_regions(self): """A template with one text field and one structured field per parser family: text/int/float/bool stream chunks with `dirty=False`, while- json/xml-inline/kv-lines stream chunks with `dirty=True`, and those- dirty chunks concatenate to the raw region body before parsing."""+ json/xml-inline/kv-lines/python-call-list stream+ chunks with `dirty=True`, and those dirty chunks concatenate to the raw region body before parsing.""" spec = { "defaults": {"role": "assistant"}, "start_anchor": "<|assistant|>",@@ -1295,9 +1358,13 @@ def test_dirty_flag_marks_structured_regions(self): "close": "</kv>", "content": "kv-lines", },+ "py_call": {"open": "<p>", "close": "</p>", "content": "python-call-list"}, }, }- text = '<t>hello world</t><n>42</n><j>{"a": 1, "b": 2}</j><x><name=foo><age=10></x><kv>k1: v1\nk2: v2</kv>'+ text = (+ '<t>hello world</t><n>42</n><j>{"a": 1, "b": 2}</j><x><name=foo><age=10></x><kv>k1: v1\nk2: v2</kv>'+ "<p>[f(x=1)]</p>"+ ) # Drive byte-by-byte to maximise chunk count. streamer = ResponseParser(spec, prefix="") events: list[dict] = []@@ -1317,7 +1384,7 @@ def test_dirty_flag_marks_structured_regions(self): for field in ("reasoning_content", "score"): self.assertEqual(per_field_dirty[field], {False}, f"{field} should be clean") # Dirty (structured) regions.- for field in ("json_call", "xml_call", "kv_call"):+ for field in ("json_call", "xml_call", "kv_call", "py_call"): self.assertEqual(per_field_dirty[field], {True}, f"{field} should be dirty") # Dirty chunks reconstruct the raw region body (un-parsed). Clean@@ -1327,6 +1394,7 @@ def test_dirty_flag_marks_structured_regions(self): self.assertEqual("".join(per_field_chunks["json_call"]), '{"a": 1, "b": 2}') self.assertEqual("".join(per_field_chunks["xml_call"]), "<name=foo><age=10>") self.assertEqual("".join(per_field_chunks["kv_call"]), "k1: v1\nk2: v2")+ self.assertEqual("".join(per_field_chunks["py_call"]), "[f(x=1)]") def test_long_regex_open_pattern_streams_byte_by_byte(self): """Regression: a regex `open_pattern` whose full match spans well past the