Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Heuristic:Mistralai Client python Resource Context Manager

From Leeroopedia
Knowledge Sources
Domains Reliability, Resource_Management
Last Updated 2026-02-15 14:00 GMT

Overview

Use context managers (`with` / `async with`) for the Mistral client in long-lived programs to ensure proper HTTP connection cleanup.

Description

The Mistral SDK client wraps an `httpx` HTTP client internally. In long-lived applications (servers, background workers), failing to properly close the client can lead to connection pool exhaustion and resource leaks. The SDK supports both synchronous and asynchronous context manager protocols to ensure automatic cleanup of HTTP connections, SSL contexts, and other resources.

Usage

Apply this heuristic in production applications and long-running programs that use the Mistral SDK. Short-lived scripts (one-shot API calls) do not strictly need context managers, but using them is still best practice.

The Insight (Rule of Thumb)

  • Action: Wrap the `Mistral` client in a `with` block (sync) or `async with` block (async).
  • Value: Guarantees HTTP connection pool cleanup even if exceptions occur.
  • Trade-off: Slightly more verbose code; negligible for the reliability benefit.

Reasoning

The httpx library maintains a connection pool internally. Without explicit cleanup:

  • Open TCP connections remain in `CLOSE_WAIT` state
  • File descriptors accumulate, potentially hitting OS limits
  • In async contexts, unclosed connections can trigger `ResourceWarning`

The context manager pattern is the standard Python idiom for resource lifecycle management and is documented in the SDK README.

Code Evidence

From `README.md` (lines 935-959):

# Synchronous context manager
with Mistral(api_key=os.getenv("MISTRAL_API_KEY", "")) as mistral:
    res = mistral.chat.complete(
        model="mistral-small-latest",
        messages=[{"role": "user", "content": "Hello"}],
    )

# Asynchronous context manager
async with Mistral(api_key=api_key) as mistral:
    res = await mistral.chat.complete_async(
        model="mistral-small-latest",
        messages=[{"role": "user", "content": "Hello"}],
    )

MCP client cleanup in `src/mistralai/extra/run/context.py:93-99`:

async def __aenter__(self):
    return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
    await self._exit_stack.aclose()
    for mcp_client in self._mcp_clients:
        await mcp_client.aclose()

EventStream context manager in `src/mistralai/client/utils/eventstreaming.py`:

# EventStream supports with/async with for automatic response cleanup
def __exit__(self, *args):
    self.response.close()

async def __aexit__(self, *args):
    await self.response.aclose()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment