Skip to content

Examples

Run these examples with dspy-run (or the helper script ./run-examples.zsh) to activate profiles without modifying the scripts. Most examples can also be executed directly with uv run python … when you want tighter control.

Hello World

examples/hello_world.py
import dspy

from dspy_profiles import profile


def main():
    """
    This example demonstrates the basic usage of the dspy-profiles library.
    It activates the "default" profile from your `profiles.toml` and uses it
    to configure dspy for a simple prediction task.
    """
    print("Running basic hello world example...")

    # The `profile` context manager activates the specified profile.
    # In this case, it loads the configuration for the "default" profile.
    # All dspy calls within this block will use the settings from that profile,
    # such as the language model, API keys, and other configurations.
    with profile("default"):
        # dspy.Predict creates a simple predictor module. The signature "question -> answer"
        # tells DSPy that this module takes a "question" as input and is expected
        # to produce an "answer" as output.
        predictor = dspy.Predict("question -> answer")

        # When the predictor is called, it uses the configured language model
        # to generate a response based on the provided question.
        result = predictor(question="What is the color of the sky?")

        # The result object contains the full completion from the language model,
        # including the predicted answer.
        print("Question: What is the color of the sky?")
        print(f"Answer: {result.answer}")


if __name__ == "__main__":
    main()

Hello Runner

examples/hello_runner.py
"""Demonstrate running a script under a profile via `dspy-run`.

Invoke this file with `uv run dspy-run --profile default -- python examples/hello_runner.py`
or via the project helper script. The wrapper sets `DSPY_PROFILE` for us,
so no context manager is required inside the script itself.
"""

import os

import dspy

print(f"{os.getenv('DSPY_PROFILE')=}")
for key in os.environ:
    if key.startswith("DSPY"):
        print(f"{key}, {os.getenv(key)}")

# No need for a context manager—the active profile is applied by dspy-run.
predictor = dspy.Predict("question -> answer")
result = predictor(question="What is the capital of Mexico?")
print(f"The answer is: {result.answer}.")

More Samples

  • hello_decorator.py: Applying @with_profile to a function (uv run python examples/hello_decorator.py)
  • decorator_usage.py: Decorator-focused walkthrough (uv run python examples/decorator_usage.py)
  • multiple_profiles.py: Switching profiles and printing the active one (requires default and creative profiles)
  • extended_profiles.py: Profile inheritance using extends (expects creative_child extending base_model)
  • profile_overrides.py: Overriding profile settings at runtime (uv run python examples/profile_overrides.py)
  • adaptive_agent.py: Advanced agent pattern with runtime escalation (use uv run dspy-run --profile creative_agent -- python examples/adaptive_agent.py; also defines a technical_agent profile for task overrides)
  • retrieval_example.py: Configuring and using a retrieval model via rm.class_name (needs a search profile with an rm section)