Set the default language model, adapter, and other settings for DSPy. Call `dspy.configure(...)` once near the top of your script. Every DSPy module will use these defaults unless you override them with `dspy.context(...)`. The values persist until you call `dspy.con
(self, **kwargs)
| 163 | ) |
| 164 | |
| 165 | def configure(self, **kwargs): |
| 166 | """Set the default language model, adapter, and other settings for DSPy. |
| 167 | |
| 168 | Call `dspy.configure(...)` once near the top of your script. Every |
| 169 | DSPy module will use these defaults unless you override them with |
| 170 | `dspy.context(...)`. The values persist until you call |
| 171 | `dspy.configure(...)` again. |
| 172 | |
| 173 | Only the thread that first calls `dspy.configure(...)` may call it |
| 174 | again. Use `dspy.context(...)` for temporary overrides in other |
| 175 | threads or async tasks. |
| 176 | |
| 177 | Args: |
| 178 | **kwargs: Settings to update. Common keys include `lm` (a |
| 179 | `dspy.LM`), `adapter` (e.g. `dspy.JSONAdapter()`), |
| 180 | `callbacks`, `track_usage`, `async_max_workers`, and |
| 181 | `num_threads`. |
| 182 | |
| 183 | Examples: |
| 184 | Set a default LM: |
| 185 | ```python |
| 186 | import dspy |
| 187 | |
| 188 | dspy.configure(lm=dspy.LM("openai/gpt-5-mini")) |
| 189 | ``` |
| 190 | |
| 191 | Set multiple defaults at once: |
| 192 | ```python |
| 193 | import dspy |
| 194 | |
| 195 | dspy.configure( |
| 196 | lm=dspy.LM("anthropic/claude-sonnet-4-6"), |
| 197 | adapter=dspy.JSONAdapter(), |
| 198 | track_usage=True, |
| 199 | ) |
| 200 | ``` |
| 201 | |
| 202 | See Also: |
| 203 | [`dspy.LM`][dspy.LM]: create the language model you pass as `lm`. |
| 204 | `dspy.context`: temporary overrides inside one block. |
| 205 | """ |
| 206 | # `dspy.configure` is documented manually in docs/docs/api/utils/context.md |
| 207 | # changes here should be reflected there as well. |
| 208 | # If no exception is raised, the `configure` call is allowed. |
| 209 | self._ensure_configure_allowed() |
| 210 | |
| 211 | # Update global config |
| 212 | for k, v in kwargs.items(): |
| 213 | main_thread_config[k] = v |
| 214 | |
| 215 | @contextmanager |
| 216 | def context(self, **kwargs): |