High severity beginner · Fix: 2-5 min

RuntimeError

dspy.errors.RuntimeError: DSPy LM not configured, call dspy.configure() first

What this error means
This error occurs when the DSPy language model is used before calling dspy.configure() to set up API keys and environment.

Stack trace

traceback
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
QUICK FIX
Add a call to dspy.configure(api_key=os.environ['DSPY_API_KEY']) before any LM usage to initialize the client.

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

1

dspy.configure() was never called before using the LM client

✓ Fix

Call dspy.configure() early in your application startup with the required API key and environment parameters.

2

dspy.configure() was called but with missing or empty API key

✓ Fix

Ensure the API key passed to dspy.configure() is a valid non-empty string, typically loaded from environment variables.

3

Multiple processes or threads use dspy LM but configuration is only done in one context

✓ Fix

Call dspy.configure() in every process or thread before using the LM client to ensure proper initialization.

Code: broken vs fixed

Broken - triggers the error
python
import dspy

# Missing configuration call
lm = dspy.LM()
response = lm.generate("Hello")  # This line raises RuntimeError
print(response)
Fixed - works correctly
python
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
Added dspy.configure() with the API key from environment variables before creating the LM instance to properly initialize the DSPy client.

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.

Python 3.9+ · dspy >=1.0.0 · tested on 1.2.0
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.