Class: OpenAI::Resources::Realtime::Calls

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/resources/realtime/calls.rb,
sig/openai/resources/realtime/calls.rbs

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ Calls

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.

Returns a new instance of Calls.

Parameters:



265
266
267
# File 'lib/openai/resources/realtime/calls.rb', line 265

def initialize(client:)
  @client = client
end

Instance Method Details

#accept(call_id, audio: nil, include: nil, instructions: nil, max_output_tokens: nil, model: nil, output_modalities: nil, parallel_tool_calls: nil, prompt: nil, reasoning: nil, tool_choice: nil, tools: nil, tracing: nil, truncation: nil, type: :realtime, request_options: {}) ⇒ nil

Accept an incoming SIP call and configure the realtime session that will handle it.

Parameters:

  • call_id (String)

    The identifier for the call provided in the realtime.call.incoming webhook.

  • audio (OpenAI::Models::Realtime::RealtimeAudioConfig)

    Configuration for input and output audio.

  • include (Array<Symbol, OpenAI::Models::Realtime::RealtimeSessionCreateRequest::Include>)

    Additional fields to include in server outputs.

    item.input_audio_transcription.logprobs: Include logprobs for input audio transcription.

  • instructions (String)

    The default system instructions (i.e. system message) prepended to model calls. This field allows the client to guide the model on desired responses. The model can be instructed on response content and format, (e.g. "be extremely succinct", "act friendly", "here are examples of good responses") and on audio behavior (e.g. "talk quickly", "inject emotion into your voice", "laugh frequently"). The instructions are not guaranteed to be followed by the model, but they provide guidance to the model on the desired behavior.

    Note that the server sets default instructions which will be used if this field is not set and are visible in the session.created event at the start of the session.

  • max_output_tokens (Integer, Symbol, :inf)

    Maximum number of output tokens for a single assistant response, inclusive of tool calls. Provide an integer between 1 and 4096 to limit output tokens, or inf for the maximum available tokens for a given model. Defaults to inf.

  • model (String, Symbol, OpenAI::Models::Realtime::RealtimeSessionCreateRequest::Model)

    The Realtime model used for this session.

  • output_modalities (Array<Symbol, OpenAI::Models::Realtime::RealtimeSessionCreateRequest::OutputModality>)

    The set of modalities the model can respond with. It defaults to ["audio"], indicating that the model will respond with audio plus a transcript. ["text"] can be used to make the model respond with text only. It is not possible to request both text and audio at the same time.

  • parallel_tool_calls (Boolean)

    Whether the model may call multiple tools in parallel. Only supported by reasoning Realtime models such as gpt-realtime-2.

  • prompt (OpenAI::Models::Responses::ResponsePrompt, nil)

    Reference to a prompt template and its variables. Learn more.

  • reasoning (OpenAI::Models::Realtime::RealtimeReasoning)

    Configuration for reasoning-capable Realtime models such as gpt-realtime-2.

  • tool_choice (Symbol, OpenAI::Models::Responses::ToolChoiceOptions, OpenAI::Models::Responses::ToolChoiceFunction, OpenAI::Models::Responses::ToolChoiceMcp)

    How the model chooses tools. Provide one of the string modes or force a specific function/MCP tool.

  • tools (Array<OpenAI::Models::Realtime::RealtimeFunctionTool, OpenAI::Models::Realtime::RealtimeToolsConfigUnion::Mcp>)

    Tools available to the model.

  • tracing (Symbol, :auto, OpenAI::Models::Realtime::RealtimeTracingConfig::TracingConfiguration, nil)

    Realtime API can write session traces to the Traces Dashboard. Set to null to disable tracing. Once tracing is enabled for a session, the configuration cannot be modified.

    auto will create a trace for the session with default values for the workflow name, group id, and metadata.

  • truncation (Symbol, OpenAI::Models::Realtime::RealtimeTruncation::RealtimeTruncationStrategy, OpenAI::Models::Realtime::RealtimeTruncationRetentionRatio)

    When the number of tokens in a conversation exceeds the model's input token limit, the conversation be truncated, meaning messages (starting from the oldest) will not be included in the model's context. A 32k context model with 4,096 max output tokens can only include 28,224 tokens in the context before truncation occurs.

    Clients can configure truncation behavior to truncate with a lower max token limit, which is an effective way to control token usage and cost.

    Truncation will reduce the number of cached tokens on the next turn (busting the cache), since messages are dropped from the beginning of the context. However, clients can also configure truncation to retain messages up to a fraction of the maximum context size, which will reduce the need for future truncations and thus improve the cache rate.

    Truncation can be disabled entirely, which means the server will never truncate but would instead return an error if the conversation exceeds the model's input token limit.

  • type (Symbol, :realtime)

    The type of session to create. Always realtime for the Realtime API.

  • request_options (OpenAI::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

  • (nil)

See Also:



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/openai/resources/realtime/calls.rb', line 164

def accept(call_id, params)
  parsed, options = OpenAI::Realtime::CallAcceptParams.dump_request(params)
  @client.request(
    method: :post,
    path: ["realtime/calls/%1$s/accept", call_id],
    body: parsed,
    model: NilClass,
    security: {bearer_auth: true},
    options: options
  )
end

#create(sdp:, session: nil, request_options: {}) ⇒ OpenAI::HTTPClient::Response

Create a new Realtime API call over WebRTC and receive the SDP answer needed to complete the peer connection.

Parameters:

Returns:

See Also:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/openai/resources/realtime/calls.rb', line 23

def create(params)
  parsed, options = OpenAI::Realtime::CallCreateParams.dump_request(params)
  normalize_keys = lambda do |value|
    case value
    when Hash
      value.to_h { |key, child| [key.to_s, normalize_keys.call(child)] }
    when Array
      value.map { |child| normalize_keys.call(child) }
    else
      value
    end
  end

  multipart_body = OpenAI::Internal::Util.deep_merge(
    normalize_keys.call(parsed),
    normalize_keys.call(options[:extra_body] || {})
  )
  options = options.except(:extra_body)
  if multipart_body.key?("sdp")
    multipart_body["sdp"] = OpenAI::FilePart.new(multipart_body["sdp"], content_type: "application/sdp")
  end

  if multipart_body.key?("session")
    multipart_body["session"] = OpenAI::FilePart.new(
      JSON.generate(multipart_body["session"]),
      content_type: "application/json"
    )
  end

  @client.request(
    method: :post,
    path: "realtime/calls",
    headers: {"content-type" => "multipart/form-data", "accept" => "application/sdp"},
    body: multipart_body,
    model: OpenAI::HTTPClient::Response,
    security: {bearer_auth: true},
    options: {**options, max_retries: options[:max_retries] || 0}
  )
end

#hangup(call_id, request_options: {}) ⇒ nil

End an active Realtime API call, whether it was initiated over SIP or WebRTC.

Parameters:

Returns:

  • (nil)

See Also:



192
193
194
195
196
197
198
199
200
# File 'lib/openai/resources/realtime/calls.rb', line 192

def hangup(call_id, params = {})
  @client.request(
    method: :post,
    path: ["realtime/calls/%1$s/hangup", call_id],
    model: NilClass,
    security: {bearer_auth: true},
    options: params[:request_options]
  )
end

#refer(call_id, target_uri:, request_options: {}) ⇒ nil

Transfer an active SIP call to a new destination using the SIP REFER verb.

Parameters:

Returns:

  • (nil)

See Also:



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/openai/resources/realtime/calls.rb', line 220

def refer(call_id, params)
  parsed, options = OpenAI::Realtime::CallReferParams.dump_request(params)
  @client.request(
    method: :post,
    path: ["realtime/calls/%1$s/refer", call_id],
    body: parsed,
    model: NilClass,
    security: {bearer_auth: true},
    options: options
  )
end

#reject(call_id, status_code: nil, request_options: {}) ⇒ nil

Decline an incoming SIP call by returning a SIP status code to the caller.

Parameters:

  • call_id (String)

    The identifier for the call provided in the realtime.call.incoming webhook.

  • status_code (Integer)

    SIP response code to send back to the caller. Defaults to 603 (Decline) when omitted.

  • request_options (OpenAI::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

  • (nil)

See Also:



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/openai/resources/realtime/calls.rb', line 250

def reject(call_id, params = {})
  parsed, options = OpenAI::Realtime::CallRejectParams.dump_request(params)
  @client.request(
    method: :post,
    path: ["realtime/calls/%1$s/reject", call_id],
    body: parsed,
    model: NilClass,
    security: {bearer_auth: true},
    options: options
  )
end