model

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 20 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package model defines JSON helpers for marshaling and unmarshaling provider message parts. This file focuses on decoding messages and discriminating concrete part types based on the Kind field.

Package model defines the provider-agnostic message and streaming types used by planners, runtimes, and provider adapters. It models messages as typed parts (thinking, text, tool use/results) plus conversation roles. Runtime-owned ToolResultPart.Parts carries image/document attachments; ordinary tools return JSON Content. ToolResultPartsInfo reports optional provider support. Unsupported adapters return UnsupportedPartError before provider I/O. Empty Parts preserves existing message JSON bytes. Protocol-18 recall copies authorized user parts into scoped blobs and renders their references as tool-result attachments. Recorded support is intersected across observed models; carry and summaries omit recalled attachments.

Index

Constants

View Source
const (
	// ChunkTypeText identifies a chunk carrying assistant text.
	ChunkTypeText = "text"

	// ChunkTypeToolCall identifies a chunk carrying a tool invocation.
	ChunkTypeToolCall = "tool_call"

	// ChunkTypeToolCallDelta identifies a chunk carrying an incremental tool-call
	// input JSON fragment.
	//
	// Naming note: this is a *delta* because fragments are not guaranteed to be
	// valid JSON boundaries. It exists solely for progressive UI previews and is
	// safe to ignore; the canonical tool payload is still emitted as
	// ChunkTypeToolCall.
	ChunkTypeToolCallDelta = "tool_call_delta"

	// ChunkTypeThinking identifies a chunk carrying thinking content.
	ChunkTypeThinking = "thinking"

	// ChunkTypeCompletionDelta identifies a chunk carrying an incremental typed
	// completion JSON fragment.
	//
	// Naming note: this is a *delta* because fragments are not guaranteed to be
	// valid JSON boundaries. It exists solely for progressive previews and is
	// safe to ignore; the canonical completion payload is emitted as
	// ChunkTypeCompletion.
	ChunkTypeCompletionDelta = "completion_delta"

	// ChunkTypeCompletion identifies a chunk carrying the canonical typed
	// completion payload for a structured-output stream.
	ChunkTypeCompletion = "completion"

	// ChunkTypeUsage identifies a chunk carrying a usage delta.
	ChunkTypeUsage = "usage"

	// ChunkTypeStop identifies the terminal chunk carrying a stop reason.
	ChunkTypeStop = "stop"
)
View Source
const (

	// MaxToolDefinitionsPerRequest is the largest exact tool catalog accepted by
	// the model contract and durable recovery boundary.
	MaxToolDefinitionsPerRequest = 256
)

Variables

View Source
var ErrRateLimited = errors.New("model: rate limited")

ErrRateLimited indicates the provider rejected the request due to rate limiting after exhausting any configured retries. Callers must not retry in a tight loop and should treat this as a transient infrastructure failure that is safe to surface to higher layers.

View Source
var ErrStreamingUnsupported = errors.New("model: streaming not supported")

ErrStreamingUnsupported indicates the provider does not support streaming.

View Source
var ErrStructuredOutputUnsupported = errors.New("model: structured output not supported")

ErrStructuredOutputUnsupported indicates the provider does not support native structured output for the requested call shape.

View Source
var ErrUnsupportedPart = errors.New("model: part not supported")

ErrUnsupportedPart is matched by unsupported message-part errors.

Functions

func DecodeSchemaJSON added in v2.1.0

func DecodeSchemaJSON(raw []byte) (any, error)

DecodeSchemaJSON preserves numeric tokens for exact comparisons while retaining JSON v2's strict UTF-8 and unique object member requirements.

func NormalizeProviderModel added in v2.1.0

func NormalizeProviderModel(config, id string) string

NormalizeProviderModel removes only Bedrock routing prefixes. Dated model versions and all other provider identities remain exact.

func SetCompletionValidator added in v2.1.0

func SetCompletionValidator(request *Request, validate func(*Response, *Completion) error) error

SetCompletionValidator attaches an application-owned decoder to the exact structured-output request. NewClient captures the function before provider work, and providers cannot replace it through exported request fields.

func ValidateTokenUsage added in v2.1.0

func ValidateTokenUsage(usage TokenUsage) error

ValidateTokenUsage checks that token counts are non-negative and that a non-zero total equals the sum of uncached input, output, and cache tokens.

func ValidateToolResultParts added in v2.1.0

func ValidateToolResultParts(req *Request, support ToolResultPartSupport, provider, modelID, unsupportedReason string) error

ValidateToolResultParts rejects unsupported nested attachments before provider I/O. unsupportedReason overrides the zero-support reason for model-specific exclusions.

func WithCallInfo added in v2.1.0

func WithCallInfo(ctx context.Context, info CallInfo) context.Context

WithCallInfo returns a child context carrying info.

Types

type CacheCheckpointPart

type CacheCheckpointPart struct{}

CacheCheckpointPart marks a cache boundary in a message. Provider adapters translate this to provider-specific caching directives (e.g., Bedrock cachePoint). Providers that do not support caching ignore this part. It is complementary to CacheOptions/CachePolicy: agents can combine explicit CacheCheckpointPart instances with policy-driven AfterSystem/AfterTools checkpoints to express complex caching layouts.

type CacheOptions

type CacheOptions struct {
	// AfterSystem places a checkpoint after all system messages.
	AfterSystem bool

	// AfterTools places a checkpoint after tool definitions. Not all
	// providers support tool-level checkpoints (e.g., Nova does not).
	AfterTools bool
}

CacheOptions configures prompt caching behavior for a request. Provider adapters translate these flags to provider-specific caching directives. Providers that do not support caching ignore these options. When Cache is nil on a Request, the runtime may populate it from the agent RunPolicy (CachePolicy) so callers do not need to thread CacheOptions through every call site. Explicit Request.Cache values always take precedence.

type CallInfo added in v2.1.0

type CallInfo struct {
	AgentID string
	RunID   string
	// RootRunID is the root run's shared run store scope.
	RootRunID string
	SessionID string
	TurnID    string
	// ModelID is the registered identifier requested by the planner, which may
	// differ from the provider model named in Request.Model.
	ModelID string
	// Seq is allocated by the run store per (RootRunID, RunID), starting at 1.
	// Parallel calls and activity retries never reuse an allocated sequence.
	Seq int
	// Attempt is the engine's activity attempt, starting at 1. It is zero
	// when the caller invokes a planner activity without an engine context.
	Attempt int
}

CallInfo identifies one model invocation made through a planner's model client. Direct Runtime.ModelClient calls do not receive this metadata automatically.

func CallInfoFrom added in v2.1.0

func CallInfoFrom(ctx context.Context) (CallInfo, bool)

CallInfoFrom returns the model invocation identity carried by ctx, if any.

type Capabilities added in v2.1.0

type Capabilities struct {
	ProviderConfiguration ProviderConfigurationInfo
	Counter               TokenCounter
	Info                  ContextInfo
	ToolResultParts       ToolResultPartsInfo
}

Capabilities selects the optional interfaces a decorated client exposes.

type Chunk

type Chunk interface {
	// Kind identifies the event variant.
	Kind() string
	// contains filtered or unexported methods
}

Chunk is one closed model-stream event. Only the value variants declared in this package are accepted by the validated stream boundary.

type Citation

type Citation struct {
	// Title is the source document title/identifier when available.
	Title string

	// Source is a provider-specific source identifier when available.
	Source string

	// Location identifies where in the source document the cited content can be found.
	Location CitationLocation

	// SourceContent is the cited excerpt from the source document when provided.
	SourceContent []string
}

Citation links generated content back to a specific location in a source document.

type CitationLocation

type CitationLocation struct {
	// DocumentChar identifies a character span within a source document.
	DocumentChar *DocumentCharLocation

	// DocumentChunk identifies a range of chunks within a source document.
	DocumentChunk *DocumentChunkLocation

	// DocumentPage identifies a page within a source document.
	DocumentPage *DocumentPageLocation
}

CitationLocation identifies where cited content can be found within a document.

Exactly one of DocumentChar, DocumentChunk, or DocumentPage should be set when present.

type CitationsPart

type CitationsPart struct {
	// Text is the generated content supported by Citations.
	Text string

	// Citations reference the source documents that informed Text.
	Citations []Citation
}

CitationsPart is a generated content block paired with citation metadata.

Providers may emit this part instead of a TextPart when citation generation is enabled.

type Client

type Client interface {
	// Complete performs a non-streaming model invocation.
	Complete(ctx context.Context, req *Request) (*Response, error)

	// Stream performs a validated streaming model invocation when supported.
	Stream(ctx context.Context, req *Request) (ValidatedStreamer, error)
}

Client is the validated provider-agnostic model client.

NewClient owns the canonical implementation. Its distinct streaming return type prevents a raw Provider from satisfying Client accidentally.

func NewClient added in v2.1.0

func NewClient(provider Provider) (Client, error)

NewClient constructs a model client that applies one immutable request contract to every raw provider completion and stream.

func WithCapabilities added in v2.1.0

func WithCapabilities(client Client, counter TokenCounter, info ContextInfo) Client

WithCapabilities constructs a decorated Client preserving exactly the optional capabilities supplied by the caller. Keeping method-set selection here avoids duplicating capability-specific wrapper types in client middleware.

func WithCapabilitySet added in v2.1.0

func WithCapabilitySet(client Client, caps Capabilities) Client

WithCapabilitySet preserves exactly the supplied optional capabilities.

type Completion

type Completion struct {
	// Name is the stable completion identifier associated with this payload.
	Name string

	// Payload is the canonical JSON value for the streamed completion.
	Payload rawjson.Message
}

Completion is the canonical structured payload emitted for a streaming typed completion.

Contract:

  • Provider adapters MUST populate Payload as valid JSON.
  • Completion is the only canonical structured payload for a typed completion stream; callers must not reconstruct the final value from completion deltas.

type CompletionChunk added in v2.1.0

type CompletionChunk struct {
	Completion Completion
}

CompletionChunk carries one canonical structured completion payload.

func (CompletionChunk) Kind added in v2.1.0

func (CompletionChunk) Kind() string

Kind returns the completion event discriminator.

type CompletionDelta

type CompletionDelta struct {
	// Name is the stable completion identifier associated with this delta.
	Name string

	// Delta is the raw JSON fragment emitted by the provider.
	Delta string
}

CompletionDelta is an incremental structured-output fragment streamed while the provider is still constructing the final completion JSON.

Contract:

  • This is a best-effort UX signal. Consumers may ignore it entirely.
  • The canonical completion payload remains Completion.Payload in the final ChunkTypeCompletion emitted before stream termination.
  • Delta is not guaranteed to be valid JSON on its own; callers must treat it as an opaque fragment suitable only for progressive previews.

type CompletionDeltaChunk added in v2.1.0

type CompletionDeltaChunk struct {
	Delta CompletionDelta
}

