Skip to content

vllm.parser.utils

Functions:

extract_json_member_object(raw, key)

Extract the raw text span of the top-level object-valued member key from a (possibly incomplete) {...} JSON wrapper.

Returns the verbatim substring (prefix-stable as the input grows, which the engine's argument-delta diffing relies on), possibly an unterminated object prefix; None when the value has not started. Raises ValueError when the member is present but its value is not a JSON object. A verbatim substring (not re-serialized JSON) is required because the engine diffs successive converter outputs and expects each to extend the previous one; the scanner is string/escape-aware so a matching literal inside another value cannot mislead it, and it recovers a partial span when the wrapper is truncated (where json.loads would fail).

Source code in vllm/parser/utils.py
def extract_json_member_object(raw: str, key: str) -> str | None:
    """Extract the raw text span of the top-level object-valued member ``key``
    from a (possibly incomplete) ``{...}`` JSON wrapper.

    Returns the verbatim substring (prefix-stable as the input grows, which
    the engine's argument-delta diffing relies on), possibly an unterminated
    object prefix; ``None`` when the value has not started. Raises
    ``ValueError`` when the member is present but its value is not a JSON
    object. A verbatim substring (not re-serialized JSON) is required because
    the engine diffs successive converter outputs and expects each to extend
    the previous one; the scanner is string/escape-aware so a matching literal
    inside another value cannot mislead it, and it recovers a partial span when
    the wrapper is truncated (where ``json.loads`` would fail)."""
    depth = 0
    in_string = False
    escape = False
    string_start = -1
    last_string: str | None = None
    for i, ch in enumerate(raw):
        if escape:
            escape = False
            continue
        if in_string:
            if ch == "\\":
                escape = True
            elif ch == '"':
                in_string = False
                if depth == 1:
                    last_string = raw[string_start + 1 : i]
            continue
        if ch == '"':
            in_string = True
            string_start = i
        elif ch == ":" and depth == 1 and last_string == key:
            value_start = i + 1
            while value_start < len(raw) and raw[value_start] in _JSON_WS:
                value_start += 1
            if value_start >= len(raw):
                return None
            if raw[value_start] != "{":
                raise ValueError(f"member {key!r} must be a JSON object")
            value_end = scan_json_object_end(raw, value_start)
            if value_end is None:
                return raw[value_start:]
            return raw[value_start:value_end]
        elif ch in "{[":
            depth += 1
        elif ch in "}]":
            depth -= 1
    return None

scan_json_object_end(raw, start)

Return the end index (exclusive) of the JSON object starting at raw[start] (which must be {), or None when it is still unterminated. String- and escape-aware so braces inside strings do not mislead the depth count.

Source code in vllm/parser/utils.py
def scan_json_object_end(raw: str, start: int) -> int | None:
    """Return the end index (exclusive) of the JSON object starting at
    ``raw[start]`` (which must be ``{``), or ``None`` when it is still
    unterminated. String- and escape-aware so braces inside strings do not
    mislead the depth count."""
    depth = 0
    in_string = False
    escape = False
    for i in range(start, len(raw)):
        ch = raw[i]
        if escape:
            escape = False
            continue
        if in_string:
            if ch == "\\":
                escape = True
            elif ch == '"':
                in_string = False
            continue
        if ch == '"':
            in_string = True
        elif ch == "{":
            depth += 1
        elif ch == "}":
            depth -= 1
            if depth == 0:
                return i + 1
    return None