Skip to main content

Mantis SDK

Project description

Mantis SDK

Python SDK for sending chat completion requests to the Mantis llm-gateway /v1/chat/completions endpoint.

Requirements

  • Python 3.13+
  • A running llm-gateway service
  • A gateway API token

Install

From PyPI:

pip install mantis_gw

From this repo:

uv sync

Non-streaming

url should be the gateway base URL and should not include /v1/chat/completions. The SDK appends that endpoint path automatically.

import asyncio

from mantis_gw import gateway


async def main() -> None:
    client = gateway.Gateway(
        url="https://gateway.example.com",
        token="gw_token-id_token-secret",
    )

    response = await client.send(
        {
            "messages": [
                {"role": "user", "content": "Write a short project summary."},
            ],
            "stream": False,
            "temperature": 0.5,
            "max_tokens": 256,
            "system": "Answer clearly and concisely.",
        },
        metadata={"task-type": "summarization"},
    )

    print(response)


asyncio.run(main())

Streaming

When stream is True, send() returns an async iterator of text chunks.

import asyncio

from mantis_gw import gateway


async def main() -> None:
    client = gateway.Gateway(
        url="https://gateway.example.com",
        token="gw_token-id_token-secret"
    )

    chunks = await client.send(
        {
            "messages": [
                {"role": "user", "content": "Write a short project summary."},
            ],
            "stream": True,
            "temperature": 0.5,
            "max_tokens": 256,
            "system": "Answer clearly and concisely.",
        },
        metadata={"task-type": "summarization"},
    )

    async for chunk in chunks:
        print(chunk, end="")


asyncio.run(main())

Request Payload

The SDK sends the request payload to llm-gateway as-is. Gateway-side validation still applies to the endpoint:

  • messages is required and must contain at least one message.
  • Each message must only contain role and content.
  • role must be "user" or "assistant".
  • content must be a non-empty string after trimming whitespace.
  • Extra top-level request fields are rejected.
  • stream defaults to False.
  • temperature can be omitted, None, or a number from 0.0 to 2.0.
  • max_tokens can be omitted, None, or an integer greater than 0.
  • system can be omitted, None, or a non-empty string after trimming whitespace.

Metadata

Pass routing metadata with the metadata keyword argument. Keys and values must be strings. The SDK sends it as the gateway's metadata HTTP header.

await client.send(
    {
        "messages": [{"role": "user", "content": "Generate a Python function."}],
        "stream": False,
    },
    metadata={"task-type": "code_generation"},
)

Errors

Gateway responses with 4xx or 5xx status codes raise httpx.HTTPStatusError.

Integration Tests

The integration tests read gateway credentials from a local .env file in the repo root:

MANTIS_GATEWAY_URL=https://gateway.example.com
MANTIS_GATEWAY_TOKEN=gw_token-id_token-secret

Run them with:

uv run pytest

The .env file is ignored by Git so real credentials stay local.

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

mantis_gw-1.0.1.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

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

mantis_gw-1.0.1-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file mantis_gw-1.0.1.tar.gz.

File metadata

  • Download URL: mantis_gw-1.0.1.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mantis_gw-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c742094573f19a6ca0c3cb430d05a820ce3017e4b1d36ad58a395e4fe527460f
MD5 d9eed9cb27adf81e965d06c26951afb7
BLAKE2b-256 0a83ef451c2e489e7c3f4882f0781e0fad1aae1f00c442167261d37d2e0757ba

See more details on using hashes here.

File details

Details for the file mantis_gw-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: mantis_gw-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for mantis_gw-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6a88f937f8be83f732e4e1bc31061809d635690a640ea033fde062b5fbbf7497
MD5 ebb9e585e719022b3c425e0bd84b0dda
BLAKE2b-256 94aba09bed886b4c883114129ef1d9936225dbf5a13d5d0309561dfc44b6b1fe

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