33
44from contextlib import contextmanager
55from dsp .utils .utils import dotdict
6+ from functools import lru_cache
67
78DEFAULT_CONFIG = dotdict (
89 lm = None ,
2728 async_max_workers = 8 ,
2829)
2930
31+ @lru_cache (maxsize = None )
32+ def warn_once (msg : str ):
33+ import logging
34+ logger = logging .getLogger (__name__ )
35+ logger .warning (msg )
36+
3037
3138class Settings :
3239 """DSP configuration settings."""
@@ -59,7 +66,11 @@ def config(self):
5966 thread_id = threading .get_ident ()
6067 # if thread_id not in self.stack_by_thread:
6168 # self.stack_by_thread[thread_id] = [self.main_stack[-1].copy()]
62- return self .stack_by_thread [thread_id ][- 1 ]
69+ try :
70+ return self .stack_by_thread [thread_id ][- 1 ]
71+ except Exception :
72+ warn_once ("Warning: You seem to be creating DSPy threads in an unsupported way." )
73+ return self .main_stack [- 1 ]
6374
6475 def __getattr__ (self , name ):
6576 if hasattr (self .config , name ):
@@ -74,6 +85,8 @@ def __append(self, config):
7485 thread_id = threading .get_ident ()
7586 # if thread_id not in self.stack_by_thread:
7687 # self.stack_by_thread[thread_id] = [self.main_stack[-1].copy()]
88+
89+ assert thread_id in self .stack_by_thread , "Error: You seem to be creating DSPy threads in an unsupported way."
7790 self .stack_by_thread [thread_id ].append (config )
7891
7992 def __pop (self ):
0 commit comments