CompletionDeltaChunk carries one provisional structured-output fragment.

func (CompletionDeltaChunk) Kind added in v2.1.0

Kind returns the completion-delta event discriminator.

type ContextInfo added in v2.1.0

type ContextInfo interface {
	// ContextLimits resolves the request model and reports its limits and known window.
	ContextLimits(ctx context.Context, req *Request) (ContextLimits, string, bool)
}

ContextInfo is an optional provider/client capability. Lookup resolves the request's model and returns its ID even when limits are unknown. The bool reports whether ContextWindow is known. Network lookups belong in activities, never workflow code.

type ContextLimits added in v2.1.0

type ContextLimits struct {
	// InvalidOverride marks a merged runtime override with unusable provider limits.
	InvalidOverride bool `json:"invalid_override,omitzero"`
	// ContextWindow is the shared token window used for input/output budgeting.
	ContextWindow int
	// MaxOutputTokens is the provider's maximum completion cap.
	MaxOutputTokens int
	// DefaultMaxTokens is the configured completion cap sent when MaxTokens is zero.
	DefaultMaxTokens int
	// ImageTokens is the per-image measurement cost; zero selects 1,500.
	ImageTokens int
}

ContextLimits describes a model's token limits. Zero fields are unknown, except ImageTokens: zero selects the measurement default of 1,500 tokens.

type ConversationRole

type ConversationRole string

ConversationRole is the role for a message in a conversation.

const (
	// ConversationRoleSystem is the role for system messages.
	ConversationRoleSystem ConversationRole = "system"

	// ConversationRoleUser is the role for user messages.
	ConversationRoleUser ConversationRole = "user"

	// ConversationRoleAssistant is the role for assistant messages.
	ConversationRoleAssistant ConversationRole = "assistant"
)

type DocumentCharLocation

type DocumentCharLocation struct {
	DocumentIndex int
	Start         int
	End           int
}

DocumentCharLocation identifies a character span within a document.

type DocumentChunkLocation

type DocumentChunkLocation struct {
	DocumentIndex int
	Start         int
	End           int
}

DocumentChunkLocation identifies a chunk range within a document.

type DocumentFormat

type DocumentFormat string

DocumentFormat identifies the on-wire format (extension) of a document part.

Provider adapters may support only a subset of formats. Callers should normalize uploads to one of the supported formats before constructing a DocumentPart.

const (
	// DocumentFormatPDF identifies a PDF document.
	DocumentFormatPDF DocumentFormat = "pdf"

	// DocumentFormatCSV identifies a CSV document.
	DocumentFormatCSV DocumentFormat = "csv"

	// DocumentFormatDOC identifies a legacy Microsoft Word document.
	DocumentFormatDOC DocumentFormat = "doc"

	// DocumentFormatDOCX identifies a Microsoft Word document.
	DocumentFormatDOCX DocumentFormat = "docx"

	// DocumentFormatXLS identifies a legacy Microsoft Excel document.
	DocumentFormatXLS DocumentFormat = "xls"

	// DocumentFormatXLSX identifies a Microsoft Excel document.
	DocumentFormatXLSX DocumentFormat = "xlsx"

	// DocumentFormatHTML identifies an HTML document.
	DocumentFormatHTML DocumentFormat = "html"

	// DocumentFormatTXT identifies a plain text document.
	DocumentFormatTXT DocumentFormat = "txt"

	// DocumentFormatMD identifies a Markdown document.
	DocumentFormatMD DocumentFormat = "md"
)

type DocumentPageLocation

type DocumentPageLocation struct {
	DocumentIndex int
	Start         int
	End           int
}

DocumentPageLocation identifies a page number within a document.

type DocumentPart

type DocumentPart struct {
	// Name is a short neutral identifier for the document (for example, "spec").
	Name string

	// Format identifies the document format/extension (for example, "pdf", "txt", "md").
	Format DocumentFormat

	// Bytes carries the raw document bytes when the document is provided as an upload.
	Bytes []byte

	// Text carries the document content when the document is provided as a single text blob.
	Text string

	// Chunks carries the document content split into logical chunks when citations
	// should reference chunk indices rather than character spans.
	Chunks []string

	// URI locates the document externally when the document should not be
	// embedded in the request payload (for example, "s3://bucket/key.pdf").
	//
	// Provider adapters fail fast when URI schemes are not supported.
	URI string

	// Context is optional contextual information about how the document should be
	// interpreted by the model when generating citations.
	Context string

	// Cite requests provider-native citations when supported.
	Cite bool
}

DocumentPart carries documents in user messages or runtime-owned tool results.

Documents are intended for models that support document inputs and citation generation. Exactly one of Bytes, Text, Chunks, or URI must be provided.

type ImageFormat

type ImageFormat string

ImageFormat identifies the on-wire format of an image part.

Provider adapters may support only a subset of formats. Callers should normalize uploads to one of the supported formats before constructing an ImagePart.

const (
	// ImageFormatPNG identifies a PNG-encoded image.
	ImageFormatPNG ImageFormat = "png"

	// ImageFormatJPEG identifies a JPEG-encoded image.
	ImageFormatJPEG ImageFormat = "jpeg"

	// ImageFormatGIF identifies a GIF-encoded image.
	ImageFormatGIF ImageFormat = "gif"

	// ImageFormatWEBP identifies a WebP-encoded image.
	ImageFormatWEBP ImageFormat = "webp"
)

type ImagePart

type ImagePart struct {
	// Format identifies the encoding of Bytes (e.g., "png").
	Format ImageFormat

	// Bytes contains the raw image bytes for the declared format.
	Bytes []byte
}

