Skip to main content

merge-gateway-python

Python SDK for the Merge Gateway API.

Installation

pip install merge-gateway-python

# Local development install
pip install -e ./

Quick start

from merge_gateway import MergeGateway

client = MergeGateway(
    api_key="sk-...",
    base_url="https://api-gateway-develop.merge.dev/v1",  # default
)

Usage

Responses

# Non-streaming
response = client.responses.create(
    model="openai/gpt-4o",
    input=[{"type": "message", "role": "user", "content": "Tell me a bedtime story about a otter."}],
)
print(response.output[0].content)

# Streaming
stream = client.responses.create(
    model="openai/gpt-4o",
    input=[{"type": "message", "role": "user", "content": "Hello"}],
    stream=True,
)
for event in stream:
    print(event)

Models

# List models (with optional pagination)
models = client.models.list()
for m in models.data:
    print(m.id, m.display_name)

# Filter by provider
openai_models = client.models.list(provider="openai")

# Retrieve a specific model
model = client.models.retrieve("openai/gpt-4o")

Embeddings

result = client.embeddings.create(
    model="openai/text-embedding-3-small",
    input="The food was delicious",
    encoding_format="float",
)
print(result.data[0].embedding)

Embedded Routing (customers)

Provision each of your end customers as a Customer with its own routing policies, provider keys, and budget, then scope LLM requests to it with the customer field.

# Provision a customer
customer = client.customers.create(name="Acme", origin_id="acme-corp")

client.customers.routing_policies.create(
    customer.id,
    name="workflow-a",
    strategy="PRIORITY",
    is_default=True,
    priority_order=[
        {"model": "openai/gpt-5.5", "priority": 1},
        {"model": "anthropic/claude-opus-4-8", "priority": 2},
    ],
)

client.customers.keys.upsert(customer.id, vendor="openai", api_key="sk-...")

client.customers.budgets.create(
    customer.id, spending_limit=50, reset_period="MONTHLY", spending_limit_type="HARD"
)

# Scope a request to the customer (omit `model` to route via its default policy)
response = client.responses.create(input="Hello!", customer=customer.id)

# Per-customer spend report
usage = client.customers.usage(customer.id, start="2026-07-01", end="2026-07-31")
print(usage.customer_byok_spend, usage.organization_byok_spend, usage.merge_spend)

Error handling

from merge_gateway import AuthenticationError, RateLimitError

try:
    response = client.responses.create(model="openai/gpt-4o", input="Hi")
except AuthenticationError:
    print("Check your API key")
except RateLimitError:
    print("Slow down!")

Publishing to PyPI

python -m build
twine upload dist/*

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

merge_gateway_python-0.3.1.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

merge_gateway_python-0.3.1-py3-none-any.whl (23.2 kB view details)

Uploaded Python 3

File details

Details for the file merge_gateway_python-0.3.1.tar.gz.

File metadata

  • Download URL: merge_gateway_python-0.3.1.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for merge_gateway_python-0.3.1.tar.gz
Algorithm Hash digest
SHA256 5438da61d26cc4d8cabd41e017d7a06738867439c5bd54c60ffe8377b6c94494
MD5 d60dbb258a515767ec4f98bb6eb95888
BLAKE2b-256 5691f98bf6654f2a1c6d38485f504d3665e57b3453eb71413a810e4e65a01e8e

See more details on using hashes here.

File details

Details for the file merge_gateway_python-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for merge_gateway_python-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aab2e92d0433bc2a9302844fdc3bcf32e7f5832466c88a3db99dd94661b1bf4a
MD5 fc844c80ef71ec741219e6d5a92b1087
BLAKE2b-256 7fa19a0519cd29e597f536cbbb911473b4bb2032b14bcc15ecf0b5195da31787

See more details on using hashes here.

Release history Release notifications | RSS feed

0.4.0

2 files

This release

0.3.1 This release

2 files

0.3.0

2 files

0.2.0

2 files

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