Class: Phronomy::OutputParser::JsonParser

Inherits:
Base
  • Object
show all
Defined in:
lib/phronomy/output_parser/json_parser.rb

Overview

Extracts and parses JSON from LLM response text. Automatically handles Markdown code fences (json ...).

Instance Method Summary collapse

Methods inherited from Base

#invoke

Methods included from Runnable

#batch, #invoke, #stream, #trace

Instance Method Details

#parse(text) ⇒ Hash, Array

Returns result parsed with symbolize_names: true.

Parameters:

  • text (String)

Returns:

  • (Hash, Array)

    result parsed with symbolize_names: true

Raises:



13
14
15
16
17
18
# File 'lib/phronomy/output_parser/json_parser.rb', line 13

def parse(text)
  json_str = extract_json(text)
  JSON.parse(json_str, symbolize_names: true)
rescue JSON::ParserError => e
  raise Phronomy::ParseError, "Failed to parse JSON: #{e.message}\nInput: #{text}"
end