gateway

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 23 Imported by: 0

README

v-ai-go-sdk

Experimental, unofficial Go client for Vercel AI Gateway. Not affiliated with or endorsed by Vercel. Requires Go 1.26+; the package name is gateway.

API Methods
Responses CreateResponse, StreamResponse
Chat Completions CreateChatCompletion, StreamChatCompletion
Evaluation Evaluate
Images (staged) GenerateImage
Speech (staged) GenerateSpeech
Buffered transcription (staged) Transcribe
Embeddings (staged) Embed
Reranking (staged) Rerank

Experimental v0. Full live-contract suites are pending; staged APIs have no hosted success evidence. See release policy.

Quick start

go get github.com/EveGoodEvening/v-ai-go-sdk

Set AI_GATEWAY_API_KEY (or VERCEL_OIDC_TOKEN), then run the example. It makes a real request and may incur charges.

package main

import (
    "context"
    "fmt"
    "log"
    "time"

    gateway "github.com/EveGoodEvening/v-ai-go-sdk"
)

func main() {
    client, err := gateway.NewClient()
    if err != nil {
        log.Fatal(err)
    }

    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()

    result, err := client.CreateResponse(ctx, gateway.ResponsesRequest{
        Model: "openai/gpt-5-nano",
        Input: gateway.ResponseTextInput("Explain Go contexts briefly."),
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("response completed: raw_json_bytes=%d\n", len(result.RawJSON()))
}

RawJSON() is not sanitized; this example prints only its size.

Guides and examples

Scope

Responses and Chat use public /v1; all other methods use provider /v4/ai. Search is opt-in and request-only; the SDK never executes tools. This is not a full JavaScript AI SDK port or a general OpenAI client. See unsupported features for the full boundary.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Answer

type Answer interface {
	// contains filtered or unexported methods
}

Answer is the closed set of supported evaluation answer variants.

type BooleanAnswer

type BooleanAnswer struct {
	Probability float64
}

BooleanAnswer reports P(true).

type BooleanCriteria

type BooleanCriteria struct {
	True  OptionalJSON
	False OptionalJSON
}

BooleanCriteria optionally describes true and false. A non-nil zero value emits an empty criteria object; each OptionalJSON controls key presence.

type BooleanQuestion

type BooleanQuestion struct {
	Instructions any
	Criteria     *BooleanCriteria
}

BooleanQuestion asks for P(true). Instructions is required and must be a JSON-compatible string, object, or array. Nil Criteria omits the wire field.

type ChatAssistantMessage

type ChatAssistantMessage struct {
	Role      JSONField[string]
	Content   JSONField[string]
	ToolCalls JSONField[[]ChatToolCall]
}

type ChatChoice

type ChatChoice struct {
	Index        JSONField[int]
	Message      JSONField[ChatAssistantMessage]
	FinishReason JSONField[string]
}

type ChatCompletionChunk

type ChatCompletionChunk struct {
	Object  string
	Choices []ChatCompletionChunkChoice
	// contains filtered or unexported fields
}

ChatCompletionChunk is one documented chat.completion.chunk payload. Fields not represented by this type remain available through RawJSON.

func (*ChatCompletionChunk) RawJSON

func (c *ChatCompletionChunk) RawJSON() []byte

RawJSON returns a fresh copy of the complete chunk JSON.

type ChatCompletionChunkChoice

type ChatCompletionChunkChoice struct {
	Delta ChatCompletionChunkDelta
}

ChatCompletionChunkChoice is one choice in a streamed chat completion chunk. Its position in Choices preserves its wire order.

type ChatCompletionChunkDelta

type ChatCompletionChunkDelta struct {
	Content string
}

ChatCompletionChunkDelta contains the documented streamed text content.

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model            string
	Messages         []ChatMessage
	Temperature      *float64
	MaxTokens        *int
	TopP             *float64
	FrequencyPenalty *float64
	PresencePenalty  *float64
	Stop             ChatStop
	SafetyIdentifier *string
	Tools            []ChatTool
	ToolChoice       ChatToolChoice
	ResponseFormat   ChatResponseFormat
	Models           []string
	ProviderOptions  *ChatProviderOptions
	Provider         *ChatProvider
}

ChatCompletionRequest is a non-streaming Chat Completions request.

type ChatCompletionResult

type ChatCompletionResult struct {
	ID      JSONField[string]
	Object  JSONField[string]
	Created JSONField[int64]
	Model   JSONField[string]
	Choices JSONField[[]ChatChoice]
	Usage   JSONField[ChatUsage]
	// contains filtered or unexported fields
}

func (*ChatCompletionResult) RawJSON

func (r *ChatCompletionResult) RawJSON() []byte

type ChatCompletionStream

type ChatCompletionStream struct {
	// contains filtered or unexported fields
}

ChatCompletionStream is a caller-owned, incrementally read Chat Completions stream. It starts no producer goroutine and retains no transcript.

func (*ChatCompletionStream) Close

func (s *ChatCompletionStream) Close() error

Close stops the stream and closes its response body exactly once. It is safe to call repeatedly and may unblock a concurrent Next.

func (*ChatCompletionStream) Err

func (s *ChatCompletionStream) Err() error

Err returns the terminal stream error. Successful [DONE] termination and an explicit Close return nil. The returned error remains stable.

func (*ChatCompletionStream) Event

Event returns the current chunk, or nil before the first successful Next and after Next terminates.

func (*ChatCompletionStream) Next

func (s *ChatCompletionStream) Next() bool

Next advances to the next JSON chunk. It returns false after the required [DONE] terminal marker, after Close, or on error. Event is valid only after Next returns true.

type ChatContentPart

type ChatContentPart interface {
	// contains filtered or unexported methods
}

type ChatExaCategory

type ChatExaCategory string
const (
	ChatExaCategoryCompany         ChatExaCategory = "company"
	ChatExaCategoryPeople          ChatExaCategory = "people"
	ChatExaCategoryResearchPaper   ChatExaCategory = "research paper"
	ChatExaCategoryNews            ChatExaCategory = "news"
	ChatExaCategoryPersonalSite    ChatExaCategory = "personal site"
	ChatExaCategoryFinancialReport ChatExaCategory = "financial report"
)

type ChatExaContents

type ChatExaContents struct {
	Text             ChatExaText
	Highlights       ChatExaHighlights
	MaxAgeHours      *int
	LivecrawlTimeout *int
	Subpages         *int
	SubpageTarget    ChatExaSubpageTarget
	Extras           *ChatExaExtras
}

type ChatExaExtras

type ChatExaExtras struct {
	Links      *int
	ImageLinks *int
}

type ChatExaHighlights

type ChatExaHighlights interface {
	// contains filtered or unexported methods
}

type ChatExaHighlightsEnabled

type ChatExaHighlightsEnabled bool

type ChatExaHighlightsOptions

type ChatExaHighlightsOptions struct {
	Query         *string
	MaxCharacters *int
}

type ChatExaSearchConfig

type ChatExaSearchConfig struct {
	Query              string
	Type               *ChatExaSearchType
	NumResults         *int
	Category           *ChatExaCategory
	UserLocation       *string
	IncludeDomains     *[]string
	ExcludeDomains     *[]string
	StartPublishedDate *string
	EndPublishedDate   *string
	Contents           *ChatExaContents
}

type ChatExaSearchTool

type ChatExaSearchTool struct{ Config ChatExaSearchConfig }

type ChatExaSearchType

type ChatExaSearchType string
const (
	ChatExaSearchAuto    ChatExaSearchType = "auto"
	ChatExaSearchFast    ChatExaSearchType = "fast"
	ChatExaSearchInstant ChatExaSearchType = "instant"
)

type ChatExaSection

type ChatExaSection string
const (
	ChatExaSectionHeader     ChatExaSection = "header"
	ChatExaSectionNavigation ChatExaSection = "navigation"
	ChatExaSectionBanner     ChatExaSection = "banner"
	ChatExaSectionBody       ChatExaSection = "body"
	ChatExaSectionSidebar    ChatExaSection = "sidebar"
	ChatExaSectionFooter     ChatExaSection = "footer"
	ChatExaSectionMetadata   ChatExaSection = "metadata"
)

type ChatExaSubpageTarget

type ChatExaSubpageTarget interface {
	// contains filtered or unexported methods
}

type ChatExaSubpageTargetString

type ChatExaSubpageTargetString string

type ChatExaSubpageTargetStrings

type ChatExaSubpageTargetStrings []string

type ChatExaText

type ChatExaText interface {
	// contains filtered or unexported methods
}

type ChatExaTextEnabled

type ChatExaTextEnabled bool

type ChatExaTextOptions

type ChatExaTextOptions struct {
	MaxCharacters   *int
	IncludeHTMLTags *bool
	Verbosity       *ChatExaVerbosity
	IncludeSections *[]ChatExaSection
	ExcludeSections *[]ChatExaSection
}

type ChatExaVerbosity

type ChatExaVerbosity string
const (
	ChatExaVerbosityCompact  ChatExaVerbosity = "compact"
	ChatExaVerbosityStandard ChatExaVerbosity = "standard"
	ChatExaVerbosityFull     ChatExaVerbosity = "full"
)

type ChatFilePart

type ChatFilePart struct {
	Data      string
	MediaType string
	Filename  string
}

type ChatFunctionCall

type ChatFunctionCall struct {
	Name      JSONField[string]
	Arguments JSONField[string]
}

type ChatGatewayOptions

type ChatGatewayOptions struct {
	Order            []string
	Models           []string
	Sort             *string
	ProviderTimeouts *ChatProviderTimeouts
}

type ChatImageURLPart

type ChatImageURLPart struct {
	URL    string
	Detail *string
}

type ChatJSONSchemaResponseFormat

type ChatJSONSchemaResponseFormat struct {
	Name        string
	Description *string
	Schema      any
	Strict      *bool
}

type ChatLegacyJSONResponseFormat

type ChatLegacyJSONResponseFormat struct {
	Schema      any
	Name        *string
	Description *string
}

type ChatMessage

type ChatMessage struct {
	Role    string
	Content ChatMessageContent
}

ChatMessage is one input message. Content must be ChatTextContent or ChatPartsContent.

type ChatMessageContent

type ChatMessageContent interface {
	// contains filtered or unexported methods
}

type ChatParallelExcerpts

type ChatParallelExcerpts struct {
	MaxCharsPerResult *int
	MaxCharsTotal     *int
}

type ChatParallelFetchPolicy

type ChatParallelFetchPolicy struct{ MaxAgeSeconds *int }

type ChatParallelMode

type ChatParallelMode string
const (
	ChatParallelModeOneShot ChatParallelMode = "one-shot"
	ChatParallelModeAgentic ChatParallelMode = "agentic"
)

type ChatParallelSearchConfig

type ChatParallelSearchConfig struct {
	Objective     string
	SearchQueries *[]string
	Mode          *ChatParallelMode
	MaxResults    *int
	SourcePolicy  *ChatParallelSourcePolicy
	Excerpts      *ChatParallelExcerpts
	FetchPolicy   *ChatParallelFetchPolicy
}

type ChatParallelSearchTool

type ChatParallelSearchTool struct{ Config ChatParallelSearchConfig }

type ChatParallelSourcePolicy

type ChatParallelSourcePolicy struct {
	IncludeDomains *[]string
	ExcludeDomains *[]string
	AfterDate      *string
}

type ChatPartsContent

type ChatPartsContent []ChatContentPart

type ChatPerplexityQuery

type ChatPerplexityQuery interface {
	// contains filtered or unexported methods
}

type ChatPerplexityQueryString

type ChatPerplexityQueryString string

type ChatPerplexityQueryStrings

type ChatPerplexityQueryStrings []string

type ChatPerplexityRecency

type ChatPerplexityRecency string
const (
	ChatPerplexityRecencyDay   ChatPerplexityRecency = "day"
	ChatPerplexityRecencyWeek  ChatPerplexityRecency = "week"
	ChatPerplexityRecencyMonth ChatPerplexityRecency = "month"
	ChatPerplexityRecencyYear  ChatPerplexityRecency = "year"
)

type ChatPerplexitySearchConfig

type ChatPerplexitySearchConfig struct {
	Query                   ChatPerplexityQuery
	MaxResults              *int
	MaxTokensPerPage        *int
	MaxTokens               *int
	Country                 *string
	SearchDomainFilter      *[]string
	SearchLanguageFilter    *[]string
	SearchAfterDate         *string
	SearchBeforeDate        *string
	LastUpdatedAfterFilter  *string
	LastUpdatedBeforeFilter *string
	SearchRecencyFilter     *ChatPerplexityRecency
}

type ChatPerplexitySearchTool

type ChatPerplexitySearchTool struct{ Config ChatPerplexitySearchConfig }

type ChatProvider

type ChatProvider struct{ Sort string }

type ChatProviderOptions

type ChatProviderOptions struct{ Gateway ChatGatewayOptions }

ChatProviderOptions contains documented AI Gateway routing options.

type ChatProviderTimeouts

type ChatProviderTimeouts struct{ BYOK map[string]int }

type ChatResponseFormat

type ChatResponseFormat interface {
	// contains filtered or unexported methods
}

type ChatResponseFormatType

type ChatResponseFormatType string
const (
	ChatResponseFormatText ChatResponseFormatType = "text"
	ChatResponseFormatJSON ChatResponseFormatType = "json"
)

type ChatServerTool

type ChatServerTool interface {
	// contains filtered or unexported methods
}

type ChatServerToolsRequest

type ChatServerToolsRequest struct {
	Request     ChatCompletionRequest
	ServerTools []ChatServerTool
}

ChatServerToolsRequest adds typed server tools without changing ChatCompletionRequest.

type ChatSpecificToolChoice

type ChatSpecificToolChoice struct{ Name string }

type ChatStop

type ChatStop interface {
	// contains filtered or unexported methods
}

type ChatStopString

type ChatStopString string

type ChatStopStrings

type ChatStopStrings []string

type ChatTakoContentFormat

type ChatTakoContentFormat string
const (
	ChatTakoContentFormatCardJSON    ChatTakoContentFormat = "card_json"
	ChatTakoContentFormatCSV         ChatTakoContentFormat = "csv"
	ChatTakoContentFormatJSONCompact ChatTakoContentFormat = "json_compact"
	ChatTakoContentFormatJSONRecords ChatTakoContentFormat = "json_records"
)

type ChatTakoDataMode

type ChatTakoDataMode string
const (
	ChatTakoDataModeInline ChatTakoDataMode = "inline"
	ChatTakoDataModeURL    ChatTakoDataMode = "url"
)

type ChatTakoDataSource

type ChatTakoDataSource struct {
	Count           *int
	IncludeContents *bool
	Mode            *ChatTakoDataMode
	ContentFormat   *ChatTakoContentFormat
	MaxRows         *int
	NodeIDs         *[]string
	Strict          *bool
}

type ChatTakoEffort

type ChatTakoEffort string
const (
	ChatTakoEffortDeep    ChatTakoEffort = "deep"
	ChatTakoEffortFast    ChatTakoEffort = "fast"
	ChatTakoEffortInstant ChatTakoEffort = "instant"
)

type ChatTakoLocation

type ChatTakoLocation struct {
	Latitude  float64
	Longitude float64
}

type ChatTakoOutputSettings

type ChatTakoOutputSettings struct {
	ImageDarkMode *bool
	ForceRefresh  *bool
}

type ChatTakoSearchConfig

type ChatTakoSearchConfig struct {
	Query          string
	Effort         *ChatTakoEffort
	Sources        *ChatTakoSources
	Location       *ChatTakoLocation
	CountryCode    *string
	Locale         *string
	Timezone       *string
	OutputSettings *ChatTakoOutputSettings
	IncludeRelated *int
}

type ChatTakoSearchTool

type ChatTakoSearchTool struct{ Config ChatTakoSearchConfig }

type ChatTakoSources

type ChatTakoSources struct {
	Data *ChatTakoDataSource
	Web  *ChatTakoWebSource
}

type ChatTakoWebCategory

type ChatTakoWebCategory string
const (
	ChatTakoWebCategoryFinance ChatTakoWebCategory = "finance"
	ChatTakoWebCategoryNews    ChatTakoWebCategory = "news"
	ChatTakoWebCategorySports  ChatTakoWebCategory = "sports"
)

type ChatTakoWebSource

type ChatTakoWebSource struct {
	Count                  *int
	IncludeContents        *bool
	Category               *ChatTakoWebCategory
	IncludeDomains         *[]string
	ExcludeDomains         *[]string
	SnippetMaxChars        *int
	Highlights             *bool
	ArticleContentMaxChars *int
	PublishedAfter         *string
	PublishedBefore        *string
}

type ChatTextContent

type ChatTextContent string

type ChatTextPart

type ChatTextPart struct{ Text string }

type ChatTool

type ChatTool struct {
	Name        string
	Description *string
	Parameters  any
}

type ChatToolCall

type ChatToolCall struct {
	ID       JSONField[string]
	Type     JSONField[string]
	Function JSONField[ChatFunctionCall]
}

type ChatToolChoice

type ChatToolChoice interface {
	// contains filtered or unexported methods
}

type ChatToolChoiceMode

type ChatToolChoiceMode string
const (
	ChatToolChoiceAuto     ChatToolChoiceMode = "auto"
	ChatToolChoiceNone     ChatToolChoiceMode = "none"
	ChatToolChoiceRequired ChatToolChoiceMode = "required"
)

type ChatUsage

type ChatUsage struct {
	PromptTokens     JSONField[int64]
	CompletionTokens JSONField[int64]
	TotalTokens      JSONField[int64]
}

type ChoiceAnswer

type ChoiceAnswer struct {
	Choice        string
	Probabilities map[string]float64
}

ChoiceAnswer reports a selected criterion and, when present, its probability distribution. A nil Probabilities map means the wire key was absent.

type ChoiceQuestion

type ChoiceQuestion struct {
	Instructions any
	Criteria     map[string]any
}

ChoiceQuestion asks the provider to select one keyed criterion. Instructions is required; Criteria must contain at least one JSON-compatible description.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client accesses the Vercel AI Gateway public Responses and Chat Completions generation APIs and the provider Evaluation Model API.

func NewClient

func NewClient(opts ...Option) (*Client, error)

NewClient applies options in order and constructs a client without network calls. Credential precedence is explicit API key, AI_GATEWAY_API_KEY, explicit OIDC token/source, then VERCEL_OIDC_TOKEN. Blank environment values are absent. Missing credentials and invalid options return ConfigurationError.

func (*Client) CreateChatCompletion

func (client *Client) CreateChatCompletion(ctx context.Context, request ChatCompletionRequest) (*ChatCompletionResult, error)

CreateChatCompletion validates and submits one non-streaming Chat Completions request.

func (*Client) CreateChatCompletionWithServerTools

func (client *Client) CreateChatCompletionWithServerTools(ctx context.Context, request ChatServerToolsRequest) (*ChatCompletionResult, error)

CreateChatCompletionWithServerTools validates and submits a request with typed Gateway server tools.

func (*Client) CreateResponse

func (client *Client) CreateResponse(ctx context.Context, request ResponsesRequest) (*ResponseResult, error)

CreateResponse validates and submits one non-streaming Responses API request.

func (*Client) CreateResponseWithBuiltInTools

func (client *Client) CreateResponseWithBuiltInTools(ctx context.Context, request ResponsesBuiltInToolsRequest) (*ResponseResult, error)

CreateResponseWithBuiltInTools validates and submits one non-streaming Responses API request with sealed built-in tool declarations.

func (*Client) Embed

func (client *Client) Embed(ctx context.Context, modelID string, request EmbeddingRequest) (*EmbeddingResult, error)

Embed validates and submits exactly one Embedding Model V4 request. It does not batch or retry.

func (*Client) Evaluate

func (client *Client) Evaluate(ctx context.Context, modelID string, request EvaluationRequest) (*EvaluationResult, error)

Evaluate validates and submits an Evaluation Model V4 request. A nil context is a ValidationError at $["context"]. Only HTTP status 200 is success; every other status is a ResponseError, and response Content-Type is ignored. Retries occur only when explicitly configured and may duplicate billable work.

func (*Client) GenerateImage

func (client *Client) GenerateImage(ctx context.Context, modelID string, request ImageRequest) (*ImageResult, error)

GenerateImage validates and submits exactly one Image Model V4 request. It never retries.

func (*Client) GenerateSpeech

func (client *Client) GenerateSpeech(ctx context.Context, modelID string, request SpeechRequest) (*SpeechResult, error)

GenerateSpeech validates and submits exactly one Speech Model V4 request. It never retries.

func (*Client) Rerank

func (client *Client) Rerank(ctx context.Context, modelID string, request RerankRequest) (*RerankResult, error)

Rerank validates and submits exactly one Reranking Model V4 request. It does not retry.

func (*Client) StreamChatCompletion

func (client *Client) StreamChatCompletion(ctx context.Context, request ChatCompletionRequest) (*ChatCompletionStream, error)

StreamChatCompletion validates and starts a streaming Chat Completions request. A non-200 response is consumed and returned as ResponseError before a stream is exposed. Streaming responses are never retried after headers.

func (*Client) StreamChatCompletionWithServerTools

func (client *Client) StreamChatCompletionWithServerTools(ctx context.Context, request ChatServerToolsRequest) (*ChatCompletionStream, error)

StreamChatCompletionWithServerTools starts a stream with typed Gateway server tools.

func (*Client) StreamResponse

func (client *Client) StreamResponse(ctx context.Context, request ResponsesRequest) (*ResponseStream, error)

StreamResponse validates and starts a streaming Responses API request. A non-200 response is consumed and returned as ResponseError before a stream is exposed. Streaming responses are never retried after headers are received.

func (*Client) StreamResponseWithBuiltInTools

func (client *Client) StreamResponseWithBuiltInTools(ctx context.Context, request ResponsesBuiltInToolsRequest) (*ResponseStream, error)

StreamResponseWithBuiltInTools validates and starts a streaming Responses API request with sealed built-in tool declarations.

func (*Client) Transcribe

func (client *Client) Transcribe(ctx context.Context, modelID string, request TranscriptionRequest) (*TranscriptionResult, error)

Transcribe validates and submits exactly one Transcription Model V4 request. It never retries.

type ConfigurationError

type ConfigurationError struct {
	// contains filtered or unexported fields
}

ConfigurationError reports client construction failure. Option is one of WithBaseURL, WithPublicBaseURL, WithHTTPClient, WithAPIKey, WithOIDCToken, WithOIDCTokenSource, WithTeam, WithHeaders, WithRetryPolicy, or credentials; Reason is a stable closed value documented by Option. A nil receiver's accessors return empty strings and Error returns "gateway configuration error".

func (*ConfigurationError) Error

func (e *ConfigurationError) Error() string

Error returns a safe diagnostic that never contains credentials or payloads.

func (*ConfigurationError) Option

func (e *ConfigurationError) Option() string

Option returns the option that failed, or an empty string when unavailable.

func (*ConfigurationError) Reason

func (e *ConfigurationError) Reason() string

Reason returns the stable failure reason, or an empty string when unavailable.

type EmbeddingRequest

type EmbeddingRequest struct {
	Values          []string
	ProviderOptions []ProviderOption
}

EmbeddingRequest contains one or more strings to embed and optional sealed provider options.

type EmbeddingResult

type EmbeddingResult struct {
	Embeddings       [][]float64
	Usage            *EmbeddingUsage
	Warnings         []ProviderWarning
	ProviderMetadata map[string]json.RawMessage
	Response         ResponseMetadata
}

EmbeddingResult is a strictly validated Embedding Model V4 result.

type EmbeddingUsage

type EmbeddingUsage struct{ Tokens *int64 }

EmbeddingUsage reports the provider token count. Tokens is non-nil whenever Usage is non-nil.

type EvaluationRequest

type EvaluationRequest struct {
	State           any
	Questions       map[string]Question
	ProviderOptions map[string]map[string]any
}

EvaluationRequest contains shared State, a nonempty keyed Questions map, and optional ProviderOptions. State and question instructions/criteria are validated recursively as JSON-compatible input before network I/O.

type EvaluationResult

type EvaluationResult struct {
	Answers          map[string]Answer
	Rounding         *Rounding
	Usage            *Usage
	Warnings         []Warning
	ProviderMetadata map[string]map[string]any
	Response         ResponseMetadata
}

EvaluationResult is a strictly validated Evaluation Model V4 result. Missing warnings normalize to a non-nil empty slice; other metadata preserves the documented absent-versus-present distinction.

type ImageAspectRatio

type ImageAspectRatio string

type ImageBase64

type ImageBase64 struct {
	MediaType       string
	Data            string
	ProviderOptions []ProviderOption
}

type ImageBytes

type ImageBytes struct {
	MediaType       string
	Data            []byte
	ProviderOptions []ProviderOption
}

type ImageInput

type ImageInput interface {
	// contains filtered or unexported methods
}

type ImageRequest

type ImageRequest struct {
	Prompt          *string
	Count           int
	Size            *ImageSize
	AspectRatio     *ImageAspectRatio
	Seed            *int64
	Files           []ImageInput
	Mask            *ImageInput
	ProviderOptions []ProviderOption
}

type ImageResult

type ImageResult struct {
	Images           [][]byte
	Retryable        *bool
	Warnings         []ProviderWarning
	ProviderMetadata map[string]json.RawMessage
	Response         ResponseMetadata
	Usage            *ImageUsage
}

type ImageSize

type ImageSize string

type ImageURL

type ImageURL struct {
	URL             string
	ProviderOptions []ProviderOption
}

type ImageUsage

type ImageUsage struct {
	InputTokens  *float64
	OutputTokens *float64
	TotalTokens  *float64
}

type JSONField

type JSONField[T any] struct {
	Present bool
	Null    bool
	Value   T
}

JSONField preserves absence, explicit null, and a non-null JSON value.

type Option

type Option func(*clientConfig) error

Option configures a Client during NewClient. Options are applied in order and construction stops at the first error.

func WithAPIKey

func WithAPIKey(key string) Option

WithAPIKey selects an explicit API key, which has highest precedence. It rejects empty/all-Unicode-whitespace input and otherwise preserves it exactly.

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL sets the provider base URL. It requires a whitespace-exact, absolute, non-opaque HTTP(S) URL with host and without userinfo, query, or fragment. Existing paths are allowed. Only trailing path slashes are removed; scheme, host, port, escaping, and the remaining path are preserved before Evaluate appends /evaluation-model.

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient rejects nil and stores the supplied pointer without cloning or mutating the client or transport. Subsequent concurrent mutation is the caller's responsibility under the same rules as http.Client.

func WithHeaders

func WithHeaders(headers http.Header) Option

WithHeaders accepts nil or clones the supplied map when applied and again per request, preserving values, order, duplicates, and nil/empty slices. It rejects protocol/auth/team-owned names case-insensitively even without values: Authorization, Content-Type, Ai-Gateway-Protocol-Version, Ai-Gateway-Auth-Method, Ai-Evaluation-Model-Specification-Version, Ai-Embedding-Model-Specification-Version, Ai-Reranking-Model-Specification-Version, Ai-Image-Model-Specification-Version, Ai-Speech-Model-Specification-Version, Ai-Transcription-Model-Specification-Version, Ai-Language-Model-Specification-Version, Ai-Model-Id, and X-Vercel-Ai-Gateway-Team.

func WithOIDCToken

func WithOIDCToken(token string) Option

WithOIDCToken selects a fixed explicit OIDC token. It rejects blank input and otherwise preserves it exactly. The last explicit OIDC form wins, but neither explicit OIDC form displaces an explicit or environment API key.

func WithOIDCTokenSource

func WithOIDCTokenSource(source TokenSource) Option

WithOIDCTokenSource selects a non-nil refresh-capable OIDC source. It rejects nil and typed-nil sources. The last explicit OIDC form wins, but neither form displaces an explicit or environment API key.

func WithPublicBaseURL

func WithPublicBaseURL(baseURL string) Option

WithPublicBaseURL sets the base URL for public Gateway APIs. It rejects the same invalid URLs as WithBaseURL plus any literal fragment delimiter, while preserving escaped path data. It does not affect the provider-protocol URL.

func WithRetryPolicy

func WithRetryPolicy(policy RetryPolicy) Option

WithRetryPolicy copies, validates, resolves, and stores policy. Invalid fields return ConfigurationError with Option "WithRetryPolicy" and a stable Reason.

func WithTeam

func WithTeam(teamIDOrSlug string) Option

WithTeam sets a nonblank Vercel team ID or slug and preserves it exactly. There is no environment team fallback.

type OptionalJSON

type OptionalJSON struct {
	Set   bool
	Value any
}

OptionalJSON distinguishes an omitted value from an explicitly present JSON null. Set=false omits the containing key and requires Value=nil; Set=true emits Value, including nil as JSON null.

type ProviderOption

type ProviderOption interface {
	// contains filtered or unexported methods
}

ProviderOption is the closed set of provider-specific request options. This release intentionally provides no concrete implementations.

type ProviderWarning

type ProviderWarning struct {
	Type    ProviderWarningType
	Feature string
	Details *string
	Setting string
	Message string
}

ProviderWarning is the closed V4 provider warning union.

type ProviderWarningType

type ProviderWarningType string

ProviderWarningType identifies a closed provider warning variant.

const (
	ProviderWarningUnsupported   ProviderWarningType = "unsupported"
	ProviderWarningCompatibility ProviderWarningType = "compatibility"
	ProviderWarningDeprecated    ProviderWarningType = "deprecated"
	ProviderWarningOther         ProviderWarningType = "other"
)

type Question

type Question interface {
	// contains filtered or unexported methods
}

Question is the closed set of supported evaluation question variants.

type RawResponseEvent

type RawResponseEvent struct {
	Type  string
	Event string
	ID    string
	// contains filtered or unexported fields
}

RawResponseEvent retains an otherwise uninterpreted JSON event. Event is the SSE event name, ID is the SSE event ID, and Type is the JSON discriminator.

func (RawResponseEvent) RawJSON

func (e RawResponseEvent) RawJSON() []byte

RawJSON returns a fresh copy of the event JSON.

type RerankDocument

type RerankDocument interface {
	// contains filtered or unexported methods
}

RerankDocument is the sealed reconstructed result-document union.

type RerankDocuments

type RerankDocuments interface {
	// contains filtered or unexported methods
}

RerankDocuments is the sealed homogeneous request-document envelope.

type RerankItem

type RerankItem struct {
	OriginalIndex int
	Score         float64
	Document      RerankDocument
}

RerankItem is one provider-ordered ranking result.

type RerankJSON

type RerankJSON struct{ Value json.RawMessage }

RerankJSON is a reconstructed JSON object document. Value is independently owned.

type RerankObjects

type RerankObjects struct{ Values []json.RawMessage }

RerankObjects contains JSON object documents.

type RerankRequest

type RerankRequest struct {
	Query           string
	Documents       RerankDocuments
	TopN            *int
	ProviderOptions []ProviderOption
}

RerankRequest contains a query, one homogeneous document envelope, and optional controls.

type RerankResult

type RerankResult struct {
	Results          []RerankItem
	Warnings         []ProviderWarning
	ProviderMetadata map[string]json.RawMessage
	Response         ResponseMetadata
}

RerankResult is a strictly validated Reranking Model V4 result.

type RerankText

type RerankText struct{ Text string }

RerankText is a reconstructed text document.

type RerankTexts

type RerankTexts struct{ Values []string }

RerankTexts contains text documents.

type ResponseBuiltInTool

type ResponseBuiltInTool interface {
	// contains filtered or unexported methods
}

ResponseBuiltInTool is the sealed set of Responses built-in tool declarations.

type ResponseError

type ResponseError struct {
	// contains filtered or unexported fields
}

ResponseError reports a non-200 Gateway HTTP response. Its structured envelope, ID, retry, truncation, and defensive-copy body accessors are safe for nil receivers and return zero values; Error omits raw bodies, headers, credentials, request data, and wrapped cause text.

func (*ResponseError) BodyTruncated

func (e *ResponseError) BodyTruncated() bool

BodyTruncated reports whether retained response bytes omit a suffix.

func (*ResponseError) Code

func (e *ResponseError) Code() string

Code returns a string code or a numeric code's original JSON lexical form.

func (*ResponseError) Error

func (e *ResponseError) Error() string

Error returns a safe diagnostic that never contains raw response bytes, request data, headers, or credentials.

func (*ResponseError) GenerationID

func (e *ResponseError) GenerationID() string

GenerationID returns the top-level Gateway generation ID.

func (*ResponseError) Message

func (e *ResponseError) Message() string

Message returns the known Gateway error message.

func (*ResponseError) Param

func (e *ResponseError) Param() any

Param returns a recursively defensive-copied JSON-compatible parameter.

func (*ResponseError) RawResponseBody

func (e *ResponseError) RawResponseBody() []byte

RawResponseBody returns a fresh copy of retained response bytes. An upstream or intermediary may have echoed sensitive request state or provider options; callers must sanitize the bytes before logging or persistence.

func (*ResponseError) RequestID

func (e *ResponseError) RequestID() string

RequestID returns the top-level Gateway request ID.

func (*ResponseError) ResponseID

func (e *ResponseError) ResponseID() string

ResponseID returns the top-level Gateway response ID.

func (*ResponseError) RetryAfter

func (e *ResponseError) RetryAfter() (time.Duration, bool)

RetryAfter returns the parsed Retry-After delay and whether it was valid.

func (*ResponseError) Retryable

func (e *ResponseError) Retryable() bool

Retryable reports the final status-only retry classification.

func (*ResponseError) StatusCode

func (e *ResponseError) StatusCode() int

StatusCode returns the HTTP status, or zero when unavailable.

func (*ResponseError) Type

func (e *ResponseError) Type() string

Type returns the known Gateway error type.

func (*ResponseError) Unwrap

func (e *ResponseError) Unwrap() error

Unwrap returns the underlying cause, if any.

type ResponseEvent

type ResponseEvent interface {
	// contains filtered or unexported methods
}

ResponseEvent is one event read from a Responses API stream. Callers should use a type switch to distinguish ResponseOutputTextDeltaEvent from RawResponseEvent.

type ResponseFunctionCall

type ResponseFunctionCall struct {
	ID        string
	CallID    string
	Name      string
	Arguments string
}

type ResponseFunctionCallOutput

type ResponseFunctionCallOutput struct {
	CallID string
	Output string
}

type ResponseInput

type ResponseInput interface {
	// contains filtered or unexported methods
}

type ResponseInputItem

type ResponseInputItem interface {
	// contains filtered or unexported methods
}

type ResponseItemsInput

type ResponseItemsInput []ResponseInputItem

type ResponseJSONSchemaFormat

type ResponseJSONSchemaFormat struct {
	Name        string
	Description *string
	Schema      any
	Strict      *bool
}

type ResponseMessage

type ResponseMessage struct {
	Role    string
	Content string
}

type ResponseMetadata

type ResponseMetadata struct {
	ModelID string
	Headers http.Header
	Body    []byte
}

ResponseMetadata describes a successful HTTP response. ModelID is the caller-supplied ID. Headers and Body are defensive copies and non-nil after a successful bounded read, including an empty body; zero values mean no response metadata. Body is limited to 1 MiB and may contain echoed state or provider options, so it must be sanitized before logging or persistence.

type ResponseOutputTextDeltaEvent

type ResponseOutputTextDeltaEvent struct {
	Type  string
	Event string
	ID    string
	Delta string
}

ResponseOutputTextDeltaEvent is the documented response.output_text.delta event. Event is the SSE event name and ID is the SSE event ID.

type ResponseReasoning

type ResponseReasoning struct {
	Effort  string
	Summary *string
}

type ResponseResult

type ResponseResult struct {
	// contains filtered or unexported fields
}

ResponseResult retains the complete bounded status-200 JSON object.

func (*ResponseResult) RawJSON

func (r *ResponseResult) RawJSON() []byte

type ResponseSpecificToolChoice

type ResponseSpecificToolChoice struct{ Name string }

type ResponseStream

type ResponseStream struct {
	// contains filtered or unexported fields
}

ResponseStream is a caller-owned, incrementally read Responses API stream. It does not start a producer goroutine and does not retain a transcript.

func (*ResponseStream) Close

func (s *ResponseStream) Close() error

Close stops the stream and closes its response body exactly once. It is safe to call repeatedly and may be called to unblock a concurrent Next.

func (*ResponseStream) Err

func (s *ResponseStream) Err() error

Err returns the terminal stream error. Clean EOF and an explicit Close return nil. The returned error remains stable after termination.

func (*ResponseStream) Event

func (s *ResponseStream) Event() ResponseEvent

Event returns the current event, or nil before the first successful Next and after Next terminates.

func (*ResponseStream) Next

func (s *ResponseStream) Next() bool

Next advances to the next event. It returns false at clean framed EOF, after Close, or on error. Event is valid only after Next returns true.

type ResponseText

type ResponseText struct{ Format ResponseTextFormat }

type ResponseTextFormat

type ResponseTextFormat interface {
	// contains filtered or unexported methods
}

type ResponseTextFormatType

type ResponseTextFormatType string
const (
	ResponseTextFormatText       ResponseTextFormatType = "text"
	ResponseTextFormatJSONObject ResponseTextFormatType = "json_object"
)

type ResponseTextInput

type ResponseTextInput string

type ResponseTool

type ResponseTool struct {
	Name        string
	Description *string
	Parameters  any
	Strict      *bool
}

type ResponseToolChoice

type ResponseToolChoice interface {
	// contains filtered or unexported methods
}

type ResponseToolChoiceMode

type ResponseToolChoiceMode string
const (
	ResponseToolChoiceAuto     ResponseToolChoiceMode = "auto"
	ResponseToolChoiceRequired ResponseToolChoiceMode = "required"
	ResponseToolChoiceNone     ResponseToolChoiceMode = "none"
)

type ResponseValidationError

type ResponseValidationError struct {
	// contains filtered or unexported fields
}

ResponseValidationError reports malformed or contract-invalid status-200 output. Its path/reason, ID, truncation, and defensive-copy body accessors are nil-safe and return zero values; Error omits raw bodies, headers, credentials, request data, and wrapped cause text.

func (*ResponseValidationError) BodyTruncated

func (e *ResponseValidationError) BodyTruncated() bool

BodyTruncated reports whether retained response bytes omit a suffix.

func (*ResponseValidationError) Error

func (e *ResponseValidationError) Error() string

Error returns a bounded diagnostic that never contains response paths, reasons, raw response bytes, request data, headers, or credentials. Detailed validation state remains available through the typed accessors.

func (*ResponseValidationError) Path

func (e *ResponseValidationError) Path() string

Path returns the canonical wire-response path.

func (*ResponseValidationError) RawResponseBody

func (e *ResponseValidationError) RawResponseBody() []byte

RawResponseBody returns a fresh copy of retained response bytes. An upstream or intermediary may have echoed sensitive request state or provider options; callers must sanitize the bytes before logging or persistence.

func (*ResponseValidationError) Reason

func (e *ResponseValidationError) Reason() string

Reason returns the stable response-validation reason.

func (*ResponseValidationError) RequestID

func (e *ResponseValidationError) RequestID() string

RequestID returns the top-level Gateway request ID.

func (*ResponseValidationError) ResponseID

func (e *ResponseValidationError) ResponseID() string

ResponseID returns the top-level Gateway response ID.

func (*ResponseValidationError) StatusCode

func (e *ResponseValidationError) StatusCode() int

StatusCode returns 200 for a response validation failure, or zero when unavailable.

func (*ResponseValidationError) Unwrap

func (e *ResponseValidationError) Unwrap() error

Unwrap returns the underlying decoding or validation cause, if any.

type ResponseWebSearchTool

type ResponseWebSearchTool struct{}

ResponseWebSearchTool declares OpenAI-provider native web search. It always emits {"type":"web_search","search_context_size":"low"}. First-party public-HTTP compatibility evidence covers openai/gpt-5.4-mini; callers must choose a compatible OpenAI model. Gateway routing and fallback compatibility are not promised, and future model IDs are not rejected locally.

type ResponseXSearchOptionsTool

type ResponseXSearchOptionsTool struct {
	AllowedXHandles          []string
	ExcludedXHandles         []string
	FromDate                 *string
	ToDate                   *string
	EnableImageUnderstanding *bool
	EnableVideoUnderstanding *bool
}

ResponseXSearchOptionsTool declares Gateway-native SpaceXAI x_search with request options. Nil fields are omitted; non-nil empty slices are emitted.

type ResponseXSearchTool

type ResponseXSearchTool struct{}

ResponseXSearchTool declares Gateway-native SpaceXAI x_search. It emits only {"type":"x_search"}.

type ResponsesBuiltInToolsRequest

type ResponsesBuiltInToolsRequest struct {
	Request ResponsesRequest
	Tools   []ResponseBuiltInTool
}

ResponsesBuiltInToolsRequest adds sealed Responses built-in tool declarations without changing the source-compatible ResponsesRequest field inventory.

type ResponsesRequest

type ResponsesRequest struct {
	Model              string
	Input              ResponseInput
	MaxOutputTokens    *int
	Temperature        *float64
	TopP               *float64
	PresencePenalty    *float64
	FrequencyPenalty   *float64
	Instructions       *string
	Tools              []ResponseTool
	ToolChoice         ResponseToolChoice
	ParallelToolCalls  *bool
	AllowedTools       []string
	Reasoning          *ResponseReasoning
	Text               *ResponseText
	Truncation         *string
	PreviousResponseID *string
	Store              *bool
	Metadata           map[string]string
	Caching            *string
	CacheAnchorItems   *int
	CacheTTL           *string
	PromptCacheKey     *string
}

ResponsesRequest is a non-streaming Responses API request.

type RetryPolicy

type RetryPolicy struct {
	MaxAttempts  int
	InitialDelay time.Duration
	MaxDelay     time.Duration
	Multiplier   float64
	Jitter       float64
}

RetryPolicy configures bounded retries. MaxAttempts counts the initial request: zero and one mean one attempt, and 2 through 10 enable retries that may duplicate billable, non-idempotent generation or evaluation work. Streaming responses are not retried after response headers are received. With retries enabled, zero InitialDelay, MaxDelay, and Multiplier resolve to 100*time.Millisecond, 2*time.Second, and 2; zero Jitter disables jitter. Explicit InitialDelay must be 1ms..1m, MaxDelay 1ms..5m and at least InitialDelay, Multiplier 1..10, and Jitter 0..1. Delay before retry r (starting at 1) is the saturated min(MaxDelay, InitialDelay*Multiplier^(r-1)), with symmetric jitter applied as a fraction and rounded to a time.Duration nanosecond. Valid Retry-After replaces and is capped by MaxDelay. Waiting is context-cancellable.

type Rounding

type Rounding struct {
	ProbabilityDecimals *int
	ScoreDecimals       *int
}

Rounding reports provider decimal places. A nil Rounding object means the wire object was absent; an empty object is non-nil. Each nil field means its key was absent, while a non-nil pointer may contain zero and must be 0..15.

type ScoreAnswer

type ScoreAnswer struct {
	Score         float64
	Probabilities map[string]float64
}

ScoreAnswer reports a score and, when present, its probability distribution. A nil Probabilities map means the wire key was absent.

type ScoreQuestion

type ScoreQuestion struct {
	Instructions any
	Criteria     []any
}

ScoreQuestion asks for a score over ordered criteria indexed from zero. Instructions is required; Criteria must contain at least two descriptions.

type SpeechRequest

type SpeechRequest struct {
	Text            string
	Voice           string
	Instructions    string
	Language        string
	OutputFormat    string
	Speed           *float64
	ProviderOptions []ProviderOption
}

SpeechRequest describes one provider-protocol speech synthesis request.

type SpeechResult

type SpeechResult struct {
	Audio            string
	Warnings         []ProviderWarning
	ProviderMetadata map[string]json.RawMessage
	Response         ResponseMetadata
}

SpeechResult contains the provider's opaque audio string and response details.

type TokenSource

type TokenSource interface {
	Token(context.Context) (string, error)
}

TokenSource supplies an OIDC bearer token once per HTTP attempt using the current operation's request context. Errors and blank tokens become a TransportError whose Operation is "resolve OIDC token"; no credential fallback occurs.

type TranscriptionAudio

type TranscriptionAudio interface {
	// contains filtered or unexported methods
}

TranscriptionAudio is the closed set of supported transcription audio inputs.

type TranscriptionBase64

type TranscriptionBase64 struct {
	MediaType string
	Data      string
}

TranscriptionBase64 carries an opaque caller-provided audio string.

type TranscriptionBytes

type TranscriptionBytes struct {
	MediaType string
	Data      []byte
}

TranscriptionBytes carries raw audio bytes, encoded once as standard base64 on the wire.

type TranscriptionRequest

type TranscriptionRequest struct {
	Audio           TranscriptionAudio
	ProviderOptions []ProviderOption
}

TranscriptionRequest describes one provider-protocol transcription request.

type TranscriptionResult

type TranscriptionResult struct {
	Text             string
	Segments         []TranscriptionSegment
	Language         *string
	DurationSeconds  *float64
	Warnings         []ProviderWarning
	ProviderMetadata map[string]json.RawMessage
	Response         ResponseMetadata
}

TranscriptionResult contains one transcription and response details.

type TranscriptionSegment

type TranscriptionSegment struct {
	Text         string
	StartSeconds float64
	EndSeconds   float64
}

TranscriptionSegment is a timestamped transcription segment.

type TransportError

type TransportError struct {
	// contains filtered or unexported fields
}

TransportError reports failure resolving an OIDC token, encoding or creating a request, sending it, or reading a body. Operation is exactly "resolve OIDC token", "encode request", "create request", "send request", or "read response body". A nil receiver returns empty Operation, nil Unwrap, and "gateway transport error" from Error.

func (*TransportError) Error

func (e *TransportError) Error() string

Error returns a safe diagnostic that never includes the wrapped error text.

func (*TransportError) Operation

func (e *TransportError) Operation() string

Operation returns the stable transport operation, or an empty string.

func (*TransportError) Unwrap

func (e *TransportError) Unwrap() error

Unwrap returns the underlying transport cause.

type Usage

type Usage struct {
	InputTokens  *int64
	OutputTokens *int64
}

Usage reports token counts. A nil Usage object means the wire object was absent; an empty object is non-nil. Each nil field means its key was absent; a non-nil pointer may contain zero and is a non-negative JSON integer that fits int64.

type ValidationError

type ValidationError struct {
	// contains filtered or unexported fields
}

ValidationError reports an invalid evaluation request with a canonical public request Path and stable Reason. It does not unwrap. A nil receiver's accessors return empty strings and Error returns "gateway validation error".

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error returns a stable diagnostic that never contains request values or caller-controlled validation metadata. Detailed state remains available through Path and Reason.

func (*ValidationError) Path

func (e *ValidationError) Path() string

Path returns the canonical request path, or an empty string when unavailable.

func (*ValidationError) Reason

func (e *ValidationError) Reason() string

Reason returns the stable failure reason, or an empty string when unavailable.

type Warning

type Warning struct {
	Type    WarningType
	Feature string
	Details *string
	Setting string
	Message string
}

Warning is the lossless public form of one wire warning. Unsupported and compatibility warnings require Feature and may set Details; deprecated warnings require Setting and Message; other warnings require Message. Nil Details distinguishes an omitted details key from an empty string.

type WarningType

type WarningType string

WarningType identifies an evaluation warning variant.

const (
	// WarningUnsupported reports an unsupported feature.
	WarningUnsupported WarningType = "unsupported"
	// WarningCompatibility reports compatibility behavior.
	WarningCompatibility WarningType = "compatibility"
	// WarningDeprecated reports a deprecated setting.
	WarningDeprecated WarningType = "deprecated"
	// WarningOther reports another provider warning.
	WarningOther WarningType = "other"
)

Directories

Path Synopsis
examples
evaluate command
Command evaluate demonstrates one Evaluation Model V4 request.
Command evaluate demonstrates one Evaluation Model V4 request.
generate command
Command generate demonstrates credentialed buffered and streaming requests to the Responses and Chat Completions surfaces.
Command generate demonstrates credentialed buffered and streaming requests to the Responses and Chat Completions surfaces.
internal
testserver
Package testserver provides hermetic HTTP request capture for SDK tests.
Package testserver provides hermetic HTTP request capture for SDK tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL