Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions dspy/adapters/chat_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
from dspy.clients.lm import LM
from dspy.signatures.signature import Signature
from dspy.utils.callback import BaseCallback
from dspy.utils.exceptions import AdapterParseError

field_header_pattern = re.compile(r"\[\[ ## (\w+) ## \]\]")
Expand All @@ -26,13 +27,33 @@ class FieldInfoWithName(NamedTuple):


class ChatAdapter(Adapter):
"""Default Adapter for most language models.

The ChatAdapter formats DSPy signatures into a format compatible with most language models.
It uses delimiter patterns like `[[ ## field_name ## ]]` to clearly separate input and output fields in
the message content.

Key features:
- Structures inputs and outputs using field header markers for clear field delineation.
- Provides automatic fallback to JSONAdapter if the chat format fails.
"""

def __init__(
self,
callbacks=None,
callbacks: list[BaseCallback] | None = None,
use_native_function_calling: bool = False,
native_response_types=None,
native_response_types: list[type[type]] | None = None,
use_json_adapter_fallback: bool = True,
):
"""
Args:
callbacks: List of callback functions to execute during adapter methods.
use_native_function_calling: Whether to enable native function calling capabilities.
native_response_types: List of output field types handled by native LM features.
use_json_adapter_fallback: Whether to automatically fallback to JSONAdapter if the ChatAdapter fails.
If True, when an error occurs (except ContextWindowExceededError), the adapter will retry using
JSONAdapter. Defaults to True.
"""
super().__init__(
callbacks=callbacks,
use_native_function_calling=use_native_function_calling,
Expand Down