pig/openai

Streaming OpenAI-compatible Chat Completions and Responses providers.

HTTP bytes are owned by pig_transport; SSE framing and provider accumulation are owned by pig_protocol. This module only coordinates those two seams and emits normalized provider events.

Types

The OpenAI API used for inference.

pub type OpenAIApi {
  ChatCompletions
  Responses
}

Constructors

  • ChatCompletions
  • Responses

Values

pub fn build_request_body(
  messages: List(message.Message),
  tools: List(tool_definition.ToolDefinition),
  model: String,
) -> String

Build the JSON request body for a streaming Chat Completions request. Pure function - no IO.

pub fn build_request_body_with_thinking(
  messages: List(message.Message),
  tools: List(tool_definition.ToolDefinition),
  model: String,
  thinking_level: option.Option(thinking.ThinkingLevel),
) -> String

Build a streaming Chat Completions request with a thinking level.

pub fn build_responses_request_body(
  messages: List(message.Message),
  tools: List(tool_definition.ToolDefinition),
  model: String,
  instructions: option.Option(String),
) -> String

Build a streaming Responses request body.

pub fn build_responses_request_body_with_thinking(
  messages: List(message.Message),
  tools: List(tool_definition.ToolDefinition),
  model: String,
  instructions: option.Option(String),
  thinking_level: option.Option(thinking.ThinkingLevel),
) -> String

Build a streaming Responses request body with a thinking level.

pub const default_base_url: String

The default OpenAI base URL.

pub const default_http_timeout_ms: Int

The default HTTP timeout for OpenAI API calls (120 seconds).

pub fn parse_response(
  raw: String,
) -> Result(inference.InferenceResult, error.AiError)

Parse an OpenAI Chat Completions JSON response into an InferenceResult. Pure function - retained for callers handling captured non-stream data.

pub fn provider(
  api_key: String,
  model: String,
) -> provider.Provider

Create a streaming Chat Completions provider.

pub fn provider_with_base_url(
  api_key: String,
  model: String,
  base_url: String,
) -> provider.Provider

Create a streaming Chat Completions provider with a custom base URL.

pub fn provider_with_base_url_and_timeout(
  api_key: String,
  model: String,
  base_url: String,
  http_timeout_ms: Int,
) -> provider.Provider

Create a streaming Chat Completions provider with a custom timeout.

pub fn provider_with_transport(
  api: OpenAIApi,
  api_key: String,
  model: String,
  base_url: String,
  http_timeout_ms: Int,
  transport: pig_transport.Transport,
) -> provider.Provider

Create a provider with a scripted or custom shared transport.

pub fn responses_provider(
  api_key: String,
  model: String,
) -> provider.Provider

Create a streaming Responses provider with the default OpenAI base URL.

pub fn responses_provider_with_base_url(
  api_key: String,
  model: String,
  base_url: String,
) -> provider.Provider

Create a streaming Responses provider with a custom base URL.

pub fn responses_provider_with_base_url_and_timeout(
  api_key: String,
  model: String,
  base_url: String,
  http_timeout_ms: Int,
) -> provider.Provider

Create a streaming Responses provider with a custom timeout.

pub fn with_default_thinking_level(
  openai_provider: provider.Provider,
  level: thinking.ThinkingLevel,
) -> provider.Provider

Set the fallback thinking level for calls made by this provider.

A request-level setting overrides this default.

pub fn with_http_timeout(
  openai_provider: provider.Provider,
  timeout_ms: Int,
) -> provider.Provider

Update an OpenAI-compatible provider’s HTTP timeout.

Search Document