Safeguard Docs
Safeguard AI SDKs

Go reference

API reference for safeguard.sh/sdk, the Safeguard AI SDK for Go, standard library only.

Go reference

Module safeguard.sh/sdk, version 0.1.0. Go 1.22 or newer, standard library only.

import safeguard "safeguard.sh/sdk"

Client

NewClient(opts ...Option) (*Client, error). Without an API key from WithAPIKey or SAFEGUARD_API_KEY it returns ErrNoAPIKey.

OptionDefault
WithAPIKey(key)SAFEGUARD_API_KEY
WithBaseURL(url)https://api.safeguard.sh/gpt/v1/ai
WithProductID(id)the key owner's active product
WithHTTPClient(c)&http.Client{}; do not set a short Timeout, use context deadlines
WithMaxRetries(n)2; 0 turns retries off
WithUserAgent(s)appended to safeguard-sdk-go/0.1.0

Methods

MethodReturns
Context(ctx)*ContextInfo, error
Query(ctx, prompt, *QueryOptions)*QueryResult, error
Stream(ctx, prompt, *QueryOptions)*Stream, error
CreateConversation(ctx, *CreateConversationOptions)*Conversation, error
ListConversations(ctx, ListOptions)*ConversationPage, error
GetConversation(ctx, id)*Conversation, error
ListMessages(ctx, id, ListOptions)*MessagePage, error
EachConversation(ctx, pageSize, func(Conversation) error)error; return ErrStopIteration to stop early
Conversations(ctx)iter.Seq2[Conversation, error] (Go 1.23 and later)

Stream methods: Next() bool, Event() Event, Err() error, ConversationID() string, FinalAnswer() (string, error), Close() error.

Types

type QueryOptions struct{ ConversationID, ProductID, Model string }
type CreateConversationOptions struct{ Title, ProductID string }
type ListOptions struct{ Limit, Offset int }

type QueryResult struct{ ConversationID, QueryID, Answer, Model string }
type ContextInfo struct {
	TenantID, OrgID, UserID, Email, ProductID string
	Products []Product // Product{ID, Name}
	Scopes   []string
}
type Conversation struct {
	ID, Title    string
	MessageCount int
	CreatedAt, UpdatedAt Timestamp
}
type ConversationPage struct{ Conversations []Conversation; Total int }
type Message struct{ ID string; CreatedAt Timestamp; Prompt, Answer string }
type MessagePage struct{ Messages []Message; Total int }

Events (the Event interface): StartEvent, ThinkingEvent, ToolEvent, FinalEvent, ErrorEvent, DoneEvent and UnknownEvent for a type this version does not know.

Errors

A non-2xx answer is an *APIError with Status, Code, Message, RetryAfter and RequestID. It matches errors.Is against ErrInvalidRequest, ErrUnauthorized, ErrForbidden, ErrInsufficientScope, ErrNotFound, ErrRateLimited, ErrRunLimit, ErrUpstream, ErrUnavailable and ErrServer. A transport failure is a *ConnError. A cancelled context returns the context's error. See Errors and retries.

On this page