Skip to main content

Python SDK for the Merge Gateway API

Project description

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/*

Project details


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.0.tar.gz (23.4 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.0-py3-none-any.whl (23.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: merge_gateway_python-0.3.0.tar.gz
  • Upload date:
  • Size: 23.4 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.0.tar.gz
Algorithm Hash digest
SHA256 dc5d84262f5ea8591dc999fab7927e841028a0260de70d8ca8b4ae4d65bbda8a
MD5 bfa0c92d33043cd826cba2acd48fa07e
BLAKE2b-256 b92fe1d5d979f0a7f5c0ef6ce360017656e7949468694b0dcf96850bf6d14bd8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for merge_gateway_python-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ac25e92e34a42dbc6adbc696b58ac524607da60ed438001d4ff34ae4486ea8e2
MD5 5b7806a835ea52b3aba315bc6c82b317
BLAKE2b-256 bc52d841075e2432e4a95ca038c9d8befa19acb2f980dbdb93b432fe2f6af47f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page