DSPy is a framework for algorithmically optimizing Language Model (LM) prompts and weights, especially within complex workflows. It enables developers to express multi-step reasoning as modular, self-improving programs, abstracting away the specifics of prompting and fine-tuning. The current version is 3.1.3, with a very active development schedule and frequent updates, often involving significant API changes between major versions.
pip install dspy-aiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure an LLM, define a simple `Signature` for a task, and use `dspy.Predict` to execute it. Make sure to set your `OPENAI_API_KEY` environment variable before running. The `dspy.configure` function sets the default language model for all subsequent DSPy operations.
Migrate `dspy.settings.<property> = value` calls to `dspy.configure(lm=...)`, `dspy.configure(rm=...)`, etc. Pass LM-specific parameters directly to the LM class constructor (e.g., `OpenAI(model='gpt-4', max_tokens=100)`).
Always use `pip install dspy-ai` to ensure you install the correct DSPy framework.
Always call `dspy.configure(lm=..., rm=...)` at the beginning of your script to set up your LLM and retrieval backend. Ensure API keys are correctly provided, typically via environment variables, to the LM constructors.
Provide a list of input-output example dictionaries (`trainset`) to the `compile` method of your teleprompter. For example: `teleprompter.compile(student=..., teacher=..., trainset=my_examples_list)`.
For DSPy versions below 3.0.0, this warning can typically be ignored as `openai` will still be installed. To explicitly install `openai` without the warning, use `pip install dspy-ai openai`. For DSPy versions 3.0.0 and above, `pip install dspy-ai[openai]` works as intended without warnings.
Install `dspy-ai` without the `[openai]` extra, e.g., `pip install dspy-ai`. OpenAI will be installed automatically as a dependency. If a specific version of OpenAI is needed, install it separately after `dspy-ai` (e.g., `pip install dspy-ai openai==X.Y.Z`).
Update your import statements to `from dspy.optimizers import ...` for teleprompters and optimizers. For example, `from dspy.optimizers import BootstrapFewShot`.
Replace calls to `dspy.settings.configure(...)` or `dspy.settings.get(...)` with `dspy.configure(...)` for global settings and use `dspy.context(...)` for temporary overrides.
Rename the conflicting field in your `dspy.Signature` to avoid using DSPy's reserved keywords. For example, use `user_instructions` instead of `instructions`.
Before running any DSPy modules, initialize a `dspy.LM` instance and configure it using `dspy.configure(lm=my_lm_instance)`.
Refer to the latest DSPy documentation for the correct way to import and initialize specific language model clients, or use the generic `dspy.OpenAI` class with appropriate parameters for Azure.