module Anthropic::Internal::Util

def decode_content(headers, stream:, suppress_error: false)

Returns:
  • (Object) -

Raises:
  • (JSON::ParserError) -

Parameters:
  • suppress_error (Boolean) --
  • stream (Enumerable) --
  • headers (Hash{String=>String}, Net::HTTPHeader) --

Other tags:
    Api: - private
def decode_content(headers, stream:, suppress_error: false)
  case (content_type = headers["content-type"])
  in Anthropic::Internal::Util::JSON_CONTENT
    json = stream.to_a.join
    begin
      JSON.parse(json, symbolize_names: true)
    rescue JSON::ParserError => e
      raise e unless suppress_error
      json
    end
  in Anthropic::Internal::Util::JSONL_CONTENT
    lines = decode_lines(stream)
    chain_fused(lines) do |y|
      lines.each { y << JSON.parse(_1, symbolize_names: true) }
    end
  in %r{^text/event-stream}
    lines = decode_lines(stream)
    decode_sse(lines)
  else
    text = stream.to_a.join
    force_charset!(content_type, text: text)
    StringIO.new(text)
  end
end