The Go AI SDK is a comprehensive toolkit designed to help you build AI-powered applications and agents using Go. It provides 1:1 feature parity with the Vercel AI SDK for backend functionality.
To learn more about how to use the Go AI SDK, check out our Documentation.
You will need Go 1.21+ installed on your local development machine.
go get github.com/digitallysavvy/go-aiThe Go AI SDK provides a unified API to interact with model providers like OpenAI, Anthropic, Google, and more.
go get github.com/digitallysavvy/go-ai/pkg/providers/openai
go get github.com/digitallysavvy/go-ai/pkg/providers/anthropic
go get github.com/digitallysavvy/go-ai/pkg/providers/googleimport (
"context"
"fmt"
"os"
"github.com/digitallysavvy/go-ai/pkg/ai"
"github.com/digitallysavvy/go-ai/pkg/providers/openai"
)
func main() {
ctx := context.Background()
provider := openai.New(openai.Config{
APIKey: os.Getenv("OPENAI_API_KEY"),
})
model, _ := provider.LanguageModel("gpt-4")
result, _ := ai.GenerateText(ctx, ai.GenerateTextOptions{
Model: model,
Prompt: "What is an agent?",
})
fmt.Println(result.Text)
}stream, _ := ai.StreamText(ctx, ai.StreamTextOptions{
Model: model,
Prompt: "Write a story about Go programming",
})
for chunk := range stream.TextChannel {
fmt.Print(chunk)
}import "github.com/digitallysavvy/go-ai/pkg/schema"
type Recipe struct {
Name string `json:"name"`
Ingredients []string `json:"ingredients"`
Steps []string `json:"steps"`
}
recipeSchema := schema.NewSimpleJSONSchema(map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"name": map[string]interface{}{"type": "string"},
"ingredients": map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "string"}},
"steps": map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "string"}},
},
"required": []string{"name", "ingredients", "steps"},
})
result, _ := ai.GenerateObject(ctx, ai.GenerateObjectOptions{
Model: model,
Prompt: "Generate a lasagna recipe.",
Schema: recipeSchema,
})
var recipe Recipe
json.Unmarshal([]byte(result.Object), &recipe)
fmt.Printf("Recipe: %s\n", recipe.Name)Build autonomous agents with multi-step reasoning:
import (
"github.com/digitallysavvy/go-ai/pkg/agent"
"github.com/digitallysavvy/go-ai/pkg/provider/types"
)
myAgent := agent.New(agent.Config{
Model: model,
Instructions: "You are a helpful research assistant.",
Tools: []types.Tool{
searchTool,
calculatorTool,
},
MaxSteps: 10,
})
result, _ := myAgent.Execute(ctx, "What is the population of Tokyo?")
fmt.Println(result.Text)Extend AI capabilities with custom tools:
import "github.com/digitallysavvy/go-ai/pkg/provider/types"
weatherTool := types.Tool{
Name: "get_weather",
Description: "Get current weather for a location",
Parameters: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"location": map[string]interface{}{
"type": "string",
"description": "City name",
},
},
"required": []string{"location"},
},
Execute: func(ctx context.Context, params map[string]interface{}, opts types.ToolExecutionOptions) (interface{}, error) {
location := params["location"].(string)
return map[string]interface{}{
"temperature": 72,
"condition": "sunny",
}, nil
},
}
result, _ := ai.GenerateText(ctx, ai.GenerateTextOptions{
Model: model,
Prompt: "What's the weather in San Francisco?",
Tools: []types.Tool{weatherTool},
})Generate embeddings for semantic search:
embeddingModel, _ := provider.EmbeddingModel("text-embedding-3-small")
result, _ := ai.Embed(ctx, ai.EmbedOptions{
Model: embeddingModel,
Input: "Go is great for building AI applications",
})
// result.Embedding contains the vectorimageModel, _ := provider.ImageModel("dall-e-3")
result, _ := ai.GenerateImage(ctx, ai.GenerateImageOptions{
Model: imageModel,
Prompt: "A serene mountain landscape at sunset",
Size: "1024x1024",
})
// result.Image contains the generated image bytes// Generate speech
speechModel, _ := provider.SpeechModel("tts-1")
result, _ := ai.GenerateSpeech(ctx, ai.GenerateSpeechOptions{
Model: speechModel,
Text: "Hello, welcome to the Go AI SDK!",
Voice: "alloy",
})
// Transcribe audio
transcriptionModel, _ := provider.TranscriptionModel("whisper-1")
transcript, _ := ai.Transcribe(ctx, ai.TranscribeOptions{
Model: transcriptionModel,
Audio: audioBytes,
})Reduce memory consumption by 50-80% for image-heavy or large-context workloads using retention settings:
import "github.com/digitallysavvy/go-ai/pkg/provider/types"
// Enable memory optimization
retention := &types.RetentionSettings{
RequestBody: types.BoolPtr(false), // Don't retain request
ResponseBody: types.BoolPtr(false), // Don't retain response
}
result, _ := ai.GenerateText(ctx, ai.GenerateTextOptions{
Model: model,
Prompt: "Analyze this image...",
ExperimentalRetention: retention,
})
// Result still has usage, finish reason, text, etc.
// But raw request/response bodies are excluded to save memoryPerfect for:
- πΌοΈ Image processing workloads
- π Large document analysis
- π Privacy-sensitive applications
- β‘ Long-running services
See examples/features/retention for detailed usage.
The Go AI SDK supports 26+ providers:
| Provider | Language Models | Embeddings | Images | Speech |
|---|---|---|---|---|
| OpenAI | GPT-4, GPT-3.5, O1 | β | DALL-E | TTS, Whisper |
| Anthropic | Claude 3.5 Sonnet, Claude 3 | - | - | - |
| Gemini Pro, Flash | β | - | - | |
| AWS Bedrock | Claude, Titan, Llama | β | - | - |
| Azure OpenAI | Azure-hosted models | β | β | β |
| Mistral | Large, Medium, Small | β | - | - |
| Cohere | Command R+, Command | β | - | - |
| Groq | Llama, Mixtral | - | - | Whisper |
| Together AI | Llama, Mixtral, Qwen | - | Stable Diffusion | - |
| Fireworks | Llama, Mixtral | β | - | - |
| Perplexity | Sonar models | - | - | - |
| DeepSeek | DeepSeek Chat, Coder | - | - | - |
| xAI | Grok | - | - | - |
| Replicate | All hosted models | - | β | - |
| Hugging Face | Inference API | - | - | - |
| Ollama | Local models | β | - | - |
And many more...
- β Unified API - One interface for 26+ providers
- β
Text Generation -
GenerateText()andStreamText() - β
Structured Output - Type-safe
GenerateObject()with JSON validation - β Tool Calling - Extend models with custom functions
- β
Agents - Autonomous multi-step reasoning with
ToolLoopAgent - β Embeddings - Generate and search with vector embeddings
- β Image Generation - Text-to-image with multiple providers
- β Speech - TTS and transcription capabilities
- β Middleware - Logging, caching, rate limiting, and more
- β Telemetry - Built-in OpenTelemetry integration
- β
Registry - Resolve models by string ID (e.g.,
"openai:gpt-4") - β Context Support - Native Go context cancellation and timeouts
- β Streaming - Real-time responses with automatic backpressure
- β Memory Optimization - Retention settings for 50-80% memory reduction
While Python dominates AI/ML model training, Go excels at building production AI applications:
- π Performance - Fast execution and low memory overhead
- β‘ Concurrency - Native goroutines and channels for parallel processing
- π¦ Single Binary - No dependencies, easy deployment
- π Type Safety - Catch errors at compile time
- βοΈ Cloud Native - Perfect for Kubernetes, Docker, microservices
- π’ Production Ready - Built for scalable backend systems
We provide 50+ production-ready examples covering every feature. See the examples directory for complete working code.
- http-server - Standard
net/httpwith SSE streaming - gin-server - Gin framework integration
- echo-server, fiber-server, chi-server - More frameworks
- generate-object - Type-safe JSON generation (basic, validation, complex)
- stream-object - Real-time structured streaming
- OpenAI - Reasoning (o1), structured outputs, vision
- Anthropic - Caching, extended thinking, PDF support
- Google, Azure - Integration patterns
- math-agent - Multi-tool math solver
- web-search-agent - Research and fact-checking
- streaming-agent - Real-time step visualization
- multi-agent, supervisor-agent - Coordinated systems
- Middleware - Logging, caching, rate limiting, retry, telemetry
- Testing - Unit and integration test patterns
- MCP - Model Context Protocol (stdio, HTTP, auth, tools)
- Image Generation - DALL-E, Stable Diffusion
- Speech - Text-to-speech and speech-to-text
- Multimodal Audio - Audio analysis patterns
- Reranking - Document reranking for search quality
- Semantic Router - AI-based intent classification
- Benchmarks - Throughput and latency measurement
All examples:
- β
Compile and pass
go vet - β Include comprehensive README with usage
- β Follow Go best practices
- β Work with real API calls
- Getting Started - Quick start guide
- Foundations - Core concepts
- AI SDK Core - Complete API reference
- Agents - Building autonomous agents
- Advanced - Production patterns
This SDK maintains 1:1 feature parity with the Vercel AI SDK for backend functionality:
- Same function signatures and patterns
- Same provider interfaces
- Same middleware system
- Compatible workflows
- Feature complete for server-side use
The Go AI SDK community can be found on GitHub where you can ask questions, voice ideas, and share your projects:
- GitHub Discussions - Ask questions and share ideas
- GitHub Issues - Report bugs and request features
Contributions to the Go AI SDK are welcome and highly appreciated. However, before you jump right into it, we would like you to review our Contribution Guidelines to make sure you have a smooth experience contributing to the Go AI SDK.
Apache 2.0 - See LICENSE for details.
This library is created by @digitallysavvy, inspired by the Vercel AI SDK, with contributions from the Open Source Community.