DSPyDSPy 3.3 · Python 3.10+
0%
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" pytest
Example
from 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.

python
import dspy

lm = dspy.LM("groq/openai/gpt-oss-120b")
dspy.configure(lm=lm)
ProviderKeyVariabledspy.LM
GroqFree, console.groq.com/keysGROQ_API_KEY"groq/openai/gpt-oss-120b"
GeminiFree, aistudio.google.com/apikeyGEMINI_API_KEY"gemini/gemini-2.5-flash"
OpenRouterFree models end in :free, openrouter.ai/keysOPENROUTER_API_KEY"openrouter/google/gemma-4-31b-it:free"
OpenAIPaid, platform.openai.com/api-keysOPENAI_API_KEY"openai/gpt-4o-mini"
export GROQ_API_KEY=gsk_...
Try it yourself
  • Run pip show dspy and find LiteLLM among the packages it requires.
PreviousDSPy overview

Little by little, you're building something great.