Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Spcl Graph of thoughts Keyword Counting Response Parsing

From Leeroopedia
Knowledge Sources
Domains Response_Parsing, Keyword_Counting
Related Implementations Implementation:Spcl_Graph_of_thoughts_KeywordCountingParser
Last Updated 2026-02-14

Overview

Domain-specific parsing pattern for extracting keyword frequency dictionaries from LLM text responses.

Description

The Keyword Counting Response Parsing principle defines how raw LLM text output is transformed into structured thought state dictionaries containing JSON frequency counts of country names. The parser must handle multiple output formats -- paragraph-split JSON for decomposition, frequency dictionary JSON for counting, and combined dictionaries for aggregation -- while gracefully recovering from malformed responses.

Core Parsing Strategy: strip_answer_json

A central helper method underpins all parsing operations. It applies a consistent extraction pipeline to any LLM response:

  1. If the text contains "Output:", strip everything before it.
  2. Locate the last occurrence of { and } in the remaining text.
  3. Extract the substring between those positions (inclusive).
  4. Attempt to parse as JSON. If parsing fails, return "{}" (empty dictionary).

This "last JSON object" strategy ensures robustness when the LLM includes intermediate reasoning, multiple dictionary attempts, or extraneous text before the final answer.

Parsing by Operation Type

Generate Parsing (parse_generate_answer): Two distinct paths:

  • GoT Phase 0 (Split): The LLM returns a JSON object with keys like "Paragraph 1"..."Paragraph 4" (or "Sentence 1"..."Sentence N"). The parser extracts the JSON, iterates over its keys, and creates a new thought state for each paragraph/sentence. Each state gets phase=1, a part identifier (the key), and sub_text (the paragraph/sentence text). The current field is set to empty string since counting has not yet occurred.
  • All other phases: The response is a frequency dictionary. The parser applies strip_answer_json and sets the result as current with phase=2.

Aggregation Parsing (parse_aggregation_answer): Processes the result of merging two frequency dictionaries:

  • Extracts the JSON dictionary from the response.
  • Concatenates sub_text fields from both input states (for sub-passage tracking).
  • Preserves the pre-aggregation dictionaries in aggr1 and aggr2 fields, which the improve prompt needs for validation.
  • Handles edge cases where 0 or 1 input states exist by substituting empty dictionaries.

Improve Parsing (parse_improve_answer): Parses the corrected dictionary from a validation-and-improve response. Asserts exactly one response text, extracts JSON, and returns an updated state.

Error Handling

  • All JSON extraction uses the "last braces" strategy to skip intermediate text.
  • Failed JSON parsing falls back to "{}" (empty dictionary) rather than raising exceptions.
  • Exceptions during generate answer parsing are caught and logged, resulting in no new states for that response.
  • Warning-level keys: missing "Paragraph" or "Sentence" keys in split responses are logged but processing continues.

Phase Transitions

  • Split responses (phase 0) produce states with phase=1 and populated sub_text.
  • Count responses (phase 1 and non-GoT) produce states with phase=2 and populated current.
  • Aggregation does not change phase -- the aggregated state inherits from the first input state.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment