Skip to content

Introduction

๐Ÿงช๐Ÿค– Pytest plugin for automatically mocking OpenAI requests. Powered by RESPX.

Why mock?

Mocking is a common practice in software testing. Instead of sending requests over the network, you can patch the requests to return predefined responses.

This has many benefits:

  • ๐Ÿค‘ Cost: Don't pay for actual API usage
  • ๐ŸŽ๏ธ Speed: Completely avoid the network
  • ๐Ÿ”’ Security: Nothing actually leaves the machine
  • ๐ŸŽฎ Control: 100% certainty on what will be received

Installation

Available on PyPi

pip install openai-responses
poetry add --group dev openai-responses

First steps

Before getting started, make sure that you've installed pytest. For async support, you can see more setup instructions here.

Quickstart

Simply decorate any test function that makes a call to the OpenAI API.

See examples for more.

Note

The API call(s) can either come from the official Python library, HTTPX, or HTTP Core. All documented examples will use the official Python library.

import openai

import openai_responses
from openai_responses import OpenAIMock


@openai_responses.mock() # (1)
def test_create_chat_completion(openai_mock: OpenAIMock): # (2)
    openai_mock.chat.completions.create.response = { # (3)
        "choices": [
            {
                "index": 0,
                "finish_reason": "stop",
                "message": {"content": "Hello! How can I help?", "role": "assistant"},
            }
        ]
    }

    client = openai.Client(api_key="sk-fake123")
    completion = client.chat.completions.create( # (4)
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Hello!"},
        ],
    )

    assert len(completion.choices) == 1
    assert completion.choices[0].message.content == "Hello! How can I help?"
    assert openai_mock.chat.completions.create.route.call_count == 1 # (5)
  1. Decorate the test function
  2. Use the test fixture
  3. Define the response
  4. Call the API the way you normally would (including async support)
  5. Optionally use the call history to add additional assertions