OpenAI.Responses.Client (openai_responses v0.3.0)

HTTP client for the OpenAI Responses API.

This module handles the communication with OpenAI's API, including authentication, request formatting, and response parsing.

Summary

Functions

Creates a new API client.

Streams a response from the OpenAI API.

Functions

new(opts \\ [])

@spec new(keyword()) :: map()

Creates a new API client.

Options

  • :api_key - Your OpenAI API key (overrides environment variable)
  • :api_base - The base URL for API requests (default: "https://api.openai.com/v1")
  • :req_options - Additional options to pass to Req
  • :request_logger - Optional 1-arity function to log requests. Receives the Req.Request struct before the request is sent.

request(client, method, path, body \\ nil, query \\ %{})

@spec request(map(), atom(), String.t(), map() | nil, map()) ::
  {:ok, map()} | {:error, any()}

Makes a request to the OpenAI API.

Parameters

  • client - The client from new/1
  • method - The HTTP method (:get, :post, :delete, etc.)
  • path - The API path (e.g., "/responses")
  • body - The request body (for POST, PUT, etc.)
  • query - Query parameters

Returns

  • {:ok, response} - On success
  • {:error, error} - On failure

stream(client, path, body)

@spec stream(map(), String.t(), map()) :: Enumerable.t()

Streams a response from the OpenAI API.

Uses Req's built-in streaming capabilities. This implementation creates a proper Elixir Stream that processes events as they arrive from the API.

Parameters

  • client - The client from new/1
  • path - The API path (e.g., "/responses")
  • body - The request body

Returns

  • A stream of events that can be processed in real-time