# Dspy Better Together

> Use for BetterTogether, prompt plus weight optimization, fine-tuning sequences, and strategy chains like p -> w -> p.

- **Type:** Skill
- **Install:** `agentstack add skill-omidzamani-dspy-skills-dspy-better-together`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [OmidZamani](https://agentstack.voostack.com/s/omidzamani)
- **Installs:** 0
- **Category:** [AI & ML](https://agentstack.voostack.com/c/ai-and-ml)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [OmidZamani](https://github.com/OmidZamani)
- **Source:** https://github.com/OmidZamani/dspy-skills/tree/master/skills/dspy-better-together

## Install

```sh
agentstack add skill-omidzamani-dspy-skills-dspy-better-together
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# DSPy BetterTogether

## Goal

Sequence prompt and weight optimizers, evaluate intermediate programs, and return the best candidate.

## Prerequisites

- Use DSPy `3.2.1` or later in the stable `3.2.x` series.
- Assign an LM directly to every predictor with `student.set_lm(lm)`.
- Keep a validation set, or allow `BetterTogether` to hold out part of the trainset.
- Confirm the LM provider supports fine-tuning before including `BootstrapFinetune`.

## Basic Pattern

```python
import dspy

lm = dspy.LM("openai/gpt-4o-mini")
dspy.configure(lm=lm)

student = dspy.ChainOfThought("question -> answer")
student.set_lm(lm)

def metric(example, pred, trace=None):
    return float(example.answer.lower() == pred.answer.lower())

optimizer = dspy.BetterTogether(
    metric=metric,
    p=dspy.GEPA(
        metric=lambda gold, pred, trace=None, pred_name=None, pred_trace=None:
            dspy.Prediction(score=metric(gold, pred), feedback="Check answer correctness."),
        reflection_lm=dspy.LM("openai/gpt-4o"),
        auto="light",
    ),
    w=dspy.BootstrapFinetune(metric=metric),
)

compiled = optimizer.compile(
    student,
    trainset=trainset,
    valset=valset,
    strategy="p -> w -> p",
)
```

## Strategy Choices

| Strategy | Use it when |
|----------|-------------|
| `"p -> w"` | Start with a simple prompt-then-weight pass |
| `"p -> w -> p"` | Re-optimize prompts after fine-tuning |
| `"w -> p"` | Fine-tuning data is already strong |
| Custom chains | Comparing prompt optimizers or conducting controlled experiments |

Optimizer names come from constructor keyword arguments. For example, `mipro=...` and `gepa=...` make `"mipro -> gepa"` valid.

## Per-Optimizer Compile Arguments

Pass optimizer-specific arguments through `optimizer_compile_args`:

```python
compiled = optimizer.compile(
    student,
    trainset=trainset,
    valset=valset,
    strategy="p -> w",
    optimizer_compile_args={
        "p": {"max_metric_calls": 150},
    },
)
```

Do not pass `student` inside `optimizer_compile_args`; `BetterTogether` manages the current program.

## Inspect Results

The returned program exposes:

- `candidate_programs`: evaluated candidates with score and strategy
- `flag_compilation_error_occurred`: whether a step failed before completion

## Related Skills

- Pick optimizers: [dspy-optimizer-selection](../dspy-optimizer-selection/SKILL.md)
- Fine-tune weights: [dspy-finetune-bootstrap](../dspy-finetune-bootstrap/SKILL.md)
- Reflect with GEPA: [dspy-gepa-reflective](../dspy-gepa-reflective/SKILL.md)

## Official Documentation

- **BetterTogether API**: https://dspy.ai/api/optimizers/BetterTogether/
- **Optimizer guide**: https://dspy.ai/learn/optimization/optimizers/

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [OmidZamani](https://github.com/OmidZamani)
- **Source:** [OmidZamani/dspy-skills](https://github.com/OmidZamani/dspy-skills)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** yes

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-omidzamani-dspy-skills-dspy-better-together
- Seller: https://agentstack.voostack.com/s/omidzamani
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
