Skip to content

Commit 7639007

Browse files
feat(tracer): enhance conditional imports and type hinting for Groq integration
- Implemented conditional import handling for the `groq` library, allowing for graceful degradation when the library is not installed. - Improved type hints using forward references for `groq` types to enhance code clarity and maintainability. - Introduced an informative error message when the `groq` 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 Groq integration.
1 parent 9801e91 commit 7639007

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

src/openlayer/lib/integrations/groq_tracer.py

Lines changed: 16 additions & 4 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 groq
9+
try:
10+
import groq
11+
HAVE_GROQ = True
12+
except ImportError:
13+
HAVE_GROQ = False
14+
15+
if TYPE_CHECKING:
16+
import groq
1017

1118
from ..tracing import tracer
1219

1320
logger = logging.getLogger(__name__)
1421

1522

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

4759
@wraps(create_func)

0 commit comments

Comments
 (0)