ImagePart carries image bytes in a user message or runtime-owned tool result.

Image parts are intended for multimodal models. Provider adapters fail fast when images are used in unsupported roles or for unsupported model families.

type Message

type Message struct {
	// Role identifies the speaker for this message.
	Role ConversationRole

	// Parts are the ordered content blocks for the message.
	Parts []Part

	// Meta carries optional provider- or application-specific metadata
	// attached to the message.
	Meta map[string]any
}

Message is a single chat message.

Messages are ordered and grouped into a transcript passed to model clients. Parts preserve structure (text, thinking, tool use, tool result) rather than flattening to plain strings.

func (Message) MarshalJSON

func (m Message) MarshalJSON() ([]byte, error)

MarshalJSON encodes a Message while preserving the concrete Part types stored in Parts via an explicit Kind discriminator.

This ensures round-trips through JSON do not lose type information when Parts are stored as an interface slice.

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a Message while materializing concrete Part implementations stored in the Parts slice.

type ModelClass

type ModelClass string

ModelClass identifies the model family.

Providers map these classes to concrete model identifiers.

const (
	// ModelClassHighReasoning selects a high-reasoning model family.
	ModelClassHighReasoning ModelClass = "high-reasoning"

	// ModelClassDefault selects the default model family.
	ModelClassDefault ModelClass = "default"

	// ModelClassSmall selects a small/cheap model family.
	ModelClassSmall ModelClass = "small"
)

type OutputValidationError added in v2.1.0

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

OutputValidationError reports that provider output failed the immutable request contract. Error deliberately renders a constant public summary; use Kind, Evidence, Usage, and errors.Is/errors.As for structured diagnostics.

func RestoreOutputValidationError added in v2.1.0

func RestoreOutputValidationError(kind OutputValidationKind, cause error, evidence ResponseEvidence, usage *TokenUsage) (*OutputValidationError, error)

RestoreOutputValidationError reconstructs a terminal output rejection after a trusted transport decodes its bounded fields.

func (*OutputValidationError) Error added in v2.1.0

func (e *OutputValidationError) Error() string

Error returns a stable summary without rendering rejected model output or provider-authored identifiers.

func (*OutputValidationError) Evidence added in v2.1.0

Evidence returns bounded identity for the rejected response.

func (*OutputValidationError) Kind added in v2.1.0

Kind returns the closed category of the first rejecting check.

func (*OutputValidationError) Unwrap added in v2.1.0

func (e *OutputValidationError) Unwrap() error

Unwrap preserves the private validation cause for trusted in-process error inspection.

func (*OutputValidationError) Usage added in v2.1.0

func (e *OutputValidationError) Usage() *TokenUsage

Usage returns a detached copy of valid scalar usage retained from the rejected response.

type OutputValidationKind added in v2.1.0

type OutputValidationKind string

OutputValidationKind identifies the request-contract rule that rejected provider output. Values are safe to persist and aggregate because they do not contain model text, tool names, arguments, identifiers, or schema paths.

const (
	// OutputValidationResponseShape means the normalized response or chunk did
	// not have a valid canonical structure.
	OutputValidationResponseShape OutputValidationKind = "response_shape"

	// OutputValidationOutputBounds means provider output exceeded a byte,
	// nesting, collection, or generation limit.
	OutputValidationOutputBounds OutputValidationKind = "output_bounds"

	// OutputValidationToolIdentity means a tool call lacked a usable identity or
	// named a tool absent from the exact current request.
	OutputValidationToolIdentity OutputValidationKind = "tool_identity"

	// OutputValidationToolArguments means tool arguments were not canonical JSON
	// or did not satisfy the advertised input schema.
	OutputValidationToolArguments OutputValidationKind = "tool_arguments"

	// OutputValidationToolChoice means returned calls violated the request's
	// tool-choice rule.
	OutputValidationToolChoice OutputValidationKind = "tool_choice"

	// OutputValidationStructuredOutput means a requested structured completion
	// was missing, malformed, or failed its schema.
	OutputValidationStructuredOutput OutputValidationKind = "structured_output"

	// OutputValidationStreamProtocol means stream events were incomplete, out of
	// order, or otherwise violated the terminal protocol.
	OutputValidationStreamProtocol OutputValidationKind = "stream_protocol"

	// OutputValidationUsage means provider token accounting was malformed.
	OutputValidationUsage OutputValidationKind = "usage"
)

type Part

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

Part is a marker interface implemented by all message parts. Concrete implementations capture user-visible text, provider-issued thinking, and tool call/result content in a strongly typed form.

type Provider added in v2.1.0

type Provider interface {
	// Complete performs a non-streaming model invocation.
	Complete(ctx context.Context, req *Request) (*Response, error)

	// Stream performs a streaming model invocation when supported.
	Stream(ctx context.Context, req *Request) (Streamer, error)
}

Provider is the raw provider-agnostic model transport. Provider adapters translate Requests into provider calls and return normalized Responses and Chunks. Callers should construct a validated Client with NewClient instead of consuming a Provider directly.

type ProviderConfigurationInfo added in v2.1.0

type ProviderConfigurationInfo interface {
	ProviderConfiguration(context.Context, *Request) string
}

ProviderConfigurationInfo names the exact provider configuration of a request.

type ProviderError

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

ProviderError describes a failure returned by a model provider (e.g. Bedrock). It is intended to cross package boundaries so runtimes can surface stable, structured information to callers.

