Class: Ask::Agent::StreamTransforms::ExtractJson

Inherits:
Base
  • Object
show all
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.

Examples:

pipeline.use :extract_json

# The final chunk will have chunk.extracted_json if JSON was parsed.

Instance Method Summary collapse

Methods inherited from Base

#finish

Constructor Details

#initializeExtractJson

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

Yields:

  • (chunk)


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_jsonHash?

Returns the parsed JSON data, if the full response was valid JSON.

Returns:

  • (Hash, nil)

    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.

Returns:

  • (Boolean)

    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