Python SDK

View as Markdown

Python SDK

The MeshAPI Python SDK provides a native, typed client for integrating the AI model gateway into your Python 3 applications.

Requirements

  • Python ≥ 3.9
  • httpx ≥ 0.27
  • pydantic ≥ 2.0

Installation

$pip install meshapi-python-sdk

Quick Start

You can interact with MeshAPI using either a synchronous or asynchronous client. We strongly suggest using one client instance per authentication realm (Data-Plane vs Control-Plane).

Synchronous Client

1from meshapi import MeshAPI, ChatCompletionParams, ChatMessage
2
3# Initialize the client with your data-plane API key
4client = MeshAPI(base_url="https://api.meshapi.ai", token="rsk_...")
5
6# Non-streaming request
7resp = client.chat.completions.create(
8 ChatCompletionParams(
9 model="openai/gpt-4o-mini",
10 messages=[ChatMessage(role="user", content="What is 2+2?")],
11 )
12)
13print(resp.choices[0].message.content)
14
15# Streaming request
16for chunk in client.chat.completions.stream(
17 ChatCompletionParams(
18 model="openai/gpt-4o-mini",
19 messages=[ChatMessage(role="user", content="Count to 5.")],
20 )
21):
22 if chunk.choices and chunk.choices[0].delta:
23 print(chunk.choices[0].delta.content, end="", flush=True)

Asynchronous Client

1import asyncio
2from meshapi import AsyncMeshAPI, ChatCompletionParams, ChatMessage
3
4async def main():
5 async with AsyncMeshAPI(base_url="https://api.meshapi.ai", token="rsk_...") as client:
6 resp = await client.chat.completions.create(
7 ChatCompletionParams(
8 model="openai/gpt-4o-mini",
9 messages=[ChatMessage(role="user", content="Hello!")],
10 )
11 )
12 print(resp.choices[0].message.content)
13
14asyncio.run(main())

Error Handling & Retries

The client automatically retries GET and non-streaming POST/PATCH requests on status codes 429, 502, 503, and 504 with exponential backoff.

If an error occurs, a MeshAPIError is raised:

1from meshapi import MeshAPIError
2
3try:
4 resp = client.chat.completions.create(...)
5except MeshAPIError as e:
6 print(e.status) # HTTP status code
7 print(e.error_code) # "rate_limit_exceeded", "unauthorized", etc.
8 print(e.request_id) # Traceable request ID
9 print(e.retry_after_seconds) # Present on 429 errors

[!WARNING] Streams do not automatically retry! If a connection drops mid-stream, the SDK raises a MeshAPIError with error_code="stream_interrupted". You must catch this and restart the request manually if required.