func AsProviderError

func AsProviderError(err error) (*ProviderError, bool)

AsProviderError returns the first ProviderError in err's chain, if any.

func NewProviderError

func NewProviderError(provider, operation string, httpStatus int, kind ProviderErrorKind, code, message, requestID string, retryable bool, cause error) *ProviderError

NewProviderError constructs a ProviderError. provider and kind are required. cause may be nil but is recommended to preserve the original error chain.

func (*ProviderError) Code

func (e *ProviderError) Code() string

Code returns the provider-specific error code when available.

func (*ProviderError) Error

func (e *ProviderError) Error() string

func (*ProviderError) HTTPStatus

func (e *ProviderError) HTTPStatus() int

HTTPStatus returns the provider HTTP status code when available, otherwise 0.

func (*ProviderError) Kind

func (e *ProviderError) Kind() ProviderErrorKind

Kind returns the coarse-grained provider error classification.

func (*ProviderError) Message

func (e *ProviderError) Message() string

Message returns the provider error message when available.

func (*ProviderError) Operation

func (e *ProviderError) Operation() string

Operation returns the provider operation name when known (for example, "converse_stream").

func (*ProviderError) Provider

func (e *ProviderError) Provider() string

Provider returns the provider identifier (for example, "bedrock").

func (*ProviderError) RequestID

func (e *ProviderError) RequestID() string

RequestID returns the provider request identifier when available.

func (*ProviderError) Retryable

func (e *ProviderError) Retryable() bool

Retryable reports whether retrying the call may succeed without changing the request.

func (*ProviderError) Unwrap

func (e *ProviderError) Unwrap() error

Unwrap returns the underlying provider error to preserve the original error chain.

type ProviderErrorKind

type ProviderErrorKind string

ProviderErrorKind classifies provider failures into a small set of categories suitable for retry and UX decisions.

const (
	// ProviderErrorKindAuth indicates authentication/authorization failures.
	ProviderErrorKindAuth ProviderErrorKind = "auth"

	// ProviderErrorKindInvalidRequest indicates the request is invalid and retrying
	// without changing the request will not succeed.
	ProviderErrorKindInvalidRequest ProviderErrorKind = "invalid_request"

	// ProviderErrorKindContextOverflow indicates the request exceeded the model's
	// context window. Retrying the same request will not succeed.
	ProviderErrorKindContextOverflow ProviderErrorKind = "context_overflow"

	// ProviderErrorKindRateLimited indicates the provider is throttling requests.
	ProviderErrorKindRateLimited ProviderErrorKind = "rate_limited"

	// ProviderErrorKindUnavailable indicates a transient provider failure (5xx,
	// network issues) where a retry may succeed.
	ProviderErrorKindUnavailable ProviderErrorKind = "unavailable"

	// ProviderErrorKindUnknown indicates an unclassified provider failure.
	ProviderErrorKindUnknown ProviderErrorKind = "unknown"
)

type Request

type Request struct {
	// RunID identifies the logical run for this request when available.
	RunID string

	// Model is the provider-specific model identifier when specified.
	Model string

	// ModelClass selects a model family when Model is not specified.
	ModelClass ModelClass

	// PromptRefs records the exact prompts and versions used to build this
	// request. Runtime components and observers use this for provenance and
	// auditing; provider adapters must treat it as metadata and must not
	// translate it to provider wire payloads.
	PromptRefs []prompt.PromptRef

	// Messages is the ordered transcript provided to the model.
	Messages []*Message

	// Temperature controls sampling when supported by the provider.
	Temperature float32

	// Tools lists the tool definitions available to the model.
	Tools []*ToolDefinition

	// ToolChoice optionally constrains how the model uses tools.
	ToolChoice *ToolChoice

	// MaxTokens caps the number of output tokens when supported.
	MaxTokens int

	// StructuredOutput constrains assistant output to a JSON schema when the
	// provider supports native structured-output enforcement.
	StructuredOutput *StructuredOutput

	// Stream requests streaming responses when true and supported.
	Stream bool

	// Thinking configures provider-specific reasoning behavior.
	Thinking *ThinkingOptions

	// Cache configures prompt caching behavior. Nil means no caching.
	Cache *CacheOptions
	// contains filtered or unexported fields
}

Request captures inputs for a model invocation.

func CloneRequest added in v2.1.0

func CloneRequest(request *Request) (*Request, error)

CloneRequest returns a bounded, ownership-safe copy of a model request.

type RequestContract added in v2.1.0

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

RequestContract is an immutable snapshot of the request fields that decide whether provider output can be accepted.

func NewRequestContract added in v2.1.0

func NewRequestContract(req *Request) (*RequestContract, error)

NewRequestContract validates req and snapshots the exact tool and structured output contracts used to accept provider output.

func (*RequestContract) ValidateResponse added in v2.1.0

func (c *RequestContract) ValidateResponse(resp *Response) (*Response, error)

ValidateResponse validates a complete provider response against the captured request contract and returns a detached response on success.

func (*RequestContract) ValidateStream added in v2.1.0

func (c *RequestContract) ValidateStream(stream Streamer) (ValidatedStreamer, error)

ValidateStream wraps a raw provider stream with request-scoped chunk and terminal validation, canonical response access, and serialized finalization.

type RequestInputUsage added in v2.1.0

type RequestInputUsage struct {
	// Present distinguishes a scoped measurement from legacy billed accounting.
	Present bool `json:"present"`
	// InputTokens includes cached tokens in the original request, excluding
	// extra provider-generated tool-use prompts billed during generation.
	InputTokens int `json:"input_tokens"`
}

