DSPy: The framework for programming—not prompting—language models
Python
Deprecate prefix, format, and parser args in InputField/OutputField (#9394)
Maxime Rivest7e02cc8e32e6
diff --git a/dspy/predict/avatar/avatar.py b/dspy/predict/avatar/avatar.pyindex 53142d21c..0a088b473 100644--- a/dspy/predict/avatar/avatar.py+++ b/dspy/predict/avatar/avatar.py@@ -56,15 +56,11 @@ class Avatar(dspy.Module):def _get_field(self, field_info: FieldInfo):if field_info.json_schema_extra["__dspy_field_type"] == "input":return dspy.InputField(- prefix=field_info.json_schema_extra["prefix"],desc=field_info.json_schema_extra["desc"],- format=field_info.json_schema_extra["format"] if "format" in field_info.json_schema_extra else None,)elif field_info.json_schema_extra["__dspy_field_type"] == "output":return dspy.OutputField(- prefix=field_info.json_schema_extra["prefix"],desc=field_info.json_schema_extra["desc"],- format=field_info.json_schema_extra["format"] if "format" in field_info.json_schema_extra else None,)else:raise ValueError(f"Unknown field type: {field_info.json_schema_extra['__dspy_field_type']}")diff --git a/dspy/predict/chain_of_thought.py b/dspy/predict/chain_of_thought.pyindex 965afe694..46a81511c 100644--- a/dspy/predict/chain_of_thought.py+++ b/dspy/predict/chain_of_thought.py@@ -28,10 +28,9 @@ class ChainOfThought(Module):"""super().__init__()signature = ensure_signature(signature)- prefix = "Reasoning: Let's think step by step in order to"desc = "${reasoning}"rationale_field_type = rationale_field.annotation if rationale_field else rationale_field_type- rationale_field = rationale_field if rationale_field else dspy.OutputField(prefix=prefix, desc=desc)+ rationale_field = rationale_field if rationale_field else dspy.OutputField(desc=desc)extended_signature = signature.prepend(name="reasoning", field=rationale_field, type_=rationale_field_type)self.predict = dspy.Predict(extended_signature, **config)diff --git a/dspy/predict/program_of_thought.py b/dspy/predict/program_of_thought.pyindex 15d3e3886..e0d7bcb75 100644--- a/dspy/predict/program_of_thought.py+++ b/dspy/predict/program_of_thought.py@@ -67,35 +67,25 @@ class ProgramOfThought(Module):fields_for_mode = {"generate": {"generated_code": dspy.OutputField(- prefix="Code:",desc="python code that answers the question",- format=str,),},"regenerate": {"previous_code": dspy.InputField(- prefix="Previous Code:",desc="previously-generated python code that errored",- format=str,),"error": dspy.InputField(- prefix="Error:",desc="error message from previously-generated python code",),"generated_code": dspy.OutputField(- prefix="Code:",desc="python code that answers the question",- format=str,),},"answer": {"final_generated_code": dspy.InputField(- prefix="Code:",desc="python code that answers the question",- format=str,),"code_output": dspy.InputField(- prefix="Code Output:",desc="output of previously-generated python code",),}diff --git a/dspy/propose/grounded_proposer.py b/dspy/propose/grounded_proposer.pyindex ead30b528..8b878d519 100644--- a/dspy/propose/grounded_proposer.py+++ b/dspy/propose/grounded_proposer.py@@ -30,18 +30,13 @@ class DescribeProgram(dspy.Signature):"""Below is some pseudo-code for a pipeline that solves tasks with calls to language models. Please describe what type of task this program appears to be designed to solve, and how it appears to work.""")program_code = dspy.InputField(- format=str,desc="Pseudocode for a language model program designed to solve a particular task.",- prefix="PROGRAM CODE:",)program_example = dspy.InputField(- format=str,desc="An example of the program in use.",- prefix="EXAMPLE OF PROGRAM IN USE:",)program_description = dspy.OutputField(desc="Describe what task the program is designed to solve, and how it goes about solving this task.",- prefix="SUMMARY OF PROGRAM ABOVE:",)@@ -50,25 +45,19 @@ class DescribeModule(dspy.Signature):"""Below is some pseudo-code for a pipeline that solves tasks with calls to language models. Please describe the purpose of one of the specified module in this pipeline.""")program_code = dspy.InputField(- format=str,desc="Pseudocode for a language model program designed to solve a particular task.",- prefix="PROGRAM CODE:",)program_example = dspy.InputField(- format=str,desc="An example of the program in use.",- prefix="EXAMPLE OF PROGRAM IN USE:",)program_description = dspy.InputField(desc="Summary of the task the program is designed to solve, and how it goes about solving it.",- prefix="SUMMARY OF PROGRAM ABOVE:",)module = dspy.InputField(- desc="The module in the program that we want to describe.", prefix="MODULE:",+ desc="The module in the program that we want to describe.",)module_description = dspy.OutputField(desc="Description of the module's role in the broader program.",- prefix="MODULE DESCRIPTION:",)@@ -86,47 +75,36 @@ def generate_instruction_class(if use_dataset_summary:dataset_description = dspy.InputField(desc="A description of the dataset that we are using.",- prefix="DATASET SUMMARY:",)if program_aware:program_code = dspy.InputField(- format=str,desc="Language model program designed to solve a particular task.",- prefix="PROGRAM CODE:",)program_description = dspy.InputField(desc="Summary of the task the program is designed to solve, and how it goes about solving it.",- prefix="PROGRAM DESCRIPTION:",)module = dspy.InputField(- desc="The module to create an instruction for.", prefix="MODULE:",+ desc="The module to create an instruction for.",)module_description = dspy.InputField(- desc="Description of the module to create an instruction for.", prefix="MODULE DESCRIPTION:",+ desc="Description of the module to create an instruction for.",)task_demos = dspy.InputField(- format=str,desc="Example inputs/outputs of our module.",- prefix="TASK DEMO(S):",)if use_instruct_history:previous_instructions = dspy.InputField(- format=str,desc="Previous instructions we've attempted, along with their associated scores.",- prefix="PREVIOUS INSTRUCTIONS:",)basic_instruction = dspy.InputField(- format=str, desc="Basic instruction.", prefix="BASIC INSTRUCTION:",+ desc="Basic instruction.",)if use_tip:tip = dspy.InputField(- format=str,desc="A suggestion for how to go about generating the new instruction.",- prefix="TIP:",)proposed_instruction = dspy.OutputField(desc="Propose an instruction that will be used to prompt a Language Model to perform this task.",- prefix="PROPOSED INSTRUCTION:",)return dspy.Predict(GenerateSingleModuleInstruction)diff --git a/dspy/signatures/field.py b/dspy/signatures/field.pyindex 49ce4b170..a7029c413 100644--- a/dspy/signatures/field.py+++ b/dspy/signatures/field.py@@ -1,3 +1,5 @@+import warnings+import pydantic# The following arguments can be used in DSPy InputField and OutputField in addition@@ -5,6 +7,21 @@ import pydantic# as it would give a name clash.DSPY_FIELD_ARG_NAMES = ["desc", "prefix", "format", "parser", "__dspy_field_type"]+_DEPRECATED_FIELD_ARGS = {+ "prefix": (+ "The 'prefix' argument in InputField/OutputField is deprecated and has no effect in DSPy. "+ "It will be removed in a future version."+ ),+ "format": (+ "The 'format' argument in InputField/OutputField is deprecated and has no effect in DSPy. "+ "It will be removed in a future version."+ ),+ "parser": (+ "The 'parser' argument in InputField/OutputField is deprecated and has no effect in DSPy. "+ "It will be removed in a future version."+ ),+}+PYDANTIC_CONSTRAINT_MAP = {"gt": "greater than: ","ge": "greater than or equal to: ",@@ -51,11 +68,19 @@ def _translate_pydantic_field_constraints(**kwargs):return ", ".join(constraints)+def _warn_deprecated_field_args(**kwargs):+ for arg, message in _DEPRECATED_FIELD_ARGS.items():+ if arg in kwargs:+ warnings.warn(message, DeprecationWarning, stacklevel=3)++def InputField(**kwargs): # noqa: N802+ _warn_deprecated_field_args(**kwargs)return pydantic.Field(**move_kwargs(**kwargs, __dspy_field_type="input"))def OutputField(**kwargs): # noqa: N802+ _warn_deprecated_field_args(**kwargs)return pydantic.Field(**move_kwargs(**kwargs, __dspy_field_type="output"))