|
24 | 24 | utils.get_env_variable("OPENLAYER_VERIFY_SSL") or "true"
|
25 | 25 | ).lower() in TRUE_LIST
|
26 | 26 | _client = None
|
27 |
| -if _publish: |
28 |
| - if _verify_ssl: |
29 |
| - _client = Openlayer() |
30 |
| - else: |
31 |
| - _client = Openlayer( |
32 |
| - http_client=DefaultHttpxClient( |
33 |
| - verify=False, |
34 |
| - ), |
35 |
| - ) |
| 27 | + |
| 28 | +def _get_client() -> Optional[Openlayer]: |
| 29 | + """Get or create the Openlayer client with lazy initialization.""" |
| 30 | + global _client |
| 31 | + if not _publish: |
| 32 | + return None |
| 33 | + |
| 34 | + if _client is None: |
| 35 | + # Lazy initialization - create client when first needed |
| 36 | + if _verify_ssl: |
| 37 | + _client = Openlayer() |
| 38 | + else: |
| 39 | + _client = Openlayer( |
| 40 | + http_client=DefaultHttpxClient( |
| 41 | + verify=False, |
| 42 | + ), |
| 43 | + ) |
| 44 | + return _client |
36 | 45 |
|
37 | 46 | _current_step = contextvars.ContextVar("current_step")
|
38 | 47 | _current_trace = contextvars.ContextVar("current_trace")
|
@@ -122,12 +131,14 @@ def create_step(
|
122 | 131 | )
|
123 | 132 | if _publish:
|
124 | 133 | try:
|
125 |
| - _client.inference_pipelines.data.stream( |
126 |
| - inference_pipeline_id=inference_pipeline_id |
127 |
| - or utils.get_env_variable("OPENLAYER_INFERENCE_PIPELINE_ID"), |
128 |
| - rows=[trace_data], |
129 |
| - config=config, |
130 |
| - ) |
| 134 | + client = _get_client() |
| 135 | + if client: |
| 136 | + client.inference_pipelines.data.stream( |
| 137 | + inference_pipeline_id=inference_pipeline_id |
| 138 | + or utils.get_env_variable("OPENLAYER_INFERENCE_PIPELINE_ID"), |
| 139 | + rows=[trace_data], |
| 140 | + config=config, |
| 141 | + ) |
131 | 142 | except Exception as err: # pylint: disable=broad-except
|
132 | 143 | logger.error("Could not stream data to Openlayer %s", err)
|
133 | 144 | else:
|
@@ -225,12 +236,14 @@ def _handle_trace_completion(
|
225 | 236 | )
|
226 | 237 | if _publish:
|
227 | 238 | try:
|
228 |
| - _client.inference_pipelines.data.stream( |
229 |
| - inference_pipeline_id=inference_pipeline_id |
230 |
| - or utils.get_env_variable("OPENLAYER_INFERENCE_PIPELINE_ID"), |
231 |
| - rows=[trace_data], |
232 |
| - config=config, |
233 |
| - ) |
| 239 | + client = _get_client() |
| 240 | + if client: |
| 241 | + client.inference_pipelines.data.stream( |
| 242 | + inference_pipeline_id=inference_pipeline_id |
| 243 | + or utils.get_env_variable("OPENLAYER_INFERENCE_PIPELINE_ID"), |
| 244 | + rows=[trace_data], |
| 245 | + config=config, |
| 246 | + ) |
234 | 247 | except Exception as err: # pylint: disable=broad-except
|
235 | 248 | logger.error("Could not stream data to Openlayer %s", err)
|
236 | 249 | else:
|
|
0 commit comments