openai

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package openai provides TruLayer auto-instrumentation for the github.com/openai/openai-go client.

The wrapper opens a TruLayer span around each chat-completion call and records model, input, output, token counts, latency, and errors on it. If the caller's context does not carry an active TruLayer trace, the wrapper transparently delegates to the underlying client.

tl := trulayer.NewClient(os.Getenv("TRULAYER_API_KEY"))
oai := openai.NewClient()
instrumented := tlopenai.InstrumentOpenAI(&oai, tl)

trace, ctx := tl.NewTrace(ctx, "answer-question")
defer trace.End(ctx)
resp, err := instrumented.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{...})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InstrumentedChatService

type InstrumentedChatService struct {
	// Completions provides the instrumented chat-completion surface. Its New
	// method has the same signature as (*openai.ChatCompletionService).New.
	Completions *InstrumentedCompletionService
}

InstrumentedChatService mirrors the shape of openai.ChatService for the Completions field.

type InstrumentedCompletionService

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

InstrumentedCompletionService mirrors the shape of openai.ChatCompletionService for the New method. It wraps calls in a TruLayer span.

func (*InstrumentedCompletionService) New

New mirrors (*openai.ChatCompletionService).New. It opens a TruLayer span (when a trace is active in ctx), invokes the underlying client, and records the result on the span before returning.

The wrapper never converts panics into returned errors and never drops the upstream error — it forwards both verbatim.

type InstrumentedOpenAIClient

type InstrumentedOpenAIClient struct {
	// Chat provides the instrumented chat service. Its shape mirrors
	// openai.ChatService so callers can use it as a drop-in replacement for
	// the field on the original client.
	Chat InstrumentedChatService
}

InstrumentedOpenAIClient wraps an openai.Client and emits a TruLayer span for each Chat.Completions.New call. The zero value is not usable — construct via InstrumentOpenAI.

func InstrumentOpenAI

func InstrumentOpenAI(client *openai.Client, tl *trulayer.Client) *InstrumentedOpenAIClient

InstrumentOpenAI returns a wrapper around client that records a TruLayer span on every Chat.Completions.New call. The span is attached to whichever trace is carried in the request context (via trulayer.TraceFromContext). If no trace is active, the wrapper delegates without recording anything.

Jump to

Keyboard shortcuts

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