Skip to main content

github.com/digitallysavvy/go-aiApache-2.0

The AI SDK,
in Go

Feature parity with Vercel’s AI SDK, in idiomatic Go. Generate, stream, call tools, run agents and serve chat UIs across 49 providers.

go get github.com/digitallysavvy/go-ai
main.go
package main

import (
"context"
"fmt"
"log"
"os"

"github.com/digitallysavvy/go-ai/pkg/ai"
"github.com/digitallysavvy/go-ai/pkg/providers/anthropic"
)

func main() {
p := anthropic.New(anthropic.Config{APIKey: os.Getenv("ANTHROPIC_API_KEY")})
model, err := p.LanguageModel(anthropic.ClaudeSonnet4_6)
if err != nil {
log.Fatal(err)
}

result, err := ai.GenerateText(context.Background(), ai.GenerateTextOptions{
Model: model,
Prompt: "Explain Go's context package in two sentences.",
})
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Text)
}

Prompt in, text out. The result also carries usage, steps and tool calls when there are any.

Generating text docs

go list ./pkg/...

Everything the AI SDK does, as Go packages.

The module is organised the way you will import it. Each row below is a package path and the exported names you will actually call.

pkg/ai

Text generation and streaming

One call for a full result, or a channel of chunks while the model is still writing.

  • GenerateText
  • StreamText
  • Chunks()
pkg/ai

Structured output

Typed objects from a Go struct or a JSON schema, generated in one shot or streamed as they fill in.

  • GenerateObject
  • StreamObject
  • ObjectOutput[T]
  • SchemaFor[T]
pkg/agent

Tools and multi-step agents

Tools are plain Go funcs with a schema. The tool loop runs them until your stop condition says otherwise.

  • NewToolLoopAgent
  • AgentConfig
  • StopWhen
  • StepCountIs
pkg/ai

Embeddings and rerank

Embed one value or a batch, and rerank documents against a query, through the same provider packages.

  • Embed
  • EmbedMany
  • Rerank
pkg/ai

Image, speech, transcription, video

Media generation uses the same options-struct shape as text, so the call site looks familiar.

  • GenerateImage
  • GenerateSpeech
  • Transcribe
  • GenerateVideo
pkg/mcp

MCP client

Connect to Model Context Protocol servers and expose their tools to the model.

  • NewMCPClient
  • NewHTTPTransport
  • ConvertToGoAITools
pkg/middleware

Middleware

Wrap a model or a whole provider to transform params, results and streams. Built-ins included.

  • WrapLanguageModel
  • WrapProvider
  • ExtractReasoningMiddleware
  • DefaultSettingsMiddleware
pkg/telemetry

Telemetry

OpenTelemetry spans for generation calls, plus a registry for pluggable telemetry integrations.

  • GetTracer
  • RegisterTelemetryIntegration
pkg/providers/gateway

AI Gateway

Language, embedding, image, speech, transcription, video and reranking models through the AI Gateway.

  • gateway.New
pkg/harness

Coding-agent harnesses

Drive coding agents from Go over the harness-v1 protocol, with bridges for Claude Code, Codex, OpenCode, GitHub Copilot and more.

  • harness.Session
  • bridges

pkg/providers

49 provider packages.

Every provider is its own package that returns the shared model interfaces. Swapping providers is a change to the import and the constructor; the calls stay the same.

Names are the package directories under pkg/providers. Provider overview

pkg/ai · UIMessage

Serve the AI SDK UI from Go.

ai.UIMessage mirrors the TypeScript UIMessage shape, with the same JSON keys and part types, and ai.PipeUIMessageStreamToResponse writes the UI message stream as server-sent events. Point an AI SDK front end such as useChat at a Go route.

  • ConvertToModelMessages turns incoming UI messages into model messages.
  • CreateUIMessageStreamResponse builds a ready-made *http.Response when you are not inside a handler.
Stream transport helpers
server.go
package main

import (
"encoding/json"
"log"
"net/http"
"os"

"github.com/digitallysavvy/go-ai/pkg/ai"
"github.com/digitallysavvy/go-ai/pkg/providers/anthropic"
)

func main() {
p := anthropic.New(anthropic.Config{APIKey: os.Getenv("ANTHROPIC_API_KEY")})
model, err := p.LanguageModel(anthropic.ClaudeSonnet4_6)
if err != nil {
log.Fatal(err)
}

http.HandleFunc("/api/chat", func(w http.ResponseWriter, r *http.Request) {
var body struct {
Messages []ai.UIMessage `json:"messages"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
messages, err := ai.ConvertToModelMessages(r.Context(), body.Messages)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

stream, err := ai.StreamText(r.Context(), ai.StreamTextOptions{
Model: model,
Messages: messages,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("X-Vercel-AI-UI-Message-Stream", "v1")
if err := ai.PipeUIMessageStreamToResponse(r.Context(), stream, w); err != nil {
log.Println(err)
}
})

log.Fatal(http.ListenAndServe(":8080", nil))
}

faq

Questions about the Go AI SDK

What is the Go AI SDK?

The Go AI SDK is an open-source Go library for building AI applications and agents. One API covers text generation and streaming, structured output, tool calling, agents, embeddings, image and speech generation, and MCP, across 49 model providers.

How does it relate to the Vercel AI SDK?

It is a Go port of the TypeScript AI SDK from Vercel. It keeps the same concepts and names (GenerateText, StreamText, tools, UIMessage) in idiomatic Go and tracks TypeScript releases; the few intentional differences are documented in the migration guides.

Which model providers does it support?

49 provider packages, including OpenAI, Anthropic, Google Gemini, Google Vertex AI, Amazon Bedrock, Azure OpenAI, Mistral, xAI Grok, Groq, DeepSeek, Cohere, Together AI, Fireworks, Perplexity and Ollama. Each provider returns the same model interfaces, so switching providers only changes the constructor.

Can a React or Next.js frontend using useChat talk to a Go backend?

Yes. The Go AI SDK speaks the same UI message stream protocol as the TypeScript SDK, so a useChat frontend pointed at a Go handler that calls ai.PipeUIMessageStreamToResponse works unmodified.

Does it support AI agents and the Model Context Protocol (MCP)?

Yes. pkg/agent runs multi-step tool-calling agents with stop conditions and approvals, and pkg/mcp connects to MCP servers and exposes their tools to any model.

What are the requirements and license?

Go 1.26 or later. Install with go get github.com/digitallysavvy/go-ai. The SDK is open source under the Apache 2.0 license.

Are the docs available for AI coding agents?

Yes. Every docs page is available as markdown by adding .md to its URL, llms.txt lists every page, and llms-full.txt contains the full documentation in one file.

Start with one file.

Install the module, set a provider key, and run the first example from the getting-started guide.

go get github.com/digitallysavvy/go-ai