DSPy: The framework for programming—not prompting—language models
Python
Refactor DSPy adapters to make it more extensible (#7996)
Chen Qiana48eff096df0
diff --git a/dspy/adapters/base.py b/dspy/adapters/base.pyindex 8fa57da42..52503781b 100644--- a/dspy/adapters/base.py+++ b/dspy/adapters/base.py@@ -1,7 +1,7 @@-from abc import ABC, abstractmethodfrom typing import TYPE_CHECKING, Any, Optional, Typefrom dspy.adapters.types import History+from dspy.adapters.types.image import try_expand_image_tagsfrom dspy.signatures.signature import Signaturefrom dspy.utils.callback import BaseCallback, with_callbacks@@ -9,7 +9,7 @@ if TYPE_CHECKING:from dspy.clients.lm import LM-class Adapter(ABC):+class Adapter:def __init__(self, callbacks: Optional[list[BaseCallback]] = None):self.callbacks = callbacks or []@@ -28,10 +28,9 @@ class Adapter(ABC):demos: list[dict[str, Any]],inputs: dict[str, Any],) -> list[dict[str, Any]]:- inputs_ = self.format(signature, demos, inputs)- inputs_ = dict(prompt=inputs_) if isinstance(inputs_, str) else dict(messages=inputs_)+ inputs = self.format(signature, demos, inputs)- outputs = lm(**inputs_, **lm_kwargs)+ outputs = lm(messages=inputs, **lm_kwargs)values = []for output in outputs:@@ -42,12 +41,6 @@ class Adapter(ABC):value = self.parse(signature, output)- if set(value.keys()) != set(signature.output_fields.keys()):- raise ValueError(- "Parsed output fields do not match signature output fields. "- f"Expected: {set(signature.output_fields.keys())}, Got: {set(value.keys())}"- )-if output_logprobs is not None:value["logprobs"] = output_logprobs@@ -55,53 +48,256 @@ class Adapter(ABC):return values- @abstractmethoddef format(self,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.+ ```+++ Args:+ signature: The DSPy signature for which to format the input messages.+ demos: A list of few-shot examples.+ inputs: The input arguments to the DSPy module.++ Returns:+ A list of multiturn messages as expected by the LM.+ """+ inputs_copy = dict(inputs)++ # If the signature and inputs have conversation history, we need to format the conversation history and+ # remove the history field from the signature.+ history_field_name = self._get_history_field_name(signature)+ if history_field_name:+ # In order to format the conversation history, we need to remove the history field from the signature.+ signature_without_history = signature.delete(history_field_name)+ conversation_history = self.format_conversation_history(+ signature_without_history,+ history_field_name,+ inputs_copy,+ )++ messages = []+ system_message = (+ f"{self.format_field_description(signature)}\n"+ f"{self.format_field_structure(signature)}\n"+ f"{self.format_task_description(signature)}"+ )+ messages.append({"role": "system", "content": system_message})+ messages.extend(self.format_demos(signature, demos))+ if history_field_name:+ # Conversation history and current input+ messages.extend(conversation_history)+ messages.append(+ {"role": "user", "content": self.format_user_message_content(signature_without_history, inputs_copy)}+ )+ else:+ # Only current input+ messages.append({"role": "user", "content": self.format_user_message_content(signature, inputs_copy)})++ messages = try_expand_image_tags(messages)+ return messages++ def format_field_description(self, 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.++ Args:+ signature: The DSPy signature for which to format the field description.++ Returns:+ A string that contains the field description for the input fields and the output fields.+ """raise NotImplementedError- @abstractmethod- def parse(self, signature: Type[Signature], completion: str) -> dict[str, Any]:+ def format_field_structure(self, 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.++ Args:+ signature: The DSPy signature for which to format the field structure.+ """raise NotImplementedError- def format_fields(self, signature: Type[Signature], values: dict[str, Any], role: str) -> str:+ def format_task_description(self, signature: Type[Signature]) -> str:+ """Format the task description for the system message.++ This method formats the task description for the system message. In most cases this is just a thin wrapper+ over `signature.instructions`.++ Args:+ signature: The DSPy signature of the DSpy module.++ Returns:+ A string that describes the task.+ """raise NotImplementedError- def format_finetune_data(+ def format_user_message_content(self,signature: Type[Signature],- demos: list[dict[str, Any]],inputs: dict[str, Any],- outputs: dict[str, Any],- ) -> dict[str, list[Any]]:+ prefix: str = "",+ suffix: str = "",+ ) -> str:+ """Format the user message content.++ This method formats the user message content, which can be used in formatting few-shot examples, conversation+ history, and the current input.++ Args:+ signature: The DSPy signature for which to format the user message content.+ inputs: The input arguments to the DSPy module.+ prefix: A prefix to the user message content.+ suffix: A suffix to the user message content.++ Returns:+ A string that contains the user message content.+ """raise NotImplementedError- def format_turn(+ def format_assistant_message_content(self,signature: Type[Signature],- values,- role: str,- incomplete: bool = False,- is_conversation_history: bool = False,- ) -> dict[str, Any]:+ outputs: dict[str, Any],+ missing_field_message: str = None,+ ) -> str:+ """Format the assistant message content.++ This method formats the assistant message content, which can be used in formatting few-shot examples,+ conversation history.++ Args:+ signature: The DSPy signature for which to format the assistant message content.+ outputs: The output fields to be formatted.+ missing_field_message: A message to be used when a field is missing.++ Returns:+ A string that contains the assistant message content.+ """raise NotImplementedError- def format_conversation_history(self, signature: Type[Signature], inputs: dict[str, Any]) -> list[dict[str, Any]]:- history_field_name = None+ def format_demos(self, signature: Type[Signature], demos: list[dict[str, Any]]) -> str:+ """Format the few-shot examples.++ This method formats the few-shot examples as multiturn messages.++ Args:+ signature: The DSPy signature for which to format the few-shot examples.+ demos: A list of few-shot examples, each element is a dictionary with keys of the input and output fields of+ the signature.++ Returns:+ A list of multiturn messages.+ """+ complete_demos = []+ incomplete_demos = []++ for demo in demos:+ # Check if all fields are present and not None+ is_complete = all(k in demo and demo[k] is not None for k in signature.fields)++ # Check if demo has at least one input and one output field+ has_input = any(k in demo for k in signature.input_fields)+ has_output = any(k in demo for k in signature.output_fields)++ if is_complete:+ complete_demos.append(demo)+ elif has_input and has_output:+ # We only keep incomplete demos that have at least one input and one output field+ incomplete_demos.append(demo)++ messages = []++ incomplete_demo_prefix = "This is an example of the task, though some input or output fields are not supplied."+ for demo in incomplete_demos:+ messages.append(+ {+ "role": "user",+ "content": self.format_user_message_content(signature, demo, prefix=incomplete_demo_prefix),+ }+ )+ messages.append(+ {+ "role": "assistant",+ "content": self.format_assistant_message_content(+ signature, demo, missing_field_message="Not supplied for this particular example. "+ ),+ }+ )++ for demo in complete_demos:+ messages.append({"role": "user", "content": self.format_user_message_content(signature, demo)})+ messages.append(+ {+ "role": "assistant",+ "content": self.format_assistant_message_content(+ signature, demo, missing_field_message="Not supplied for this conversation history message. "+ ),+ }+ )++ return messages++ def _get_history_field_name(self, signature: Type[Signature]) -> bool:for name, field in signature.input_fields.items():if field.annotation == History:- history_field_name = name- break+ return name+ return None- if history_field_name is None:- return []+ def format_conversation_history(+ self,+ 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.- # In order to format the conversation history, we need to remove the history field from the signature.- signature_without_history = signature.delete(history_field_name)+ Args:+ signature: The DSPy signature for which to format the conversation history.+ history_field_name: The name of the history field in the signature.+ inputs: The input arguments to the DSPy module.++ Returns:+ A list of multiturn messages.+ """conversation_history = inputs[history_field_name].messages if history_field_name in inputs else Noneif conversation_history is None:@@ -110,14 +306,33 @@ class Adapter(ABC):messages = []for message in conversation_history:messages.append(- self.format_turn(signature_without_history, message, role="user", is_conversation_history=True)+ {+ "role": "user",+ "content": self.format_user_message_content(signature, message),+ })messages.append(- self.format_turn(signature_without_history, message, role="assistant", is_conversation_history=True)+ {+ "role": "assistant",+ "content": self.format_assistant_message_content(signature, message),+ })- inputs_copy = dict(inputs)- del inputs_copy[history_field_name]+ # Remove the history field from the inputs+ del inputs[history_field_name]- messages.append(self.format_turn(signature_without_history, inputs_copy, role="user"))return messages++ def parse(self, signature: Type[Signature], completion: str) -> dict[str, Any]:+ """Parse the LM output into a dictionary of the output fields.++ This method parses the LM output into a dictionary of the output fields.++ Args:+ signature: The DSPy signature for which to parse the LM output.+ completion: The LM output to be parsed.++ Returns:+ A dictionary of the output fields.+ """+ raise NotImplementedErrordiff --git a/dspy/adapters/chat_adapter.py b/dspy/adapters/chat_adapter.pyindex edadc4ed6..abb381f96 100644--- a/dspy/adapters/chat_adapter.py+++ b/dspy/adapters/chat_adapter.py@@ -1,24 +1,20 @@-import enum-import inspect-import jsonimport reimport textwrap-from collections.abc import Mapping-from typing import Any, Dict, Literal, NamedTuple, Optional, Type+from typing import Any, Dict, NamedTuple, Optional, Type-import pydanticfrom litellm import ContextWindowExceededErrorfrom pydantic.fields import FieldInfofrom dspy.adapters.base import Adapter-from dspy.adapters.json_adapter import JSONAdapter-from dspy.adapters.types.history import History-from dspy.adapters.types.image import try_expand_image_tags-from dspy.adapters.utils import format_field_value, get_annotation_name, parse_value+from dspy.adapters.utils import (+ format_field_value,+ get_annotation_name,+ get_field_description_string,+ parse_value,+ translate_field_type,+)from dspy.clients.lm import LM-from dspy.signatures.field import OutputField-from dspy.signatures.signature import Signature, SignatureMeta-from dspy.signatures.utils import get_dspy_field_type+from dspy.signatures.signature import Signaturefrom dspy.utils.callback import BaseCallbackfield_header_pattern = re.compile(r"\[\[ ## (\w+) ## \]\]")@@ -29,10 +25,6 @@ class FieldInfoWithName(NamedTuple):info: FieldInfo-# Built-in field indicating that a chat turn has been completed.-BuiltInCompletedOutputFieldInfo = FieldInfoWithName(name="completed", info=OutputField())--class ChatAdapter(Adapter):def __init__(self, callbacks: Optional[list[BaseCallback]] = None):super().__init__(callbacks)@@ -48,46 +40,109 @@ class ChatAdapter(Adapter):try:return super().__call__(lm, lm_kwargs, signature, demos, inputs)except Exception as e:- if isinstance(e, ContextWindowExceededError):- # On context window exceeded error, we don't want to retry with a different adapter.- raise e# fallback to JSONAdapter+ from dspy.adapters.json_adapter import JSONAdapter++ if isinstance(e, ContextWindowExceededError) or isinstance(self, JSONAdapter):+ # On context window exceeded error or already using JSONAdapter, we don't want to retry with a different+ # adapter.+ raise ereturn JSONAdapter()(lm, lm_kwargs, signature, demos, inputs)- def format(- self, signature: Type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]- ) -> list[dict[str, Any]]:- messages: list[dict[str, Any]] = []-- # Extract demos where some of the output_fields are not filled in.- incomplete_demos = [- demo for demo in demos if not all(k in demo and demo[k] is not None for k in signature.fields)- ]- complete_demos = [demo for demo in demos if demo not in incomplete_demos]- # Filter out demos that don't have at least one input and one output field.- incomplete_demos = [- demo- for demo in incomplete_demos- if any(k in demo for k in signature.input_fields) and any(k in demo for k in signature.output_fields)- ]-- demos = incomplete_demos + complete_demos- prepared_instructions = prepare_instructions(signature)- messages.append({"role": "system", "content": prepared_instructions})-- # Add the few-shot examples- for demo in demos:- messages.append(self.format_turn(signature, demo, role="user", incomplete=demo in incomplete_demos))- messages.append(self.format_turn(signature, demo, role="assistant", incomplete=demo in incomplete_demos))-- # Add the chat history after few-shot examples- if any(field.annotation == History for field in signature.input_fields.values()):- messages.extend(self.format_conversation_history(signature, inputs))- else:- messages.append(self.format_turn(signature, inputs, role="user"))-- messages = try_expand_image_tags(messages)- return messages+ def format_field_description(self, signature: Type[Signature]) -> str:+ return (+ f"Your input fields are:\n{get_field_description_string(signature.input_fields)}\n"+ f"Your output fields are:\n{get_field_description_string(signature.output_fields)}"+ )++ def format_field_structure(self, 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.+ """+ parts = []+ parts.append("All interactions will be structured in the following way, with the appropriate values filled in.")++ def format_signature_fields_for_instructions(fields: Dict[str, FieldInfo]):+ return self.format_field_with_value(+ fields_with_values={+ FieldInfoWithName(name=field_name, info=field_info): translate_field_type(field_name, field_info)+ for field_name, field_info in fields.items()+ },+ )++ parts.append(format_signature_fields_for_instructions(signature.input_fields))+ parts.append(format_signature_fields_for_instructions(signature.output_fields))+ parts.append("[[ ## completed ## ]]\n")+ return "\n\n".join(parts).strip()++ def format_task_description(self, signature: Type[Signature]) -> str:+ instructions = textwrap.dedent(signature.instructions)+ objective = ("\n" + " " * 8).join([""] + instructions.splitlines())+ return f"In adhering to this structure, your objective is: {objective}"++ def format_user_message_content(+ self,+ signature: Type[Signature],+ inputs: dict[str, Any],+ prefix: str = "",+ suffix: str = "",+ ) -> str:+ messages = [prefix]+ for k, v in signature.input_fields.items():+ value = inputs[k]+ formatted_field_value = format_field_value(field_info=v, value=value)+ messages.append(f"[[ ## {k} ## ]]\n{formatted_field_value}")++ output_requirements = self.user_message_output_requirements(signature)+ if output_requirements is not None:+ messages.append(output_requirements)++ messages.append(suffix)+ return "\n\n".join(messages).strip()++ def user_message_output_requirements(self, 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.++ Args:+ signature (Type[Signature]): The DSPy signature defining the expected input/output fields.++ Returns:+ 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.+ """++ def type_info(v):+ if v.annotation is not str:+ return f" (must be formatted as a valid Python {get_annotation_name(v.annotation)})"+ else:+ return ""++ message = "Respond with the corresponding output fields, starting with the field "+ message += ", then ".join(f"`[[ ## {f} ## ]]`{type_info(v)}" for f, v in signature.output_fields.items())+ message += ", and then ending with the marker for `[[ ## completed ## ]]`."+ return message++ def format_assistant_message_content(+ self,+ signature: Type[Signature],+ outputs: dict[str, Any],+ missing_field_message=None,+ ) -> str:+ return self.format_field_with_value(+ {+ FieldInfoWithName(name=k, info=v): outputs.get(k, missing_field_message)+ for k, v in signature.output_fields.items()+ },+ )def parse(self, signature: Type[Signature], completion: str) -> dict[str, Any]:sections = [(None, [])]@@ -113,12 +168,31 @@ class ChatAdapter(Adapter):raise ValueError(f"Error parsing field {k}: {e}.\n\n\t\tOn attempting to parse the value\n```\n{v}\n```")-if fields.keys() != signature.output_fields.keys():raise ValueError(f"Expected {signature.output_fields.keys()} but got {fields.keys()}")return fields+ def format_field_with_value(self, 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 is a multiline string if there are multiple fields.++ Args:+ fields_with_values: A dictionary mapping information about a field to its corresponding+ value.++ Returns:+ The joined formatted values of the fields, represented as a string+ """+ output = []+ for field, field_value in fields_with_values.items():+ formatted_field_value = format_field_value(field_info=field.info, value=field_value)+ output.append(f"[[ ## {field.name} ## ]]\n{formatted_field_value}")++ return "\n\n".join(output).strip()+# TODO(PR): Looks ok?def format_finetune_data(self, signature: Type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any], outputs: dict[str, Any]@@ -134,193 +208,3 @@ class ChatAdapter(Adapter):# Wrap the messages in a dictionary with a "messages" keyreturn dict(messages=messages)-- def format_fields(self, signature: Type[Signature], values: dict[str, Any], role: str) -> str:- fields_with_values = {- FieldInfoWithName(name=field_name, info=field_info): values.get(- field_name, "Not supplied for this particular example."- )- for field_name, field_info in signature.fields.items()- if field_name in values- }- return format_fields(fields_with_values)-- def format_turn(- self,- signature: Type[Signature],- values: dict[str, Any],- role: str,- incomplete: bool = False,- is_conversation_history: bool = False,- ) -> dict[str, Any]:- return format_turn(signature, values, role, incomplete, is_conversation_history)---def format_fields(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 is a multiline string if there are multiple fields.-- Args:- fields_with_values: A dictionary mapping information about a field to its corresponding- value.- Returns:- The joined formatted values of the fields, represented as a string- """- output = []- for field, field_value in fields_with_values.items():- formatted_field_value = format_field_value(field_info=field.info, value=field_value)- output.append(f"[[ ## {field.name} ## ]]\n{formatted_field_value}")-- return "\n\n".join(output).strip()---def format_turn(- signature: Type[Signature], values: dict[str, Any], role: str, incomplete=False, is_conversation_history=False-):- """- Constructs a new message ("turn") to append to a chat thread. The message is carefully formatted- so that it can instruct an LLM to generate responses conforming to the specified DSPy signature.-- Args:- signature: The DSPy signature to which future LLM responses should conform.- values: A dictionary mapping field names (from the DSPy signature) to corresponding values- that should be included in the message.- role: The role of the message, which can be either "user" or "assistant".- incomplete: If True, indicates that output field values are present in the set of specified- `values`. If False, indicates that `values` only contains input field values. Only used if- `is_conversation_history` is False.- is_conversation_history: If True, indicates that the message is part of the chat history, otherwise- it is a demo (few-shot example).-- Returns:- A chat message that can be appended to a chat thread. The message contains two string fields:- `role` ("user" or "assistant") and `content` (the message text).- """- if role == "user":- fields = signature.input_fields- if incomplete and not is_conversation_history:- message_prefix = "This is an example of the task, though some input or output fields are not supplied."- else:- message_prefix = ""- else:- # Add the completed field or chat history for the assistant turn- fields = {**signature.output_fields}- values = {**values}- message_prefix = ""- if not is_conversation_history:- fields.update({BuiltInCompletedOutputFieldInfo.name: BuiltInCompletedOutputFieldInfo.info})- values.update({BuiltInCompletedOutputFieldInfo.name: ""})-- if not incomplete and not is_conversation_history and not set(values).issuperset(fields.keys()):- raise ValueError(f"Expected {fields.keys()} but got {values.keys()}")-- messages = []- if message_prefix:- messages.append(message_prefix)-- field_messages = format_fields(- {- FieldInfoWithName(name=k, info=v): values.get(- k,- "Not supplied for this conversation history message. "- if is_conversation_history- else "Not supplied for this particular example. ",- )- for k, v in fields.items()- },- )- messages.append(field_messages)-- def type_info(v):- if v.annotation is not str:- return f" (must be formatted as a valid Python {get_annotation_name(v.annotation)})"- else:- return ""-- # Add output field instructions for user messages- if role == "user" and signature.output_fields:- field_instructions = (- "Respond with the corresponding output fields, starting with the field "- + ", then ".join(f"`[[ ## {f} ## ]]`{type_info(v)}" for f, v in signature.output_fields.items())- + ", and then ending with the marker for `[[ ## completed ## ]]`."- )- messages.append(field_instructions)- joined_messages = "\n\n".join(msg for msg in messages)- return {"role": role, "content": joined_messages}---def enumerate_fields(fields: dict) -> str:- parts = []- for idx, (k, v) in enumerate(fields.items()):- parts.append(f"{idx + 1}. `{k}`")- parts[-1] += f" ({get_annotation_name(v.annotation)})"- parts[-1] += f": {v.json_schema_extra['desc']}" if v.json_schema_extra["desc"] != f"${{{k}}}" else ""- parts[-1] += (- f"\nConstraints: {v.json_schema_extra['constraints']}" if v.json_schema_extra.get("constraints") else ""- )- return "\n".join(parts).strip()---def move_type_to_front(d):- # Move the 'type' key to the front of the dictionary, recursively, for LLM readability/adherence.- if isinstance(d, Mapping):- return {k: move_type_to_front(v) for k, v in sorted(d.items(), key=lambda item: (item[0] != "type", item[0]))}- elif isinstance(d, list):- return [move_type_to_front(item) for item in d]- return d---def prepare_schema(field_type):- schema = pydantic.TypeAdapter(field_type).json_schema()- schema = move_type_to_front(schema)- return schema---def prepare_instructions(signature: SignatureMeta):- parts = []- parts.append("Your input fields are:\n" + enumerate_fields(signature.input_fields))- parts.append("Your output fields are:\n" + enumerate_fields(signature.output_fields))- parts.append("All interactions will be structured in the following way, with the appropriate values filled in.")-- def field_metadata(field_name, field_info):- field_type = field_info.annotation-- if get_dspy_field_type(field_info) == "input" or field_type is str:- desc = ""- elif field_type is bool:- desc = "must be True or False"- elif field_type in (int, float):- desc = f"must be a single {field_type.__name__} value"- elif inspect.isclass(field_type) and issubclass(field_type, enum.Enum):- desc = f"must be one of: {'; '.join(field_type.__members__)}"- elif hasattr(field_type, "__origin__") and field_type.__origin__ is Literal:- desc = (- # Strongly encourage the LM to avoid choosing values that don't appear in the- # literal or returning a value of the form 'Literal[<selected_value>]'- f"must exactly match (no extra characters) one of: {'; '.join([str(x) for x in field_type.__args__])}"- )- else:- desc = "must adhere to the JSON schema: "- desc += json.dumps(prepare_schema(field_type), ensure_ascii=False)-- desc = (" " * 8) + f"# note: the value you produce {desc}" if desc else ""- return f"{{{field_name}}}{desc}"-- def format_signature_fields_for_instructions(fields: Dict[str, FieldInfo]):- return format_fields(- fields_with_values={- FieldInfoWithName(name=field_name, info=field_info): field_metadata(field_name, field_info)- for field_name, field_info in fields.items()- },- )-- parts.append(format_signature_fields_for_instructions(signature.input_fields))- parts.append(format_signature_fields_for_instructions(signature.output_fields))- parts.append(format_fields({BuiltInCompletedOutputFieldInfo: ""}))- instructions = textwrap.dedent(signature.instructions)- objective = ("\n" + " " * 8).join([""] + instructions.splitlines())- parts.append(f"In adhering to this structure, your objective is: {objective}")-- return "\n\n".join(parts).strip()diff --git a/dspy/adapters/json_adapter.py b/dspy/adapters/json_adapter.pyindex ed364b066..b5486c8d9 100644--- a/dspy/adapters/json_adapter.py+++ b/dspy/adapters/json_adapter.py@@ -1,10 +1,7 @@-import enum-import inspectimport jsonimport logging-import textwrapfrom copy import deepcopy-from typing import Any, Dict, KeysView, Literal, NamedTuple, Type+from typing import Any, Dict, Typeimport json_repairimport litellm@@ -12,26 +9,21 @@ import pydanticfrom pydantic import create_modelfrom pydantic.fields import FieldInfo-from dspy.adapters.base import Adapter-from dspy.adapters.types.history import History-from dspy.adapters.types.image import try_expand_image_tags-from dspy.adapters.utils import format_field_value, get_annotation_name, parse_value, serialize_for_json+from dspy.adapters.chat_adapter import ChatAdapter, FieldInfoWithName+from dspy.adapters.utils import (+ format_field_value,+ get_annotation_name,+ parse_value,+ serialize_for_json,+ translate_field_type,+)from dspy.clients.lm import LMfrom dspy.signatures.signature import Signature, SignatureMeta-from dspy.signatures.utils import get_dspy_field_typelogger = logging.getLogger(__name__)-class FieldInfoWithName(NamedTuple):- name: str- info: FieldInfo---class JSONAdapter(Adapter):- def __init__(self):- pass-+class JSONAdapter(ChatAdapter):def __call__(self,lm: LM,@@ -40,70 +32,71 @@ class JSONAdapter(Adapter):demos: list[dict[str, Any]],inputs: dict[str, Any],) -> list[dict[str, Any]]:- inputs = self.format(signature, demos, inputs)- inputs = dict(prompt=inputs) if isinstance(inputs, str) else dict(messages=inputs)-- try:- provider = lm.model.split("/", 1)[0] or "openai"- params = litellm.get_supported_openai_params(model=lm.model, custom_llm_provider=provider)- if params and "response_format" in params:- try:- response_format = _get_structured_outputs_response_format(signature)- outputs = lm(**inputs, **lm_kwargs, response_format=response_format)- except Exception as e:- logger.debug(- f"Failed to obtain response using signature-based structured outputs"- f" response format: Falling back to default 'json_object' response format."- f" Exception: {e}"- )- outputs = lm(**inputs, **lm_kwargs, response_format={"type": "json_object"})- else:- outputs = lm(**inputs, **lm_kwargs)-- except litellm.UnsupportedParamsError:- outputs = lm(**inputs, **lm_kwargs)-- values = []-- for output in outputs:- value = self.parse(signature, output)- assert set(value.keys()) == set(- signature.output_fields.keys()- ), f"Expected {signature.output_fields.keys()} but got {value.keys()}"- values.append(value)-- return values-- def format(- self, signature: Type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]- ) -> list[dict[str, Any]]:- messages = []+ provider = lm.model.split("/", 1)[0] or "openai"+ params = litellm.get_supported_openai_params(model=lm.model, custom_llm_provider=provider)- # Extract demos where some of the output_fields are not filled in.- incomplete_demos = [demo for demo in demos if not all(k in demo for k in signature.fields)]- complete_demos = [demo for demo in demos if demo not in incomplete_demos]- incomplete_demos = [- demo- for demo in incomplete_demos- if any(k in demo for k in signature.input_fields) and any(k in demo for k in signature.output_fields)- ]+ # If response_format is not supported, use basic call+ if not params or "response_format" not in params:+ return super().__call__(lm, lm_kwargs, signature, demos, inputs)- demos = incomplete_demos + complete_demos+ # Try structured output first, fall back to basic json if it fails+ try:+ structured_output_format = self._get_structured_outputs_response_format(signature)+ lm_kwargs["response_format"] = structured_output_format+ return super().__call__(lm, lm_kwargs, signature, demos, inputs)+ except Exception as e:+ logger.warning(f"Failed to use structured output format. Falling back to JSON mode. Error: {e}")+ try:+ lm_kwargs["response_format"] = {"type": "json_object"}+ return super().__call__(lm, lm_kwargs, signature, demos, inputs)+ except Exception as e:+ raise RuntimeError(+ "Both structured output format and JSON mode failed. Please choose a model that supports "+ f"`response_format` argument. Original error: {e}"+ ) from e++ def format_field_structure(self, signature: Type[Signature]) -> str:+ parts = []+ parts.append("All interactions will be structured in the following way, with the appropriate values filled in.")++ def format_signature_fields_for_instructions(fields: Dict[str, FieldInfo]):+ return self.format_field_with_value(+ fields_with_values={+ FieldInfoWithName(name=field_name, info=field_info): translate_field_type(field_name, field_info)+ for field_name, field_info in fields.items()+ },+ )- messages.append({"role": "system", "content": prepare_instructions(signature)})+ parts.append("Inputs will have the following structure:")+ parts.append(format_signature_fields_for_instructions(signature.input_fields))+ parts.append("Outputs will be a JSON object with the following fields.")+ parts.append(format_signature_fields_for_instructions(signature.output_fields))+ return "\n\n".join(parts).strip()- for demo in demos:- messages.append(self.format_turn(signature, demo, role="user", incomplete=demo in incomplete_demos))- messages.append(self.format_turn(signature, demo, role="assistant", incomplete=demo in incomplete_demos))+ def user_message_output_requirements(self, signature: Type[Signature]) -> str:+ def type_info(v):+ return (+ f" (must be formatted as a valid Python {get_annotation_name(v.annotation)})"+ if v.annotation is not str+ else ""+ )- # Add the chat history after few-shot examples- if any(field.annotation == History for field in signature.input_fields.values()):- messages.extend(self.format_conversation_history(signature, inputs))- else:- messages.append(self.format_turn(signature, inputs, role="user"))+ message = "Respond with a JSON object in the following order of fields: "+ message += ", then ".join(f"`{f}`{type_info(v)}" for f, v in signature.output_fields.items())+ message += "."+ return message- messages = try_expand_image_tags(messages)- return messages+ def format_assistant_message_content(+ self,+ signature: Type[Signature],+ outputs: dict[str, Any],+ missing_field_message=None,+ ) -> str:+ fields_with_values = {+ FieldInfoWithName(name=k, info=v): outputs.get(k, missing_field_message)+ for k, v in signature.output_fields.items()+ }+ return self.format_field_with_value(fields_with_values, role="assistant")def parse(self, signature: Type[Signature], completion: str) -> dict[str, Any]:fields = json_repair.loads(completion)@@ -119,25 +112,27 @@ class JSONAdapter(Adapter):return fields- def format_fields(self, signature: Type[Signature], values: dict[str, Any], role: str) -> str:- fields_with_values = {- FieldInfoWithName(name=field_name, info=field_info): values.get(- field_name, "Not supplied for this particular example."- )- for field_name, field_info in signature.fields.items()- if field_name in values- }- return format_fields(role=role, fields_with_values=fields_with_values)-- def format_turn(- self,- signature: Type[Signature],- values,- role: str,- incomplete: bool = False,- is_conversation_history: bool = False,- ) -> dict[str, Any]:- return format_turn(signature, values, role, incomplete, is_conversation_history)+ def format_field_with_value(self, fields_with_values: Dict[FieldInfoWithName, Any], role: str = "user") -> 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 is a multiline string if there are multiple fields.++ Args:+ fields_with_values: A dictionary mapping information about a field to its corresponding value.+ Returns:+ The joined formatted values of the fields, represented as a string+ """+ if role == "user":+ output = []+ for field, field_value in fields_with_values.items():+ formatted_field_value = format_field_value(field_info=field.info, value=field_value)+ output.append(f"[[ ## {field.name} ## ]]\n{formatted_field_value}")+ return "\n\n".join(output).strip()+ else:+ d = fields_with_values.items()+ d = {k.name: v for k, v in d}+ return json.dumps(serialize_for_json(d), indent=2)def format_finetune_data(self, signature: Type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any], outputs: dict[str, Any]@@ -145,215 +140,49 @@ class JSONAdapter(Adapter):# TODO: implement format_finetune_data method in JSONAdapterraise NotImplementedError--def format_fields(role: str, 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.-- Args:- role: The role of the message ('user' or 'assistant')- fields_with_values: A dictionary mapping information about a field to its corresponding value.-- Returns:- The joined formatted values of the fields, represented as a string.- """-- if role == "assistant":- d = fields_with_values.items()- d = {k.name: v for k, v in d}- return json.dumps(serialize_for_json(d), indent=2)-- output = []- for field, field_value in fields_with_values.items():- formatted_field_value = format_field_value(field_info=field.info, value=field_value)- output.append(f"[[ ## {field.name} ## ]]\n{formatted_field_value}")-- return "\n\n".join(output).strip()---def format_turn(- signature: SignatureMeta,- values: Dict[str, Any],- role: str,- incomplete=False,- is_conversation_history=False,-) -> Dict[str, str]:- """- Constructs a new message ("turn") to append to a chat thread. The message is carefully formatted- so that it can instruct an LLM to generate responses conforming to the specified DSPy signature.-- Args:- signature: The DSPy signature to which future LLM responses should conform.- values: A dictionary mapping field names (from the DSPy signature) to corresponding values- that should be included in the message.- role: The role of the message, which can be either "user" or "assistant".- incomplete: If True, indicates that output field values are present in the set of specified- `values`. If False, indicates that `values` only contains input field values. Only- relevant if `is_conversation_history` is False.- is_conversation_history: If True, indicates that the message is part of a chat history instead of a- few-shot example.- Returns:- A chat message that can be appended to a chat thread. The message contains two string fields:- ``role`` ("user" or "assistant") and ``content`` (the message text).- """- content = []-- if role == "user":- fields: Dict[str, FieldInfo] = signature.input_fields- if incomplete and not is_conversation_history:- content.append("This is an example of the task, though some input or output fields are not supplied.")- else:- fields: Dict[str, FieldInfo] = signature.output_fields-- if not incomplete and not is_conversation_history:- # For complete few-shot examples, ensure that the values contain all the fields.- field_names: KeysView = fields.keys()- if not set(values).issuperset(set(field_names)):- raise ValueError(f"Expected {field_names} but got {values.keys()}")-- fields_with_values = {}- for field_name, field_info in fields.items():- if is_conversation_history:- fields_with_values[FieldInfoWithName(name=field_name, info=field_info)] = values.get(- field_name, "Not supplied for this conversation history message. "- )- else:- fields_with_values[FieldInfoWithName(name=field_name, info=field_info)] = values.get(- field_name, "Not supplied for this particular example. "- )-- formatted_fields = format_fields(role=role, fields_with_values=fields_with_values)- content.append(formatted_fields)-- if role == "user":-- def type_info(v):- return (- f" (must be formatted as a valid Python {get_annotation_name(v.annotation)})"- if v.annotation is not str- else ""- )-- # TODO: Consider if not incomplete:- content.append(- "Respond with a JSON object in the following order of fields: "- + ", then ".join(f"`{f}`{type_info(v)}" for f, v in signature.output_fields.items())- + "."- )-- return {"role": role, "content": "\n\n".join(content).strip()}---def enumerate_fields(fields):- parts = []- for idx, (k, v) in enumerate(fields.items()):- parts.append(f"{idx+1}. `{k}`")- parts[-1] += f" ({get_annotation_name(v.annotation)})"- parts[-1] += f": {v.json_schema_extra['desc']}" if v.json_schema_extra["desc"] != f"${{{k}}}" else ""- parts[-1] += (- f"\nConstraints: {v.json_schema_extra['constraints']}" if v.json_schema_extra.get("constraints") else ""- )-- return "\n".join(parts).strip()---def prepare_instructions(signature: SignatureMeta):- parts = []- parts.append("Your input fields are:\n" + enumerate_fields(signature.input_fields))- parts.append("Your output fields are:\n" + enumerate_fields(signature.output_fields))- parts.append("All interactions will be structured in the following way, with the appropriate values filled in.")-- def field_metadata(field_name, field_info):- type_ = field_info.annotation-- if get_dspy_field_type(field_info) == "input" or type_ is str:- desc = ""- elif type_ is bool:- desc = "must be True or False"- elif type_ in (int, float):- desc = f"must be a single {type_.__name__} value"- elif inspect.isclass(type_) and issubclass(type_, enum.Enum):- desc = f"must be one of: {'; '.join(type_.__members__)}"- elif hasattr(type_, "__origin__") and type_.__origin__ is Literal:- desc = f"must be one of: {'; '.join([str(x) for x in type_.__args__])}"- else:- desc = "must adhere to the JSON schema: "- desc += json.dumps(pydantic.TypeAdapter(type_).json_schema())-- desc = (" " * 8) + f"# note: the value you produce {desc}" if desc else ""- return f"{{{field_name}}}{desc}"-- def format_signature_fields_for_instructions(role, fields: Dict[str, FieldInfo]):- return format_fields(- role=role,- fields_with_values={- FieldInfoWithName(name=field_name, info=field_info): field_metadata(field_name, field_info)- for field_name, field_info in fields.items()- },- )-- parts.append("Inputs will have the following structure:")- parts.append(format_signature_fields_for_instructions("user", signature.input_fields))- parts.append("Outputs will be a JSON object with the following fields.")- parts.append(format_signature_fields_for_instructions("assistant", signature.output_fields))- # parts.append(format_fields({BuiltInCompletedOutputFieldInfo: ""}))-- instructions = textwrap.dedent(signature.instructions)- objective = ("\n" + " " * 8).join([""] + instructions.splitlines())- parts.append(f"In adhering to this structure, your objective is: {objective}")-- # parts.append("You will receive some input fields in each interaction. " +- # "Respond only with the corresponding output fields, starting with the field " +- # ", then ".join(f"`{f}`" for f in signature.output_fields) +- # ", and then ending with the marker for `completed`.")-- return "\n\n".join(parts).strip()---def _get_structured_outputs_response_format(signature: SignatureMeta) -> pydantic.BaseModel:- """- Obtains the LiteLLM / OpenAI `response_format` parameter for generating structured outputs from- an LM request, based on the output fields of the specified DSPy signature.-- Args:- signature: The DSPy signature for which to obtain the `response_format` request parameter.- Returns:- A Pydantic model representing the `response_format` parameter for the LM request.- """-- def filter_json_schema_extra(field_name: str, field_info: FieldInfo) -> FieldInfo:+ def _get_structured_outputs_response_format(self, signature: SignatureMeta) -> pydantic.BaseModel:"""- Recursively filter the `json_schema_extra` of a FieldInfo to exclude DSPy internal attributes- (e.g. `__dspy_field_type`) and remove descriptions that are placeholders for the field name.- """- field_copy = deepcopy(field_info) # Make a copy to avoid mutating the original-- # Update `json_schema_extra` for the copied field- if field_copy.json_schema_extra:- field_copy.json_schema_extra = {- key: value- for key, value in field_info.json_schema_extra.items()- if key not in ("desc", "__dspy_field_type")- }- field_desc = field_info.json_schema_extra.get("desc")- if field_desc is not None and field_desc != f"${{{field_name}}}":- field_copy.json_schema_extra["desc"] = field_desc-- # Handle nested fields- if hasattr(field_copy.annotation, "__pydantic_model__"):- # Recursively update fields of the nested model- nested_model = field_copy.annotation.__pydantic_model__- updated_fields = {- key: filter_json_schema_extra(key, value) for key, value in nested_model.__fields__.items()- }- # Create a new model with the same name and updated fields- field_copy.annotation = create_model(nested_model.__name__, **updated_fields)+ Obtains the LiteLLM / OpenAI `response_format` parameter for generating structured outputs from+ an LM request, based on the output fields of the specified DSPy signature.- return field_copy+ Args:+ signature: The DSPy signature for which to obtain the `response_format` request parameter.+ Returns:+ A Pydantic model representing the `response_format` parameter for the LM request.+ """- output_pydantic_fields = {- key: (value.annotation, filter_json_schema_extra(key, value)) for key, value in signature.output_fields.items()- }- return create_model("DSPyProgramOutputs", **output_pydantic_fields)+ def filter_json_schema_extra(field_name: str, field_info: FieldInfo) -> FieldInfo:+ """+ Recursively filter the `json_schema_extra` of a FieldInfo to exclude DSPy internal attributes+ (e.g. `__dspy_field_type`) and remove descriptions that are placeholders for the field name.+ """+ field_copy = deepcopy(field_info) # Make a copy to avoid mutating the original++ # Update `json_schema_extra` for the copied field+ if field_copy.json_schema_extra:+ field_copy.json_schema_extra = {+ key: value+ for key, value in field_info.json_schema_extra.items()+ if key not in ("desc", "__dspy_field_type")+ }+ field_desc = field_info.json_schema_extra.get("desc")+ if field_desc is not None and field_desc != f"${{{field_name}}}":+ field_copy.json_schema_extra["desc"] = field_desc++ # Handle nested fields+ if hasattr(field_copy.annotation, "__pydantic_model__"):+ # Recursively update fields of the nested model+ nested_model = field_copy.annotation.__pydantic_model__+ updated_fields = {+ key: filter_json_schema_extra(key, value) for key, value in nested_model.__fields__.items()+ }+ # Create a new model with the same name and updated fields+ field_copy.annotation = create_model(nested_model.__name__, **updated_fields)++ return field_copy++ output_pydantic_fields = {+ key: (value.annotation, filter_json_schema_extra(key, value))+ for key, value in signature.output_fields.items()+ }+ return create_model("DSPyProgramOutputs", **output_pydantic_fields)diff --git a/dspy/adapters/utils.py b/dspy/adapters/utils.pyindex ad07a5e63..4a3a48011 100644--- a/dspy/adapters/utils.py+++ b/dspy/adapters/utils.py@@ -1,12 +1,17 @@import astimport enum+import inspectimport json+from collections.abc import Mappingfrom typing import Any, List, Literal, Union, get_args, get_originimport json_repair+import pydanticfrom pydantic import TypeAdapterfrom pydantic.fields import FieldInfo+from dspy.signatures.utils import get_dspy_field_type+def serialize_for_json(value: Any) -> Any:"""@@ -58,6 +63,46 @@ def format_field_value(field_info: FieldInfo, value: Any, assume_text=True) -> Ureturn {"type": "text", "text": string_value}+def _get_json_schema(field_type):+ def move_type_to_front(d):+ # Move the 'type' key to the front of the dictionary, recursively, for LLM readability/adherence.+ if isinstance(d, Mapping):+ return {+ k: move_type_to_front(v) for k, v in sorted(d.items(), key=lambda item: (item[0] != "type", item[0]))+ }+ elif isinstance(d, list):+ return [move_type_to_front(item) for item in d]+ return d++ schema = pydantic.TypeAdapter(field_type).json_schema()+ schema = move_type_to_front(schema)+ return schema+++def translate_field_type(field_name, field_info):+ field_type = field_info.annotation++ if get_dspy_field_type(field_info) == "input" or field_type is str:+ desc = ""+ elif field_type is bool:+ desc = "must be True or False"+ elif field_type in (int, float):+ desc = f"must be a single {field_type.__name__} value"+ elif inspect.isclass(field_type) and issubclass(field_type, enum.Enum):+ desc = f"must be one of: {'; '.join(field_type.__members__)}"+ elif hasattr(field_type, "__origin__") and field_type.__origin__ is Literal:+ desc = (+ # Strongly encourage the LM to avoid choosing values that don't appear in the+ # literal or returning a value of the form 'Literal[<selected_value>]'+ f"must exactly match (no extra characters) one of: {'; '.join([str(x) for x in field_type.__args__])}"+ )+ else:+ desc = f"must adhere to the JSON schema: {json.dumps(_get_json_schema(field_type), ensure_ascii=False)}"++ desc = (" " * 8) + f"# note: the value you produce {desc}" if desc else ""+ return f"{{{field_name}}}{desc}"++def find_enum_member(enum, identifier):"""Finds the enum member corresponding to the specified identifier, which may be the@@ -125,6 +170,18 @@ def get_annotation_name(annotation):args_str = ", ".join(get_annotation_name(a) for a in args)return f"{get_annotation_name(origin)}[{args_str}]"+def get_field_description_string(fields: dict) -> str:+ field_descriptions = []+ for idx, (k, v) in enumerate(fields.items()):+ field_message = f"{idx + 1}. `{k}`"+ field_message += f" ({get_annotation_name(v.annotation)})"+ field_message += f": {v.json_schema_extra['desc']}" if v.json_schema_extra["desc"] != f"${{{k}}}" else ""+ field_message += (+ f"\nConstraints: {v.json_schema_extra['constraints']}" if v.json_schema_extra.get("constraints") else ""+ )+ field_descriptions.append(field_message)+ return "\n".join(field_descriptions).strip()+def _format_input_list_field_value(value: List[Any]) -> str:"""diff --git a/dspy/predict/predict.py b/dspy/predict/predict.pyindex a86422719..8813697ae 100644--- a/dspy/predict/predict.py+++ b/dspy/predict/predict.py@@ -13,6 +13,7 @@ from dspy.primitives.program import Modulefrom dspy.signatures.signature import ensure_signaturefrom dspy.utils.callback import with_callbacks+logger = logging.getLogger(__name__)diff --git a/dspy/predict/react.py b/dspy/predict/react.pyindex cd79c1e27..395a8f5ea 100644--- a/dspy/predict/react.py+++ b/dspy/predict/react.py@@ -69,7 +69,7 @@ class ReAct(Module):def _format_trajectory(self, trajectory: dict[str, Any]):adapter = dspy.settings.adapter or dspy.ChatAdapter()trajectory_signature = dspy.Signature(f"{', '.join(trajectory.keys())} -> x")- return adapter.format_fields(trajectory_signature, trajectory, role="user")+ return adapter.format_user_message_content(trajectory_signature, trajectory)def forward(self, **input_args):trajectory = {}diff --git a/dspy/predict/refine.py b/dspy/predict/refine.pyindex 49d4d80c2..9991750d6 100644--- a/dspy/predict/refine.py+++ b/dspy/predict/refine.py@@ -5,7 +5,7 @@ from typing import Callable, Optionalimport ujsonimport dspy-from dspy.adapters.chat_adapter import enumerate_fields+from dspy.adapters.utils import get_field_description_stringfrom dspy.predict.predict import Predictionfrom dspy.signatures import InputField, OutputField, Signature@@ -180,9 +180,9 @@ def inspect_modules(program):output.append(f"Module {name}")output.append("\n\tInput Fields:")- output.append(("\n" + "\t" * 2).join([""] + enumerate_fields(signature.input_fields).splitlines()))+ output.append(("\n" + "\t" * 2).join([""] + get_field_description_string(signature.input_fields).splitlines()))output.append("\tOutput Fields:")- output.append(("\n" + "\t" * 2).join([""] + enumerate_fields(signature.output_fields).splitlines()))+ output.append(("\n" + "\t" * 2).join([""] + get_field_description_string(signature.output_fields).splitlines()))output.append(f"\tOriginal Instructions: {instructions}")output.append(separator)diff --git a/dspy/teleprompt/simba_utils.py b/dspy/teleprompt/simba_utils.pyindex 0aea7b5d3..3765a33f1 100644--- a/dspy/teleprompt/simba_utils.py+++ b/dspy/teleprompt/simba_utils.py@@ -4,7 +4,7 @@ import inspectimport loggingimport textwrap-from dspy.adapters.chat_adapter import enumerate_fields+from dspy.adapters.utils import get_field_description_stringfrom dspy.signatures import InputField, OutputFieldfrom typing import Callable@@ -181,9 +181,9 @@ def inspect_modules(program):output.append(f"Module {name}")output.append("\n\tInput Fields:")- output.append(("\n" + "\t" * 2).join([""] + enumerate_fields(signature.input_fields).splitlines()))+ output.append(("\n" + "\t" * 2).join([""] + get_field_description_string(signature.input_fields).splitlines()))output.append("\tOutput Fields:")- output.append(("\n" + "\t" * 2).join([""] + enumerate_fields(signature.output_fields).splitlines()))+ output.append(("\n" + "\t" * 2).join([""] + get_field_description_string(signature.output_fields).splitlines()))output.append(f"\tOriginal Instructions: {instructions}")output.append(separator)diff --git a/dspy/utils/callback.py b/dspy/utils/callback.pyindex af5aa953d..6ba0c4cd0 100644--- a/dspy/utils/callback.py+++ b/dspy/utils/callback.py@@ -269,7 +269,12 @@ def with_callbacks(fn):call_id = uuid.uuid4().hexinputs = inspect.getcallargs(fn, instance, *args, **kwargs)- inputs.pop("self") # Not logging self as input++ # We don't include the instance information in the inpus+ if "self" in inputs:+ inputs.pop("self")+ elif "instance" in inputs:+ inputs.pop("instance")for callback in callbacks:try:diff --git a/dspy/utils/dummies.py b/dspy/utils/dummies.pyindex 1d1dbe1e8..9b96012b9 100644--- a/dspy/utils/dummies.py+++ b/dspy/utils/dummies.py@@ -4,7 +4,7 @@ from typing import Any, Dict, Unionimport numpy as np-from dspy.adapters.chat_adapter import FieldInfoWithName, field_header_pattern, format_fields+from dspy.adapters.chat_adapter import ChatAdapter, FieldInfoWithName, field_header_patternfrom dspy.clients.lm import LMfrom dspy.dsp.utils.utils import dotdictfrom dspy.signatures.field import OutputField@@ -19,8 +19,7 @@ class DummyLM(LM):Mode 1: List of dictionariesIf a list of dictionaries is provided, the dummy model will return the next dictionary- in the list for each request, formatted according to the `format_fields` function.- from the chat adapter.+ in the list for each request, formatted according to the `format_field_with_value` function.Example:@@ -41,7 +40,7 @@ class DummyLM(LM):If a dictionary of dictionaries is provided, the dummy model will return the valuecorresponding to the key which is contained with the final message of the prompt,- formatted according to the `format_fields` function from the chat adapter.+ formatted according to the `format_field_with_value` function from the chat adapter.```lm = DummyLM({"What color is the sky?": {"answer": "blue"}})@@ -95,7 +94,7 @@ class DummyLM(LM):@with_callbacksdef __call__(self, prompt=None, messages=None, **kwargs):def format_answer_fields(field_names_and_values: Dict[str, Any]):- return format_fields(+ return ChatAdapter().format_field_with_value(fields_with_values={FieldInfoWithName(name=field_name, info=OutputField()): valuefor field_name, value in field_names_and_values.items()