DEV Community

Cover image for Testing OpenAI integrations without paying per call
Chiheb Nabil
Chiheb Nabil

Posted on Edited on

Testing OpenAI integrations without paying per call

Testing code that calls the OpenAI API gets expensive fast. Every iteration costs money, you burn through rate limits, and CI pipelines need a real API key to run. So I built a small library that fakes the OpenAI API: openai-api-mock.

It works with anything that speaks the OpenAI API since the mock intercepts by base URL, it also covers OpenAI-compatible providers like Azure OpenAI, Groq, Together, or a local Ollama instance.

The problem it solves

While building a project that used function calling, I had to test structured responses over and over. Each run cost money, and I couldn't run tests in CI without exposing a key. I wanted something I could drop into a test setup and forget about, so I wrote it.

Usage

nstall it as a dev dependency:

npm install -D openai-api-mock
Enter fullscreen mode Exit fullscreen mode

Then call one function before your code makes OpenAI requests:

const { mockOpenAIResponse } = require('openai-api-mock');
mockOpenAIResponse(); // from here on, every OpenAI API call returns a mock response
Enter fullscreen mode Exit fullscreen mode

Under the hood it uses nock to intercept HTTP requests to the OpenAI API and @faker-js/faker to generate the fake data.

What's new in 0.4.1

Version 0.4.1 adds support for the embeddings endpoint — including deterministic vectors (same input → same output), all input types, and both floatand base64encoding formats. This means you can test semantic search, RAG pipelines, and clustering with reproducible results.

// Request base64-encoded embeddings
const response = await openai.embeddings.create({
  model: 'text-embedding-3-small',
  input: 'test',
  encoding_format: 'base64',
});
console.log(response.data[0].embedding); // base64 string
Enter fullscreen mode Exit fullscreen mode

If you hit a case it doesn't handle, open an issue on the repo. Feedback welcome.

Github Repo

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.