dspy.ChatAdapter¶
dspy.ChatAdapter(callbacks: list[BaseCallback] | None = None, use_native_function_calling: bool = False, native_response_types: list[type[type]] | None = None, use_json_adapter_fallback: bool = True, parallel_tool_calls: bool | None = None)
¶
Bases: 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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callbacks
|
list[BaseCallback] | None
|
List of callback functions to execute during adapter methods. |
None
|
use_native_function_calling
|
bool
|
Whether to enable native function calling capabilities. |
False
|
native_response_types
|
list[type[type]] | None
|
List of output field types handled by native LM features. |
None
|
use_json_adapter_fallback
|
bool
|
Whether to try JSONAdapter after an AdapterParseError. Only invalid model output can trigger this extra call, and never after visible stream output. Configuration errors, engine failures and programming bugs propagate. Defaults to True. |
True
|
parallel_tool_calls
|
bool | None
|
Whether to request provider-side parallel tool-call generation when native function calling is active. If None, the adapter does not set the provider option. |
None
|
Source code in dspy/adapters/chat_adapter.py
Methods:¶
__call__(lm: BaseLM, lm_kwargs: dict[str, Any], signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]
¶
Source code in dspy/adapters/chat_adapter.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 dspy/adapters/chat_adapter.py
format(signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]
¶
Format the input messages for the LM call.
This method converts the DSPy structured input along with few-shot examples and conversation history into multiturn messages as expected by the LM. For custom adapters, this method can be overridden to customize the formatting of the input messages.
In general we recommend the messages to have the following structure:
[
{"role": "system", "content": system_message},
# Begin few-shot examples
{"role": "user", "content": few_shot_example_1_input},
{"role": "assistant", "content": few_shot_example_1_output},
{"role": "user", "content": few_shot_example_2_input},
{"role": "assistant", "content": few_shot_example_2_output},
...
# End few-shot examples
# Begin conversation history
{"role": "user", "content": conversation_history_1_input},
{"role": "assistant", "content": conversation_history_1_output},
{"role": "user", "content": conversation_history_2_input},
{"role": "assistant", "content": conversation_history_2_output},
...
# End conversation history
{"role": "user", "content": current_input},
]
And system message should contain the field description, field structure, and task description.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
type[Signature]
|
The DSPy signature for which to format the input messages. |
required |
demos
|
list[dict[str, Any]]
|
A list of few-shot examples. |
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 dspy/adapters/base.py
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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
format_assistant_message_content(signature: type[Signature], outputs: dict[str, Any], missing_field_message=None) -> str
¶
Source code in dspy/adapters/chat_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 dspy/adapters/base.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 | |
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 dspy/adapters/base.py
format_field_description(signature: type[Signature]) -> str
¶
Source code in dspy/adapters/chat_adapter.py
format_field_structure(signature: type[Signature]) -> str
¶
ChatAdapter requires input and output fields to be in their own sections, with section header using markers
[[ ## field_name ## ]]. An arbitrary field completed ([[ ## completed ## ]]) is added to the end of the
output fields section to indicate the end of the output fields.
Source code in dspy/adapters/chat_adapter.py
format_field_with_value(fields_with_values: dict[FieldInfoWithName, Any]) -> str
¶
Formats the values of the specified fields according to the field’s DSPy type (input or output), annotation (e.g. str, int, etc.), and the type of the value itself. Joins the formatted values into a single string, which is a multiline string if there are multiple fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields_with_values
|
dict[FieldInfoWithName, Any]
|
A dictionary mapping information about a field to its corresponding value. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The joined formatted values of the fields, represented as a string |
Source code in dspy/adapters/chat_adapter.py
format_finetune_data(signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any], outputs: dict[str, Any]) -> dict[str, list[Any]]
¶
Format the call data into finetuning data according to the OpenAI API specifications.
For the chat adapter, this means formatting the data as a list of messages, where each message is a dictionary with a “role” and “content” key. The role can be “system”, “user”, or “assistant”. Then, the messages are wrapped in a dictionary with a “messages” key.
Source code in dspy/adapters/chat_adapter.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 dspy/adapters/base.py
format_task_description(signature: type[Signature]) -> str
¶
Source code in dspy/adapters/chat_adapter.py
format_user_message_content(signature: type[Signature], inputs: dict[str, Any], prefix: str = '', suffix: str = '', main_request: bool = False) -> str
¶
Source code in dspy/adapters/chat_adapter.py
parse(signature: type[Signature], completion: str) -> dict[str, Any]
¶
Source code in dspy/adapters/chat_adapter.py
user_message_output_requirements(signature: type[Signature]) -> str
¶
Returns a simplified format reminder for the language model.
In chat-based interactions, language models may lose track of the required output format as the conversation context grows longer. This method generates a concise reminder of the expected output structure that can be included in user messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature
|
Type[Signature]
|
The DSPy signature defining the expected input/output fields. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A simplified description of the required output format. |
Note
This is a more lightweight version of format_field_structure specifically designed
for inline reminders within chat messages.