ValueError
dspy.errors.ValueError
Stack trace
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 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
Model name contains spaces or uppercase letters
Use only lowercase letters, digits, and hyphens in the model name without spaces.
Model name includes unsupported special characters like underscores or symbols
Remove unsupported characters and replace them with hyphens if needed to conform to DSPy's naming rules.
Model name is empty or None
Provide a valid non-empty string that matches DSPy's required model name format.
Code: broken vs fixed
import os
import dspy
model_name = "Invalid Model Name!" # invalid format with spaces and exclamation
model = dspy.load_model(model_name) # triggers ValueError 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}") 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.