High severity beginner · Fix: 2-5 min

ValueError

dspy.errors.ValueError

What this error means
DSPy raises a ValueError when the provided model name does not match the required naming format or pattern.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 12, in <module>
    model = dspy.load_model("invalid model name")  # triggers error
  File "/usr/local/lib/python3.9/site-packages/dspy/__init__.py", line 45, in load_model
    raise ValueError(f"Invalid model name format: {model_name}")
ValueError: Invalid model name format: invalid model name
QUICK FIX
Ensure your model name is a lowercase string with only letters, digits, and hyphens, no spaces or special characters.

Why it happens

DSPy enforces strict naming conventions for model names to ensure compatibility and correct loading. If the model name contains spaces, special characters, or does not follow the expected pattern (e.g., lowercase letters, digits, hyphens), DSPy raises this error to prevent runtime failures.

Detection

Validate model names against DSPy's naming pattern before loading, or catch ValueError exceptions and log the invalid model name for correction.

Causes & fixes

1

Model name contains spaces or uppercase letters

✓ Fix

Use only lowercase letters, digits, and hyphens in the model name without spaces.

2

Model name includes unsupported special characters like underscores or symbols

✓ Fix

Remove unsupported characters and replace them with hyphens if needed to conform to DSPy's naming rules.

3

Model name is empty or None

✓ Fix

Provide a valid non-empty string that matches DSPy's required model name format.

Code: broken vs fixed

Broken - triggers the error
python
import os
import dspy

model_name = "Invalid Model Name!"  # invalid format with spaces and exclamation
model = dspy.load_model(model_name)  # triggers ValueError
Fixed - works correctly
python
import os
import dspy

model_name = "valid-model-name-123"  # fixed: lowercase, digits, hyphens only
model = dspy.load_model(model_name)  # works correctly
print(f"Loaded model: {model_name}")
Corrected the model name to use only lowercase letters, digits, and hyphens, matching DSPy's required format.

Workaround

Wrap the model loading call in try/except ValueError, and sanitize or normalize the model name string by removing invalid characters before retrying.

Prevention

Implement validation functions for model names in your codebase that enforce DSPy's naming rules before calling load_model, preventing invalid names from reaching DSPy.

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

Community Notes

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