Skip to main content
This example demonstrates how to build a RAG (Retrieval-Augmented Generation) pipeline with DSPy and instrument it with OpenInference tracing.

Prerequisites

  • Python 3.9+
  • OpenAI API key
  • Phoenix or another OpenTelemetry collector

Installation

1

Install dependencies

2

Set environment variables

Instrumentation Setup

Create an instrumentation module:

Basic DSPy RAG Module

Complete FastAPI Example

Here’s a production-ready FastAPI application with DSPy:

Optimizing with DSPy Compiler

DSPy’s key feature is automatic optimization:

Key Features

Automatic Module Tracing

DSPy instrumentation captures:
  • Module execution: All DSPy module forwards
  • LM calls: Language model predictions with prompts
  • Retrieval: Document retrieval operations
  • Optimization: Compiler operations and few-shot selection

Signature Tracking

The instrumentation records:
  • Input and output fields
  • Field descriptions and constraints
  • Type annotations

Compilation Observability

When using DSPy optimizers:
  • Bootstrap demonstration selection
  • Metric evaluations
  • Prompt evolution

Next Steps