services/api/openai/completions

OpenAI-compatible chat completions client. Works with OpenAI, DeepSeek, and any provider that speaks the /v1/chat/completions API.

Synchronous (no streaming). Tool calling supported.

Types

pub type ChatFn =
  fn(Client, List(Message), List(ToolDefinition)) -> Result(
    ChatResponse,
    ClientError,
  )
pub type ChatResponse {
  TextResponse(
    content: String,
    reasoning_content: String,
    finish_reason: String,
  )
  ToolCallsResponse(
    reasoning_content: String,
    tool_calls: List(ToolCall),
    finish_reason: String,
  )
}

Constructors

  • TextResponse(
      content: String,
      reasoning_content: String,
      finish_reason: String,
    )
  • ToolCallsResponse(
      reasoning_content: String,
      tool_calls: List(ToolCall),
      finish_reason: String,
    )
pub type Client {
  Client(api_key: String, base_url: String, model: String)
}

Constructors

  • Client(api_key: String, base_url: String, model: String)
pub type ClientError {
  AuthError(status: Int, body: String)
  RateLimitError(status: Int, body: String, retry_after_ms: Int)
  ServerError(status: Int, body: String)
  ContextOverflowError(status: Int, body: String)
  PayloadTooLargeError(status: Int, body: String)
  NetworkError(message: String)
  JsonParseError(detail: String)
}

Constructors

  • AuthError(status: Int, body: String)
  • RateLimitError(status: Int, body: String, retry_after_ms: Int)
  • ServerError(status: Int, body: String)
  • ContextOverflowError(status: Int, body: String)
  • PayloadTooLargeError(status: Int, body: String)
  • NetworkError(message: String)
  • JsonParseError(detail: String)
pub type Message {
  SystemMessage(content: String)
  UserMessage(content: String)
  AssistantMessage(
    content: String,
    reasoning_content: String,
    tool_calls: List(ToolCall),
  )
  ToolResultMessage(
    tool_call_id: String,
    content: String,
    name: String,
  )
}

Constructors

  • SystemMessage(content: String)
  • UserMessage(content: String)
  • AssistantMessage(
      content: String,
      reasoning_content: String,
      tool_calls: List(ToolCall),
    )
  • ToolResultMessage(
      tool_call_id: String,
      content: String,
      name: String,
    )
pub type ToolCall {
  ToolCall(id: String, name: String, arguments: String)
}

Constructors

  • ToolCall(id: String, name: String, arguments: String)
pub type ToolDefinition {
  ToolDefinition(
    name: String,
    description: String,
    parameters: json.Json,
  )
}

Constructors

  • ToolDefinition(
      name: String,
      description: String,
      parameters: json.Json,
    )

Values

pub fn chat(
  client: Client,
  messages: List(Message),
  tools: List(ToolDefinition),
) -> Result(ChatResponse, ClientError)
pub fn chat_with_retry(
  client: Client,
  messages: List(Message),
  tools: List(ToolDefinition),
  max_retries: Int,
) -> Result(ChatResponse, ClientError)
pub fn classify_error(status: Int, body: String) -> ClientError

Classify an HTTP status + body into a structured ClientError variant. Only call this for error responses (non-2xx status codes).

pub fn is_retryable(error: ClientError) -> Bool

Returns True if the error is retryable (backoff + retry makes sense).

pub fn message_to_json(msg: Message) -> String
pub fn new(api_key: String) -> Client
pub fn parse_response(
  body: String,
) -> Result(ChatResponse, ClientError)
pub fn parse_retry_after(body: String) -> Int

Parse a Retry-After value in milliseconds from an error response body. Looks for “retry after” patterns and plain integer values in the body. Returns 0 if no Retry-After hint is found.

pub fn service() -> shapes.Service
pub fn should_compress(error: ClientError) -> Bool

Returns True if context compression should be triggered before retry.

pub fn tool_to_json(tool: ToolDefinition) -> String
pub fn with_base_url(client: Client, url: String) -> Client
pub fn with_model(client: Client, model: String) -> Client
Search Document