RequestInputUsage scopes input measurement to the original canonical request, separately from billed usage across provider continuation requests. Its zero value preserves legacy accounting. A present zero count is unmeasured or zero.

type Response

type Response struct {
	// Content is the ordered list of assistant messages produced.
	Content []Message

	// ToolCalls lists tool invocations requested by the model.
	ToolCalls []ToolCall

	// Usage reports token consumption for the request.
	Usage TokenUsage

	// RequestInputUsage measures only the original request when the provider
	// bills several internal continuation requests as one logical completion.
	RequestInputUsage RequestInputUsage `json:"request_input_usage,omitzero"`

	// StopReason records why generation stopped (provider-specific).
	StopReason string

	// OutputLimited reports that the provider stopped because generated
	// output reached its configured token limit. A validated
	// client never exposes such a response as successful completed work.
	OutputLimited bool
}

Response is the canonical result of a model invocation.

Content carries assistant messages; ToolCalls holds any tool invocations requested by the model; Usage and StopReason mirror provider metadata.

func CloneResponse added in v2.1.0

func CloneResponse(response *Response) (*Response, error)

CloneResponse returns a bounded, ownership-safe copy of a model response.

type ResponseEvidence added in v2.1.0

type ResponseEvidence struct {
	Present     bool
	ByteCount   int
	Fingerprint [sha256.Size]byte
	// RequestInputUsage retains original-request measurement, never billed totals.
	RequestInputUsage RequestInputUsage `json:"request_input_usage,omitzero"`
}

ResponseEvidence is bounded identity for rejected output. It intentionally contains no response content.

type StopChunk added in v2.1.0

type StopChunk struct {
	Reason        string
	OutputLimited bool
}

StopChunk terminates a model stream.

func (StopChunk) Kind added in v2.1.0

func (StopChunk) Kind() string

Kind returns the terminal stop event discriminator.

type StreamResponseBuilder added in v2.1.0

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

StreamResponseBuilder assembles a provider-owned terminal response from the chunks emitted by one provider stream. A builder is not safe for concurrent use.

func (*StreamResponseBuilder) Add added in v2.1.0

func (b *StreamResponseBuilder) Add(chunk Chunk) error

Add records one chunk in the provider's terminal response.

func (*StreamResponseBuilder) Response added in v2.1.0

func (b *StreamResponseBuilder) Response() *Response

Response returns the assembled response after a stop chunk. It returns nil while the stream is incomplete.

type Streamer

type Streamer interface {
	// Recv returns the next streaming chunk or an error.
	Recv() (Chunk, error)

	// Close releases any resources associated with the stream.
	Close() error

	// Response returns the provider-owned terminal response after Recv has
	// returned a literal io.EOF. It returns nil before then or after failure.
	Response() *Response
}

Streamer delivers incremental model output.

Raw provider owners must drain the stream until Recv returns io.EOF or another terminal error, then call Close. Consumers receive a ValidatedStreamer and call Finalize instead.

type StructuredOutput

type StructuredOutput struct {
	// Name identifies the response format for providers that require one.
	Name string

	// Schema is the JSON Schema payload sent to the provider.
	Schema rawjson.Message
}

StructuredOutput configures provider-enforced JSON output when supported. Schema must contain a JSON Schema object accepted by the target provider.

type TextChunk added in v2.1.0

type TextChunk struct {
	Message Message
}

TextChunk carries one incremental assistant message.

func (TextChunk) Kind added in v2.1.0

func (TextChunk) Kind() string

Kind returns the text event discriminator.

type TextPart

type TextPart struct {
	// Text is the human-readable content for this part.
	Text string
}

TextPart is a plain text content block in a message.

Text is emitted as-is to the UI or consumer when the message is rendered.

type ThinkingChunk added in v2.1.0

type ThinkingChunk struct {
	Message Message
}

ThinkingChunk carries one incremental assistant thinking message.

func (ThinkingChunk) Kind added in v2.1.0

func (ThinkingChunk) Kind() string

Kind returns the thinking event discriminator.

type ThinkingOptions

type ThinkingOptions struct {
	// Enable turns provider thinking features on when supported.
	Enable bool

	// Effort selects a named reasoning level when supported. Ollama accepts
	// "low", "medium", or "high"; a nonempty value takes precedence over
	// Enable. Empty preserves the provider's existing Enable behavior.
	Effort string `json:"effort,omitempty"`

	// Interleaved requests interleaved thinking and assistant content when
	// supported.
	Interleaved bool

	// BudgetTokens caps the number of thinking tokens when supported.
	BudgetTokens int
}

ThinkingOptions configures provider thinking behavior.

type ThinkingPart

type ThinkingPart struct {
	// Text is the provider-visible reasoning text when available.
	Text string

	// Signature is the provider-issued signature for Text when present.
	Signature string

	// Redacted carries provider-issued reasoning content in redacted form
	// when plaintext Text is not available.
	Redacted []byte

	// Index is the position of this block in the reasoning sequence.
	Index int

	// Final reports whether this reasoning block is the last one for the
	// current turn.
	Final bool
}

ThinkingPart represents provider-issued reasoning content.

Providers may attach a signature or redacted payload; callers treat this as opaque metadata and surface it according to UI policy.

type TokenCount

type TokenCount struct {
	// Model is the provider-resolved model identifier that would receive this
	// request. It is empty when an estimator cannot resolve the concrete model.
	Model string

	// ModelClass is the logical model family requested by the caller.
	ModelClass ModelClass

	// InputTokens is the number of input tokens counted or estimated.
	InputTokens int

	// Exact reports whether InputTokens came from provider-native counting.
	Exact bool
}

