Skip to content

dspy/configure-in-forward

Pack Severity Fix Versions
DSPy error none, edit by hand All versions

dspy.configure(...) mutates process-wide settings and raises a thread-ownership error when called from worker threads during dspy.Evaluate, dspy.Parallel, or optimizer runs.

def forward(self, question: str) -> dspy.Prediction:
dspy.configure(adapter=dspy.JSONAdapter())
return self.predict(question=question)
with dspy.context(adapter=dspy.JSONAdapter()):
return self.predict(question=question)

Calls to dspy.configure(...) or imported configure(...) inside a forward function or method.

Replace global dspy.configure(...) inside forward with a thread-local with dspy.context(...): block, and leave model binding to the caller or runner.

dspy.configure(...) mutates global settings and fails across threads inside forward(). Use 'with dspy.context(...):' instead.

See all rules in the DSPy pack.