Class: Ask::Agent::StreamTransforms::ExtractJson
- Defined in:
- lib/ask/agent/stream_transforms/extract_json.rb
Overview
Attempts to parse JSON from a streaming LLM text response.
When the LLM is instructed to return JSON but structured output (via schema) is not used, the model may emit text that happens to be valid JSON. This transform buffers the text, attempts a JSON parse on each addition, and emits a special chunk with the parsed data when valid JSON is found.
This is useful for fallback scenarios where you want structured data without requiring native structured output support from the provider.
Instance Method Summary collapse
- #call(chunk) {|chunk| ... } ⇒ Object
-
#extracted_json ⇒ Hash?
The parsed JSON data, if the full response was valid JSON.
-
#initialize ⇒ ExtractJson
constructor
A new instance of ExtractJson.
-
#json? ⇒ Boolean
Whether the accumulated response was valid JSON.
Methods inherited from Base
Constructor Details
#initialize ⇒ ExtractJson
Returns a new instance of ExtractJson.
25 26 27 28 |
# File 'lib/ask/agent/stream_transforms/extract_json.rb', line 25 def initialize @buffer = +"" @parsed = nil end |
Instance Method Details
#call(chunk) {|chunk| ... } ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ask/agent/stream_transforms/extract_json.rb', line 30 def call(chunk, &block) if chunk.content.to_s.strip.length > 0 @buffer << chunk.content # Attempt to parse the accumulated buffer as JSON begin @parsed = JSON.parse(@buffer) rescue JSON::ParserError # Not valid JSON yet — keep buffering end end # Pass the original chunk through yield chunk end |
#extracted_json ⇒ Hash?
Returns the parsed JSON data, if the full response was valid JSON.
47 48 49 |
# File 'lib/ask/agent/stream_transforms/extract_json.rb', line 47 def extracted_json @parsed end |
#json? ⇒ Boolean
Returns whether the accumulated response was valid JSON.
52 53 54 |
# File 'lib/ask/agent/stream_transforms/extract_json.rb', line 52 def json? !@parsed.nil? end |