Skip to main content

Type-safe, zero-SDK adapter layer that normalizes any LLM provider into one consistent interface.

Project description

modelgate

A lightweight, type-safe adapter layer that gives you one consistent interface across every major LLM provider. No bloated SDKs — just httpx and pydantic under the hood.

Install

pip install modelgate

# AWS Bedrock support
pip install "modelgate[aws]"

# Vertex AI support
pip install "modelgate[vertex]"

Quick start

import asyncio
from modelgate import ModelGate, ModelGateConfig

async def main():
    client = ModelGate(ModelGateConfig(
        openai_api_key="sk-...",
        anthropic_api_key="sk-ant-...",
    ))

    response = await client.chat(
        model="openai/gpt-4o-mini",
        messages=[{"role": "user", "content": "What is 2+2?"}],
    )
    print(response.text)  # "4"

asyncio.run(main())

Supported providers

Provider Model string prefix Config key
OpenAI openai/ openai_api_key
Anthropic anthropic/ anthropic_api_key
AWS Bedrock bedrock/ aws_region, boto3_session
Gemini gemini/ gemini_api_key
Vertex AI vertex/ vertex_credentials
Groq groq/ groq_api_key
Ollama ollama/ ollama_base_url

Any OpenAI-compatible API works via GenericOpenAIAdapter — no new adapter code needed.

Streaming

async for chunk in client.stream(
    model="anthropic/claude-opus-4-5",
    messages=[{"role": "user", "content": "Tell me a story"}],
):
    if chunk.type == "text":
        print(chunk.text, end="", flush=True)

Tool use

from modelgate import Tool, ToolParameter

weather = Tool(
    name="get_weather",
    description="Get current weather for a location",
    parameters={"location": ToolParameter(type="string", description="City name")},
    required=["location"],
)

response = await client.chat(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Weather in NYC?"}],
    tools=[weather],
)

for tc in response.tool_calls:
    print(tc.tool_name)   # "get_weather"
    print(tc.tool_input)  # {"location": "NYC"}

Tool calls return the same ContentBlock shape regardless of provider.

Error handling

from modelgate import AuthenticationError, RateLimitError, InvalidRequestError

try:
    response = await client.chat(...)
except AuthenticationError:
    pass  # bad or missing API key
except RateLimitError:
    pass  # hit provider rate limit — retry with backoff
except InvalidRequestError:
    pass  # malformed request

Full error hierarchy:

ModelGateError
├── AuthenticationError   # 401
├── RateLimitError        # 429
├── InvalidRequestError   # 400
├── ProviderError         # 5xx
│   ├── BedrockError
│   └── VertexError
└── StreamingError        # error mid-stream

ModelGateConfig reference

Field Type Default Description
openai_api_key str|None None OpenAI API key
anthropic_api_key str|None None Anthropic API key
gemini_api_key str|None None Gemini API key
groq_api_key str|None None Groq API key
aws_region str "us-east-1" AWS region for Bedrock
boto3_session Any|None None Custom boto3 session
vertex_credentials Any|None None Google auth credentials
ollama_base_url str "http://localhost:11434/v1" Ollama server URL
groq_base_url str "https://api.groq.com/openai/v1" Groq API base URL

API keys can also be set via environment variables: OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, GROQ_API_KEY.

chat() / stream() parameters

Parameter Type Default Description
model str required "provider/model-id" format
messages list[dict|Message] required Conversation history
tools list[Tool]|None None Tools available to the model
system str|None None System prompt
max_tokens int 4096 Maximum tokens to generate
temperature float 1.0 Sampling temperature

Contributing

See CONTRIBUTING.md.

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

modelgate-0.1.2.tar.gz (35.7 kB view details)

Uploaded Source

Built Distribution

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

modelgate-0.1.2-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file modelgate-0.1.2.tar.gz.

File metadata

  • Download URL: modelgate-0.1.2.tar.gz
  • Upload date:
  • Size: 35.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for modelgate-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e994d9d1c32cf1bc66faa090454f47bb19fd3c75d7113930f573067e751d7fb3
MD5 35ead26d47f32d0c086bfc72f340eb20
BLAKE2b-256 8373aa9b360f8142271c05ae523615625c908e8dccea49dc2b256db094b2e890

See more details on using hashes here.

Provenance

The following attestation bundles were made for modelgate-0.1.2.tar.gz:

Publisher: publish.yml on PavanPapiReddy22/modelgate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file modelgate-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: modelgate-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 27.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for modelgate-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cbe0b70bc2dbd854d0dfa0f51e349c881170b2268831d4ed7d517323bebee13e
MD5 7e15fb7293c55d737e5f88774c73828d
BLAKE2b-256 5ea9ab0a47a2164c2b6a6986b2a1edda036bb4e42be3e5ebf38253e325c73e1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for modelgate-0.1.2-py3-none-any.whl:

Publisher: publish.yml on PavanPapiReddy22/modelgate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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