Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
fix(python-sdk): support mistralai 2.x instrumentation paths (#1539)
Gthejesraj2e5cd2d9264e
diff --git a/sdk/python/src/openlit/guard/_integration.py b/sdk/python/src/openlit/guard/_integration.pyindex 29a4dc50..c2966636 100644--- a/sdk/python/src/openlit/guard/_integration.py+++ b/sdk/python/src/openlit/guard/_integration.py@@ -251,6 +251,18 @@ GUARDED_METHODS: List[Tuple[str, str, Extractor, Extractor]] = [_extract_generic_input,_extract_generic_output,),+ (+ "mistralai.client.chat",+ "Chat.complete",+ _extract_generic_input,+ _extract_generic_output,+ ),+ (+ "mistralai.client.chat",+ "Chat.complete_async",+ _extract_generic_input,+ _extract_generic_output,+ ),# Cohere("cohere.client_v2",diff --git a/sdk/python/src/openlit/instrumentation/mistral/__init__.py b/sdk/python/src/openlit/instrumentation/mistral/__init__.pyindex 6c758325..6277fe6c 100644--- a/sdk/python/src/openlit/instrumentation/mistral/__init__.py+++ b/sdk/python/src/openlit/instrumentation/mistral/__init__.py@@ -1,5 +1,6 @@"""Initializer of Auto Instrumentation of Mistral Functions"""+import loggingfrom typing import Collectionimport importlib.metadatafrom opentelemetry import _logs@@ -17,6 +18,32 @@ from openlit.instrumentation.mistral.async_mistral import (_instruments = ("mistralai >= 1.0.0",)+logger = logging.getLogger(__name__)++# 1.x keeps chat/embeddings at the package root. 2.x moved them under+# mistralai.client. Both layouts expose the same class and method names.+_CHAT_MODULES = ("mistralai.chat", "mistralai.client.chat")+_EMBEDDINGS_MODULES = ("mistralai.embeddings", "mistralai.client.embeddings")+++def _safe_wrap(module, class_method, wrapper):+ """Wrap a function, skipping SDK module layouts that are not installed.++ wrapt 2.x imports the target module immediately, so wrapping the 1.x+ path on mistralai 2.x (and vice versa) raises ``ModuleNotFoundError``.+ wrapt 2.4+ wraps that as ``TargetModuleNotFoundError``, a+ ``ModuleNotFoundError`` subclass, so catching the base exception covers+ both. wrapt 1.x registers a post-import hook instead and does not raise.+ """+ try:+ wrap_function_wrapper(module, class_method, wrapper)+ except (ModuleNotFoundError, AttributeError):+ logger.debug(+ "Skipping %s.%s - not available in this mistralai version",+ module,+ class_method,+ )+class MistralInstrumentor(BaseInstrumentor):"""@@ -37,105 +64,107 @@ class MistralInstrumentor(BaseInstrumentor):event_provider = _logs.get_logger_provider().get_logger(__name__)version = importlib.metadata.version("mistralai")- # sync chat completions- wrap_function_wrapper(- "mistralai.chat",- "Chat.complete",- complete(- version,- environment,- application_name,- tracer,- pricing_info,- capture_message_content,- metrics,- disable_metrics,- event_provider,- ),- )+ for chat_module in _CHAT_MODULES:+ # sync chat completions+ _safe_wrap(+ chat_module,+ "Chat.complete",+ complete(+ version,+ environment,+ application_name,+ tracer,+ pricing_info,+ capture_message_content,+ metrics,+ disable_metrics,+ event_provider,+ ),+ )- # sync chat streaming- wrap_function_wrapper(- "mistralai.chat",- "Chat.stream",- stream(- version,- environment,- application_name,- tracer,- pricing_info,- capture_message_content,- metrics,- disable_metrics,- event_provider,- ),- )+ # sync chat streaming+ _safe_wrap(+ chat_module,+ "Chat.stream",+ stream(+ version,+ environment,+ application_name,+ tracer,+ pricing_info,+ capture_message_content,+ metrics,+ disable_metrics,+ event_provider,+ ),+ )- # sync embeddings- wrap_function_wrapper(- "mistralai.embeddings",- "Embeddings.create",- embed(- version,- environment,- application_name,- tracer,- pricing_info,- capture_message_content,- metrics,- disable_metrics,- ),- )+ # async chat completions+ _safe_wrap(+ chat_module,+ "Chat.complete_async",+ async_complete(+ version,+ environment,+ application_name,+ tracer,+ pricing_info,+ capture_message_content,+ metrics,+ disable_metrics,+ event_provider,+ ),+ )- # async chat completions- wrap_function_wrapper(- "mistralai.chat",- "Chat.complete_async",- async_complete(- version,- environment,- application_name,- tracer,- pricing_info,- capture_message_content,- metrics,- disable_metrics,- event_provider,- ),- )+ # async chat streaming+ _safe_wrap(+ chat_module,+ "Chat.stream_async",+ async_stream(+ version,+ environment,+ application_name,+ tracer,+ pricing_info,+ capture_message_content,+ metrics,+ disable_metrics,+ event_provider,+ ),+ )- # async chat streaming- wrap_function_wrapper(- "mistralai.chat",- "Chat.stream_async",- async_stream(- version,- environment,- application_name,- tracer,- pricing_info,- capture_message_content,- metrics,- disable_metrics,- event_provider,- ),- )+ for embeddings_module in _EMBEDDINGS_MODULES:+ # sync embeddings+ _safe_wrap(+ embeddings_module,+ "Embeddings.create",+ embed(+ version,+ environment,+ application_name,+ tracer,+ pricing_info,+ capture_message_content,+ metrics,+ disable_metrics,+ ),+ )- # async embeddings- wrap_function_wrapper(- "mistralai.embeddings",- "Embeddings.create_async",- async_embed(- version,- environment,- application_name,- tracer,- pricing_info,- capture_message_content,- metrics,- disable_metrics,- ),- )+ # async embeddings+ _safe_wrap(+ embeddings_module,+ "Embeddings.create_async",+ async_embed(+ version,+ environment,+ application_name,+ tracer,+ pricing_info,+ capture_message_content,+ metrics,+ disable_metrics,+ ),+ )def _uninstrument(self, **kwargs):passdiff --git a/sdk/python/tests/test_guard_integration.py b/sdk/python/tests/test_guard_integration.pyindex 7a260da4..876e1c86 100644--- a/sdk/python/tests/test_guard_integration.py+++ b/sdk/python/tests/test_guard_integration.py@@ -9,6 +9,7 @@ import pytestfrom openlit.guard import _integrationfrom openlit.guard._base import GuardDeniedErrorfrom openlit.guard._integration import (+ GUARDED_METHODS,_extract_openai_input,_extract_anthropic_input,_extract_generic_input,@@ -85,6 +86,21 @@ class TestExtractors:assert text == "Generate something"+def test_guarded_methods_include_mistral_v1_and_v2_sdk_layouts():+ """Mistral SDK 1.x and 2.x expose chat under different modules."""++ def has_guarded_method(module_path, class_method):+ return any(+ method[0] == module_path and method[1] == class_method+ for method in GUARDED_METHODS+ )++ assert has_guarded_method("mistralai.chat", "Chat.complete")+ assert has_guarded_method("mistralai.chat", "Chat.complete_async")+ assert has_guarded_method("mistralai.client.chat", "Chat.complete")+ assert has_guarded_method("mistralai.client.chat", "Chat.complete_async")++class TestPreflightIntegration:"""``_apply_preflight`` runs guards on extracted input kwargs."""diff --git a/sdk/python/tests/test_mistral_instrumentation.py b/sdk/python/tests/test_mistral_instrumentation.pynew file mode 100644index 00000000..1bf5c074--- /dev/null+++ b/sdk/python/tests/test_mistral_instrumentation.py@@ -0,0 +1,129 @@+# pylint: disable=protected-access, missing-class-docstring, missing-function-docstring, too-few-public-methods+"""Tests for Mistral instrumentation setup."""++import sys+import types++import openlit.instrumentation.mistral as mistral_instrumentation+from openlit._config import OpenlitConfig+from openlit.instrumentation.mistral import MistralInstrumentor++EXPECTED_TARGETS = {+ ("mistralai.chat", "Chat.complete"),+ ("mistralai.chat", "Chat.stream"),+ ("mistralai.chat", "Chat.complete_async"),+ ("mistralai.chat", "Chat.stream_async"),+ ("mistralai.embeddings", "Embeddings.create"),+ ("mistralai.embeddings", "Embeddings.create_async"),+ ("mistralai.client.chat", "Chat.complete"),+ ("mistralai.client.chat", "Chat.stream"),+ ("mistralai.client.chat", "Chat.complete_async"),+ ("mistralai.client.chat", "Chat.stream_async"),+ ("mistralai.client.embeddings", "Embeddings.create"),+ ("mistralai.client.embeddings", "Embeddings.create_async"),+}+++def _instrument(**kwargs):+ OpenlitConfig.reset_to_defaults()+ MistralInstrumentor()._instrument(+ environment="test",+ application_name="test",+ pricing_info={},+ disable_metrics=True,+ **kwargs,+ )+++def test_mistral_instrumentor_registers_v1_and_v2_sdk_layouts(monkeypatch):+ """Mistral SDK 1.x and 2.x expose chat and embeddings under different modules."""+ wrapped_targets = []++ monkeypatch.setattr(+ mistral_instrumentation.importlib.metadata,+ "version",+ lambda package_name: "2.9.4",+ )+ monkeypatch.setattr(+ mistral_instrumentation,+ "wrap_function_wrapper",+ lambda module, class_method, wrapper: wrapped_targets.append(+ (module, class_method)+ ),+ )++ _instrument()++ assert set(wrapped_targets) == EXPECTED_TARGETS+++def test_mistral_instrumentor_skips_missing_layout_and_wraps_available(monkeypatch):+ """A missing 1.x module must not abort 2.x wrapping (wrapt 2.x raises)."""+ wrapped_targets = []+ v1_modules = {"mistralai.chat", "mistralai.embeddings"}++ def fake_wrap(module, class_method, wrapper):+ if module in v1_modules:+ raise ModuleNotFoundError(module)+ wrapped_targets.append((module, class_method))++ monkeypatch.setattr(+ mistral_instrumentation.importlib.metadata,+ "version",+ lambda package_name: "2.9.4",+ )+ monkeypatch.setattr(+ mistral_instrumentation, "wrap_function_wrapper", fake_wrap+ )++ _instrument()++ assert set(wrapped_targets) == {+ target for target in EXPECTED_TARGETS if target[0] not in v1_modules+ }+++def test_mistral_instrumentor_binds_v2_chat_complete(monkeypatch):+ """Wrapping mistralai.client.chat must intercept Chat.complete."""+ package = types.ModuleType("mistralai")+ package.__path__ = []+ client = types.ModuleType("mistralai.client")+ client.__path__ = []+ chat = types.ModuleType("mistralai.client.chat")++ class Chat:+ def complete(self, **kwargs):+ return kwargs.get("model", "ok")++ chat.Chat = Chat+ client.chat = chat+ package.client = client++ added = {+ "mistralai": package,+ "mistralai.client": client,+ "mistralai.client.chat": chat,+ }+ original = {name: sys.modules.get(name) for name in added}+ sys.modules.update(added)++ monkeypatch.setattr(+ mistral_instrumentation.importlib.metadata,+ "version",+ lambda package_name: "2.9.4",+ )+ monkeypatch.setattr(+ mistral_instrumentation, "_CHAT_MODULES", ("mistralai.client.chat",)+ )+ monkeypatch.setattr(mistral_instrumentation, "_EMBEDDINGS_MODULES", ())++ try:+ _instrument()+ assert Chat().complete(model="mistral-small-latest") == "mistral-small-latest"+ assert hasattr(Chat.complete, "__wrapped__")+ finally:+ for name, previous in original.items():+ if previous is None:+ sys.modules.pop(name, None)+ else:+ sys.modules[name] = previous