sashabaranov/go-openai

OpenAI, GPT 6.1, GPT-Image-2, Whisper API clients for Go. Zero-dependency.

What it is

go‑openai is an unofficial Go client library for the OpenAI API. It lets Go programs call the same endpoints that developers use on the OpenAI platform – text generation, chat, embeddings, image creation, audio transcription, moderation, file handling, fine‑tuning, batch jobs, vector‑store helpers, and the legacy Assistants API.

Who it’s for

  • Go developers who want to integrate OpenAI models into services, CLI tools, or agents without writing raw HTTP requests.
  • Teams building AI‑augmented applications (e.g., chat bots, code assistants, RAG pipelines) that need a typed, idiomatic Go wrapper.
  • Anyone who prefers the newer Responses API (tool‑calling, multi‑turn state) but still needs backward‑compatible Chat Completion support.

Core features (as described in the README)

Area What the library provides
Responses API CreateResponse, CreateResponseStream; supports multi‑turn conversations via PreviousResponseID, streaming deltas, and a convenience GetOutputText helper.
Chat Completions CreateChatCompletion for existing integrations; returns the same shape as the official OpenAI endpoint.
Model selection Constants for the latest GPT‑6 series (GPT6Dot1Sol, GPT6Astra, GPT6Sol, GPT6Luna) plus all earlier models.
Embeddings / Images / Audio Helper methods to call the corresponding OpenAI endpoints (e.g., image generation, speech‑to‑text).
Fine‑tuning, batches, vector stores Thin wrappers around those API surfaces, making it easy to launch training jobs or manage vector data.
Azure OpenAI support DefaultAzureConfig lets you point the client at Azure‑hosted OpenAI endpoints with the required deployment mapping.
Custom HTTP config DefaultConfig lets you change base URL, headers, or swap the underlying HTTP client (useful for proxies, testing, or self‑hosted compatible servers).
Error handling Errors are typed as *openai.APIError; you can inspect HTTP status, error code, and message via errors.As.
Examples A set of runnable examples covering responses, chat, function tools, image generation, and voice‑to‑text.

How to get started (quick steps from the README)

  1. Install: go get github.com/sashabaranov/go-openai (requires Go 1.18+).
  2. Set your API key in the environment: export OPENAI_API_KEY="...".
  3. Create a client:
    client := openai.NewClient(os.Getenv("OPENAI_API_KEY"))
    
  4. Make a request – e.g., a simple response using the new Responses API:
    resp, err := client.CreateResponse(context.Background(), openai.CreateResponseRequest{
        Model:        openai.GPT5Dot6Sol,
        Instructions: "You are a concise technical explainer.",
        Input:        "Why is the sky blue?",
    })
    if err != nil { log.Fatal(err) }
    fmt.Println(resp.GetOutputText())
    
  5. Explore the examples/ folder for more complex use‑cases like streaming, multi‑turn dialogs, or image generation.

When you might choose something else

  • If you need an official, OpenAI‑maintained SDK (the official Go SDK is still in beta), you may prefer that for guaranteed version parity.
  • For very lightweight scripts where pulling in a full client feels heavyweight, a simple net/http call could suffice.

Community & maintenance

  • The project is open‑source, with CI badges for Go reference and code coverage.
  • Contributions are welcomed; see CONTRIBUTING.md for guidelines.
  • Sponsors and contributors are listed in the README, indicating active community support.

All information above is taken directly from the repository’s README; no external assumptions have been added.

Related

  • Project
  • Project
  • Project
  • Project
  • Project