dspy.History¶
dspy.History ¶
Bases: BaseModel
Class representing the conversation history.
The conversation history is a list of messages, each message entity should have keys from the associated signature. For example, if you have the following signature:
class MySignature(dspy.Signature):
question: str = dspy.InputField()
history: dspy.History = dspy.InputField()
answer: str = dspy.OutputField()
Then the history should be a list of dictionaries with keys “question” and “answer”.
Examples:
import dspy
dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))
class MySignature(dspy.Signature):
question: str = dspy.InputField()
history: dspy.History = dspy.InputField()
answer: str = dspy.OutputField()
history = dspy.History(
messages=[
{"question": "What is the capital of France?", "answer": "Paris"},
{"question": "What is the capital of Germany?", "answer": "Berlin"},
]
)
predict = dspy.Predict(MySignature)
outputs = predict(question="What is the capital of France?", history=history)
Example of capturing the conversation history
import dspy
dspy.configure(lm=dspy.LM("openai/gpt-4o-mini"))
class MySignature(dspy.Signature):
question: str = dspy.InputField()
history: dspy.History = dspy.InputField()
answer: str = dspy.OutputField()
predict = dspy.Predict(MySignature)
outputs = predict(question="What is the capital of France?")
history = dspy.History(messages=[{"question": "What is the capital of France?", **outputs}])
outputs_with_history = predict(question="Are you sure?", history=history)
Attributes¶
messages: list[dict[str, Any]] instance-attribute ¶
model_config = pydantic.ConfigDict(frozen=True, str_strip_whitespace=True, validate_assignment=True, extra='forbid') class-attribute instance-attribute ¶
Methods:¶
format(adapter: Adapter, signature: type[Signature]) -> list[Message] ¶
Render recorded events using the adapter’s field syntax and native-tool policy.
History owns event dispatch and conversation ordering. REPL events own their action/observation representation; the adapter only renders fields and does not inspect their serialized layout.
Source code in dspy/adapters/types/history.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |