JsonLiteralWithPosition) used
to extract and re-score tokens inside structured model outputs. The module also defines
compiled regular expressions for JSON structural characters, numbers, booleans, and
null values that are used throughout the Granite intrinsic formatting pipeline.
Functions
FUNC find_string_offsets
json_data: String containing valid JSON.
- Begin and end offsets of all strings in
json_data, including - the double quotes.
FUNC non_string_offsets
json_str: Original string of valid JSON data.compiled_regex: Compiled regex for the target token type.string_begins: Table of string begin offsets withinjson_str.string_ends: Table of string end offsets withinjson_str.
- List of
(begin, end, matched_string)tuples.
FUNC tokenize_json
json_str: String representation of valid JSON data.
- List of tuples of
(begin, end, value, type).
FUNC reparse_value
tokens: Token stream as produced bytokenize_json().offset: Token offset at which to start parsing.
- Tuple of
(parsed_value, next_offset).
ValueError: If an unexpected delimiter token or unknown token type is encountered at the current offset.
FUNC reparse_object
\{.
Subroutine called by :func:reparse_value when an opening curly brace is
encountered. Consumes tokens until the matching closing \} is found.
Args:
tokens: Token stream as produced bytokenize_json().offset: Token offset immediately after the opening\{delimiter.
- tuple[dict, int]: A tuple of
(parsed_dict, next_offset)whereparsed_dictmaps string keys to parsed values (possibly :class:JsonLiteralWithPositioninstances) andnext_offsetis the position of the next unconsumed token.
ValueError: If the token stream does not conform to valid JSON object syntax (e.g. missing colon, unexpected delimiter, or non-string key).
FUNC reparse_list
[.
Subroutine called by :func:reparse_value when an opening square bracket is
encountered. Consumes tokens until the matching closing ] is found.
Args:
tokens: Token stream as produced bytokenize_json().offset: Token offset immediately after the opening[delimiter.
- tuple[list, int]: A tuple of
(parsed_list, next_offset)whereparsed_listcontains the parsed elements (possibly :class:JsonLiteralWithPositioninstances) andnext_offsetis the position of the next unconsumed token.
ValueError: If the token stream does not conform to valid JSON array syntax (e.g. unexpected delimiter between elements).
FUNC reparse_json_with_offsets
json_str: String known to contain valid JSON data.
- Parsed representation of
json_str, with literals at the leaf nodes of - the parse tree replaced with
JsonLiteralWithPositioninstances containing - position information.
FUNC scalar_paths
parsed_json: JSON data parsed into native Python objects.
- A list of paths to scalar values within
parsed_json, where each - path is expressed as a tuple. The root element of a bare scalar is an empty
- tuple.
FUNC all_paths
parsed_json: JSON data parsed into native Python objects.
- A list of paths to all elements of the parse tree of
parsed_json, - where each path is expressed as a tuple. The root element of a bare scalar is
- an empty tuple.
FUNC fetch_path
json_value: Parsed JSON value.path: A tuple of names/numbers that indicates a path from root to a leaf or internal node ofjson_value.
- The node at the indicated path.
TypeError: Ifpathis not a tuple, if a path element is not a string or integer, or if an intermediate node is not a dict or list.
FUNC replace_path
json_value: Parsed JSON value.path: A tuple of names/numbers indicating a path from root to the target node.new_value: New value to place at the indicated location.
- The modified input, or
new_valueitself if the root was replaced.
TypeError: Ifpathis not a tuple, or if any error propagated from :func:fetch_pathduring path traversal.
FUNC parse_inline_json
json_response: Parsed JSON representation of aChatCompletionResponseobject.
- Deep copy of the input with JSON message content strings replaced by parsed
- Python objects.
FUNC make_begin_to_token_table
logprobs: The token log probabilities from the chat completion, orNoneif the chat completion request did not ask for logprobs.
- A dictionary mapping token begin positions to token indices,
- or
NoneiflogprobsisNone.
Classes
CLASS JsonLiteralWithPosition
JSON literal value with its position in the source string.
Attributes:
value: The parsed Python value of the JSON literal (string, boolean, integer, or float).begin: Start offset (inclusive) of the literal within the source JSON string.end: End offset (exclusive) of the literal within the source JSON string.