Skip to main content

Mock AI API

A local mock API for testing applications that integrate with OpenAI and Anthropic-compatible endpoints. It returns deterministic responses and can simulate streaming, tool calls, API errors, embeddings, moderation, files, batches, and response retrieval without making requests to a real provider.

Features

  • OpenAI-compatible endpoints on port 8011
  • Anthropic-compatible endpoints on port 8012
  • Non-streaming and Server-Sent Events (SSE) streaming responses
  • Scenario-based responses selected with a :mock-* model suffix
  • Tool calls and tool-use streams
  • Simulated rate limits and other API errors
  • OpenAI models, completions, responses, embeddings, moderation, image, and audio endpoints
  • Anthropic messages, token counting, batches, and file endpoints
  • In-memory state with no API keys, accounts, or external services required

Requirements

  • Python 3.11+

Quick start

Install the command-line server from PyPI with pipx:

pipx install mock-ai-api
mock-ai-api

If you prefer a virtual environment, install it with pip instead:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install mock-ai-api
mock-ai-api

From source

For local development or contributing, clone the repository and install the editable development dependencies:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install -e ".[dev]"
mock-ai-api

The requirements.txt file remains available for environments that prefer it: python -m pip install -r requirements.txt followed by python server.py.

The servers start at:

Use --openai-port or --anthropic-port to customize the launch:

mock-ai-api --openai-port 9001 --anthropic-port 9002
# or: python3 server.py --openai-port 9001 --anthropic-port 9002

Examples

Check both health endpoints:

curl http://127.0.0.1:8011/health
curl http://127.0.0.1:8012/health

Create an OpenAI-compatible chat completion:

curl http://127.0.0.1:8011/v1/chat/completions \
  -H 'content-type: application/json' \
  -d '{
    "model": "gpt-4.1:mock-text",
    "messages": [{"role": "user", "content": "Hello"}]
  }'

Request a simulated tool call:

curl http://127.0.0.1:8011/v1/chat/completions \
  -H 'content-type: application/json' \
  -d '{
    "model": "gpt-4.1:mock-tool-call",
    "messages": [{"role": "user", "content": "Write a file"}]
  }'

Create an Anthropic-compatible message:

curl http://127.0.0.1:8012/v1/messages \
  -H 'content-type: application/json' \
  -d '{
    "model": "claude-sonnet-4-20250514:mock-text",
    "max_tokens": 128,
    "messages": [{"role": "user", "content": "Hello"}]
  }'

Set "stream": true in supported requests to receive streaming events.

Scenarios

Append a scenario name to a model with :mock-*. The registry in app/scenarios.py is the source of truth for scenario behavior, documented examples, and advertised scenario models.

Scenario Provider Endpoint Response mode Status Example model Description
mock-text openai /v1/chat/completions text — gpt-4.1:mock-text Returns a fixed greeting.
mock-tool-call openai /v1/chat/completions tool call — gpt-4.1:mock-tool-call Returns one function tool call.
mock-tool-stream openai /v1/chat/completions stream — gpt-4.1:mock-tool-stream Streams function tool-call deltas.
mock-bad-tool-json openai /v1/chat/completions tool call — gpt-4.1:mock-bad-tool-json Returns intentionally malformed tool arguments.
mock-delay openai /v1/chat/completions delay — gpt-4.1:mock-delay Adds deterministic streaming delay.
mock-400 openai /v1/chat/completions error 400 gpt-4.1:mock-400 Returns an invalid-request error.
mock-401 openai /v1/chat/completions error 401 gpt-4.1:mock-401 Returns an authentication error.
mock-429 openai /v1/chat/completions error 429 gpt-4.1:mock-429 Returns a rate-limit error.
mock-500 openai /v1/chat/completions error 500 gpt-4.1:mock-500 Returns an API error.
mock-503 openai /v1/chat/completions error 503 gpt-4.1:mock-503 Returns a service-unavailable error.
mock-text anthropic /v1/messages text — claude-sonnet-4-20250514:mock-text Returns a fixed greeting.
mock-anthropic-stream anthropic /v1/messages stream — claude-sonnet-4-20250514:mock-anthropic-stream Streams text in Anthropic event order.
mock-tool-use anthropic /v1/messages tool call — claude-sonnet-4-20250514:mock-tool-use Returns one tool-use content block.
mock-tool-stream anthropic /v1/messages stream — claude-sonnet-4-20250514:mock-tool-stream Streams tool-use JSON deltas.
mock-delay anthropic /v1/messages delay — claude-sonnet-4-20250514:mock-delay Adds deterministic streaming delay.
mock-stream-error anthropic /v1/messages error — claude-sonnet-4-20250514:mock-stream-error Emits an API error event after streaming begins.
mock-400 anthropic /v1/messages error 400 claude-sonnet-4-20250514:mock-400 Returns an invalid-request error.
mock-401 anthropic /v1/messages error 401 claude-sonnet-4-20250514:mock-401 Returns an authentication error.
mock-429 anthropic /v1/messages error 429 claude-sonnet-4-20250514:mock-429 Returns a rate-limit error.
mock-500 anthropic /v1/messages error 500 claude-sonnet-4-20250514:mock-500 Returns an API error.
mock-503 anthropic /v1/messages error 503 claude-sonnet-4-20250514:mock-503 Returns a service-unavailable error.

