MCPcopy Index your code
hub / github.com/digitallysavvy/go-ai

github.com/digitallysavvy/go-ai @v0.4.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.4.0 ↗ · + Follow
5,427 symbols 19,125 edges 616 files 3,540 documented · 65% updated 11d agov0.4.0 · 2026-03-31★ 37
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Go AI SDK

CI Go Report Card Go Reference License

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.

What's new in v0.4.0

  • Top-level reasoning — portable Reasoning parameter across 10+ providers
  • Deferred provider tools — step loop continues for async server-side tools (web search, code execution)
  • Streaming refactor — tools execute after stream end, telemetry via global registry
  • Security — SSRF redirect protection, constant-time OAuth state validation
  • Anthropic — webSearch/webFetch 20260209, eager input streaming
  • OpenAI — GPT-5.4, Responses API compaction, ToolSearch
  • XAI — Responses API as default, logprobs, ReasoningSummary
  • Google — VALIDATED tool mode, native Vertex tools, multimodal embeddings
  • New providers — Prodia (image + video), KlingAI v3.0 motion control

See the full release notes and changelog.

Installation

You will need Go 1.21+ installed on your local development machine.

go get github.com/digitallysavvy/go-ai@v0.4.0

Unified Provider Architecture

The 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/google

Usage

Generating Text

import (
    "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-5.4")

    result, _ := ai.GenerateText(ctx, ai.GenerateTextOptions{
        Model:  model,
        Prompt: "What is an agent?",
    })

    fmt.Println(result.Text)
}

Streaming Text

stream, _ := ai.StreamText(ctx, ai.StreamTextOptions{
    Model:  model,
    Prompt: "Write a story about Go programming",
})

for chunk := range stream.TextChannel {
    fmt.Print(chunk)
}

Generating Structured Data

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)

Agents

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)

Tool Calling

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},
})

Embeddings

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 vector

Image Generation

imageModel, _ := 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

Speech and Transcription

// 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,
})

Memory Optimization

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 memory

Perfect for: - 🖼️ Image processing workloads - 📄 Large document analysis - 🔒 Privacy-sensitive applications - ⚡ Long-running services

See examples/features/retention for detailed usage.

Supported Providers

The Go AI SDK supports 30+ providers:

Provider Language Models Embeddings Images / Video Speech
OpenAI GPT-5.4, GPT-5.3, O3, O4 DALL-E TTS, Whisper
Anthropic Claude Sonnet 4.6, Opus 4.6 - - -
Google Gemini 3, 2.5 Pro/Flash - -
Google Vertex Gemini (enterprise) Imagen -
AWS Bedrock Claude, Titan, Nova, Llama - -
Azure OpenAI Azure-hosted models
xAI Grok-3 (Responses API) - -
Mistral Large, Small - -
Cohere Command R+, Command - -
Groq Llama, Mixtral - - Whisper
Together AI Llama, Mixtral, Qwen - Stable Diffusion -
Fireworks Llama, Mixtral FLUX Kontext -
Perplexity Sonar models - - -
DeepSeek DeepSeek R1, Chat - - -
Alibaba Qwen models - -
KlingAI - - Video (v3.0) -
Prodia img2img - Video (T2V/I2V) -
Ollama Local models - -

And more (Replicate, Hugging Face, Stability, ElevenLabs, Deepgram, Gladia, LMNT, ByteDance, Baseten, Cerebras, DeepInfra, Gateway)...

Features

  • Unified API — one interface for 30+ providers
  • Text GenerationGenerateText() and StreamText()
  • Structured Output — type-safe GenerateObject() with JSON validation
  • Tool Calling — custom functions with per-tool timeouts
  • Reasoning — portable Reasoning parameter across providers (Anthropic, OpenAI, Google, Bedrock, xAI, and more)
  • Deferred Tools — async provider tools (web search, code execution) with step loop continuation
  • Agents — autonomous multi-step reasoning with ToolLoopAgent
  • Embeddings — generate and search with vector embeddings, multimodal support
  • Image Generation — text-to-image with multiple providers
  • Video Generation — text/image-to-video (KlingAI, Prodia, ByteDance, xAI)
  • Speech — TTS and transcription capabilities
  • Middleware — logging, caching, rate limiting, and more
  • Telemetry — global registry with OpenTelemetry integration
  • Security — SSRF protection, constant-time OAuth validation
  • MCP — Model Context Protocol client with redirect control
  • Registry — resolve models by string ID (e.g., "openai:gpt-5.4")
  • Context Support — native Go context cancellation and timeouts
  • Streaming — deferred tool execution, real-time responses with backpressure

Why Go for AI?

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

Examples

We provide 50+ production-ready examples covering every feature. See the examples directory for complete working code.

🚀 HTTP Servers (5 examples)

📦 Structured Output (4 examples)

🤖 Provider Features (8 examples)

  • OpenAI - Reasoning (o1), structured outputs, vision
  • Anthropic - Caching, extended thinking, PDF support
  • Google, Azure - Integration patterns

🧠 Agents (5 examples)

🛠️ Production Patterns (7 examples)

  • Middleware - Logging, caching, rate limiting, retry, telemetry
  • Testing - Unit and integration test patterns
  • MCP - Model Context Protocol (stdio, HTTP, auth, tools)

🎨 Multimodal (5 examples)

🔬 Advanced (4 examples)

All examples: - ✅ Comp

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Function 2,731
Method 1,720
Struct 896
TypeAlias 34
Interface 30
FuncType 16

Languages

Go100%

Modules by API surface

pkg/ai/output.go55 symbols
pkg/telemetry/registry.go54 symbols
pkg/agent/toolloop_test.go53 symbols
pkg/testutil/mocks.go52 symbols
pkg/ai/output_test.go46 symbols
pkg/provider/language_model.go45 symbols
pkg/providers/anthropic/tools/code_execution_20260120.go44 symbols
pkg/providers/anthropic/context_management.go41 symbols
pkg/provider/types/message.go41 symbols
pkg/providers/bytedance/video_model_test.go38 symbols
pkg/provider/errors/errors_test.go38 symbols
pkg/ai/validate_download_url_test.go38 symbols

For agents

$ claude mcp add go-ai \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page