Skip to content

Quickstart

This guide will walk you through the basics of setting up and using dspy-profiles.

1. Installation

First, install the package from PyPI:

uv add dspy-profilesInstalleduv run dspy-profiles --versiondspy-profiles version: 0.2.2

pip install dspy-profilesInstalled

uvx run dspy-profiles --versiondspy-profiles version: 0.2.2

uv tool install dspy-profilesInstalleddspy-profiles --versiondspy-profiles version: 0.2.2

2. Core Concepts

A profile is a named collection of settings for DSPy. It can define:

  • A Language Model (lm)
  • A Retrieval Model (rm)
  • Global settings (e.g., cache_dir, retries)

Profiles are stored in a profiles.toml file. dspy-profiles locates this file with a clear, git-like precedence:

  1. Project-Specific File: It searches for a profiles.toml in the current directory and its parent directories.
  2. Environment Variable: You can set the DSPY_PROFILES_PATH environment variable to point to a specific configuration file.
  3. Global File: If neither of the above is found, it falls back to the global default at ~/.dspy/profiles.toml.

By default, the init command will create or edit the global file.

Profile Inheritance

Profiles can inherit and extend other profiles using the extends key. This allows you to create a base configuration and then create specialized variations without repeating yourself.

For example, you could have a base profile with common settings, and dev and prod profiles that extend it but use different language models.

graph TD
    A[base] --> B[dev];
    A --> C[prod];
    subgraph profiles.toml
        B --> D{lm: gpt-4o-mini};
        C --> E{lm: gpt-4-turbo};
    end

3. Initialize Your First Profile

The easiest way to get started is with the interactive init command. This will create a default profile for you.

uvx dspy-profiles init

dspy-profiles init

This command will ask for the language model, and optionally, your API key and an API base. It will then create the configuration file at ~/.dspy/profiles.toml.

WARNING

If you provide an API key, it will be stored in plaintext in the configuration file. Please ensure that this file is kept secure and is not committed to version control.

4. View Your Profile

You can view the contents of any profile with the show command:

uvx dspy-profiles show default

dspy-profiles show default

5. Using Profiles in Your Code

dspy-profiles provides multiple ways to activate a profile, catering to different use cases.

The profile context manager is the most common way to activate a profile for a specific block of code.

import dspy
from dspy_profiles import profile

# DSPy settings are configured automatically within this block
with profile("default"):
    predictor = dspy.Predict("question -> answer")
    result = predictor(question="What is the color of the sky?")
    print(result.answer)

Any DSPy calls made inside the with block will use the settings from your default profile. Outside the block, the global DSPy settings are left untouched.

The @with_profile decorator is useful for applying a profile to an entire function.

import dspy
from dspy_profiles import with_profile

@with_profile("default")
def my_dspy_program(question):
    predictor = dspy.Predict("question -> answer")
    return predictor(question=question)

result = my_dspy_program("What is the capital of Spain?")
print(result.answer)

The dspy-run command is the most powerful feature for your daily workflow. It lets you run any Python script with a specific profile, without needing to modify the script's code at all.

Imagine you have a standard DSPy script, my_script.py:

# my_script.py
import dspy

# This script is clean. No dspy-profiles code is needed.
predictor = dspy.Predict("question -> answer")
result = predictor(question="What is the capital of Spain?")
print(f"The capital of Spain is {result.answer}.")

Instead of adding a with profile(...) block to your code, you can simply tell dspy-run to handle it for you.

If you have installed dspy-profiles as a tool with uv tool install dspy-profiles, you can call it directly. This is the cleanest and most convenient method.

dspy-run my_script.pyNo profile specified. Using default profile: 'default'
The capital of Spain is Madrid.
dspy-run --profile production my_script.pyThe capital of Spain is Madrid.

If you haven't installed the tool, you can use uvx to run it directly from PyPI.

uvx dspy-run --profile default my_script.py

If dspy-profiles is a dependency in your project's pyproject.toml, you can use uv run.

uv run dspy-run --profile default my_script.py

6. Next Steps

You've now learned the basics of dspy-profiles. From here, you can:

  • Explore the CLI Reference to see all available commands.
  • Dive into the API Reference for advanced usage.
  • Learn about creating more complex profiles with inheritance.