TokenCount reports preflight input-token usage for a model request.

type TokenCounter

type TokenCounter interface {
	CountTokens(ctx context.Context, req *Request) (TokenCount, error)
}

TokenCounter is an optional model-client capability for preflight token counting. Provider adapters that can count tokens natively should implement it with Exact=true. Local estimators should report Exact=false so callers can reject approximation when enforcing hard context-window contracts.

Counts measure the durable transcript. Implementations exclude replayed thinking blocks from the counted surface because thinking signatures are provider-specific replay metadata, not portable user-visible input.

type TokenEstimator

type TokenEstimator struct {
	// CharactersPerToken is the approximate byte-to-token conversion ratio.
	// When zero, the estimator uses three characters per token.
	CharactersPerToken int

	// OverheadTokens adds a fixed allowance for provider framing and request
	// fields that are not represented directly in message text.
	// When zero, the estimator uses 500 tokens.
	OverheadTokens int

	// MinimumTokens is the minimum non-zero estimate returned for tiny
	// requests. When zero, the estimator uses 500 tokens.
	MinimumTokens int
}

TokenEstimator provides an explicit local fallback for clients that cannot count tokens natively. It measures the provider-visible request surface by byte size and converts it with a conservative character-per-token ratio.

func (TokenEstimator) CountTokens

func (e TokenEstimator) CountTokens(ctx context.Context, req *Request) (TokenCount, error)

CountTokens estimates req's input-token usage with Exact=false. It is intended for explicit fallback paths such as rate limiting or non-native providers, not for provider-specific billing or hard context-window guarantees.

type TokenUsage

type TokenUsage struct {
	// Model is the provider-resolved model identifier that produced this
	// usage (e.g., "us.anthropic.claude-sonnet-4-20250514-v1:0"). Set by
	// the model adapter; empty when the adapter does not report it.
	Model string

	// ModelClass is the logical model family that was requested (e.g.,
	// "default", "high-reasoning", "small"). Set by the model adapter
	// from the original request.
	ModelClass ModelClass

	// InputTokens is the number of uncached input tokens consumed. It does
	// not include CacheReadTokens or CacheWriteTokens.
	InputTokens int

	// OutputTokens is the number of tokens produced by outputs.
	OutputTokens int

	// TotalTokens is the total number of tokens consumed by the call. When
	// non-zero, it equals InputTokens + OutputTokens + CacheReadTokens +
	// CacheWriteTokens.
	TotalTokens int

	// CacheReadTokens is the number of input tokens read from cache. This
	// count is disjoint from InputTokens.
	CacheReadTokens int

	// CacheWriteTokens is the number of input tokens written to cache. This
	// count is disjoint from InputTokens.
	CacheWriteTokens int
}

TokenUsage tracks token counts and model attribution for a single model invocation. The counts are additive; the identity fields (Model, ModelClass) describe the source of the delta and are not aggregated by addTokenUsage.

type ToolCall

type ToolCall struct {
	// Name is the tool identifier requested by the model.
	Name tools.Ident

	// Payload is the canonical JSON arguments supplied by the model.
	//
	// Provider adapters MUST populate this as a valid JSON value. Planners and
	// runtimes treat it as opaque JSON and rely on codecs for any schema-aware
	// decoding. The RawJSON type exists to make workflow-boundary encoding safe
	// (it normalizes empty payloads to JSON null).
	Payload rawjson.Message

	// ID is an optional provider-issued identifier for the tool call.
	ID string
}

ToolCall is a requested tool invocation from the model.

Tool calls capture the tool identity, raw arguments, and an optional provider-issued call identifier.

type ToolCallChunk added in v2.1.0

type ToolCallChunk struct {
	ToolCall ToolCall
}

ToolCallChunk carries one complete model tool call.

func (ToolCallChunk) Kind added in v2.1.0

func (ToolCallChunk) Kind() string

Kind returns the tool-call event discriminator.

type ToolCallDelta

type ToolCallDelta struct {
	// Name is the canonical tool identifier for this delta stream.
	//
	// Provider adapters MUST populate Name for every emitted delta so
	// downstream consumers can render tool-specific previews deterministically.
	Name tools.Ident

	// ID is the provider-issued tool call identifier used to correlate all
	// deltas and the final ToolCall payload.
	ID string

	// Delta is a raw JSON fragment emitted by the provider.
	Delta string
}

ToolCallDelta is an incremental tool-call payload fragment streamed by providers while they are still constructing the full tool input JSON.

Contract:

  • This is a best-effort UX signal. Consumers may ignore it entirely.
  • The canonical tool payload remains ToolCall.Payload in the final ChunkTypeToolCall emitted once the provider closes the tool block.
  • Delta is not guaranteed to be valid JSON on its own; callers must treat it as an opaque fragment suitable only for progressive UI previews.

type ToolCallDeltaChunk added in v2.1.0

type ToolCallDeltaChunk struct {
	Delta ToolCallDelta
}

ToolCallDeltaChunk carries one incremental tool-call payload fragment.

func (ToolCallDeltaChunk) Kind added in v2.1.0

func (ToolCallDeltaChunk) Kind() string

Kind returns the tool-call-delta event discriminator.

type ToolChoice

