1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
24 small wins to finish your pathNext lesson →
Installation and setup
DSPy installs with pip and reaches hosted models through LiteLLM. The course runs on a stand-in model, and a free Groq or Gemini key is enough when you want a real one.
pip install dspy 3.3.1
DSPy's optimizers change between releases, so the version is pinned to match the outputs in the lessons. pytest is for lesson 22.
pip install "dspy==3.3.1" pytestfrom importlib.metadata import version
print(version("dspy"))Choosing a hosted modelOptional
Until lesson 6 every program runs on ShopLM, the keyword-rule model you write in lesson 4, so you can skip this section for now. dspy.LM takes a provider prefix and a model name, and LiteLLM reads the matching key from the environment.
import dspy
lm = dspy.LM("groq/openai/gpt-oss-120b")
dspy.configure(lm=lm)| Provider | Key | Variable | dspy.LM |
|---|---|---|---|
| Groq | Free, console.groq.com/keys | GROQ_API_KEY | "groq/openai/gpt-oss-120b" |
| Gemini | Free, aistudio.google.com/apikey | GEMINI_API_KEY | "gemini/gemini-2.5-flash" |
| OpenRouter | Free models end in :free, openrouter.ai/keys | OPENROUTER_API_KEY | "openrouter/google/gemma-4-31b-it:free" |
| OpenAI | Paid, platform.openai.com/api-keys | OPENAI_API_KEY | "openai/gpt-4o-mini" |
export GROQ_API_KEY=gsk_...Try it yourself
- Run
pip show dspyand find LiteLLM among the packages it requires.
Little by little, you're building something great.