RuntimeError
dspy.errors.RuntimeError: DSPy LM not configured, call dspy.configure() first
Stack trace
Traceback (most recent call last):
File "app.py", line 10, in <module>
response = dspy.LM().generate("Hello")
File "/usr/local/lib/python3.9/site-packages/dspy/lm.py", line 45, in generate
raise RuntimeError("DSPy LM not configured, call dspy.configure() first")
RuntimeError: DSPy LM not configured, call dspy.configure() first Why it happens
DSPy requires explicit configuration with API keys and environment settings before any LM calls. If dspy.configure() is not called, the LM client lacks credentials and context, causing this runtime error.
Detection
Check for RuntimeError exceptions with the message 'DSPy LM not configured' during LM calls; add logging around dspy.configure() to ensure it runs before usage.
Causes & fixes
dspy.configure() was never called before using the LM client
Call dspy.configure() early in your application startup with the required API key and environment parameters.
dspy.configure() was called but with missing or empty API key
Ensure the API key passed to dspy.configure() is a valid non-empty string, typically loaded from environment variables.
Multiple processes or threads use dspy LM but configuration is only done in one context
Call dspy.configure() in every process or thread before using the LM client to ensure proper initialization.
Code: broken vs fixed
import dspy
# Missing configuration call
lm = dspy.LM()
response = lm.generate("Hello") # This line raises RuntimeError
print(response) import os
import dspy
# Fixed: configure DSPy with API key before use
api_key = os.environ.get('DSPY_API_KEY')
dspy.configure(api_key=api_key)
lm = dspy.LM()
response = lm.generate("Hello")
print(response) # Works correctly now Workaround
Wrap LM calls in try/except RuntimeError, catch the configuration error, then call dspy.configure() dynamically before retrying the LM call.
Prevention
Always call dspy.configure() once at application startup with valid credentials and ensure environment variables are set correctly in all runtime contexts.