Documentation
¶
Overview ¶
Package testopenai provides a test OpenAI API server for testing. It uses VCR (Video Cassette Recorder) pattern to replay pre-recorded API responses, allowing deterministic testing without requiring actual API access or credentials.
Index ¶
Constants ¶
const CassetteNameHeader = "X-Cassette-Name"
CassetteNameHeader is the header used to specify which cassette to use for matching.
Variables ¶
This section is empty.
Functions ¶
func NewRequest ¶
NewRequest creates a new OpenAI request for the given cassette.
The returned request is an http.MethodPost with the body and CassetteNameHeader according to the pre-recorded cassette.
func ResponseBody ¶
ResponseBody is used in tests to avoid duplicating body content when the proxy serialization matches exactly the upstream (testopenai) server.
Types ¶
type Cassette ¶
type Cassette int
Cassette is an HTTP interaction recording.
Note: At the moment, our tests are optimized for single request/response pairs and do not include scenarios requiring multiple round-trips, such as `cached_tokens`.
const ( // CassetteChatBasic is the canonical OpenAI chat completion request. CassetteChatBasic Cassette = iota // CassetteChatJSONMode is a chat completion request with JSON response format. CassetteChatJSONMode // CassetteChatMultimodal is a multimodal chat request with text and image inputs. CassetteChatMultimodal // CassetteChatMultiturn is a multi-turn conversation with message history. CassetteChatMultiturn // CassetteChatNoMessages is a request missing the required messages field. CassetteChatNoMessages // CassetteChatParallelTools is a chat completion with parallel function calling enabled. CassetteChatParallelTools // CassetteChatStreaming is the canonical OpenAI chat completion request, // with streaming enabled. CassetteChatStreaming // CassetteChatTools is a chat completion request with function tools. CassetteChatTools // CassetteChatUnknownModel is a request with a non-existent model. CassetteChatUnknownModel // CassetteChatBadRequest is a request with multiple validation errors. CassetteChatBadRequest // CassetteChatReasoning tests capture of reasoning_tokens in completion_tokens_details for O1 models. CassetteChatReasoning // CassetteChatImageToText tests image input processing showing image token // count in usage details. CassetteChatImageToText // CassetteChatTextToImageTool tests image generation through tool calls since // chat completions cannot natively output images. CassetteChatTextToImageTool // CassetteChatAudioToText tests audio input transcription and audio_tokens // in prompt_tokens_details. CassetteChatAudioToText // CassetteChatTextToAudio tests audio output generation where the model // produces audio content, showing audio_tokens in completion_tokens_details. CassetteChatTextToAudio // CassetteChatDetailedUsage tests capture of all token usage detail fields in a single response. CassetteChatDetailedUsage // CassetteChatStreamingDetailedUsage tests capture of detailed token usage in streaming responses with include_usage. CassetteChatStreamingDetailedUsage // CassetteChatWebSearch tests OpenAI Web Search tool with a small URL response, including citations. CassetteChatWebSearch // CassetteChatStreamingWebSearch is CassetteChatWebSearch except with streaming enabled. CassetteChatStreamingWebSearch // CassetteChatOpenAIAgentsPython is a real request from OpenAI Agents Python library for financial research. // See https://github.com/openai/openai-agents-python/tree/main/examples/financial_research_agent CassetteChatOpenAIAgentsPython // CassetteEmbeddingsBasic is the canonical OpenAI embeddings request with a single string input. CassetteEmbeddingsBasic // CassetteEmbeddingsBase64 tests base64 encoding format for embedding vectors. CassetteEmbeddingsBase64 // CassetteEmbeddingsTokens tests embeddings with token array input instead of text. CassetteEmbeddingsTokens // CassetteEmbeddingsLargeText tests embeddings with a longer text input. CassetteEmbeddingsLargeText // CassetteEmbeddingsUnknownModel tests error handling for non-existent model. CassetteEmbeddingsUnknownModel // CassetteEmbeddingsDimensions tests embeddings with specified output dimensions. CassetteEmbeddingsDimensions // CassetteEmbeddingsMixedBatch tests batch with varying text lengths. CassetteEmbeddingsMixedBatch // CassetteEmbeddingsMaxTokens tests input that approaches token limit. CassetteEmbeddingsMaxTokens // CassetteEmbeddingsWhitespace tests handling of various whitespace patterns. CassetteEmbeddingsWhitespace // CassetteEmbeddingsBadRequest tests request with multiple validation errors. CassetteEmbeddingsBadRequest )
func ChatCassettes ¶
func ChatCassettes() []Cassette
ChatCassettes returns a slice of all cassettes for chat completions. Unlike image generation—which *requires* an image_generation tool call— audio synthesis is natively supported.
func EmbeddingsCassettes ¶
func EmbeddingsCassettes() []Cassette
EmbeddingsCassettes returns a slice of all cassettes for embeddings.