Skip to content

dspy-cli

Deploy DSPy programs as HTTP APIs in minutes.

Get startedGetting Started
DeployDeployment Guide

What is dspy-cli?

Turns DSPy modules into production-ready HTTP APIs. Scaffolds projects, auto-discovers modules as JSON endpoints, and packages everything in Docker. Use it to ship LLM services fast with minimal ops.

Three commands:

  • new — Create a project with directory structure, initial program, configuration, and Dockerfile
  • generate — Add programs, signatures, and modules with proper imports
  • serve — HTTP API with auto-detected modules, hot-reload, and testing UI

Prerequisites

  • Python 3.11+ and uv (or pipx/pip) installed
  • A model provider API key (e.g., OpenAI)

Quick Start

# Install
uv tool install dspy-cli

# Create project (interactive mode - recommended)
dspy-cli new

# Or with arguments
dspy-cli new my-feature -s "text -> summary"

# Serve locally
cd my-feature && dspy-cli serve

Access your API at http://localhost:8000/{ModuleName} and the testing UI at http://localhost:8000/.

How it works

Discovery → Routing → Execution: dspy-cli scans for DSPy modules, creates POST /{ModuleName} endpoints with JSON schemas from signatures, validates requests, and runs your module's forward/predict.

Example:

import dspy

class Rewrite(dspy.Signature):
    text: str = dspy.InputField()
    cleaned: str = dspy.OutputField()

class Rewriter(dspy.Module):
    def __init__(self):
        super().__init__()
        self.predict = dspy.ChainOfThought(Rewrite)

    def forward(self, text: str) -> dspy.Prediction:
        return self.predict(text=text)

Becomes: POST /Rewriter with body {"text": "..."} → response {"cleaned": "..."}

Try it:

curl -X POST http://localhost:8000/Rewriter \
  -H "Content-Type: application/json" \
  -d '{"text":"make this shorter"}'

Deploy

Your project includes a production-ready Docker container. Deploy to Fly.io, Render, AWS, or any Docker platform. See the Deployment Guide.

Architecture

Project structure:

my-feature/
├── modules/       # DSPy modules (exposed as endpoints)
├── signatures/    # DSPy signatures (request/response schema)
├── app/           # Server, discovery, settings
├── Dockerfile
├── .env
├── dspy.config.yaml
└── pyproject.toml

Request flow: JSON request → validate against signature → module forward/predict → JSON response

Discovery: Exposes dspy.Module subclasses as POST /{ClassName} endpoints with schemas from signatures

Top Tasks

Create — Get up and running with your first DSPy module

Configure — Environment variables and model settings

Deploy — Ship to production

Operate — Test and iterate

What You Get

  • Project scaffolding — Standardized structure with DSPy signatures, modules, and Docker configs
  • HTTP interface — FastAPI endpoints with automatic module discovery and OpenAPI docs
  • Hot-reload server — Built-in testing UI with live code updates
  • Deployment-ready — Deploy to any Docker platform

Defaults: Local server at http://localhost:8000, testing UI at /


Run dspy-cli --help for all commands. View more examples on GitHub.