schema

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 17 Imported by: 1 module, 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 schema has the messages which providers reply with, which implement llm.Message, so providers share them

Index

Constants

View Source
const ResultOK = ResultStop

ResultOK is an alias for ResultStop (normal completion).

Variables

This section is empty.

Functions

func AudioName

func AudioName(name, contentType string) string

AudioName returns the name of an audio file, which is its name, when it has the extension of an audio file, or otherwise "audio", with the extension of its type, such as "audio.wav" for audio/wave, as an API may detect an audio file's format from its name

func CodeFence added in v1.3.0

func CodeFence(text string) string

CodeFence returns the fence of a code block of the text, of backticks, which is longer than any run of backticks in the text, and at least three, so a fenced code block in the text, such as of a Markdown file, doesn't close it

func IsText added in v1.3.0

func IsText(data []byte, contentType string) bool

IsText returns true when the data is text, which is UTF-8, without NUL bytes, and isn't of a type, such as a PDF or an image, which is sent to a model as it is, even when it's UTF-8

func LanguageCode

func LanguageCode(language string) string

LanguageCode returns the ISO 639-1 code of a language, by the name whisper returns it as, such as "fr" for "french", in any case, or the language, in lower case, when it isn't one of whisper's, such as a code, or an empty string when there's none

func ResourceType

func ResourceType(data []byte, name string) string

ResourceType returns the MIME type of a resource's data, detected from the data, such as image/png or audio/wave, or from the extension of its name, when the data isn't recognised, or is only text, or is an Ogg container, which the extension is more specific about, such as text/markdown or audio/ogg. Ogg data without an extension is audio/ogg, as Ogg is almost always audio, and MP4 data whose brand is M4A or M4B, or which is named .m4a or .m4b, is audio/mp4. Without data, it's the type of the extension. Returns application/octet-stream when neither is recognised, and an empty string when there's no data, and the extension isn't known.

func UnknownType

func UnknownType(contentType string) bool

UnknownType returns true when the MIME type is empty, or is application/octet-stream, in any case, and with any parameters, so it's detected from the data

func Unreadable added in v1.4.0

func Unreadable(uri, contentType string) string

Unreadable returns the text which is sent to a model in place of an attachment it can't read, such as audio, for a model which doesn't hear it, or an image in a format its provider doesn't take, which says its URI, so the model can give it to a tool which reads it

func WAVSamples added in v1.4.0

func WAVSamples(data []byte) ([]byte, int, int, bool)

WAVSamples returns the samples of a WAV file, from its data chunk, its rate, and channels, of 16-bit PCM, or false when it isn't one, or is cut short, such as when a chunk is larger than what's left of it, or its samples aren't whole frames. A data chunk whose size is unknown, as a stream's is, which is 0xFFFFFFFF, is the rest of the file.

Types

type ContentBlock

type ContentBlock struct {
	Text       *string     `` /* 141-byte string literal not displayed */
	Thinking   *string     `` /* 136-byte string literal not displayed */
	Signature  *string     `` /* 168-byte string literal not displayed */
	Redacted   *string     `` /* 171-byte string literal not displayed */
	Resource   *Resource   `` /* 173-byte string literal not displayed */
	ToolCall   *ToolCall   `` /* 161-byte string literal not displayed */
	ToolResult *ToolResult `` /* 190-byte string literal not displayed */
}

ContentBlock represents a single piece of content within a message. Exactly one of the fields should be non-nil/non-empty, apart from the signature, which is with the thinking it signs, or what a model made, such as an image, which Gemini signs.

func NewToolError

func NewToolError(id, name string, err error) ContentBlock

NewToolError creates a content block containing the error from the tool call with the id and name, with the error message as a JSON string

func NewToolResult

func NewToolResult(id, name string, v any) ContentBlock

NewToolResult creates a content block containing the result of the tool call with the id and name, or a tool error if the result is an error, or can't be encoded as JSON

func TextBlock added in v1.3.0

func TextBlock(name string, data []byte) ContentBlock

TextBlock returns the content block of a file of text, such as code or subtitles, which is sent to a model as text, rather than as an attachment, which most models don't accept for text: its name, and then its text, in a fenced code block

type Message

type Message struct {
	MessageMeta
	Tokens uint64     `json:"tokens,omitempty" help:"Token count attributed to this message" example:"12"`
	Result ResultType `` /* 170-byte string literal not displayed */
	Time   time.Time  `json:"time,omitzero" readonly:"" help:"When the message was added to the conversation, which the registry sets"`
}

Message is a message in a conversation with a model, as a provider replies with it, with the tokens it used, and why the model stopped generating it

func MessageFrom

func MessageFrom(message llm.Message) *Message

MessageFrom returns the message, which is a schema message, or otherwise a new message with the message's role and text. Resources are left out of a new message, as they'd need to be read.

func NewMessage

func NewMessage(role string, text string) (*Message, error)

Create a new message with the given role and text content

func (Message) String

func (m Message) String() string

type MessageMeta

type MessageMeta struct {
	Sender  string         `` /* 139-byte string literal not displayed */
	Content []ContentBlock `` /* 162-byte string literal not displayed */
}

MessageMeta is a message as it's sent to a model: its role, which is the user's when omitted, and its content. It uses a universal content block representation that can be marshaled to any provider's format.

func (MessageMeta) Resources

func (m MessageMeta) Resources() []llm.Resource

Resources returns the resource content blocks of the message

func (MessageMeta) Role

func (m MessageMeta) Role() string

Role returns the role of the message, which is the user's when it's omitted

func (MessageMeta) Text

func (m MessageMeta) Text() string

Text returns the text content blocks of the message, joined with newlines

func (MessageMeta) Thinking

func (m MessageMeta) Thinking() string

Thinking returns the thinking content blocks of the message, joined with newlines, which some providers need sent back with the message's tool calls

func (MessageMeta) ToolCalls

func (m MessageMeta) ToolCalls() []llm.ToolCall

ToolCalls returns the tool call content blocks of the message

func (MessageMeta) ToolResults

func (m MessageMeta) ToolResults() []llm.ToolResult

ToolResults returns the tool result content blocks of the message

type Resource

type Resource struct {
	ResourceMeta
}

Resource represents binary or URI-referenced media (images, documents, etc.) in a message

func PCM16 added in v1.3.0

func PCM16(data []byte, rate, channels int) *Resource

PCM16 returns raw audio, of signed 16-bit little-endian samples, at the rate, with the channels, as speech models return PCM, as audio/L16, whose samples are big-endian, and which has its rate and channels, so it's the audio it says it is. A trailing odd byte is left out.

func WAV16 added in v1.4.0

func WAV16(data []byte, rate, channels int) *Resource

WAV16 returns raw audio, of signed 16-bit little-endian samples, at the rate, with the channels, as a WAV file, audio/wav, which can be played, as audio/L16 often can't be. A trailing odd byte is left out.

func (Resource) Description

func (r Resource) Description() string

Description returns an empty string, as resources in a message don't have a description

func (Resource) Name

func (r Resource) Name() string

Name returns the last element of the path of the resource's URL, or an empty string when it's inline data only

func (Resource) Read

func (r Resource) Read(ctx context.Context) (llm.ReadFunc, error)

Read returns a function which reads the inline data of the resource. Resources referred to by a URL can't be read yet, as reading files or fetching URLs on the server would give clients access to its files and network.

func (Resource) Type

func (r Resource) Type() string

Type returns the MIME type of the resource

func (Resource) URI

func (r Resource) URI() string

URI returns the URL of the resource, or an empty string when it's inline data only

type ResourceMeta

type ResourceMeta struct {
	ContentType string   `json:"type" help:"Attachment MIME type, for example image/png or application/pdf" example:"image/png"`
	Data        []byte   `json:"data,omitempty" help:"Inline attachment payload encoded as a byte string" example:"iVBORw0KGgo="`
	URL         *url.URL `json:"url,omitempty" help:"Attachment URL reference, for example https, gs, or file" example:"https://example.com/image.png"`
}

ResourceMeta holds the fields of a resource which can be set: its type and its data, a URL, or both

func (ResourceMeta) MarshalJSON added in v1.3.0

func (r ResourceMeta) MarshalJSON() ([]byte, error)

MarshalJSON returns the resource as JSON, whose URL is a string, such as resource://<hash>, rather than its parts

func (*ResourceMeta) UnmarshalJSON added in v1.3.0

func (r *ResourceMeta) UnmarshalJSON(data []byte) error

UnmarshalJSON reads the resource from JSON, whose URL is a string, or, as it was once written, its parts. Returns an error when the URL isn't one.

type ResultType

type ResultType string

The result of generating a message (stopped, error, etc.)

const (
	ResultStop          ResultType = "stop"           // Normal completion
	ResultMaxTokens     ResultType = "max_tokens"     // Truncated due to max tokens
	ResultBlocked       ResultType = "blocked"        // Blocked by safety, recitation, or content filter
	ResultToolCall      ResultType = "tool_call"      // Model requested a tool call
	ResultError         ResultType = "error"          // Generation error
	ResultOther         ResultType = "other"          // Other/unknown finish reason
	ResultMaxIterations ResultType = "max_iterations" // Tool-calling loop exhausted max iterations
	ResultMaxContext    ResultType = "max_context"    // The conversation doesn't fit in the model's context
)

func (ResultType) String

func (r ResultType) String() string

func (*ResultType) UnmarshalJSON

func (r *ResultType) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes a result, which must be one of the results

type ToolCall

type ToolCall struct {
	ID    string          `json:"id,omitempty" help:"Provider-assigned tool call identifier" example:"call_123"`
	Name  string          `json:"name" help:"Tool name to invoke" example:"get_weather"`
	Input json.RawMessage `json:"input,omitempty" help:"JSON-encoded arguments passed to the tool" example:"{\"city\":\"London\"}"`
	Meta  map[string]any  `json:"meta,omitempty" help:"Provider-specific metadata associated with the tool call" example:"{\"provider\":\"demo\"}"`
}

ToolCall represents a tool invocation requested by the model

type ToolResult

type ToolResult struct {
	ID      string          `json:"id,omitempty" help:"Tool call identifier this result belongs to" example:"call_123"`
	Name    string          `json:"name,omitempty" help:"Tool name that produced this result" example:"get_weather"`
	Content json.RawMessage `json:"content,omitempty" help:"JSON-encoded tool output content" example:"{\"temperature_c\":18}"`
	IsError bool            `json:"is_error,omitempty" help:"Whether the tool result represents an error" example:"false"`
	Usage   *llm.Usage      `json:"usage,omitempty" help:"Tokens the tool used, such as an agent, which asks a model, or none"`
}

ToolResult represents the result of running a tool

Jump to

Keyboard shortcuts

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