dspy.TwoStepAdapter¶
dspy.TwoStepAdapter(extraction_model: BaseLM, **kwargs)
¶
Bases: Adapter
A two-stage adapter that
- Uses a simpler, more natural prompt for the main LM
- Uses a smaller LM with chat adapter to extract structured data from the response of main LM
This adapter uses a common call logic defined in base Adapter class. This class is particularly useful when interacting with reasoning models as the main LM since reasoning models are known to struggle with structured outputs.
Examples:
import dspy
lm = dspy.LM(model="openai/o3-mini", max_tokens=16000, temperature = 1.0)
adapter = dspy.TwoStepAdapter(dspy.LM("openai/gpt-4o-mini"))
dspy.configure(lm=lm, adapter=adapter)
program = dspy.ChainOfThought("question->answer")
result = program("What is the capital of France?")
print(result)
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/two_step_adapter.py
Functions¶
__call__(lm: BaseLM, lm_kwargs: dict[str, Any], signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]
¶
Execute the adapter pipeline: format inputs, call LM, and parse outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lm
|
BaseLM
|
The Language Model instance to use for generation. Must be an instance of |
required |
lm_kwargs
|
dict[str, Any]
|
Additional keyword arguments to pass to the LM call (e.g., temperature, max_tokens). These are passed directly to the LM. |
required |
signature
|
type[Signature]
|
The DSPy signature associated with this LM call. |
required |
demos
|
list[dict[str, Any]]
|
List of few-shot examples to include in the prompt. Each dictionary should contain keys matching the signature’s input and output field names. Examples are formatted as user/assistant message pairs. |
required |
inputs
|
dict[str, Any]
|
The current input values for this call. Keys must match the signature’s input field names. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dictionaries representing parsed LM responses. Each dictionary contains keys matching the |
list[dict[str, Any]]
|
signature’s output field names. For multiple generations (n > 1), returns multiple dictionaries. |
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/base.py
acall(lm: BaseLM, lm_kwargs: dict[str, Any], signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]
async
¶
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/two_step_adapter.py
format(signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]
¶
Format a prompt for the first stage with the main LM. This no specific structure is required for the main LM, we customize the format method instead of format_field_description or format_field_structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
type[Signature]
|
The signature of the original task |
required |
demos
|
list[dict[str, Any]]
|
A list of demo examples |
required |
inputs
|
dict[str, Any]
|
The current input |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
A list of messages to be passed to the main LM. |
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/two_step_adapter.py
format_assistant_message_content(signature: type[Signature], outputs: dict[str, Any], missing_field_message: str | None = None) -> str
¶
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/two_step_adapter.py
format_conversation_history(signature: type[Signature], history_field_name: str, inputs: dict[str, Any]) -> list[dict[str, Any]]
¶
Format the conversation history.
This method formats the conversation history and the current input as multiturn messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
type[Signature]
|
The DSPy signature for which to format the conversation history. |
required |
history_field_name
|
str
|
The name of the history field in the signature. |
required |
inputs
|
dict[str, Any]
|
The input arguments to the DSPy module. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
A list of multiturn messages as expected by the LM. |
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/base.py
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 | |
format_demos(signature: type[Signature], demos: list[dict[str, Any]]) -> list[dict[str, Any]]
¶
Format the few-shot examples.
This method formats the few-shot examples as multiturn messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
type[Signature]
|
The DSPy signature for which to format the few-shot examples. |
required |
demos
|
list[dict[str, Any]]
|
A list of few-shot examples, each element is a dictionary with keys of the input and output fields of the signature. |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
A list of multiturn messages. |
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/base.py
format_field_description(signature: type[Signature]) -> str
¶
Format the field description for the system message.
This method formats the field description for the system message. It should return a string that contains the field description for the input fields and the output fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
type[Signature]
|
The DSPy signature for which to format the field description. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A string that contains the field description for the input fields and the output fields. |
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/base.py
format_field_structure(signature: type[Signature]) -> str
¶
Format the field structure for the system message.
This method formats the field structure for the system message. It should return a string that dictates the format the input fields should be provided to the LM, and the format the output fields will be in the response. Refer to the ChatAdapter and JsonAdapter for an example.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
type[Signature]
|
The DSPy signature for which to format the field structure. |
required |
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/base.py
format_system_message(signature: type[Signature]) -> str
¶
Format the system message for the LM call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
type[Signature]
|
The DSPy signature for which to format the system message. |
required |
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/base.py
format_task_description(signature: Signature) -> str
¶
Create a description of the task based on the signature
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/two_step_adapter.py
format_user_message_content(signature: type[Signature], inputs: dict[str, Any], prefix: str = '', suffix: str = '') -> str
¶
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/two_step_adapter.py
parse(signature: Signature, completion: str) -> dict[str, Any]
¶
Use a smaller LM (extraction_model) with chat adapter to extract structured data from the raw completion text of the main LM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
Signature
|
The signature of the original task |
required |
completion
|
str
|
The completion from the main LM |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary containing the extracted structured data. |