Skip to content

Commit 9801e91

Browse files
feat(tracer): enhance conditional imports and type hinting for Mistral integration
- Implemented conditional import handling for the `mistralai` library, allowing for graceful degradation when the library is not installed. - Improved type hints using forward references for `mistralai` types to enhance code clarity and maintainability. - Introduced an informative error message when the `mistralai` library is missing, guiding users on how to install it. - This update ensures better compatibility and user experience when working with optional dependencies in the Mistral integration.
1 parent eda36c0 commit 9801e91

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

src/openlayer/lib/integrations/mistral_tracer.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
import logging
55
import time
66
from functools import wraps
7-
from typing import Any, Dict, Iterator, Optional, Union
7+
from typing import Any, Dict, Iterator, Optional, Union, TYPE_CHECKING
88

9-
import mistralai
9+
try:
10+
import mistralai
11+
HAVE_MISTRAL = True
12+
except ImportError:
13+
HAVE_MISTRAL = False
14+
15+
if TYPE_CHECKING:
16+
import mistralai
1017

1118
from ..tracing import tracer
1219

1320
logger = logging.getLogger(__name__)
1421

1522

1623
def trace_mistral(
17-
client: mistralai.Mistral,
18-
) -> mistralai.Mistral:
24+
client: "mistralai.Mistral",
25+
) -> "mistralai.Mistral":
1926
"""Patch the Mistral client to trace chat completions.
2027
2128
The following information is collected for each chat completion:
@@ -42,6 +49,11 @@ def trace_mistral(
4249
mistralai.Mistral
4350
The patched Mistral client.
4451
"""
52+
if not HAVE_MISTRAL:
53+
raise ImportError(
54+
"Mistral library is not installed. Please install it with: pip install mistralai"
55+
)
56+
4557
stream_func = client.chat.stream
4658
create_func = client.chat.complete
4759

@@ -184,7 +196,7 @@ def handle_non_streaming_create(
184196
*args,
185197
inference_id: Optional[str] = None,
186198
**kwargs,
187-
) -> mistralai.models.ChatCompletionResponse:
199+
) -> "mistralai.models.ChatCompletionResponse":
188200
"""Handles the create method when streaming is disabled.
189201
190202
Parameters
@@ -231,7 +243,7 @@ def handle_non_streaming_create(
231243

232244

233245
def parse_non_streaming_output_data(
234-
response: mistralai.models.ChatCompletionResponse,
246+
response: "mistralai.models.ChatCompletionResponse",
235247
) -> Union[str, Dict[str, Any], None]:
236248
"""Parses the output data from a non-streaming completion.
237249

0 commit comments

Comments
 (0)