@@ -264,6 +264,8 @@ def __init__(
264264 self .project_id = project_id
265265 self .base_url = base_url .rstrip ("/" )
266266 self .timeout = timeout
267+ # Reuse a single session for connection pooling and to avoid leaking FDs.
268+ self ._session = requests .Session ()
267269
268270 def search_logs (self , tags : List [str ], limit : int = 100 , hours_back : int = 24 ) -> List [Dict [str , Any ]]:
269271 """Fetch logs from Fireworks tracing gateway /logs endpoint.
@@ -287,14 +289,14 @@ def search_logs(self, tags: List[str], limit: int = 100, hours_back: int = 24) -
287289 last_error : Optional [str ] = None
288290 for url in urls_to_try :
289291 try :
290- response = requests . get (url , params = params , timeout = self .timeout , headers = headers )
291- if response .status_code == 404 :
292- # Try next variant
293- last_error = f"404 for { url } "
294- continue
295- response .raise_for_status ()
296- data = response .json () or {}
297- break
292+ with self . _session . get (url , params = params , timeout = self .timeout , headers = headers ) as response :
293+ if response .status_code == 404 :
294+ # Try next variant (must close response to release connection)
295+ last_error = f"404 for { url } "
296+ continue
297+ response .raise_for_status ()
298+ data = response .json () or {}
299+ break
298300 except requests .exceptions .RequestException as e :
299301 last_error = str (e )
300302 continue
@@ -412,9 +414,9 @@ def get_evaluation_rows(
412414
413415 result = None
414416 try :
415- response = requests . get (url , params = params , timeout = self .timeout , headers = headers )
416- response .raise_for_status ()
417- result = response .json ()
417+ with self . _session . get (url , params = params , timeout = self .timeout , headers = headers ) as response :
418+ response .raise_for_status ()
419+ result = response .json ()
418420 except requests .exceptions .HTTPError as e :
419421 error_msg = str (e )
420422
@@ -451,3 +453,10 @@ def get_evaluation_rows(
451453
452454 logger .info ("Successfully converted %d traces to evaluation rows" , len (eval_rows ))
453455 return eval_rows
456+
457+ def close (self ) -> None :
458+ """Close underlying HTTP resources."""
459+ try :
460+ self ._session .close ()
461+ except Exception :
462+ pass
0 commit comments