Skip to main content
This guide walks you through your first calls with the Mira Network Python SDK.
Before you start: install the SDK and have your API key ready (it starts with sk-mira-). The examples read it from the MIRA_API_KEY environment variable — replace "your-api-key" with your own key or set that variable.

Basic Chat Completion

Send a message and read the model’s reply:
from mira_network import MiraClient

async def main():
    async with MiraClient(api_key="your-api-key") as client:
        response = await client.chat_completions_create(
            model="gpt-4o-mini",  # See list_models() for all available models
            messages=[
                {"role": "user", "content": "What is the capital of France?"}
            ]
        )
        print(response["choices"][0]["message"]["content"])

if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

Streaming Responses

For real-time responses:
async def stream_response():
    async with MiraClient(api_key="your-api-key") as client:
        stream = await client.chat_completions_create(
            model="your-chosen-model",
            messages=[{"role": "user", "content": "Write a story"}],
            stream=True
        )
        async for chunk in stream:
            print(chunk["choices"][0]["delta"]["content"], end="")

Managing Conversations

Handle multi-turn conversations:
messages = [
    {"role": "system", "content": "You are a helpful assistant"},
    {"role": "user", "content": "Hi! Can you help me?"},
]

async with MiraClient(api_key="your-api-key") as client:
    response = await client.chat_completions_create(
        model="your-chosen-model",
        messages=messages
    )

Error Handling

Handle validation and request errors:
try:
    response = await client.chat_completions_create(
        model="your-chosen-model",
        messages=[{"role": "user", "content": "Hello"}]
    )
except ValueError as e:
    print(f"Validation error: {e}")
except Exception as e:
    print(f"An error occurred: {e}")

Next Steps