openai

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 7 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 openai implements the model_protocol adapter for the OpenAI protocol family (OpenAI chat/completions, DeepSeek, Groq and the OpenAI Responses API).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adapter

type Adapter struct{}

Adapter is the OpenAI-family protocol adapter. It is stateless.

func New

func New() *Adapter

New returns the OpenAI-family protocol adapter.

func (*Adapter) CredentialHeader added in v1.8.9

func (a *Adapter) CredentialHeader() string

CredentialHeader returns the header InjectAuth writes, in canonical form.

func (*Adapter) ErrorNormalizer

func (a *Adapter) ErrorNormalizer() utils.ErrorNormalizer

ErrorNormalizer returns the phase-1 default normalizer (never recognizes errors, callers keep their status-code whitelist). The client-facing parser for normalization (AIConf.NormalizeUpstreamError) lives in ErrorParser below; the two seams stay decoupled on purpose.

func (*Adapter) ErrorParser added in v1.8.9

func (a *Adapter) ErrorParser() utils.ErrorParser

ErrorParser returns the OpenAI-family error parser for client-facing normalization (AIConf.NormalizeUpstreamError).

func (*Adapter) ExtraHeaders

func (a *Adapter) ExtraHeaders() map[string]string

ExtraHeaders returns nil: the OpenAI protocol needs no supplementary headers.

func (*Adapter) ExtractUsageFields

func (a *Adapter) ExtractUsageFields(data []byte) utils.UsageFields

ExtractUsageFields extracts usage fields via the OpenAI main chain plus the DeepSeek and Responses API cache-read fallbacks (see utils.ParseOpenAIUsageFields).

func (*Adapter) InjectAuth

func (a *Adapter) InjectAuth(outreq *bfe_http.Request, key string) error

InjectAuth writes the upstream credential as an Authorization: Bearer header. An empty key is a no-op.

func (*Adapter) IsFinalUsageEvent

func (a *Adapter) IsFinalUsageEvent(ev utils.StreamEvent) bool

IsFinalUsageEvent carries the legacy hard-coded final-usage check: an Anthropic message_delta event, the Anthropic non-streaming top-level type "message", or a protocol-agnostic final usage chunk (an OpenAI stream_options.include_usage chunk has no top-level type). The Responses API response.completed event carries the final usage under response.usage (issue #1381). Without "message_delta" a cross-protocol stream (Bearer request detected as openai while the backend answers with an Anthropic body) would lose the final usage and be undercharged; without "message" the final usage of a non-streaming Anthropic JSON body would never be recognized (issue #1364).

func (*Adapter) IsStreamTerminal

func (a *Adapter) IsStreamTerminal(ev utils.StreamEvent) bool

IsStreamTerminal carries the legacy hard-coded termination check that lived in mod_body_process.SSEEvent.GetQuotaUsage: an Anthropic-style message_stop event or the OpenAI [DONE] marker ends the stream. The Responses API terminal event response.completed (issue #1381) joins the rule; its usage arrives in the same event, so the stream is both terminated and finally billed there. The OpenAI adapter is also the registry fallback (unknown/empty auth styles), so it keeps the full combined rule to preserve the previous behavior for every request that does not identify a concrete protocol.

func (*Adapter) Key

func (a *Adapter) Key() string

func (*Adapter) NativeStreamErrorEvent added in v1.9.0

func (a *Adapter) NativeStreamErrorEvent(message string) []byte

NativeStreamErrorEvent builds an OpenAI-style stream error event: the data payload is the {"error":{message,type,param,code}} envelope with type "server_error" and no event: line (OpenAI chat streams are untyped).

func (*Adapter) ParseError added in v1.8.9

func (a *Adapter) ParseError(statusCode int, body []byte, header http.Header) *utils.ProtocolError

ParseError parses an OpenAI-style error envelope ({"error":{message,type,param,code}}) into a normalized ProtocolError. code may be a string or a nested object (some providers nest it); a nested object is ignored and the mapping falls back to type/status. Returning nil means the body is not a recognizable OpenAI error envelope and the caller applies its unrecognized-action.

func (*Adapter) ParseStreamError added in v1.8.9

func (a *Adapter) ParseStreamError(ev utils.StreamEvent) *utils.ProtocolError

ParseStreamError inspects one SSE event. OpenAI signals in-stream errors with an event whose data is an {"error":{...}} object (the OpenAI adapter is also the registry fallback, so this check doubles as the generic rule for unknown protocols). The HTTP status of a stream is 200; the mapping is driven by the envelope's code/type fields.

func (*Adapter) StreamErrorParser added in v1.8.9

func (a *Adapter) StreamErrorParser() utils.StreamErrorParser

StreamErrorParser returns the per-event SSE error parser.

func (*Adapter) StreamTerminalMarkers added in v1.9.0

func (a *Adapter) StreamTerminalMarkers() [][]byte

StreamTerminalMarkers returns the wire-level markers that terminate an OpenAI-family stream (issue #1474 truncation detection).

func (*Adapter) WrapStreamErrorPayload added in v1.9.0

func (a *Adapter) WrapStreamErrorPayload(payload string) []byte

WrapStreamErrorPayload wraps an already-serialized error JSON payload (e.g. the unified AiError body) into an OpenAI stream error event.

Jump to

Keyboard shortcuts

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