Method: OpenAI::Internal::Util.encode_content

Defined in:
lib/openai/internal/util.rb

.encode_content(headers, body) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • headers (Hash{String=>String})
  • body (Object)

Returns:

  • (Object)


601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/openai/internal/util.rb', line 601

def encode_content(headers, body)
  # rubocop:disable Style/CaseEquality
  # rubocop:disable Layout/LineLength
  content_type = headers["content-type"]
  case [content_type, body]
  in [OpenAI::Internal::Util::JSON_CONTENT, Hash | Array | -> { primitive?(_1) }]
    [headers, JSON.generate(body)]
  in [OpenAI::Internal::Util::JSONL_CONTENT, Enumerable] unless OpenAI::Internal::Type::FileInput === body
    [headers, body.lazy.map { JSON.generate(_1) }]
  in [%r{^multipart/form-data}, Hash | OpenAI::Internal::Type::FileInput]
    boundary, strio = encode_multipart_streaming(body)
    headers = {**headers, "content-type" => "#{content_type}; boundary=#{boundary}"}
    [headers, strio]
  in [_, Symbol | Numeric]
    [headers, body.to_s]
  in [_, StringIO]
    [headers, body.string]
  in [_, OpenAI::FilePart]
    [headers, body.content]
  else
    [headers, body]
  end
  # rubocop:enable Layout/LineLength
  # rubocop:enable Style/CaseEquality
end