Apple's Foundation Models provides powerful on-device AI capabilities in macOS 26 Tahoe, but it's only accessible through Swift/Objective-C APIs. This package bridges that gap, offering:
- π Privacy-focused: All AI processing happens on-device, no data leaves your Mac
- β‘ High performance: Optimized for Apple Silicon with no network latency
- π Streaming-first: Simulated real-time response streaming with typing indicators for modern UX
- π οΈ Rich tooling: Advanced features like input validation, context cancellation, and generation control
- π¦ Self-contained: Embedded Swift shim library - no external dependencies
- π― Production-ready: Comprehensive error handling, memory management, and structured logging
- Temperature control: Deterministic (0.0) to creative (1.0) output
- Token limiting: Control response length with max tokens
- Helper functions:
WithDeterministic()
,WithCreative()
,WithBalanced()
- Custom tool creation: Define tools that Foundation Models can call autonomously
- Real-time data access: Via custom integrations
- Input validation: Type checking, required fields, enum constraints, regex patterns
- Automatic error handling: Comprehensive validation before execution
- Swift-Go bridge: Seamless callback mechanism between Foundation Models and Go tools
- Timeout support: Cancel long-running requests automatically
- Manual cancellation: User-controlled request cancellation
- Context tracking: 4096-token window with usage monitoring
- Session refresh: Seamless context window management
- Dual build modes: Pure Go (purego) or static compilation (CGO) options
- Memory safety: Automatic C string cleanup and proper resource management
- Error resilience: Graceful initialization failure handling
- Self-contained: Embedded Swift shim library with automatic extraction
- Structured logging: Go slog integration with debug logging for both Go and Swift layers
Warning
I've noticed Apple's model is very finicky and is overly cautious and may refuse when answering any questions.
- macOS 26 Tahoe (beta) or later
- Apple Intelligence enabled on your device
- Apple Silicon Mac (M1/M2/M3/M4 series)
- Go 1.24+ (uses latest Go features)
- Xcode 15.x or later (for Swift shim compilation if needed)
go get github.com/blacktop/go-foundationmodels
This package supports two build modes:
go build # Uses embedded dynamic library (.dylib)
CGO_ENABLED=1 go build # Compiles Swift code directly into binary
The static build creates a self-contained binary with the Swift Foundation Models bridge compiled directly into the executable. This eliminates the need for a separate dynamic library while maintaining full functionality.
package main
import (
"fmt"
"log"
fm "github.com/blacktop/go-foundationmodels"
)
func main() {
// Check availability
if fm.CheckModelAvailability() != fm.ModelAvailable {
log.Fatal("Foundation Models not available")
}
// Create session
sess := fm.NewSession()
defer sess.Release()
// Generate text
response := sess.Respond("What is artificial intelligence?", nil)
fmt.Println(response)
// Use generation options
creative := sess.Respond("Write a story", fm.WithCreative())
fmt.Println(creative)
}
package main
import (
"fmt"
"log"
fm "github.com/blacktop/go-foundationmodels"
)
// Simple calculator tool
type CalculatorTool struct{}
func (c *CalculatorTool) Name() string { return "calculate" }
func (c *CalculatorTool) Description() string {
return "Calculate mathematical expressions with add, subtract, multiply, or divide operations"
}
func (c *CalculatorTool) GetParameters() []fm.ToolArgument {
return []fm.ToolArgument{{
Name: "arguments", Type: "string", Required: true,
Description: "Mathematical expression with two numbers and one operation",
}}
}
func (c *CalculatorTool) Execute(args map[string]any) (fm.ToolResult, error) {
expr := args["arguments"].(string)
// ... implement expression parsing and calculation
return fm.ToolResult{Content: "42.00"}, nil
}
func main() {
sess := fm.NewSessionWithInstructions("You are a helpful calculator assistant.")
defer sess.Release()
// Register tool
calculator := &CalculatorTool{}
sess.RegisterTool(calculator)
// AI will autonomously call the tool when needed
response := sess.RespondWithTools("What is 15 plus 27?")
fmt.Println(response) // "The result is 42.00"
}
Dynamic build (default):
make build # Creates 'found' binary with .dylib dependency
Static build:
make build-static # Creates 'found-static' binary with embedded Swift code
The static build automatically compiles the Swift Foundation Models bridge into a static library and links it directly into the Go binary.
Install with Homebrew:
brew install blacktop/tap/go-foundationmodels
Install with Go:
go install github.com/blacktop/go-foundationmodels/cmd/found@latest
Or download from the latest release
Use found --help
or found [command] --help
to see all available commands and examples.
Available commands:
found info
- Display model availability and system informationfound quest
- Interactive chat with streaming support, system instructions and JSON outputfound stream
- Real-time streaming text generation with optional toolsfound tool calc
- Mathematical calculations with real arithmeticfound tool weather
- Real-time weather data with geocoding
Weather Tool: Get real-time weather data
found tool weather "New York"
# Returns actual weather from OpenMeteo API with temperature, conditions, humidity, etc.
Calculator Tool: Perform mathematical operations
found tool calc "add 15 plus 27"
# Returns: The result of "15 + 27" is **42.00**.
Debug Mode: See comprehensive logging in action
found tool weather --verbose "Paris"
# Shows both Go debug logs (slog) and Swift logs with detailed execution flow
While tool calling is functional, Foundation Models exhibits some variability:
- β Tool execution works: When called, tools successfully return real data
- β Callback mechanism fixed: Swift β Go communication is reliable
β οΈ Inconsistent invocation: Foundation Models sometimes refuses to call tools due to safety restrictions- β Error handling: Graceful failures with helpful explanations
- Foundation Models Safety: Some queries may be blocked by built-in safety guardrails
- Context Window: 4096 token limit requires session refresh for long conversations
- Tool Parameter Mapping: Complex expressions may not parse correctly into tool parameters
- Streaming Implementation: Currently uses simulated streaming (post-processing chunks) as Foundation Models doesn't yet provide native streaming APIs
- Fix tool calling reliability - Tools now work with real data
- Swift-Go callback mechanism - Reliable bidirectional communication
- Tool debugging capabilities -
--verbose
flag for comprehensive debug logs - Direct tool testing -
--direct
flag bypasses Foundation Models - Streaming responses - Simulated streaming with word/sentence chunks (native streaming pending Foundation Models API)
- Structured logging - Go slog integration with consolidated debug logging
- Advanced tool schemas with OpenAPI-style definitions
- Multi-modal support (images, audio) when available
- Performance optimizations for large contexts
- Enhanced error handling with detailed diagnostics
- Plugin system for extensible tool management
- Native streaming support - Upgrade to Foundation Models native streaming API when available
- Improve Foundation Models consistency - Research better prompting strategies
MIT Copyright (c) 2025 blacktop