openai/openai-go

The official Go library for the OpenAI API

openai/openai-go

What it is

  • An official Go client library for the OpenAI REST API. It lets Go programs call OpenAI models (ChatGPT, GPT‑4o, etc.) for text generation, tool calling, streaming, structured outputs, and WebSocket‑based real‑time responses.

Key features

  • Simple openai.NewClient construction with API‑key handling (env var fallback).
  • High‑level helpers for the Responses API (the newest unified endpoint) including:
    • Single‑turn and multi‑turn conversations.
    • Conversation objects for stateful chats.
    • Streaming responses via NewStreaming.
    • Tool/function calling support.
    • Structured‑output generation using JSON‑Schema.
  • WebSocket support for low‑latency, incremental response delivery, lane‑based concurrency, and recovery handling.
  • Compatibility shim for the older Chat Completions API.
  • Typed request/response structs generated from the OpenAI spec, with helper functions like openai.String, openai.Bool.

Typical usage

import (
    "context"
    "github.com/openai/openai-go/v3"
    "github.com/openai/openai-go/v3/option"
    "github.com/openai/openai-go/v3/responses"
)

func main() {
    ctx := context.Background()
    client := openai.NewClient(option.WithAPIKey("YOUR_KEY"))
    resp, _ := client.Responses.New(ctx, responses.ResponseNewParams{
        Model: openai.ChatModelGPT5_2,
        Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Write a haiku about computers")},
    })
    println(resp.OutputText())
}

The README includes many more examples (multi‑turn, conversations, streaming, tool calling, structured outputs, WebSocket usage).

Installation

# Import path in code
import "github.com/openai/openai-go/v3"
# Or fetch a specific version
go get -u 'github.com/openai/openai-go/v3@v3.66.0'

Requires Go 1.25+ (or pin to v3.44.0 for Go 1.22‑1.24).

Links


All details are taken directly from the repository’s README; no additional claims are made.

Related

  • Project
  • Project
  • Project
  • Project
  • Project