Skip to content

Commit 68984e2

Browse files
[Docstring Fix] Add docstring for Adapter (#8915)
* add docstring for Adapter * nit
1 parent 15cbbd4 commit 68984e2

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

dspy/adapters/base.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,39 @@
2020

2121

2222
class Adapter:
23+
"""Base Adapter class.
24+
25+
The Adapter serves as the interface layer between DSPy module/signature and Language Models (LMs). It handles the
26+
complete transformation pipeline from DSPy inputs to LM calls and back to structured outputs.
27+
28+
Key responsibilities:
29+
- Transform user inputs and signatures into properly formatted LM prompts, which also instructs the LM to format
30+
the response in a specific format.
31+
- Parse LM outputs into dictionaries matching the signature's output fields.
32+
- Enable/disable native LM features (function calling, citations, etc.) based on configuration.
33+
- Handle conversation history, few-shot examples, and custom type processing.
34+
35+
The adapter pattern allows DSPy to work with different LM interfaces while maintaining a consistent programming
36+
model for users.
37+
"""
38+
2339
def __init__(
2440
self,
2541
callbacks: list[BaseCallback] | None = None,
2642
use_native_function_calling: bool = False,
2743
native_response_types: list[type[Type]] | None = None,
2844
):
45+
"""
46+
Args:
47+
callbacks: List of callback functions to execute during `format()` and `parse()` methods. Callbacks can be
48+
used for logging, monitoring, or custom processing. Defaults to None (empty list).
49+
use_native_function_calling: Whether to enable native function calling capabilities when the LM supports it.
50+
If True, the adapter will automatically configure function calling when input fields contain `dspy.Tool`
51+
or `list[dspy.Tool]` types. Defaults to False.
52+
native_response_types: List of output field types that should be handled by native LM features rather than
53+
adapter parsing. For example, `dspy.Citations` can be populated directly by citation APIs
54+
(e.g., Anthropic's citation feature). Defaults to `[Citations]`.
55+
"""
2956
self.callbacks = callbacks or []
3057
self.use_native_function_calling = use_native_function_calling
3158
self.native_response_types = native_response_types or _DEFAULT_NATIVE_RESPONSE_TYPES
@@ -149,6 +176,22 @@ def __call__(
149176
demos: list[dict[str, Any]],
150177
inputs: dict[str, Any],
151178
) -> list[dict[str, Any]]:
179+
"""
180+
Execute the adapter pipeline: format inputs, call LM, and parse outputs.
181+
182+
Args:
183+
lm: The Language Model instance to use for generation. Must be an instance of `dspy.BaseLM`.
184+
lm_kwargs: Additional keyword arguments to pass to the LM call (e.g., temperature, max_tokens). These are
185+
passed directly to the LM.
186+
signature: The DSPy signature associated with this LM call.
187+
demos: List of few-shot examples to include in the prompt. Each dictionary should contain keys matching the
188+
signature's input and output field names. Examples are formatted as user/assistant message pairs.
189+
inputs: The current input values for this call. Keys must match the signature's input field names.
190+
191+
Returns:
192+
List of dictionaries representing parsed LM responses. Each dictionary contains keys matching the
193+
signature's output field names. For multiple generations (n > 1), returns multiple dictionaries.
194+
"""
152195
processed_signature = self._call_preprocess(lm, lm_kwargs, signature, inputs)
153196
inputs = self.format(processed_signature, demos, inputs)
154197

0 commit comments

Comments
 (0)