dspy.LM¶
dspy.LM(model: str, model_type: Literal['chat', 'text', 'responses'] = 'chat', temperature: float | None = None, max_tokens: int | None = None, cache: bool = True, callbacks: list[BaseCallback] | None = None, num_retries: int = 3, provider: Provider | None = None, finetuning_model: str | None = None, launch_kwargs: dict[str, Any] | None = None, train_kwargs: dict[str, Any] | None = None, use_developer_role: bool = False, engine: Any = 'auto', async_engine: Any = None, prompt_cache: CacheConfig | None = None, **kwargs)
¶
Bases: BaseLM
A language model supporting chat or text completion requests for use with DSPy modules.
Use lm(“hello”) for a list-returning convenience call, or pass an explicit dspy.lm15.Request to receive a dspy.lm15.Response. OpenAI-style messages= dictionaries are deprecated and scheduled for removal in DSPy 3.5. Adapters and custom engines must migrate to the canonical request/response contract. See https://dspy.ai/community/normalized-lm-api-migration/.
Create a new language model instance for use with DSPy modules and programs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
The model to use. This should be a string of the form
|
required |
model_type
|
Literal['chat', 'text', 'responses']
|
The type of the model, such as |
'chat'
|
temperature
|
float | None
|
The sampling temperature to use when generating responses. |
None
|
max_tokens
|
int | None
|
The maximum number of tokens to generate per response. |
None
|
cache
|
bool
|
Whether to cache the model responses for reuse to improve performance and reduce costs. |
True
|
callbacks
|
list[BaseCallback] | None
|
A list of callback functions to run before and after each request. |
None
|
num_retries
|
int
|
The number of times to retry a request if it fails transiently due to network error, rate limiting, etc. Requests are retried with exponential backoff. |
3
|
engine
|
Any
|
‘auto’ prefers lm15 for representable requests; ‘litellm’ preserves the compatibility backend; ‘lm15’ refuses unsupported mappings rather than selecting LiteLLM. A custom engine implements complete(Request) -> Response and optionally stream(Request). Engines are borrowed and own their connection: api_key, api_base, timeout and the other client settings are refused with a custom engine, at construction and on every call. |
'auto'
|
async_engine
|
Any
|
Async counterpart when supplying a custom engine object. The pair is one unit: copy(engine=…) replaces both unless async_engine= is given too. |
None
|
prompt_cache
|
CacheConfig | None
|
Optional lm15 CacheConfig for provider-side prompt caching on ordinary calls. Separate from DSPy’s response cache. Requires native lm15 or a canonical custom engine; may incur cache-write/storage charges. A call-time value overrides this default, and None removes it. Explicit Request calls use only Request.config.cache. No cache resource is created. |
None
|
provider
|
Provider | None
|
The training/launch provider. This does not select the inference engine. |
None
|
finetuning_model
|
str | None
|
The model to finetune. In some providers, the models available for finetuning is different from the models available for inference. |
None
|
rollout_id
|
Optional integer used to differentiate cache entries for otherwise
identical requests. Different values bypass DSPy’s caches while still caching
future calls with the same inputs and rollout ID. Note that |
required |
Source code in dspy/clients/lm.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
Methods:¶
__call__(prompt=None, *, messages=None, **kwargs)
¶
Return legacy outputs, or one lm15.Response for an explicit Request.
Source code in dspy/clients/base_lm.py
acall(prompt=None, *, messages=None, **kwargs)
async
¶
Async equivalent of call, with the same execution ownership.
Source code in dspy/clients/base_lm.py
aforward(prompt=None, messages=None, **kwargs)
async
¶
Source code in dspy/clients/lm.py
copy(**kwargs)
¶
Source code in dspy/clients/lm.py
dump_state()
¶
Return a sanitized reconstruction state for this LM.
A custom engine is recorded as its class path and its own
dump_state(); the class must be importable by that path in the
process that loads the state. A class defined in __main__ (a
script or notebook) loads only where __main__ defines it again;
for durable state define the engine in an importable module.
Returns:
| Type | Description |
|---|---|
|
A dictionary that can be passed to |
|
|
reconstruct this |
Source code in dspy/clients/lm.py
finetune(train_data: list[dict[str, Any]], train_data_format: TrainDataFormat | None, train_kwargs: dict[str, Any] | None = None) -> TrainingJob
¶
Source code in dspy/clients/lm.py
forward(prompt=None, messages=None, **kwargs)
¶
Compatibility forward entry point; public calls also record history.