dspy.XMLAdapter¶
dspy.XMLAdapter(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: ChatAdapter
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 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. |
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 .venv/lib/python3.14/site-packages/dspy/adapters/chat_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]]
¶
Source code in .venv/lib/python3.14/site-packages/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 .venv/lib/python3.14/site-packages/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 .venv/lib/python3.14/site-packages/dspy/adapters/base.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | |
format_assistant_message_content(signature: type[Signature], outputs: dict[str, Any], missing_field_message=None) -> str
¶
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/xml_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
¶
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/chat_adapter.py
format_field_structure(signature: type[Signature]) -> str
¶
XMLAdapter requires input and output fields to be wrapped in XML tags like <field_name>.
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/xml_adapter.py
format_field_with_value(fields_with_values: dict[FieldInfoWithName, Any]) -> str
¶
Source code in .venv/lib/python3.14/site-packages/dspy/adapters/xml_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 .venv/lib/python3.14/site-packages/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 |