type ToolChoice struct {
	// Mode selects the desired tool behavior for the request.
	Mode ToolChoiceMode

	// Name identifies the tool to request when Mode is ToolChoiceModeTool.
	// It must match the Name of one of the tool definitions in Request.Tools.
	Name string
}

ToolChoice configures optional tool-use behavior for a Request.

When ToolChoice is nil, providers use their default tool behavior (typically auto-selection). When non-nil, providers apply the requested mode or fail fast if the mode is not supported.

type ToolChoiceMode

type ToolChoiceMode string

ToolChoiceMode controls how the model uses tools for a request.

Not all providers support all modes. Provider adapters fail fast when a mode is not supported rather than silently degrading behavior.

const (
	// ToolChoiceModeAuto lets the provider decide whether to call tools or
	// respond with text. This is the default when ToolChoice is nil.
	ToolChoiceModeAuto ToolChoiceMode = "auto"

	// ToolChoiceModeNone disables tool use for the request when supported by
	// the provider.
	ToolChoiceModeNone ToolChoiceMode = "none"

	// ToolChoiceModeAny forces the model to request at least one tool when
	// supported by the provider.
	ToolChoiceModeAny ToolChoiceMode = "any"

	// ToolChoiceModeTool forces the model to request the specific tool
	// identified by ToolChoice.Name when supported by the provider.
	ToolChoiceModeTool ToolChoiceMode = "tool"
)

type ToolDefinition

type ToolDefinition struct {
	// Name is the tool identifier as seen by the model.
	Name string

	// Description is a concise summary presented to the model to decide
	// when to call the tool.
	Description string

	// InputSchema is a JSON Schema describing the tool input payload.
	InputSchema any
}

ToolDefinition describes a tool exposed to the model.

Definitions are derived from Goa tool specifications and include the name, description, and JSON Schema input.

func CloneToolDefinitions added in v2.1.0

func CloneToolDefinitions(definitions []*ToolDefinition) ([]*ToolDefinition, error)

CloneToolDefinitions returns a bounded, ownership-safe copy of tool definitions and their input schemas.

type ToolResultContentPart added in v2.1.0

type ToolResultContentPart interface {
	Part
	// contains filtered or unexported methods
}

ToolResultContentPart is a non-text attachment in a runtime-owned tool result. Only images and documents (including pointers) implement it.

type ToolResultPart

type ToolResultPart struct {
	// ToolUseID correlates this result to a prior tool use declaration.
	ToolUseID string

	// Content is the provider-facing tool result payload.
	//
	// Contract:
	//   - Successful results carry a decoded JSON-compatible value.
	//   - Failed results carry plain error text with IsError=true.
	//   - Runtime/tooling code must not store raw JSON bytes or Go error
	//     structs here.
	Content any

	// IsError reports whether Content represents an error from the tool.
	IsError bool

	// Parts contains runtime-owned attachments; ordinary tools still return JSON.
	// Message JSON encodes this as Parts only when non-empty.
	Parts []ToolResultContentPart `json:"-"`
}

ToolResultPart carries a tool result provided by the user side.

Tool results are attached to user messages so the model can read them in subsequent turns.

type ToolResultPartSupport added in v2.1.0

type ToolResultPartSupport struct {
	ImageFormats                    []ImageFormat
	DocumentFormats                 []DocumentFormat
	DocumentURISchemes              []string
	MaxImageBytes, MaxDocumentBytes int
}

ToolResultPartSupport describes accepted byte uploads and document URI schemes. Zero byte limits mean no uploads of that kind are supported.

func (ToolResultPartSupport) Accepts added in v2.1.0

Accepts reports whether the attachment fits this support, or its rejection reason.

func (ToolResultPartSupport) Intersect added in v2.1.0

Intersect returns sorted common formats/schemes and the smaller byte limits.

type ToolResultPartsInfo added in v2.1.0

type ToolResultPartsInfo interface {
	ToolResultParts(ctx context.Context, req *Request) ToolResultPartSupport
}

ToolResultPartsInfo is an optional capability resolved for each request model.

type ToolUsePart

type ToolUsePart struct {
	// ID uniquely identifies this tool call within the run.
	ID string

	// Name is the tool identifier requested by the model.
	Name string

	// Input is the JSON-compatible arguments object provided by the model.
	Input any
}

ToolUsePart declares a tool invocation by the assistant.

The planner/runtime turns these declarations into concrete tool executions and correlates results via ToolResultPart.ToolUseID.

type UnsupportedPartError added in v2.1.0

type UnsupportedPartError struct {
	Provider, Model, Placement, Kind, Format, Reason string
}

UnsupportedPartError identifies a message part rejected before provider I/O.

func (*UnsupportedPartError) Error added in v2.1.0

func (e *UnsupportedPartError) Error() string

Error describes the unsupported placement and reason.

func (*UnsupportedPartError) Is added in v2.1.0

func (e *UnsupportedPartError) Is(target error) bool

Is matches ErrUnsupportedPart independently of provider details.

type UsageChunk added in v2.1.0

type UsageChunk struct {
	Usage TokenUsage
}

UsageChunk carries one incremental token-usage record.

func (UsageChunk) Kind added in v2.1.0

func (UsageChunk) Kind() string

Kind returns the usage event discriminator.

type ValidatedStreamer added in v2.1.0

type ValidatedStreamer interface {
	Streamer

	Finalize(primaryErr error) error
}

ValidatedStreamer is the complete consumer-facing stream contract returned by clients constructed with NewClient. Response is available only after a clean EOF. Finalize combines the caller's terminal result with exactly-once provider cleanup.

Jump to

Keyboard shortcuts

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