MCPcopy Create free account
hub / github.com/abetlen/llama-cpp-python / _advance_json_scanner

Method _advance_json_scanner

examples/server/server.py:5419–5477  ·  view source on GitHub ↗
(
        item_state: Dict[str, Any],
        text: str,
        *,
        schema_type: Optional[str],
    )

Source from the content-addressed store, hash-verified

5417
5418 @staticmethod
5419 def _advance_json_scanner(
5420 item_state: Dict[str, Any],
5421 text: str,
5422 *,
5423 schema_type: Optional[str],
5424 ) -> bool:
5425 started = cast(bool, item_state["json_started"])
5426 complete = cast(bool, item_state["json_complete"])
5427 depth = cast(int, item_state["json_depth"])
5428 in_string = cast(bool, item_state["json_in_string"])
5429 escaped = cast(bool, item_state["json_escaped"])
5430 for char in text:
5431 if complete:
5432 if not char.isspace():
5433 return False
5434 continue
5435 if not started:
5436 if char.isspace():
5437 continue
5438 if schema_type == "object" and char != "{":
5439 return False
5440 if schema_type == "array" and char != "[":
5441 return False
5442 if char == "{":
5443 started = True
5444 depth = 1
5445 continue
5446 if char == "[":
5447 started = True
5448 depth = 1
5449 continue
5450 return False
5451 if in_string:
5452 if escaped:
5453 escaped = False
5454 elif char == "\\":
5455 escaped = True
5456 elif char == '"':
5457 in_string = False
5458 continue
5459 if char == '"':
5460 in_string = True
5461 continue
5462 if char in "{[":
5463 depth += 1
5464 continue
5465 if char in "}]":
5466 depth -= 1
5467 if depth < 0:
5468 return False
5469 if depth == 0:
5470 complete = True
5471 continue
5472 item_state["json_started"] = started
5473 item_state["json_complete"] = complete
5474 item_state["json_depth"] = depth
5475 item_state["json_in_string"] = in_string
5476 item_state["json_escaped"] = escaped

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected