Help us shape the future of Go! Take the 2026 Go Developer Survey

oaistream

package
v1.154.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const ProviderOptReplayReasoning = "replay_reasoning"

ProviderOptReplayReasoning is the provider_opts key that overrides, per model, whether stored assistant reasoning is replayed as a reasoning_content field on Chat Completions requests. A boolean; when unset, replay defaults on. It does not apply to the Responses API, where provider_opts.preserve_reasoning governs the replay of encrypted reasoning items instead.

Variables

This section is empty.

Functions

func ConvertMessages

func ConvertMessages(ctx context.Context, messages []chat.Message, id modelsdev.ID, store *modelsdev.Store, override *modelinfo.CapsOverride) []openai.ChatCompletionMessageParamUnion

ConvertMessages converts chat.Message slices to OpenAI message params, resolving attachment capabilities from the provided modelsdev.Store. A non-nil override declares the model's attachment capabilities explicitly, bypassing the models.dev lookup (issue #2741).

func ConvertMessagesWithCaps added in v1.87.0

func ConvertMessagesWithCaps(ctx context.Context, messages []chat.Message, mc modelinfo.ModelCapabilities) []openai.ChatCompletionMessageParamUnion

ConvertMessagesWithCaps is the caps-injectable variant of ConvertMessages. It is used by providers whose models are not in the models.dev catalog (e.g. Docker Model Runner), which must supply attachment capabilities explicitly rather than resolving them from a store.

func ConvertMessagesWithTransform added in v1.152.0

func ConvertMessagesWithTransform(ctx context.Context, messages []chat.Message, mc modelinfo.ModelCapabilities, transform func(int, []openai.ChatCompletionMessageParamUnion)) []openai.ChatCompletionMessageParamUnion

ConvertMessagesWithTransform decorates each source message's converted items before deferring tool attachments. The transform receives the source index, keeping provider-specific metadata attached to its original content.

func ConvertMultiContent

func ConvertMultiContent(ctx context.Context, multiContent []chat.MessagePart, id modelsdev.ID, store *modelsdev.Store, override *modelinfo.CapsOverride) []openai.ChatCompletionContentPartUnionParam

ConvertMultiContent converts chat.MessagePart slices to OpenAI content parts, resolving attachment capabilities from the provided modelsdev.Store. A non-nil override declares the model's attachment capabilities explicitly, bypassing the models.dev lookup (issue #2741).

func ErrorBodyMiddleware

func ErrorBodyMiddleware() option.Middleware

ErrorBodyMiddleware returns an OpenAI SDK middleware that preserves full error details in HTTP error responses.

The OpenAI SDK extracts only the "error" field from error response bodies (via gjson). When a provider returns a body without an "error" object (e.g. a string "error" field, plain text, or a different JSON structure), the details are silently lost. This middleware rewrites such responses into {"error": <original body>} so the SDK preserves the full content.

func MergeConsecutiveMessages

func MergeConsecutiveMessages(openaiMessages []openai.ChatCompletionMessageParamUnion) []openai.ChatCompletionMessageParamUnion

MergeConsecutiveMessages merges consecutive system or user messages into single messages. This is needed by some local models (like those run by DMR) that don't handle consecutive same-role messages well.

func ReplayReasoning added in v1.153.0

func ReplayReasoning(providerOpts map[string]any) bool

ReplayReasoning reports whether assistant reasoning stored in the session may be replayed to a model's endpoint. Replay defaults on. An explicit provider_opts.replay_reasoning overrides the default. A value that is present but not a boolean turns replay off instead of falling back to the default: the option is how an operator keeps reasoning away from an endpoint, so a typo must not leave replay enabled.

Replaying chat.Message.ReasoningContent lets a reasoning model see its own previous reasoning after a tool call (issue #4363). Because reasoning_content is a non-standard extension, the explicit false value is the compatibility escape hatch for endpoints that reject or must not receive it.

func WithoutReplayedReasoning added in v1.153.0

func WithoutReplayedReasoning(messages []chat.Message) []chat.Message

WithoutReplayedReasoning returns messages with assistant reasoning cleared, for a model that opted out with ProviderOptReplayReasoning. Only the reasoning field is affected: content, tool calls, and media are left alone, so an assistant turn that had both reasoning and content still converts to a valid message.

The input is never mutated -- the slice is copied only when something has to change -- because the messages belong to the session, not to the provider.

func WrapOpenAIError added in v1.32.3

func WrapOpenAIError(err error) error

WrapOpenAIError wraps an OpenAI SDK error in a *modelerrors.StatusError to carry HTTP status code and Retry-After metadata for the retry loop. Non-OpenAI errors (e.g. io.EOF, network errors) pass through unchanged. Exported so openai/response_stream.go can reuse it without duplication.

Types

type JSONSchema

type JSONSchema map[string]any

JSONSchema is a helper type that implements json.Marshaler for map[string]any. This allows us to pass schema maps to the OpenAI library which expects json.Marshaler.

func (JSONSchema) MarshalJSON

func (j JSONSchema) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for JSONSchema.

type StreamAdapter

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

StreamAdapter adapts the OpenAI stream to our interface

func NewStreamAdapter

func NewStreamAdapter(stream *ssestream.Stream[openai.ChatCompletionChunk], trackUsage bool) *StreamAdapter

func (*StreamAdapter) Close

func (a *StreamAdapter) Close()

Close closes the stream

func (*StreamAdapter) Recv

Recv gets the next completion chunk

Jump to

Keyboard shortcuts

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