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)

Structured output

Request schema-constrained JSON with response_format. strict is tri-state: omit it for the provider default, pass strict=False for schemas with optional fields or unions (OpenAI-style strict mode rejects those). If the routed model can't honor the schema, the gateway returns a clear 400 unsupported_params — never silent best-effort JSON.

from typing import Optional

from pydantic import BaseModel

from merge_gateway import response_format_from_model


class Person(BaseModel):
    name: str
    age: Optional[int] = None


response = client.responses.create(
    model="openai/gpt-4o",
    input="Invent a person",
    response_format=response_format_from_model("person", Person, strict=False),
)

Or hand-build the payload (typed models in merge_gateway.types or a plain dict):

response = client.responses.create(
    model="openai/gpt-4o",
    input="Invent a person",
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "person",
            "schema": {
                "type": "object",
                "properties": {"name": {"type": "string"}},
                "required": ["name"],
                "additionalProperties": False,
            },
            "strict": True,
        },
    },
)

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.4.0.tar.gz (28.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.4.0-py3-none-any.whl (26.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: merge_gateway_python-0.4.0.tar.gz
  • Upload date:
  • Size: 28.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.4.0.tar.gz
Algorithm Hash digest
SHA256 0b3811f1f97d1aed10dbc20803b0b6643f898a2e19609fd2aab7f04a7b7488d9
MD5 a0339c82effe590c1062c53f9e7d0e1b
BLAKE2b-256 a0067771ad11e333483f2d584d0089a3a2b3eff736623d77cd7053470e8dfb5a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for merge_gateway_python-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a5c7c6c4144d10bae93ca65eff23de54cbf1f94d2ed86343f44faa5bd9c4a6c
MD5 f7918a13fcd6bdeb801fc22376d227b6
BLAKE2b-256 d6d0297fda781845167695221754a576eab10e5bad54b2177439d16711c49a15

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 files

0.3.1

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