← Back to Dashboard

DSPy Conversation History

Experiment with `dspy-ai` conversation history helpers—messages persist in memory and each response is generated with the full transcript as context.

Prefer OpenAI's native chat instead? Compare it with the Chat playground to see how the DSPy flow differs.

Status: ready

How it works

A DSPy `Predict` module runs with a signature that includes the full conversation transcript before asking for the next reply.

Execution flow

  • Append each user turn to in-memory history before calling the model.
  • Expose `history` + `user_query` in the signature for structured context.
  • Fallback to the OpenAI helper if DSPy fails, mirroring the tutorial's reliability pattern.
  • Caching disabled here to align with stateless tutorial behavior.
View DSPy sample code
import dspy

class ConversationSignature(dspy.Signature):
    history = dspy.InputField()
    user_query = dspy.InputField()
    response = dspy.OutputField()

lm = dspy.LM("openai/gpt-4o-mini", temperature=0.6)
dspy.configure(lm=lm)

predict = dspy.Predict(ConversationSignature)
prediction = predict(
    history="Assistant: Hello!",
    user_query="How does DSPy track history?",
)
print(prediction.response)

Further reading

Start the conversation by sending a message.