Skip to main content
LangWatch integrates with DSPy to automatically capture detailed information about your DSPy program executions, including module calls and language model interactions.

Installation

pip install langwatch dspy

Usage

The LangWatch API key is configured by default via the LANGWATCH_API_KEY environment variable.
Use autotrack_dspy() to automatically capture all DSPy operations within a trace.
import langwatch
import dspy
import os

langwatch.setup()

# Initialize your DSPy LM (Language Model)
lm = dspy.LM(
    "openai/gpt-5",
    api_key=os.environ.get("OPENAI_API_KEY"),
    temperature=1.0,
    max_tokens=16000,
)
dspy.settings.configure(lm=lm)


@langwatch.trace(name="DSPy RAG Execution")
def run_dspy_program(user_query: str):
    langwatch.get_current_trace().autotrack_dspy()

    module = dspy.Predict("question -> answer")
    prediction = module(question=user_query)
    return prediction.answer


def main():
    user_question = "What is the capital of France?"
    response = run_dspy_program(user_question)
    print(f"Question: {user_question}")
    print(f"Answer: {response}")


if __name__ == "__main__":
    main()
The @langwatch.trace() decorator creates a parent trace, and autotrack_dspy() enables automatic tracking of all DSPy operations, including module calls and underlying LM interactions, for the duration of that trace.