Text generation and streaming
One call for a full result, or a channel of chunks while the model is still writing.
- GenerateText
- StreamText
- Chunks()

github.com/digitallysavvy/go-aiApache-2.0
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-aipackage 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 docsgo list ./pkg/...
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.
One call for a full result, or a channel of chunks while the model is still writing.
Typed objects from a Go struct or a JSON schema, generated in one shot or streamed as they fill in.
Tools are plain Go funcs with a schema. The tool loop runs them until your stop condition says otherwise.
Embed one value or a batch, and rerank documents against a query, through the same provider packages.
Media generation uses the same options-struct shape as text, so the call site looks familiar.
Connect to Model Context Protocol servers and expose their tools to the model.
Wrap a model or a whole provider to transform params, results and streams. Built-ins included.
OpenTelemetry spans for generation calls, plus a registry for pluggable telemetry integrations.
Language, embedding, image, speech, transcription, video and reranking models through the AI Gateway.
Drive coding agents from Go over the harness-v1 protocol, with bridges for Claude Code, Codex, OpenCode, GitHub Copilot and more.
pkg/providers
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
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.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
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.
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.
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.
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.
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.
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.
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.
Install the module, set a provider key, and run the first example from the getting-started guide.
go get github.com/digitallysavvy/go-ai