dspy.BaseLM¶
dspy.BaseLM(model, model_type='chat', temperature=None, max_tokens=None, cache=True, callbacks: list[BaseCallback] | None = None, num_retries: int = 3, **kwargs)
¶
Base class for DSPy language models.
Legacy subclasses implement forward(prompt=None, messages=None, **kwargs) and optionally aforward with the same arguments. Ordinary calls return lists of strings or dictionaries. The built-in LM also accepts explicit dspy.lm15.Request calls. The experimental setting does not change outputs.
Implementing custom LMs through forward()/aforward() is deprecated. The old subclass interface remains supported throughout DSPy 3.4 and is scheduled for removal in 3.5. Implement an engine with complete(Request) -> Response and pass it to dspy.LM(engine=…) instead. LegacyEngine and AsyncLegacyEngine are transition wrappers for 3.4 only and are also scheduled for removal in 3.5. OpenAI-style messages= dictionaries are deprecated too: use lm15.Request and Message objects instead. lm(“hello”) remains a list-returning convenience. See the migration guide.
Persistent custom state belongs in dump_state/load_state. Runtime clients are shared by copy(), while DSPy history, callbacks and kwargs are isolated.
Initialize a base language model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
The model identifier. |
required | |
model_type
|
The LM API type, such as |
'chat'
|
|
temperature
|
The default sampling temperature. |
None
|
|
max_tokens
|
The default maximum number of output tokens. |
None
|
|
cache
|
Whether requests should use DSPy’s cache by default. |
True
|
|
num_retries
|
int
|
The default number of provider request retries. |
3
|
callbacks
|
list[BaseCallback] | None
|
Optional instance-level callback handlers. |
None
|
**kwargs
|
Additional default request parameters stored in
|
{}
|
Source code in dspy/clients/base_lm.py
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
¶
Asynchronously return an OpenAI-shaped provider response.
copy(**kwargs)
¶
Return a copy of the language model with updated parameters.
The default implementation makes a shallow runtime copy. Provider
clients, sessions, and local model handles are preserved by reference.
DSPy-owned mutable state is isolated for history, the callbacks
list, and the kwargs dict. Other attributes are shared by reference.
Subclasses with additional mutable DSPy-owned state should override this
method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Attribute or request-parameter updates to apply to the
copy. For example, |
{}
|
Returns:
| Type | Description |
|---|---|
|
A copied LM instance. |
Source code in dspy/clients/base_lm.py
dump_state() -> dict[str, Any]
¶
Return a sanitized reconstruction state for this LM.
Subclasses whose state is captured by BaseLM.__init__ can use this
default. Subclasses with extra persistent state should override both
dump_state and load_state.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary that can be passed to |
dict[str, Any]
|
excludes API keys. |
Source code in dspy/clients/base_lm.py
forward(prompt=None, messages=None, **kwargs)
¶
Return an OpenAI-shaped provider response for a legacy LM call.
inspect_history(n: int = 1, file: TextIO | None = None) -> None
¶
load_state(state: dict[str, Any], *, allow_custom_lm_class: bool = False) -> BaseLM
classmethod
¶
Reconstruct an LM from dump_state output.
Legacy states without a class marker load as dspy.LM. Custom LM
classes must be importable by their module-qualified class path and are
only loaded when allow_custom_lm_class=True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict[str, Any]
|
Serialized LM state produced by |
required |
allow_custom_lm_class
|
bool
|
If True, allow importing and loading custom
|
False
|
Returns:
| Type | Description |
|---|---|
BaseLM
|
The reconstructed LM instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ImportError
|
If the serialized LM class cannot be imported. |
TypeError
|
If the serialized class is not a |