Extending scenarios

  1. Add the scenario metadata to SCENARIOS in app/scenarios.py, including its provider, endpoint, response mode, optional error status, and example model.
  2. Implement the provider-specific behavior in the relevant builder or streaming path.
  3. The provider model catalog is generated from the registry; do not add a duplicate catalog entry by hand.
  4. Add direct API tests, SDK coverage where the endpoint is SDK-supported, and update this table to describe the behavior.

Development

Run the test suite with:

python3 -m pytest -q

The test clients instantiate each application directly, so the servers do not need to be running when tests execute. The SDK compatibility suite starts isolated local Uvicorn processes and is tested with openai==2.16.0 and anthropic==0.76.0. Its process-level fixture relies on POSIX file-descriptor inheritance, so those integration tests are skipped on non-POSIX platforms.

Project layout

app/
  openai_app.py       OpenAI-compatible routes
  anthropic_app.py    Anthropic-compatible routes
  openai/             OpenAI response builders and catalog
  anthropic/          Anthropic response builders and catalog
  scenarios.py        Scenario registry and model-suffix parsing
  schemas.py          Request models
server.py             Starts both mock servers
tests/                API behavior tests

Limitations

This project is intended for local development and automated tests. It is not a production inference server: responses are mocked, state is process-local and in-memory, authentication is not implemented, and the supported API surface is intentionally incomplete.

License

This project is licensed under the MIT License.

Metadata

Release files for mock-ai-api 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mock-ai-api 0.1.1
File Size Uploaded Provenance Actions
mock_ai_api-0.1.1.tar.gz 25.2 kB With provenance Details for mock_ai_api-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mock-ai-api 0.1.1
File Interpreter ABI Platform Actions
mock_ai_api-0.1.1-py3-none-any.whl Python 3 none any Details for mock_ai_api-0.1.1-py3-none-any.whl

Total release size: 47.7 kB

Release files / mock_ai_api-0.1.1.tar.gz

Download URL mock_ai_api-0.1.1.tar.gz
Size 25.2 kB
Tags Source
SHA-256 checksum
How to use checksums
69d21677aadb384804537118500527a8590e114a26d0fbfe629f468eae957cec
BLAKE2b-256 checksum
How to use checksums
da6e694d42d1330ea76227f6a8a5b13a26fa61c323133df019d9820af9080ba1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 27, 2026.

Transparency log

Release files / mock_ai_api-0.1.1-py3-none-any.whl

Download URL mock_ai_api-0.1.1-py3-none-any.whl
Size 22.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3dc9d7e3b4fd5b162daf3689aed6d825830bbd1fbe3e99b2478cc3965d042362
BLAKE2b-256 checksum
How to use checksums
36bffd5956110bbbf26f494baf97875ee5a5a4705e0f4621d3b3b5e669e1b915
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 27, 2026.

Transparency log

Security for mock-ai-api 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mock_ai_api-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page