Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

fix(python-sdk): run postflight guards on Mistral async chat calls (#1496)

Aniket Waghe9159da79bc8
diff --git a/sdk/python/src/openlit/guard/_integration.py b/sdk/python/src/openlit/guard/_integration.py
index fd87c4fe..29a4dc50 100644
--- a/sdk/python/src/openlit/guard/_integration.py
+++ b/sdk/python/src/openlit/guard/_integration.py
@@ -24,6 +24,8 @@ completions. Preflight guards always run.
from __future__ import annotations
+import importlib
+import inspect
import logging
from typing import Any, Callable, Dict, List, Optional, Tuple
@@ -353,6 +355,21 @@ def _apply_postflight(
return response
+def _is_async_method(module_path: str, class_method: str) -> bool:
+ """Return True when the provider method has to be awaited.
+
+ The name is not a reliable signal: Mistral spells its async chat method
+ ``Chat.complete_async``, so a ``"Async" in class_method`` check installs the
+ sync wrapper on a coroutine function and the postflight guards never run.
+ Resolve the attribute instead, unwrapping the instrumentor wrapper that is
+ already installed on it by the time guards are set up.
+ """
+ class_name, method_name = class_method.split(".", 1)
+ module = importlib.import_module(module_path)
+ method = getattr(getattr(module, class_name), method_name)
+ return inspect.iscoroutinefunction(inspect.unwrap(method))
+
+
def _make_sync_guard_wrapper(
pipeline: Pipeline,
extract_input: Extractor,
@@ -403,9 +420,8 @@ def setup_auto_guards(
wrapped_count = 0
for module_path, class_method, extract_in, extract_out in GUARDED_METHODS:
- is_async = "Async" in class_method
try:
- if is_async:
+ if _is_async_method(module_path, class_method):
wrapper = _make_async_guard_wrapper(pipeline, extract_in, extract_out)
else:
wrapper = _make_sync_guard_wrapper(pipeline, extract_in, extract_out)
diff --git a/sdk/python/tests/test_guard_integration.py b/sdk/python/tests/test_guard_integration.py
index e0dc2b92..7a260da4 100644
--- a/sdk/python/tests/test_guard_integration.py
+++ b/sdk/python/tests/test_guard_integration.py
@@ -1,12 +1,18 @@
"""Tests for auto-guard integration (no real LLM calls)."""
+import asyncio
+import sys
+import types
+
import pytest
+from openlit.guard import _integration
from openlit.guard._base import GuardDeniedError
from openlit.guard._integration import (
_extract_openai_input,
_extract_anthropic_input,
_extract_generic_input,
+ _extract_generic_output,
_apply_preflight,
_apply_postflight,
)
@@ -182,3 +188,78 @@ class TestPostflightIntegration:
result = _apply_postflight(pipeline, FakeResponse(), _extract_openai_output)
assert result is not None
+
+
+class TestAsyncMethodDetection:
+ """``setup_auto_guards`` picks the wrapper from the method, not its name."""
+
+ @staticmethod
+ def _fake_provider_module():
+ """Build a provider module that names its coroutine method ``*_async``."""
+
+ # pylint: disable=too-few-public-methods,missing-class-docstring
+ class Message:
+ def __init__(self):
+ self.content = "Reach me at [email protected]"
+
+ class Choice:
+ def __init__(self):
+ self.message = Message()
+
+ class Response:
+ def __init__(self):
+ self.choices = [Choice()]
+
+ class Chat:
+ def complete(self, **kwargs):
+ """Mistral-style sync chat method."""
+ return Response()
+
+ async def complete_async(self, **kwargs):
+ """Mistral-style async chat method - no ``Async`` in the name."""
+ return Response()
+
+ module = types.ModuleType("fake_provider_sdk")
+ module.Chat = Chat
+ return module
+
+ def test_detects_coroutine_regardless_of_name(self, monkeypatch):
+ """The check resolves the attribute rather than matching on the name."""
+ module = self._fake_provider_module()
+ monkeypatch.setitem(sys.modules, "fake_provider_sdk", module)
+
+ assert _integration._is_async_method("fake_provider_sdk", "Chat.complete_async")
+ assert not _integration._is_async_method("fake_provider_sdk", "Chat.complete")
+
+ def test_underscore_async_method_runs_postflight(self, monkeypatch):
+ """A ``complete_async`` coroutine is guarded like its sync twin."""
+ module = self._fake_provider_module()
+ monkeypatch.setitem(sys.modules, "fake_provider_sdk", module)
+ monkeypatch.setattr(
+ _integration,
+ "GUARDED_METHODS",
+ [
+ (
+ "fake_provider_sdk",
+ "Chat.complete",
+ _extract_generic_input,
+ _extract_generic_output,
+ ),
+ (
+ "fake_provider_sdk",
+ "Chat.complete_async",
+ _extract_generic_input,
+ _extract_generic_output,
+ ),
+ ],
+ )
+
+ _integration.setup_auto_guards([PII(action="redact")])
+
+ chat = module.Chat()
+ kwargs = {"messages": [{"content": "hello"}]}
+ sync_response = chat.complete(**kwargs)
+ async_response = asyncio.run(chat.complete_async(**kwargs))
+
+ assert "[REDACTED:email]" in sync_response.choices[0].message.content
+ assert "[REDACTED:email]" in async_response.choices[0].message.content